function mailhandler_update_6200 in Mailhandler 6.2
Update to work with 6.x-2.x branch.
File
- ./
mailhandler.install, line 102 - Install, update and uninstall functions for the Mailhandler module.
Code
function mailhandler_update_6200() {
$ret = array();
$modules = array(
'autoload',
'ctools',
'feeds',
'features',
);
drupal_install_modules($modules);
foreach ($modules as $module) {
if (!module_exists($module)) {
return array(
'#abort' => array(
'success' => FALSE,
'query' => 'Mailhandler is missing one or more dependencies. Please enable all dependencies (' . implode(', ', $modules) . ') and re-run update.php.',
),
);
}
}
drupal_install_schema('mailhandler');
mailhandler_reset_schema();
module_enable(array(
'mailhandler',
'mailhandler_php_imap',
'mailhandler_default',
));
ctools_include('cleanstring');
$result = db_query('SELECT * FROM {mailhandler}');
$mailboxes = array();
$formats = array();
// Port each existing mailbox to the new format
while ($row = db_fetch_object($result)) {
// First, build and create the new mailbox
$mailhandler_mailbox = array(
'mail' => $row->mail,
'settings' => array(
'mailto' => $row->mailto,
'folder' => $row->folder,
'imap' => $row->imap,
'domain' => $row->domain,
'port' => $row->port,
'name' => $row->name,
'pass' => $row->pass,
'extraimap' => $row->extraimap,
'limit' => variable_get('mailhandler_max_retrieval', 0),
'encoding' => variable_get('mailhandler_default_encoding', 'UTF-8'),
'mime' => $row->mime,
'delete_after_read' => $row->delete_after_read,
'fromheader' => $row->fromheader,
'security' => $row->security,
'replies' => $row->replies,
),
);
drupal_write_record('mailhandler_mailbox', $mailhandler_mailbox);
$mailboxes[$row->mail] = array(
'format' => $row->format,
'sigseparator' => $row->sigseparator,
'commands' => $row->commands,
'enabled' => $row->enabled,
);
if (!empty($row->sigseparator)) {
$formats[$row->format][$row->sigseparator][] = $row->mail;
}
}
foreach ($formats as $format => $seps) {
foreach ($seps as $sep => $mails) {
// Copy $format and add on the sig separator input filter
$new_format = filter_format_load($format);
$filters = filter_list_format($format);
$new_format->name = $new_format->name . ' (' . implode(', ', $mails) . ')';
drupal_write_record('filter_formats', $new_format);
$filters[] = (object) array(
'module' => 'mailhandler',
'delta' => 0,
'weight' => 0,
);
foreach ($filters as $filter) {
$new_filter = array(
'format' => $new_format->format,
'module' => $filter->module,
'delta' => $filter->delta,
'weight' => $filter->weight,
);
drupal_write_record('filters', $new_filter);
}
variable_set('mailhandler_filter_' . $new_format->format, $sep);
foreach ($mails as $mail) {
$mailboxes[$mail]['format'] = $new_format->format;
}
}
}
foreach ($mailboxes as $mail => $mailbox) {
// Now build a corresponding feeds importer with matching commands and filter setting
module_load_include('inc', 'mailhandler_default', 'mailhandler_default.feeds_importer_default');
$export = mailhandler_default_feeds_importer_default();
$config = $export['mailhandler_nodes']->config;
$config['name'] = $mail . ' nodes';
$config['processor']['config']['input_format'] = $mailbox['format'];
if (!$mailbox['enabled']) {
$config['content_type'] = '';
}
$importer = array(
'id' => ctools_cleanstring($mail, array(
'separator' => '_',
'lower case' => 'true',
)) . '_nodes',
'config' => $config,
);
drupal_write_record('feeds_importer', $importer);
// If cron is enabled, create a source node.
if ($mailbox['enabled']) {
$node = new stdClass();
$node->type = 'mailhandler_source';
$node->title = $mail;
module_load_include('inc', 'node', 'node.pages');
node_object_prepare($node);
// Populate properties that are set by node_object_prepare().
$node->log = 'Created by Mailhandler update 6200.';
node_save($node);
$source = array(
'id' => $importer['id'],
'feed_nid' => $node->nid,
'config' => array(
'MailhandlerFetcher' => array(
'mailbox' => $mail,
),
'MailhandlerParser' => array(
'auth_required' => 0,
),
),
'source' => 'mailhandler' . $node->nid,
);
drupal_write_record('feeds_source', $source);
}
}
db_drop_table($ret, 'mailhandler');
drupal_set_message(t('Any existing Mailhandler mailboxes have been converted and corresponding input formats, Feeds importers, and source nodes have been created.'));
return $ret;
}