diff --git a/metatag.drush.inc b/metatag.drush.inc new file mode 100644 index 0000000..81f4f7e --- /dev/null +++ b/metatag.drush.inc @@ -0,0 +1,246 @@ + 'Imports data from the Meta Tags quick module into the Meta Tags module.', + 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_DATABASE, + ); + + return $items; +} + +/** + * Implements hook_drush_help(). + */ +function metatag_drush_help($section) { + switch ($section) { + case 'drush:metatag-import-mtq': + return dt('Imports data from the Meta Tags quick module into the Meta Tags module.'); + } +} + +/** + * Drush command callback for metatag-import-mtq. + */ +function drush_metatag_import_mtq() { + // Attempt to import node-based metatag data. + drush_print('Attempting to import Meta Tags Quick node data.'); + $node = _metatag_import_mtq_nodes(); + drush_print('Created ' . $node['insert'] . ' and updated ' . $node['update'] . ' overrides for nodes.'); + + // Attempt to import path-based metatag data. + if (module_exists('metatag_context')) { + drush_print('Attempting to import Meta Tags Quick path data.'); + $path = _metatag_import_mtq_paths(); + drush_print('Created ' . $path['insert'] . ' and updated ' . $path['update'] . ' contexts.'); + } + else { + drush_print('To import Meta Tags Quick path data, you must enable the Meta Tags: Context module.'); + } + + // Notify the user. + drush_print('Finished importing Meta Tags Quick data.'); +} + +/** + * Returns a map of Meta Tags Quick tables to their relevant value fields. + */ +function _metatag_import_mtq_get_fields() { + return array( + 'field_data_meta_abstract' => 'meta_abstract_metatags_quick', + 'field_data_meta_canonical' => 'meta_canonical_metatags_quick', + 'field_data_meta_copyright' => 'meta_copyright_metatags_quick', + 'field_data_meta_description' => 'meta_description_metatags_quick', + 'field_data_meta_keywords' => 'meta_keywords_metatags_quick', + // Meta Tags module doesn't currently support this field. + // 'field_data_meta_revisit_after' => 'meta_revisit_after_metatags_quick', + 'field_data_meta_robots' => 'meta_robots_metatags_quick', + ); +} + +/** + * Returns a map of Meta Tags Quick values to Meta Tags field names. + */ +function _metatag_import_mtq_get_map() { + return array( + 'meta_abstract_metatags_quick' => 'abstract', + 'meta_canonical_metatags_quick' => 'canonical', + 'meta_copyright_metatags_quick' => 'copyright', + 'meta_description_metatags_quick' => 'description', + 'meta_keywords_metatags_quick' => 'keywords', + // Meta Tags module doesn't currently support this field. + // 'meta_revisit_after_metatags_quick' => 'revisit_after', + 'meta_robots_metatags_quick' => 'robots', + ); +} + +/** + * Imports Meta Tags Quick data for all nodes. + * + * @return + * An array containing the number of nodes whose data was inserted/updated. + */ +function _metatag_import_mtq_nodes() { + $mtq_fields = _metatag_import_mtq_get_fields(); + $map = _metatag_import_mtq_get_map(); + + // Construct and execute the query. + $query = db_select('node') + ->fields('node', array('nid', 'language', 'vid')); + foreach ($mtq_fields as $table => $field) { + if (db_table_exists($table)) { + $condition = $table . ".entity_type = 'node' AND " . $table . ".entity_id = node.nid"; + $query->leftJoin($table, NULL, $condition); + $query->fields($table, array($field)); + } + } + $result = $query->execute(); + + // Build an array of data suitable for the Meta Tags module. + $metatags = array(); + foreach ($result as $record) { + foreach ($mtq_fields as $field) { + if (!empty($record->$field)) { + $metatags[$record->nid]['language'] = $record->language; + $metatags[$record->nid]['revision_id'] = $record->vid; + + // Special handling for the robots field. + if ($field == 'meta_robots_metatags_quick') { + $robots = explode(',', $record->$field); + $metatags[$record->nid]['config'][$map[$field]]['value'] = array_combine($robots, $robots); + } + else { + $metatags[$record->nid]['config'][$map[$field]]['value'] = $record->$field; + } + } + } + } + + // Add these computed values to the {metatag} table. + $counts = array( + 'insert' => 0, + 'update' => 0, + ); + foreach ($metatags as $id => $item) { + $merge_status = db_merge('metatag') + ->key(array( + 'entity_type' => 'node', + 'entity_id' => $id, + 'revision_id' => $item['revision_id'], + )) + ->fields(array( + 'data' => serialize($item['config']), + 'language' => $item['language'], + )) + ->execute(); + + // Count db_merge behavior. + if ($merge_status == 1) { + $counts['insert']++; + } + elseif ($merge_status == 2) { + $counts['update']++; + } + } + + // Clear the Meta Tags cache for all nodes. + metatag_metatags_cache_clear('node'); + + return $counts; +} + +/** + * Imports Meta Tags Quick data for all paths. + * + * @return + * An array containing the number of paths whose data was inserted/updated. + */ +function _metatag_import_mtq_paths() { + $mtq_fields = _metatag_import_mtq_get_fields(); + $map = _metatag_import_mtq_get_map(); + + // Construct and execute the query. + $query = db_select('metatags_quick_path_based') + ->fields('metatags_quick_path_based', array('id', 'path', 'lang')); + foreach ($mtq_fields as $table => $field) { + if (db_table_exists($table)) { + $condition = $table . ".entity_type = 'metatags_path_based' AND " . $table . ".entity_id = metatags_quick_path_based.id"; + $query->leftJoin($table, NULL, $condition); + $query->fields($table, array($field)); + } + } + $result = $query->execute(); + + // Build an array of data suitable for the Context module. + $metatags = array(); + foreach ($result as $record) { + foreach ($mtq_fields as $field) { + if (!empty($record->$field)) { + // Generate a reasonably unique, readable name. + $context_name = strtolower(str_replace('/', '_', $record->path)); + $context_name = 'mtq_' . preg_replace("/[^a-z0-9_]/", '', $context_name); + $context_name = truncate_utf8($context_name, 255); + + // Add a note that this was auto-generated. + $metatags[$context_name]['description'] = 'Automatically generated from Meta Tags Quick data.'; + + // Tag this so it appears in the Meta Tag module admin. + $metatags[$context_name]['tag'] = 'Metatag'; + + // The condition and condition mode as expected by Context. + $metatags[$context_name]['conditions'] = array('path' => array('values' => array($record->path => $record->path))); + $metatags[$context_name]['condition_mode'] = 0; + + // The reaction as expected by Context / Meta Tags. + $metatags[$context_name]['reactions']['metatag_context_reaction']['metatag_admin'] = 1; + + // Special handling for the robots field. + if ($field == 'meta_robots_metatags_quick') { + $robots = explode(',', $record->$field); + $metatags[$record->nid]['config'][$map[$field]]['value'] = array_combine($robots, $robots); + } + else { + $metatags[$context_name]['reactions']['metatag_context_reaction']['metatags'][$map[$field]]['value'] = $record->$field; + } + } + } + } + + // Add these computed values to the {metatag} table. + $counts = array( + 'insert' => 0, + 'update' => 0, + ); + foreach ($metatags as $id => $item) { + // Prepare conditions/reactions by serializing them. + $item['conditions'] = serialize($item['conditions']); + $item['reactions'] = serialize($item['reactions']); + + $merge_status = db_merge('context') + ->key(array( + 'name' => $id, + )) + ->fields($item) + ->execute(); + + // Count db_merge behavior. + if ($merge_status == 1) { + $counts['insert']++; + } + elseif ($merge_status == 2) { + $counts['update']++; + } + } + + return $counts; +}