metatag_importer.nodewords.inc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. <?php
  2. /**
  3. * @file
  4. * Convert data from Nodewords to Metatag.
  5. */
  6. // The Nodwords record types.
  7. define('NODEWORDS_TYPE_DEFAULT', 1);
  8. define('NODEWORDS_TYPE_ERRORPAGE', 2);
  9. define('NODEWORDS_TYPE_FRONTPAGE', 3);
  10. define('NODEWORDS_TYPE_NONE', 0);
  11. define('NODEWORDS_TYPE_NODE', 5);
  12. define('NODEWORDS_TYPE_PAGE', 10);
  13. define('NODEWORDS_TYPE_PAGER', 4);
  14. define('NODEWORDS_TYPE_TERM', 6);
  15. define('NODEWORDS_TYPE_TRACKER', 7);
  16. define('NODEWORDS_TYPE_USER', 8);
  17. define('NODEWORDS_TYPE_VOCABULARY', 9);
  18. /**
  19. * Form generator for the migration selection form.
  20. */
  21. function metatag_importer_nodewords_form($form, &$form_state) {
  22. $types = array();
  23. if (db_table_exists('nodewords')) {
  24. $types += _metatag_importer_list_nodewords();
  25. }
  26. if (!empty($types)) {
  27. $form['types'] = array(
  28. '#type' => 'checkboxes',
  29. '#title' => t('Records to import'),
  30. '#options' => $types,
  31. );
  32. $form['notes'] = array(
  33. '#markup' => '<p>' . t('Notes') . ':' . '</p>'
  34. . '<ul>'
  35. . ' <li>' . t('All compatible records will be imported.') . '</li>'
  36. . ' <li>' . t('Records <strong>will be removed</strong> from the {nodewords} table upon completion, make sure to keep a backup of the table in case needed.') . '</li>'
  37. . ' <li>' . t('The import process may take some time, please be patient.') . '</li>'
  38. . ' <li>' . t('Nodewords stored each meta tag as a separate record, so there were many records for each entity or configuration.') . '</li>'
  39. . ' <li>' . t('Empty values will be removed, no additional logic is added to verify them.') . '</li>'
  40. . ' <li>' . t('Only node, taxonomy term, user, global, front page and error page records will be converted.') . '</li>'
  41. . ' <li>' . t('Custom paths, trackers, pagers and vocabularies are not supported yet.') . '</li>'
  42. . '</ul>',
  43. );
  44. $form['actions']['migrate'] = array(
  45. '#type' => 'submit',
  46. '#value' => t('Migrate all records'),
  47. );
  48. }
  49. else {
  50. $form['ohbother'] = array(
  51. '#markup' => t('Nothing has been found that needs to be imported.'),
  52. '#prefix' => '<p>',
  53. '#suffix' => '</p>',
  54. );
  55. }
  56. return $form;
  57. }
  58. /**
  59. * Handles submission of the Nodewords migration form.
  60. */
  61. function metatag_importer_nodewords_form_submit($form, &$form_state) {
  62. $types = array_filter($form_state['values']['types']);
  63. _metatag_importer_import($types);
  64. }
  65. function _metatag_importer_list_nodewords() {
  66. $keys = array(
  67. NODEWORDS_TYPE_DEFAULT => t('Default'),
  68. NODEWORDS_TYPE_ERRORPAGE => t('Error page'),
  69. NODEWORDS_TYPE_FRONTPAGE => t('Front page'),
  70. NODEWORDS_TYPE_NONE => t('None'),
  71. NODEWORDS_TYPE_NODE => t('Node'),
  72. NODEWORDS_TYPE_PAGE => t('Page'),
  73. NODEWORDS_TYPE_PAGER => t('Pager'),
  74. NODEWORDS_TYPE_TERM => t('Taxonomy term'),
  75. NODEWORDS_TYPE_TRACKER => t('Tracker'),
  76. NODEWORDS_TYPE_USER => t('User'),
  77. NODEWORDS_TYPE_VOCABULARY => t('Vocabulary'),
  78. );
  79. // Get a list of all records grouped by type.
  80. $query = db_select('nodewords', 'nw')
  81. ->fields('nw', array('type'))
  82. ->orderBy('nw.type')
  83. ->orderBy('nw.id')
  84. // Exclude records that are empty.
  85. ->condition('nw.content', 'a:1:{s:5:"value";s:0:"";}', '<>')
  86. ->groupBy('nw.type');
  87. // Group-by.
  88. $query->addExpression('COUNT(nw.id)', 'id_count');
  89. $filtered = $query->execute();
  90. // Get a list of all records grouped by type.
  91. $query = db_select('nodewords', 'nw')
  92. ->fields('nw', array('type'))
  93. ->orderBy('nw.type')
  94. ->orderBy('nw.id')
  95. ->groupBy('nw.type');
  96. // Group-by.
  97. $query->addExpression('COUNT(nw.id)', 'id_count');
  98. $all = $query->execute()->fetchAllKeyed();
  99. $types = array();
  100. foreach ($filtered as $record) {
  101. $types['nodewords:' . $record->type] = t('Nodewords: @type - @non_empty records with values, @total total.',
  102. array(
  103. '@type' => $keys[$record->type],
  104. '@non_empty' => $record->id_count,
  105. '@total' => $all[$record->type],
  106. ));
  107. }
  108. return $types;
  109. }
  110. /**
  111. * Migrates Nodewords data to the Metatag module.
  112. */
  113. function _metatag_importer_import(array $types = array()) {
  114. $batch = array(
  115. 'title' => t('Importing Nodewords data..'),
  116. 'init_message' => t('Nodewords import is starting.'),
  117. 'progress_message' => t('Processed @current out of @total.'),
  118. 'error_message' => t('Nodewords import has encountered an error.'),
  119. 'operations' => array(
  120. array('_metatag_importer_migrate', array($types)),
  121. ),
  122. 'finished' => '_metatag_importer_finished',
  123. 'file' => drupal_get_path('module', 'metatag_importer') . '/metatag_importer.nodewords.inc',
  124. );
  125. // Kick off the batch, using Drush if available.
  126. if (drupal_is_cli() && function_exists('drush_backend_batch_process')) {
  127. // Add some special magic for CLI before setting the batch.
  128. $batch['progressive'] = FALSE;
  129. batch_set($batch);
  130. // Process the batch.
  131. drush_backend_batch_process();
  132. }
  133. else {
  134. batch_set($batch);
  135. batch_process();
  136. }
  137. }
  138. /**
  139. * Migrates Nodewords data to the Metatag module.
  140. */
  141. function _metatag_importer_migrate(array $types = array(), &$context = array()) {
  142. // Process this number of {nodewords} records at a time.
  143. $limit = 50;
  144. if (empty($context['sandbox'])) {
  145. // @todo Expand this so it can handle other types of things.
  146. foreach ($types as $key => $val) {
  147. $types[$key] = str_replace('nodewords:', '', $val);
  148. }
  149. $context['sandbox']['progress'] = 0;
  150. $context['sandbox']['current'] = 0;
  151. $query = db_select('nodewords', 'nw')
  152. ->fields('nw', array('mtid'))
  153. ->orderBy('nw.mtid');
  154. if (!empty($types)) {
  155. $query->condition('nw.type', $types, 'IN');
  156. }
  157. $context['sandbox']['dataset'] = array_keys($query->execute()->fetchAllAssoc('mtid', PDO::FETCH_ASSOC));
  158. $context['sandbox']['max'] = count($context['sandbox']['dataset']);
  159. // Track all of the entities that could not be loaded.
  160. $context['sandbox']['skipped'] = array();
  161. }
  162. // Retrieve Nodewords data.
  163. $query = db_select('nodewords', 'nw')
  164. ->fields('nw', array('mtid', 'type', 'id', 'name', 'content'))
  165. // Continue on from the last record that was processed.
  166. ->condition('nw.mtid', $context['sandbox']['current'], '>')
  167. ->orderBy('nw.mtid');
  168. // @todo Finish off / test the $types handling.
  169. // if (!empty($types)) {
  170. // $query->condition('nw.type', $types, 'IN');
  171. // }
  172. $query->range(0, $limit);
  173. $results = $query->execute();
  174. // Records that are being converted.
  175. $records = array();
  176. // Track records that are converted and will be ready to be deleted.
  177. $to_delete = array();
  178. // Convert Nodewords data into the Metatag format.
  179. foreach ($results as $result) {
  180. // Log the progress.
  181. $context['sandbox']['current'] = $result->mtid;
  182. $context['sandbox']['progress']++;
  183. // Convert the Nodewords record 'type' into something Metatag can use.
  184. $type = _metatag_importer_convert_type($result->type);
  185. // Skip record types we're not handling just yet.
  186. if (empty($type)) {
  187. continue;
  188. }
  189. // This could be an entity ID, but also possibly just a placeholder integer.
  190. $record_id = $result->id;
  191. // Check if this record was skipped previously.
  192. if (isset($context['sandbox']['skipped'][$type][$record_id])) {
  193. // Delete this record anyway.
  194. $to_delete[] = $result->mtid;
  195. continue;
  196. }
  197. // If this record is for an entity, verify that the entity exists.
  198. if (in_array($type, array('node', 'taxonomy_term', 'user'))) {
  199. $entity = entity_load($type, array($record_id));
  200. if (empty($entity)) {
  201. $context['sandbox']['skipped'][$type][$record_id] = $record_id;
  202. watchdog('metatag_importer', 'Unable to load @entity_type ID @id', array('@entity_type' => $type, '@id' => $record_id), WATCHDOG_WARNING);
  203. // Delete this record anyway.
  204. $to_delete[] = $result->mtid;
  205. continue;
  206. }
  207. }
  208. // Process the meta tag value, possibly also rename the meta tag name
  209. // itself.
  210. list($meta_tag, $value) = _metatag_importer_convert_data($result->name, unserialize($result->content));
  211. // Don't import empty values.
  212. if (!empty($value)) {
  213. // Add the value to the stack.
  214. $records[$type][$record_id][$meta_tag] = $value;
  215. }
  216. // Note that this record is ready to be deleted.
  217. $to_delete[] = $result->mtid;
  218. }
  219. // Update or create Metatag records.
  220. foreach ($records as $type => $data) {
  221. foreach ($data as $record_id => $values) {
  222. switch ($type) {
  223. // Standard D7 entities are converted to {metatag} records using
  224. // metatag_metatags_save().
  225. case 'node':
  226. case 'taxonomy_term':
  227. case 'user':
  228. // watchdog('metatag_importer', 'Importing meta tags for @entity_type ID @id..', array('@entity_type' => $type, '@id' => $record_id), WATCHDOG_INFO);
  229. $entity = entity_load($type, array($record_id));
  230. $entity = reset($entity);
  231. $langcode = metatag_entity_get_language($type, $entity);
  232. list($entity_id, $revision_id, $bundle) = entity_extract_ids($type, $entity);
  233. // Add these meta tags to the entity, overwriting anything that's
  234. // already there.
  235. foreach ($values as $name => $value) {
  236. $entity->metatags[$langcode][$name] = $value;
  237. }
  238. metatag_metatags_save($type, $entity_id, $revision_id, $entity->metatags);
  239. // watchdog('metatag_importer', 'Imported meta tags for @entity_type ID @id.', array('@entity_type' => $type, '@id' => $record_id), WATCHDOG_INFO);
  240. break;
  241. // Other Nodewords settings are converted to {metatag_config} records
  242. // using metatag_config_save().
  243. case 'global':
  244. case 'global:frontpage':
  245. case 'global:404':
  246. $config = metatag_config_load($type);
  247. // If a configuration was not found create a config object.
  248. if (empty($config)) {
  249. $config = (object) array(
  250. 'instance' => $type,
  251. );
  252. }
  253. // Add these meta tags to the configuration, overwriting anything
  254. // that's already there.
  255. foreach ($values as $name => $value) {
  256. $config->config[$name] = $value;
  257. }
  258. // Save the configuration.
  259. metatag_config_save($config);
  260. break;
  261. // @todo: A 'vocabulary' setting becomes a default configuration?
  262. case 'vocabulary':
  263. // $metatags = metatag_metatags_load($record->entity_type, $record->entity_id);
  264. // $metatags = array_merge($metatags, $record->data);
  265. // $vocabulary = taxonomy_vocabulary_load($record->entity_id);
  266. // metatag_metatags_save($record->entity_type, $record->entity_id, $vocabulary->vid, $metatags);
  267. break;
  268. }
  269. }
  270. }
  271. // Delete some records.
  272. if (!empty($to_delete)) {
  273. db_delete('nodewords')
  274. ->condition('mtid', $to_delete)
  275. ->execute();
  276. }
  277. $context['finished'] = (empty($context['sandbox']['max']) || $context['sandbox']['progress'] >= $context['sandbox']['max']) ? TRUE : ($context['sandbox']['progress'] / $context['sandbox']['max']);
  278. if ($context['finished'] === TRUE) {
  279. $message = 'Imported @imported Nodewords records.';
  280. $vars = array('@imported' => $context['sandbox']['progress']);
  281. if (drupal_is_cli() && function_exists('drush_print')) {
  282. drush_print(dt($message, $vars));
  283. }
  284. else {
  285. drupal_set_message(t($message, $vars));
  286. }
  287. if (!empty($context['sandbox']['skipped'])) {
  288. $message = '@skipped records were skipped because the corresponding entities were previously deleted.';
  289. $vars = array('@skipped' => count($context['sandbox']['skipped']));
  290. if (drupal_is_cli() && function_exists('drush_print')) {
  291. drush_print(dt($message, $vars));
  292. }
  293. else {
  294. drupal_set_message(t($message, $vars));
  295. }
  296. }
  297. }
  298. }
  299. /**
  300. * BatchAPI callback for when the import finishes.
  301. */
  302. function _metatag_importer_finished($success, $results, $operations) {
  303. if ($success) {
  304. if (drupal_is_cli() && function_exists('drush_print')) {
  305. // Make a bulleted list of messages for the command line.
  306. foreach ($results as $result) {
  307. drush_print(dt('* ' . $result));
  308. }
  309. }
  310. else {
  311. // Make a bulleted list of messages for the browser.
  312. $message = theme('item_list', array('items' => $results));
  313. drupal_set_message(t($message));
  314. }
  315. }
  316. else {
  317. // An error occurred.
  318. // $operations contains the operations that remained unprocessed.
  319. $error_operation = reset($operations);
  320. $message = 'An error occurred while processing %error_operation';
  321. $vars = array(
  322. '%error_operation' => $error_operation[0],
  323. );
  324. if (drupal_is_cli() && function_exists('drush_print')) {
  325. drush_set_error('metatag_importer', dt($message, $vars));
  326. }
  327. else {
  328. $message .= ' with arguments: @arguments';
  329. $vars[] = array('@arguments' => print_r($error_operation[1], TRUE));
  330. drupal_set_message(t($message, $vars), 'error');
  331. }
  332. }
  333. }
  334. /**
  335. * Converts the Nodewords type to a Metatag entity or Metatag config instance.
  336. *
  337. * @param $type
  338. * Nodewords type.
  339. *
  340. * @return
  341. * Metatag entity type or configuration instance.
  342. */
  343. function _metatag_importer_convert_type($type) {
  344. // define('NODEWORDS_TYPE_DEFAULT', 1);
  345. // define('NODEWORDS_TYPE_ERRORPAGE', 2);
  346. // define('NODEWORDS_TYPE_FRONTPAGE', 3);
  347. // define('NODEWORDS_TYPE_NONE', 0);
  348. // define('NODEWORDS_TYPE_NODE', 5);
  349. // define('NODEWORDS_TYPE_PAGE', 10);
  350. // define('NODEWORDS_TYPE_PAGER', 4);
  351. // define('NODEWORDS_TYPE_TERM', 6);
  352. // define('NODEWORDS_TYPE_TRACKER', 7);
  353. // define('NODEWORDS_TYPE_USER', 8);
  354. // define('NODEWORDS_TYPE_VOCABULARY', 9);
  355. switch ($type) {
  356. case 1:
  357. return 'global';
  358. case 2:
  359. return 'global:404';
  360. case 3:
  361. return 'global:frontpage';
  362. // @todo Not yet sure how to handle pager items?
  363. // case 4:
  364. // return 'pager';
  365. case 5:
  366. return 'node';
  367. case 6:
  368. return 'taxonomy_term';
  369. // @todo Not sure what to do with tracker pages.
  370. // case 7:
  371. // return 'tracker';
  372. case 8:
  373. return 'user';
  374. // Vocabulary records convert into a config for that entity bundle.
  375. case 9:
  376. return 'vocabulary';
  377. // @todo Page records need to be converted to Context definitions.
  378. // case 10:
  379. // return 'page';
  380. }
  381. return FALSE;
  382. }
  383. /**
  384. * Converts a meta tag's name and value from Nodewords to Metatag format.
  385. *
  386. * @param $name
  387. * Meta tag name.
  388. * @param $value
  389. * Meta tag value in Nodewords format.
  390. *
  391. * @return
  392. * The two arguments returned after being converted, in an array.
  393. */
  394. function _metatag_importer_convert_data($name, $value) {
  395. // Initial simplification of simple values.
  396. if (is_array($value) && isset($value['value']) && count($value) === 1 && empty($value['value'])) {
  397. $value = FALSE;
  398. }
  399. // Reformat the meta tag data, and possibly name.
  400. switch ($name) {
  401. // The Dublin Core date value was stored as three separarate strings.
  402. case 'dcterms.date':
  403. // Skip this value if it doesn't contain an array of three values.
  404. if (!is_array($value) || empty($value['month']) || empty($value['day']) || empty($value['year'])) {
  405. $value = FALSE;
  406. }
  407. else {
  408. $date = mktime(0, 0, 0, $value['month'], $value['day'], $value['year']);
  409. $value = date('Y-m-d\TH:iP', $date);
  410. }
  411. break;
  412. // The location meta tag gets renamed and converted to a semi-colon
  413. // -separated string.
  414. case 'location':
  415. // Catch empty values.
  416. if (!is_array($value) || empty($value['latitutde']) || empty($value['longitude'])) {
  417. $value = FALSE;
  418. }
  419. else {
  420. $name = 'geo.position';
  421. $value = implode(';', $value);
  422. }
  423. break;
  424. // These values always seem to be wrong, just use the Metatag defaults.
  425. case 'og:type':
  426. $value = FALSE;
  427. break;
  428. // Nodewords handle the title tag differently.
  429. case 'page_title':
  430. $name = 'title';
  431. // Remove two options that are no longer used.
  432. unset($value['append']);
  433. unset($value['divider']);
  434. break;
  435. // A bug in Nodewords resulted in lots of junk data for this meta tag.
  436. case 'revisit-after':
  437. if (isset($value['value']) && intval($value['value']) === 1) {
  438. $value = FALSE;
  439. }
  440. // Robots needs some extra processing.
  441. case 'robots':
  442. // The value didn't exist or it was set to use the defaults.
  443. if (!is_array($value) || empty($value['value']) || !empty($value['use_default'])) {
  444. $value = FALSE;
  445. }
  446. // Try parsing the data.
  447. else {
  448. $robot_data = array();
  449. // Convert each value to display the name if it is "on" and 0 if it is
  450. // off.
  451. $found = FALSE;
  452. foreach ($value['value'] as $robot_key => $robot_val) {
  453. // Ignore junk values.
  454. if ($robot_key == 'value') {
  455. continue;
  456. }
  457. // Only keep non-empty values.
  458. elseif (!empty($robot_val)) {
  459. $robot_data[$robot_key] = $robot_key;
  460. $found = TRUE;
  461. }
  462. }
  463. // Catch empty values.
  464. if (empty($robot_data)) {
  465. $value = FALSE;
  466. }
  467. // Return any data that's remaining. The data must be stored in an
  468. // array with a single item named 'value'.
  469. else {
  470. $value = array(
  471. 'value' => $robot_data,
  472. );
  473. }
  474. }
  475. break;
  476. // This meta tag was renamed.
  477. case 'shorturl':
  478. $name = 'shortlink';
  479. break;
  480. // Everything else should be ok.
  481. default:
  482. // Nothing to see here.
  483. }
  484. // A final tidy-up.
  485. if (is_array($value)) {
  486. foreach ($value as $key => $val) {
  487. $value[$key] = trim($val);
  488. }
  489. $value = array_filter($value);
  490. }
  491. return array($name, $value);
  492. }
  493. /**
  494. * The following will not be converted because they refer to site-wide defaults
  495. * that should be customized appropriately based on the D7 site's content type
  496. * architecture.
  497. */
  498. // 'nodewords_metatags_generation_method_' . $type:
  499. // 0 - NODEWORDS_GENERATION_NEVER - never auto-generate the string.
  500. // 1 - NODEWORDS_GENERATION_WHEN_EMPTY - when the field is empty. Default.
  501. // 2 - NODEWORDS_GENERATION_ALWAYS - always use the generated string.
  502. // 'nodewords_metatags_generation_method_' . $type:
  503. // 1 - NODEWORDS_GENERATION_BODY - use the body field.
  504. // 2 - NODEWORDS_GENERATION_TEASER - use the node teaser. Default.
  505. // 3 - NODEWORDS_GENERATION_TEASER_BODY - use teaser, failover to body if empty.