metatag_importer.nodewords.inc 17 KB

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