diff --git a/metatag.drush.inc b/metatag.drush.inc new file mode 100644 index 0000000..c68f42c --- /dev/null +++ b/metatag.drush.inc @@ -0,0 +1,98 @@ + '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 cache-get-stale. + */ +function drush_metatag_import_mtq() { + // Define Meta Tags Quick tables and fields. + $mtq_fields = array( + 'field_data_meta_abstract' => 'meta_abstract_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', + 'field_data_meta_robots' => 'meta_robots_metatags_quick', + ); + + // Define a map of Meta Tags Quick fields to Meta Tags fields. + $map = array( + 'meta_abstract_metatags_quick' => 'abstract', + 'meta_copyright_metatags_quick' => 'copyright', + 'meta_description_metatags_quick' => 'description', + 'meta_keywords_metatags_quick' => 'keywords', + 'meta_robots_metatags_quick' => 'robots', + ); + + // Construct and execute the query. + $query = db_select('node') + ->fields('node', array('nid', 'language')); + foreach ($mtq_fields as $table => $field) { + $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; + + // Special handling for the robots field. + if ($field == 'meta_robots_metatags_quick') { + $metatags[$record->nid]['config'][$map[$field]]['value'] = explode(',', $record->$field); + } + else { + $metatags[$record->nid]['config'][$map[$field]]['value'] = $record->$field; + } + } + } + } + + // Add these computed values to the {metatag} table. + foreach ($metatags as $id => $item) { + db_merge('metatag') + ->key(array( + 'entity_type' => 'node', + 'entity_id' => $id, + )) + ->fields(array( + 'data' => serialize($item['config']), + 'language' => $item['language'], + )) + ->execute(); + } + + // Clear the Meta Tags cache for all nodes. + metatag_metatags_cache_clear('node'); +}