metatag.install 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551
  1. <?php
  2. /**
  3. * @file
  4. * Install, update, and uninstall functions for the metatag module.
  5. */
  6. /**
  7. * Implements hook_requirements().
  8. */
  9. function metatag_requirements($phase) {
  10. $requirements = array();
  11. // Ensure translations don't break during installation.
  12. $t = get_t();
  13. if ($phase == 'install') {
  14. // Handle scenarios where the site had the legacy "metatags" module
  15. // installed but then had Metatag installed on top of it.
  16. if (function_exists('db_table_exists') && function_exists('db_field_exists') && function_exists('db_query') && Database::isActiveConnection()) {
  17. // Check if the primary table already exists.
  18. if (db_table_exists('metatag')) {
  19. // Check to see if all of the fields exist in the table. If one of the
  20. // fields does not exist, proceed with the fix.
  21. $fields = array(
  22. 'entity_type',
  23. 'entity_id',
  24. 'revision_id',
  25. 'language',
  26. 'data',
  27. );
  28. foreach ($fields as $field) {
  29. // This field doesn't exist, so determine what to do.
  30. if (!db_field_exists('metatag', $field)) {
  31. // The table contains data, so rename it.
  32. if (db_query("SELECT COUNT(*) FROM {metatag}")->fetchField() > 0) {
  33. db_query("RENAME TABLE {metatag} TO {metatag}_legacy");
  34. $message = 'An out-of-date version of the {metatag} table was discovered. As the table contained data it was renamed with a suffix of "_legacy". This will not prevent installation from continuing, but will need to be dealt with later. See <a href="https://www.drupal.org/node/1391554">https://www.drupal.org/node/1391554</a> for further details.';
  35. }
  36. // The table is empty, so delete it.
  37. else {
  38. db_query("DROP TABLE {metatag}");
  39. $message = 'An out-of-date version of the {metatag} table was discovered. As the table was empty it was simply removed so that it could be recreated in the correct format. Installation may now proceed. See <a href="https://www.drupal.org/node/1391554">https://www.drupal.org/node/1391554</a> for further details.';
  40. }
  41. $requirements['metatag'] = array(
  42. 'severity' => REQUIREMENT_WARNING,
  43. 'title' => 'Metatag',
  44. 'value' => $t('Legacy data discovered.'),
  45. 'description' => $t($message),
  46. );
  47. drupal_set_message($t($message), 'warning');
  48. break;
  49. }
  50. }
  51. }
  52. }
  53. }
  54. elseif ($phase == 'runtime') {
  55. // Work out the release of D7 that is currently running.
  56. list($major, $minor) = explode('.', VERSION);
  57. // Strip off any suffixes on the version string, e.g. "17-dev".
  58. if (strpos('-', $minor)) {
  59. list($minor, $suffix) = explode('-', $minor);
  60. }
  61. // Releases of Drupal older than 7.28 did not have entity_language(), which
  62. // is now required, and had a broken [node:summary] token.
  63. if ($minor < 28) {
  64. $requirements['metatag'] = array(
  65. 'severity' => REQUIREMENT_WARNING,
  66. 'title' => 'Metatag',
  67. 'value' => $t('Upgrade Drupal core to v7.28 or newer'),
  68. 'description' => $t("This older version of Drupal core is missing functionality necessary for the module's multilingual support and contains a broken [node:summary] token, it must be upgraded to version 7.28 or newer."),
  69. );
  70. }
  71. // Everything's OK.
  72. else {
  73. $requirements['metatag'] = array(
  74. 'severity' => REQUIREMENT_OK,
  75. 'title' => 'Metatag',
  76. 'value' => $t('Drupal core is compatible'),
  77. 'description' => $t('Older versions of Drupal core contained bugs that made them incompatible with Metatag, but this version will work correctly.'),
  78. );
  79. }
  80. // Add a note if Page Title is also installed.
  81. if (module_exists('page_title')) {
  82. $requirements['metatag_page_title'] = array(
  83. 'severity' => REQUIREMENT_INFO,
  84. 'title' => 'Metatag',
  85. 'value' => $t('Possible conflicts with Page Title module'),
  86. 'description' => $t('The Metatag module is able to customize page titles so running the Page Title module simultaneously can lead to complications.'),
  87. );
  88. }
  89. // Add a note if the deprecated metatag.entity_translation.inc file still
  90. // exists.
  91. $filename = 'metatag.entity_translation.inc';
  92. if (file_exists(dirname(__FILE__) . '/' . $filename)) {
  93. $requirements['metatag_deprecated_et_file'] = array(
  94. 'severity' => REQUIREMENT_ERROR,
  95. 'title' => 'Metatag',
  96. 'value' => $t('Unwanted :filename file found', array(':filename' => $filename)),
  97. 'description' => $t("The :filename file was removed in v7.x-1.0-beta5 but it still exists in the site's Metatag module's directory and will cause problems. This file needs to be removed. The file's path in the Drupal directory structure is:<br /><code>!short_path</code><br />The file's full path is:<br /><code>!full_path</code>", array(':filename' => $filename, '!short_path' => drupal_get_path('module', 'metatag') . '/' . $filename, '!full_path' => dirname(__FILE__) . $filename)),
  98. );
  99. }
  100. // Check that Entity_Translation is current.
  101. if (module_exists('entity_translation')) {
  102. $rev = db_query("SELECT schema_version FROM {system} WHERE name = :module", array(':module' => 'entity_translation'))->fetchColumn();
  103. if ($rev < 7004) {
  104. $requirements['metatag_et_old'] = array(
  105. 'severity' => REQUIREMENT_ERROR,
  106. 'title' => 'Metatag',
  107. 'value' => $t('<a href="@url">Entity_Translation</a> is out of date and requires updating', array('@url' => 'http://drupal.org/project/entity_translation')),
  108. 'description' => $t('The Entity_Translation module is out of date and needs to be updated in order to be compatible with Metatag.'),
  109. );
  110. }
  111. }
  112. // It's recommended to install the Transliteration module to clean up file
  113. // paths for use with image meta tags.
  114. if (!module_exists('transliteration')) {
  115. $requirements['metatag_transliteration'] = array(
  116. 'severity' => REQUIREMENT_INFO,
  117. 'title' => 'Metatag',
  118. 'value' => $t('The Transliteration module is recommended.'),
  119. 'description' => $t("It is recommended to install the <a href=\"@url\">Transliteration module</a> to clean up filenames of uploaded files that may be used with image meta tags.", array('@url' => 'https://drupal.org/project/transliteration')),
  120. );
  121. }
  122. // It's recommended to install the Imagecache Token module to make image
  123. // tokens easier to do.
  124. if (!module_exists('imagecache_token')) {
  125. $requirements['metatag_imagecache_token'] = array(
  126. 'severity' => REQUIREMENT_INFO,
  127. 'title' => 'Metatag',
  128. 'value' => $t('The Imagecache Token module is recommended.'),
  129. 'description' => $t("It is recommended to install the <a href=\"@url\">Imagecache Token module</a> to make it easier to control image meta tags, e.g. og:image.", array('@url' => 'https://drupal.org/project/imagecache_token')),
  130. );
  131. }
  132. // The Admin Language module can cause problems.
  133. if (!module_exists('admin_language') && variable_get('admin_language_force_neutral', 0)) {
  134. $requirements['metatag_admin_language'] = array(
  135. 'severity' => REQUIREMENT_WARNING,
  136. 'title' => 'Metatag',
  137. 'value' => $t('Conflict with Admin Language module.'),
  138. 'description' => $t("Using the \"@option\" with Metatag can lead to data loss, so it is recommended to <a href=\"@url\">disable that option</a>.", array('@option' => t('Force language neutral aliases'), '@url' => url('admin/config/regional/language/admin_language'))),
  139. );
  140. }
  141. }
  142. return $requirements;
  143. }
  144. /**
  145. * Implements hook_schema().
  146. */
  147. function metatag_schema() {
  148. $schema['metatag_config'] = array(
  149. 'description' => 'Storage of meta tag configuration and defaults.',
  150. 'export' => array(
  151. 'key' => 'instance',
  152. 'key name' => 'Instance',
  153. 'primary key' => 'cid',
  154. 'identifier' => 'config',
  155. 'default hook' => 'metatag_config_default',
  156. 'api' => array(
  157. 'owner' => 'metatag',
  158. 'api' => 'metatag',
  159. 'minimum_version' => 1,
  160. 'current_version' => 1,
  161. ),
  162. 'cache defaults' => TRUE,
  163. 'default cache bin' => 'cache_metatag',
  164. ),
  165. 'fields' => array(
  166. 'cid' => array(
  167. 'type' => 'serial',
  168. 'unsigned' => TRUE,
  169. 'not null' => TRUE,
  170. 'description' => 'The primary identifier for a metatag configuration set.',
  171. 'no export' => TRUE,
  172. ),
  173. 'instance' => array(
  174. 'type' => 'varchar',
  175. 'length' => 255,
  176. 'not null' => TRUE,
  177. 'default' => '',
  178. 'description' => 'The machine-name of the configuration, typically entity-type:bundle.',
  179. ),
  180. 'config' => array(
  181. 'type' => 'blob',
  182. 'size' => 'big',
  183. 'not null' => TRUE,
  184. 'serialize' => TRUE,
  185. 'description' => 'Serialized data containing the meta tag configuration.',
  186. 'translatable' => TRUE,
  187. ),
  188. ),
  189. 'primary key' => array('cid'),
  190. 'unique keys' => array(
  191. 'instance' => array('instance'),
  192. ),
  193. );
  194. $schema['metatag'] = array(
  195. 'fields' => array(
  196. 'entity_type' => array(
  197. 'type' => 'varchar',
  198. 'length' => 32,
  199. 'not null' => TRUE,
  200. 'default' => '',
  201. 'description' => 'The entity type this data is attached to.',
  202. ),
  203. 'entity_id' => array(
  204. 'type' => 'int',
  205. 'unsigned' => TRUE,
  206. 'not null' => TRUE,
  207. 'default' => 0,
  208. 'description' => 'The entity id this data is attached to.',
  209. ),
  210. 'revision_id' => array(
  211. 'type' => 'int',
  212. 'unsigned' => TRUE,
  213. 'not null' => TRUE,
  214. 'default' => 0,
  215. 'description' => 'The revision_id for the entity object this data is attached to.',
  216. ),
  217. 'language' => array(
  218. 'type' => 'varchar',
  219. 'length' => 32,
  220. 'not null' => TRUE,
  221. 'default' => '',
  222. 'description' => 'The language of the tag.',
  223. ),
  224. 'data' => array(
  225. 'type' => 'blob',
  226. 'size' => 'big',
  227. 'not null' => TRUE,
  228. 'serialize' => TRUE,
  229. ),
  230. ),
  231. 'indexes' => array(
  232. 'type_revision' => array('entity_type','revision_id'),
  233. ),
  234. 'primary key' => array('entity_type', 'entity_id', 'revision_id', 'language'),
  235. );
  236. $schema['cache_metatag'] = drupal_get_schema_unprocessed('system', 'cache');
  237. $schema['cache_metatag']['description'] = t('Cache table for the generated meta tag output.');
  238. return $schema;
  239. }
  240. /**
  241. * Implements hook_install().
  242. */
  243. function metatag_install() {
  244. drupal_set_message(t("Thank you for installing the Metatag module. It is recommended to read the module's <a href=\"!url\" title=\"Read the Metatag module's documentation\">README.txt</a> file as there are some known issues that may affect this site.", array('!url' => url(drupal_get_path('module', 'metatag') . '/README.txt'))));
  245. }
  246. /**
  247. * Implements hook_uninstall().
  248. */
  249. function metatag_uninstall() {
  250. // This variable is created via hook_enable.
  251. variable_del('metatag_schema_installed');
  252. // Used to control whether 403/404 pages are cached.
  253. variable_del('metatag_cache_error_pages');
  254. // Used to make meta tags display on admin pages.
  255. variable_del('metatag_tag_admin_pages');
  256. // Temp variables, just in case they weren't removed already.
  257. variable_del('metatag_skip_update_7017');
  258. // Used to note that the schema for the main {metatag} table were sufficiently
  259. // updated.
  260. variable_del('metatag_has_revision_id');
  261. // Used to force an entity's default language values to be used if nothing
  262. // else matched.
  263. variable_del('metatag_entity_no_lang_default');
  264. }
  265. /**
  266. * Implements hook_enable().
  267. */
  268. function metatag_enable() {
  269. variable_set('metatag_schema_installed', TRUE);
  270. }
  271. /**
  272. * Implements hook_disable().
  273. */
  274. // function metatag_disable() {
  275. // }
  276. /**
  277. * Disable the deprecated metatag_ui module which has been merged into metatag.
  278. */
  279. function metatag_update_7000() {
  280. if (module_exists('metatag_ui')) {
  281. module_disable(array('metatag_ui'), FALSE);
  282. drupal_uninstall_modules(array('metatag_ui'), FALSE);
  283. }
  284. }
  285. /**
  286. * Fix the "{metatag_config}.cid column cannot be NULL" error.
  287. */
  288. function metatag_update_7001() {
  289. $table_name = 'metatag_config';
  290. $field_name = 'cid';
  291. $field_spec = array(
  292. 'type' => 'serial',
  293. 'unsigned' => TRUE,
  294. 'not null' => TRUE,
  295. 'description' => 'The primary identifier for a metatag configuration set.',
  296. );
  297. $keys = array('primary key' => array($field_name));
  298. // Before making any changes, drop the existing primary key.
  299. // Let's add a temporary unique key for cid so MySQL will let it go.
  300. // Hint taken from https://drupal.org/node/2064305#comment-7753197.
  301. db_add_unique_key($table_name, 'temp_key', array($field_name, 'instance'));
  302. // Unforunately there is no API way to check if a primary key exists, so if
  303. // it doesn't exist the db_drop_primary_key() call will fail.
  304. try {
  305. db_drop_primary_key($table_name);
  306. }
  307. catch (Exception $e) {
  308. drupal_set_message('Caught an exception: ', $e->getMessage());
  309. }
  310. // Rejig the field, and turn on the primary key again.
  311. db_change_field($table_name, $field_name, $field_name, $field_spec, $keys);
  312. // Finally, remove the temporary unique key because it's no longer useful.
  313. db_drop_unique_key($table_name, 'temp_key');
  314. }
  315. /**
  316. * Disable the deprecated metatag_ui module which has been merged into metatag,
  317. * again.
  318. */
  319. function metatag_update_7002() {
  320. if (module_exists('metatag_ui')) {
  321. module_disable(array('metatag_ui'), FALSE);
  322. drupal_uninstall_modules(array('metatag_ui'), FALSE);
  323. drupal_set_message(t('The deprecated Metatag UI module has been disabled.'));
  324. }
  325. }
  326. /**
  327. * Add the {metatag}.language field.
  328. */
  329. function metatag_update_7003() {
  330. // Set the target table and field name.
  331. $table_name = 'metatag';
  332. $field_name = 'language';
  333. // Don't add the new field if it already exists.
  334. if (!db_field_exists($table_name, $field_name)) {
  335. // Describe the new field.
  336. $field_spec = array(
  337. 'type' => 'varchar',
  338. 'length' => 32,
  339. 'not null' => TRUE,
  340. 'default' => '',
  341. 'description' => 'The language of the tag',
  342. );
  343. // Add it and update the primary key.
  344. db_add_field($table_name, $field_name, $field_spec);
  345. db_drop_primary_key($table_name);
  346. db_add_primary_key($table_name, array('entity_type', 'entity_id', 'language'));
  347. }
  348. }
  349. /**
  350. * Replaced by updates 7009, 7010, 7011, 7012 and 7013.
  351. */
  352. function metatag_update_7004() {
  353. // Do nothing.
  354. }
  355. /**
  356. * Removing wrong metatag watchdog entries that break the admin/reports/dblog
  357. * page.
  358. */
  359. function metatag_update_7005() {
  360. if (db_table_exists('watchdog')) {
  361. db_delete('watchdog')
  362. ->condition('type', 'metatag')
  363. ->condition('variables', serialize('info'))
  364. ->execute();
  365. }
  366. }
  367. /**
  368. * Remove {metatag} records that were added by old versions of the module for
  369. * entities that don't actually support Metatag. A more complete version of
  370. * this will be added later on after it's (hopefully) guaranteed that all
  371. * modules have updated to the correct API usage.
  372. */
  373. function metatag_update_7006() {
  374. $entity_types = array(
  375. // Core.
  376. 'comment',
  377. 'menu_link',
  378. 'taxonomy_vocabulary',
  379. // Some contrib entities.
  380. 'mailchimp_list',
  381. 'profile2',
  382. 'profile2_type',
  383. 'redirect',
  384. 'rules_config',
  385. 'wysiwyg_profile',
  386. );
  387. foreach ($entity_types as $entity_type) {
  388. $num_deleted = db_delete('metatag')
  389. ->condition('entity_type', $entity_type)
  390. ->execute();
  391. if ($num_deleted > 0) {
  392. drupal_set_message(t('Removed @count meta tag record(s) for the @type entity type, it does not support meta tags.', array('@count' => $num_deleted, '@type' => $entity_type)));
  393. }
  394. }
  395. }
  396. /**
  397. * Remove {metatag} records for nodes, users and taxonomy terms that have been
  398. * deleted; older versions of Metatag may have failed to purge these.
  399. */
  400. function metatag_update_7007() {
  401. $nodes = db_query("SELECT m.entity_id
  402. FROM {metatag} m
  403. LEFT OUTER JOIN {node} n
  404. ON m.entity_id=n.nid
  405. WHERE m.entity_type='node'
  406. AND n.nid IS NULL")
  407. ->fetchCol();
  408. if (count($nodes) > 0) {
  409. $deleted = db_delete('metatag')
  410. ->condition('entity_type', 'node')
  411. ->condition('entity_id', $nodes)
  412. ->execute();
  413. if ($deleted > 0) {
  414. drupal_set_message(t('Removed @count meta tag record(s) for nodes that had been purged.', array('@count' => $deleted)));
  415. }
  416. else {
  417. drupal_set_message(t('There were no meta tag records to purge for removed nodes. This is a good thing :)'));
  418. }
  419. }
  420. $users = db_query("SELECT m.entity_id
  421. FROM {metatag} m
  422. LEFT OUTER JOIN {users} u
  423. ON m.entity_id=u.uid
  424. WHERE m.entity_type='user'
  425. AND u.uid IS NULL")
  426. ->fetchCol();
  427. if (count($users) > 0) {
  428. $deleted = db_delete('metatag')
  429. ->condition('entity_type', 'user')
  430. ->condition('entity_id', $users)
  431. ->execute();
  432. if ($deleted > 0) {
  433. drupal_set_message(t('Removed @count meta tag record(s) for users that had been purged.', array('@count' => $deleted)));
  434. }
  435. else {
  436. drupal_set_message(t('There were no meta tag records to purge for removed users. This is a good thing :)'));
  437. }
  438. }
  439. // Only run this if the Taxonomy module is enabled.
  440. if (module_exists('taxonomy')) {
  441. $terms = db_query("SELECT m.entity_id
  442. FROM {metatag} m
  443. LEFT OUTER JOIN {taxonomy_term_data} t
  444. ON m.entity_id=t.tid
  445. WHERE m.entity_type='taxonomy_term'
  446. AND t.tid IS NULL")
  447. ->fetchCol();
  448. if (count($terms) > 0) {
  449. $deleted = db_delete('metatag')
  450. ->condition('entity_type', 'taxonomy_term')
  451. ->condition('entity_id', $terms)
  452. ->execute();
  453. if ($deleted > 0) {
  454. drupal_set_message(t('Removed @count meta tag record(s) for taxonomy terms that had been purged.', array('@count' => $deleted)));
  455. }
  456. else {
  457. drupal_set_message(t('There were no meta tag records to purge for removed taxonomy terms. This is a good thing :)'));
  458. }
  459. }
  460. }
  461. }
  462. /**
  463. * Remove any empty records that may be hanging around from old releases.
  464. */
  465. function metatag_update_7008() {
  466. $conditions = db_or()
  467. ->isNull('data')
  468. ->condition('data', '')
  469. ->condition('data', serialize(array()));
  470. $deleted = db_delete("metatag")
  471. ->condition($conditions)
  472. ->execute();
  473. if ($deleted > 0) {
  474. drupal_set_message(t('Purged @count empty meta tag record(s).', array('@count' => $deleted)));
  475. }
  476. }
  477. /**
  478. * Fix {metatag} records for taxonomy terms.
  479. */
  480. function metatag_update_7009() {
  481. if (module_exists('taxonomy')) {
  482. // Fix the {metatag} table first.
  483. metatag_update_7015();
  484. // Remove duplicates.
  485. _metatag_remove_dupes('taxonomy_term');
  486. }
  487. // The taxonomy term entity doesn't support a 'language' option, so reset it
  488. // to LANGUAGE_NONE.
  489. $result = db_query("UPDATE {metatag} SET language = :language WHERE entity_type='taxonomy_term'", array(':language' => LANGUAGE_NONE));
  490. if ($result->rowCount() > 0) {
  491. drupal_set_message(t('Fixed language values for @count taxonomy terms.', array('@count' => $result->rowCount())));
  492. }
  493. }
  494. /**
  495. * Fix {metatag} records for users.
  496. */
  497. function metatag_update_7010() {
  498. // Fix the {metatag} table first.
  499. metatag_update_7015();
  500. // Remove duplicates.
  501. _metatag_remove_dupes('user');
  502. // Update User values.
  503. $result = db_query("UPDATE {metatag} SET language = :language WHERE entity_type='user'", array(':language' => LANGUAGE_NONE));
  504. if ($result->rowCount() > 0) {
  505. drupal_set_message(t('Fixed language values for @count user records.', array('@count' => $result->rowCount())));
  506. }
  507. }
  508. /**
  509. * Fix {metatag} records for nodes.
  510. */
  511. function metatag_update_7011(&$sandbox) {
  512. // Fix the {metatag} table first.
  513. metatag_update_7015();
  514. // Only proceed if Entity_Translation is not enabled as it allows each node
  515. // record to have multiple languages available.
  516. if (module_exists('entity_translation')) {
  517. drupal_set_message(t("Entity Translation is enabled, so node meta tags will not be updated, to avoid accidental dataloss."));
  518. return;
  519. }
  520. // Process records by groups of 10 (arbitrary value).
  521. // When a group is processed, the batch update engine determines whether it
  522. // should continue processing in the same request or provide progress
  523. // feedback to the user and wait for the next request.
  524. $limit = 10;
  525. // When ran through Drush it's Ok to process a larger number of objects at a
  526. // time.
  527. if (drupal_is_cli()) {
  528. $limit = 100;
  529. }
  530. // Use the sandbox at your convenience to store the information needed
  531. // to track progression between successive calls to the function.
  532. if (!isset($sandbox['progress'])) {
  533. // The count of records visited so far.
  534. $sandbox['progress'] = 0;
  535. // Remove duplicates.
  536. _metatag_remove_dupes('node');
  537. // Update Node values.
  538. $nodes = db_query("SELECT n.nid, n.language FROM {node} n INNER JOIN {metatag} m ON n.nid = m.entity_id WHERE m.entity_type = 'node' AND n.language != m.language ORDER BY nid");
  539. $sandbox['records'] = array();
  540. foreach ($nodes as $record) {
  541. $sandbox['records'][] = $record;
  542. }
  543. // If there's no data, don't bother with the extra work.
  544. if (empty($sandbox['records'])) {
  545. watchdog('metatag', 'Update 7011: No nodes need the Metatag language values fixed.', array(), WATCHDOG_INFO);
  546. if (drupal_is_cli()) {
  547. drupal_set_message(t('Update 7011: No nodes need the Metatag language values fixed.'));
  548. }
  549. return t('No nodes need the Metatag language values fixed.');
  550. }
  551. // Total records that must be visited.
  552. $sandbox['max'] = count($sandbox['records']);
  553. // A place to store messages during the run.
  554. $sandbox['messages'] = array();
  555. // An initial record of the number of records to be updated.
  556. watchdog('metatag', 'Update 7011: !count records to update.', array('!count' => $sandbox['max']), WATCHDOG_INFO);
  557. if (drupal_is_cli()) {
  558. drupal_set_message(t('Update 7011: !count records to update.', array('!count' => $sandbox['max'])));
  559. }
  560. // Last record processed.
  561. $sandbox['current_record'] = -1;
  562. // Because a lot of other processing happens on the first iteration, just do
  563. // one.
  564. $limit = 1;
  565. }
  566. // Set default values.
  567. for ($ctr = 0; $ctr < $limit; $ctr++) {
  568. $sandbox['current_record']++;
  569. if (empty($sandbox['records'][$sandbox['current_record']])) {
  570. break;
  571. }
  572. // Shortcuts for later.
  573. $langcode = $sandbox['records'][$sandbox['current_record']]->language;
  574. $nid = $sandbox['records'][$sandbox['current_record']]->nid;
  575. db_update('metatag')
  576. ->fields(array('language' => $langcode))
  577. ->condition('entity_type', 'node')
  578. ->condition('entity_id', $nid)
  579. ->execute();
  580. // Update our progress information.
  581. $sandbox['progress']++;
  582. }
  583. // Set the "finished" status, to tell batch engine whether this function
  584. // needs to run again. If you set a float, this will indicate the progress of
  585. // the batch so the progress bar will update.
  586. $sandbox['#finished'] = ($sandbox['progress'] >= $sandbox['max']) ? TRUE : ($sandbox['progress'] / $sandbox['max']);
  587. if ($sandbox['#finished']) {
  588. // Clear all caches so the fixed data will be reloaded.
  589. cache_clear_all('*', 'cache_metatag', TRUE);
  590. // A final log of the number of records that were converted.
  591. watchdog('metatag', 'Update 7011: !count records were updated in total.', array('!count' => $sandbox['progress']), WATCHDOG_INFO);
  592. if (drupal_is_cli()) {
  593. drupal_set_message(t('Update 7011: !count records were updated.', array('!count' => $sandbox['progress'])));
  594. }
  595. // hook_update_N() may optionally return a string which will be displayed
  596. // to the user.
  597. return t('Fixed the Metatag language values for @count nodes.', array('!count' => $sandbox['progress']));
  598. }
  599. }
  600. /**
  601. * Remove duplicate {metatag} records for non-core entities.
  602. */
  603. function metatag_update_7012() {
  604. // Fix the {metatag} table first.
  605. metatag_update_7015();
  606. if (module_exists('entity_translation')) {
  607. drupal_set_message(t("Entity Translation is enabled, duplicate meta tags will not be removed for custom entities, to avoid accidental dataloss."));
  608. return;
  609. }
  610. $records = db_select('metatag', 'm')
  611. ->fields('m', array('entity_type'))
  612. ->condition('m.entity_type', array('node', 'taxonomy_term', 'user'), 'NOT IN')
  613. ->orderBy('m.entity_type', 'ASC')
  614. ->orderBy('m.entity_id', 'ASC')
  615. ->distinct()
  616. ->execute();
  617. $entity_types = array();
  618. foreach ($records as $record) {
  619. $entity_types[] = $record->entity_type;
  620. // Remove duplicates.
  621. _metatag_remove_dupes($record->entity_type);
  622. }
  623. if (empty($entity_types)) {
  624. drupal_set_message(t('There were no other records to fix.'));
  625. }
  626. }
  627. /**
  628. * Fix the {metatag} language value for all non-core entity records. This might
  629. * take a while, depending on how much data needs to be converted.
  630. */
  631. function metatag_update_7013(&$sandbox) {
  632. // Fix the {metatag} table first.
  633. metatag_update_7015();
  634. if (module_exists('entity_translation')) {
  635. drupal_set_message(t("Entity Translation is enabled, meta tags will not be updated for custom entities, to avoid accidental dataloss."));
  636. return;
  637. }
  638. // Use the sandbox at your convenience to store the information needed
  639. // to track progression between successive calls to the function.
  640. if (!isset($sandbox['progress'])) {
  641. // The count of records visited so far.
  642. $sandbox['progress'] = 0;
  643. // Because the {metatag} table uses multiple primary keys, there's no easy
  644. // way to do this, so we're going to cache all record keys and manually
  645. // step through them.
  646. $records = db_select('metatag', 'm')
  647. ->fields('m', array('entity_type', 'entity_id'))
  648. ->condition('m.entity_type', array('node', 'taxonomy_term', 'user'), 'NOT IN')
  649. ->orderBy('m.entity_type', 'ASC')
  650. ->orderBy('m.entity_id', 'ASC')
  651. ->execute();
  652. $sandbox['records'] = array();
  653. foreach ($records as $record) {
  654. $sandbox['records'][] = $record;
  655. }
  656. // If there's no data, don't bother with the extra work.
  657. if (empty($sandbox['records'])) {
  658. watchdog('metatag', 'Update 7013: No meta tag records need updating.', array(), WATCHDOG_INFO);
  659. if (drupal_is_cli()) {
  660. drupal_set_message(t('Update 7013: No meta tag records need updating.'));
  661. }
  662. return t('No meta tag records need updating.');
  663. }
  664. // Total records that must be visited.
  665. $sandbox['max'] = count($sandbox['records']);
  666. // A place to store messages during the run.
  667. $sandbox['messages'] = array();
  668. // An initial record of the number of records to be updated.
  669. watchdog('metatag', 'Update 7013: !count records to update.', array('!count' => $sandbox['max']), WATCHDOG_INFO);
  670. if (drupal_is_cli()) {
  671. drupal_set_message(t('Update 7013: !count records to update.', array('!count' => $sandbox['max'])));
  672. }
  673. // Last record processed.
  674. $sandbox['current_record'] = -1;
  675. }
  676. // Process records by groups of 10 (arbitrary value).
  677. // When a group is processed, the batch update engine determines whether it
  678. // should continue processing in the same request or provide progress
  679. // feedback to the user and wait for the next request.
  680. $limit = 10;
  681. // When ran through Drush it's Ok to process a larger number of objects at a
  682. // time.
  683. if (drupal_is_cli()) {
  684. $limit = 100;
  685. }
  686. // Set default values.
  687. for ($ctr = 0; $ctr < $limit; $ctr++) {
  688. $sandbox['current_record']++;
  689. if (empty($sandbox['records'][$sandbox['current_record']])) {
  690. break;
  691. }
  692. // Shortcuts for later.
  693. $entity_type = $sandbox['records'][$sandbox['current_record']]->entity_type;
  694. $entity_id = $sandbox['records'][$sandbox['current_record']]->entity_id;
  695. // Load the entity.
  696. $entities = entity_load($entity_type, array($entity_id));
  697. if (!empty($entities)) {
  698. $entity = array_pop($entities);
  699. // Make sure that the entity has a language set.
  700. if (!empty($entity)) {
  701. // If there's a (non-empty) language value, use it.
  702. $new_language = entity_language($entity_type, $entity);
  703. if (empty($new_language)) {
  704. $new_language = LANGUAGE_NONE;
  705. }
  706. // Update the 'language' value.
  707. db_update('metatag')
  708. ->fields(array('language' => $new_language))
  709. ->condition('entity_type', $entity_type)
  710. ->condition('entity_id', $entity_id)
  711. ->execute();
  712. }
  713. }
  714. // Update our progress information.
  715. $sandbox['progress']++;
  716. }
  717. // Set the "finished" status, to tell batch engine whether this function
  718. // needs to run again. If you set a float, this will indicate the progress of
  719. // the batch so the progress bar will update.
  720. $sandbox['#finished'] = ($sandbox['progress'] >= $sandbox['max']) ? TRUE : ($sandbox['progress'] / $sandbox['max']);
  721. if ($sandbox['#finished']) {
  722. // Clear all caches so the fixed data will be reloaded.
  723. cache_clear_all('*', 'cache_metatag', TRUE);
  724. // A final log of the number of records that were converted.
  725. watchdog('metatag', 'Update 7013: !count records were updated in total.', array('!count' => $sandbox['progress']), WATCHDOG_INFO);
  726. if (drupal_is_cli()) {
  727. drupal_set_message(t('Update 7013: !count records were updated.', array('!count' => $sandbox['progress'])));
  728. }
  729. // hook_update_N() may optionally return a string which will be displayed
  730. // to the user.
  731. return t('!count records were updated in total.', array('!count' => $sandbox['progress']));
  732. }
  733. }
  734. /**
  735. * Remove duplicate records for a given entity.
  736. *
  737. * It should be OK to run this without doing a separate batch process as there
  738. * shouldn't be many records that have this problem. Hopefully.
  739. *
  740. * @param $entity_type
  741. * The name of an entity type to check for.
  742. */
  743. function _metatag_remove_dupes($entity_type) {
  744. $purge_count = 0;
  745. // First step: fix the records. There should not be multiple records for the
  746. // same entity_id with different languages.
  747. $dupe_records = db_query("SELECT m.entity_id, count(m.language) AS the_count
  748. FROM {metatag} m
  749. WHERE
  750. m.entity_type = :type
  751. GROUP BY m.entity_id
  752. HAVING count(m.language) > 1", array(':type' => $entity_type));
  753. if (!empty($dupe_records)) {
  754. foreach ($dupe_records as $record) {
  755. $entity_id = $record->entity_id;
  756. $langs = db_query("SELECT m.entity_id, m.language, m.data FROM {metatag} m WHERE m.entity_type = :type AND m.entity_id = :id", array(':type' => $entity_type, ':id' => $entity_id))->fetchAll();
  757. // Work out which language record to remove. Will need to store this as
  758. // an array incase there are multiple records to purge.
  759. $langs_to_remove = array();
  760. // Check for duplicate records.
  761. // Outer loop starts from the beginning.
  762. for ($outer = 0; $outer < count($langs); $outer++) {
  763. // This record may have been removed already.
  764. if (isset($langs[$outer])) {
  765. // Inner loop starts from the end.
  766. for ($inner = count($langs) - 1; $inner > 0; $inner--) {
  767. // Work out if the outer loop's data is the same as the inner
  768. // loop's.
  769. if (isset($langs[$inner]) && $langs[$outer]->data == $langs[$inner]->data) {
  770. // Remove the second record.
  771. $langs_to_remove[] = $langs[$inner]->language;
  772. unset($langs[$inner]);
  773. }
  774. }
  775. }
  776. }
  777. // Only one record left.
  778. if (count($langs) == 1) {
  779. // This is how it should be, this record is fine.
  780. }
  781. // More than one record, work out which one to keep.
  782. elseif (count($langs) > 1) {
  783. // Work out the entity's language.
  784. $entity = entity_load($entity_type, $entity_id);
  785. $entity_language = entity_language($entity_type, $entity);
  786. if (empty($language)) {
  787. $entity_language = LANGUAGE_NONE;
  788. }
  789. // Work out if the entity's language record exists.
  790. $lang_pos = NULL;
  791. foreach ($langs as $key => $record) {
  792. if ($record->language == $entity_language) {
  793. $lang_pos = $key;
  794. break;
  795. }
  796. }
  797. // If the language record exists, delete the others.
  798. if (isset($lang_pos)) {
  799. foreach ($langs as $key => $record) {
  800. if ($record->language != $entity_language) {
  801. $langs_to_remove[] = $record->language;
  802. }
  803. }
  804. }
  805. // Otherwise look for a record for the site's default language.
  806. else {
  807. foreach ($langs as $key => $record) {
  808. if ($record->language == $GLOBALS['language']->language) {
  809. $lang_pos = $key;
  810. break;
  811. }
  812. }
  813. if (isset($lang_pos)) {
  814. foreach ($langs as $key => $record) {
  815. if ($record->language != $GLOBALS['language']->language) {
  816. $langs_to_remove[] = $record->language;
  817. }
  818. }
  819. }
  820. // Finally check for LANGUAGE_NONE.
  821. else {
  822. foreach ($langs as $key => $record) {
  823. if ($record->language == LANGUAGE_NONE) {
  824. $lang_pos = $key;
  825. break;
  826. }
  827. }
  828. if (isset($lang_pos)) {
  829. foreach ($langs as $key => $record) {
  830. if ($record->language != LANGUAGE_NONE) {
  831. $langs_to_remove[] = $record->language;
  832. }
  833. }
  834. }
  835. }
  836. }
  837. }
  838. // Purge the redundant records.
  839. if (!empty($langs_to_remove)) {
  840. $purge_count += db_delete('metatag')
  841. ->condition('entity_type', $entity_type)
  842. ->condition('entity_id', $entity_id)
  843. ->condition('language', $langs_to_remove)
  844. ->execute();
  845. }
  846. }
  847. }
  848. if (empty($purge_count)) {
  849. drupal_set_message(t('No duplicate :entity_type records were found (this is a good thing).', array(':entity_type' => $entity_type)));
  850. watchdog('metatag', 'No duplicate :entity_type records were found (this is a good thing).', array(':entity_type' => $entity_type));
  851. }
  852. else {
  853. drupal_set_message(t('Purged :count duplicate :entity_type record(s).', array(':count' => $purge_count, ':entity_type' => $entity_type)));
  854. watchdog('metatag', 'Purged :count duplicate :entity_type record(s).', array(':count' => $purge_count, ':entity_type' => $entity_type));
  855. return;
  856. }
  857. }
  858. /**
  859. * Fix {metatag} records that may have been corrupted by #1871020.
  860. */
  861. function metatag_update_7014() {
  862. $records = db_query("SELECT *
  863. FROM {metatag} m
  864. WHERE
  865. m.data LIKE :nolang
  866. OR m.data LIKE :lang
  867. OR m.data LIKE :und",
  868. array(
  869. ':nolang' => 'a:1:{s:0:"";a:%:{s:%;a:%:{%;}}}',
  870. ':lang' => 'a:1:{s:2:"__";a:%:{s:%;a:%:{%;}}}',
  871. ':und' => 'a:1:{s:3:"___";a:%:{s:%;a:%:{%;}}}',
  872. ));
  873. // Nothing to fix.
  874. if ($records->rowCount() == 0) {
  875. drupal_set_message(t('No corrupt records to fix, this is good news :-)'));
  876. }
  877. // Fix the faulty records.
  878. else {
  879. foreach ($records as $record) {
  880. // Extract the data and get the first element of the array, this should be
  881. // valid data.
  882. $record->data = reset(unserialize($record->data));
  883. // Update the record.
  884. drupal_write_record('metatag', $record, array('entity_type', 'entity_id', 'language'));
  885. }
  886. drupal_set_message(t('Fixed @count corrupt meta tag record(s).', array('@count' => $records->rowCount())));
  887. }
  888. }
  889. /**
  890. * Add the revision_id from the entity into metatag schema, adjust the primary
  891. * keys accordingly.
  892. */
  893. function metatag_update_7015() {
  894. if (!db_field_exists('metatag', 'revision_id')) {
  895. // Leave a note for metatag_metatags_load_multiple() that the revision_id
  896. // field has been added.
  897. variable_set('metatag_has_revision_id', TRUE);
  898. // Tell update 7017 that it isn't needed.
  899. variable_set('metatag_skip_update_7017', TRUE);
  900. // Add the new field.
  901. db_add_field('metatag', 'revision_id', array(
  902. 'type' => 'int',
  903. 'unsigned' => TRUE,
  904. 'not null' => TRUE,
  905. 'default' => 0,
  906. 'description' => 'The revision_id for the entity object this data is attached to.',
  907. ));
  908. // Remove the existing primary key. This may take some work so it can be
  909. // database agnostic, i.e. some databases will not like it.
  910. db_drop_primary_key('metatag');
  911. // Add the new primary key.
  912. db_add_primary_key('metatag', array('entity_type', 'entity_id', 'revision_id', 'language'));
  913. drupal_set_message(t('Added the {metatag}.revision_id field.'));
  914. }
  915. else {
  916. drupal_set_message(t('The {metatag}.revision_id field has already been added, nothing to do.'));
  917. }
  918. }
  919. /**
  920. * Update the revision ID to fix the NULL values, help avoid problems with
  921. * update 7017.
  922. */
  923. function metatag_update_7016() {
  924. // It's possible that 7015 was not executed if the site had been updated to
  925. // an early dev release, so make sure the revision_id field exists.
  926. metatag_update_7015();
  927. // Run the update.
  928. db_query("UPDATE {metatag} SET revision_id = 0 WHERE revision_id IS NULL");
  929. }
  930. /**
  931. * The {metatag}.revision_id field is required.
  932. */
  933. function metatag_update_7017() {
  934. if (!variable_get('metatag_skip_update_7017', FALSE)) {
  935. // Let's add a temporary unique key so MySQL will let it go.
  936. db_add_unique_key('metatag', 'temp_key', array('entity_type', 'entity_id', 'revision_id', 'language'));
  937. // Now remove the PK before changing a field from serial.
  938. db_drop_primary_key('metatag');
  939. // Change the field.
  940. db_change_field('metatag', 'revision_id', 'revision_id', array(
  941. 'type' => 'int',
  942. 'unsigned' => TRUE,
  943. 'not null' => TRUE,
  944. 'default' => 0,
  945. 'description' => 'The revision_id for the entity object this data is attached to.',
  946. ));
  947. // Manually re-add the PK.
  948. db_add_primary_key('metatag', array('entity_type', 'entity_id', 'revision_id', 'language'));
  949. // Finally, remove the temporary unique key because it's no longer useful.
  950. db_drop_unique_key('metatag', 'temp_key');
  951. drupal_set_message(t('Fixed the {metatag}.revision_id field.'));
  952. }
  953. else {
  954. drupal_set_message(t("Didn't need to fix the {metatag}.revision_id field; please disperse, nothing to see here."));
  955. }
  956. // Delete the temporary variable.
  957. variable_del('metatag_skip_update_7017');
  958. }
  959. /**
  960. * Update the revision ID for each record. This may take some time. Should any
  961. * nodes be discovered with a meta tag record for both revision_id 0 and the
  962. * correct revision_id, the "0" value will be deleted; if this is not the
  963. * desired result the {metatag} table must be manually pruned to have the
  964. * correct records prior to letting this update run.
  965. */
  966. function metatag_update_7018(&$sandbox) {
  967. // Process records in small groups.
  968. // When a group is processed, the batch update engine determines whether it
  969. // should continue processing in the same request or provide progress
  970. // feedback to the user and wait for the next request.
  971. $limit = 10;
  972. // When ran through Drush it's Ok to process a larger number of objects at a
  973. // time.
  974. if (drupal_is_cli()) {
  975. $limit = 100;
  976. }
  977. // Use the sandbox at your convenience to store the information needed
  978. // to track progression between successive calls to the function.
  979. if (!isset($sandbox['progress'])) {
  980. // The count of records visited so far.
  981. $sandbox['progress'] = 0;
  982. // Get a list of all records affected.
  983. $sandbox['records'] = db_query("SELECT entity_type, entity_id, language
  984. FROM {metatag}
  985. WHERE revision_id = 0")
  986. ->fetchAll();
  987. // If there's no data, don't bother with the extra work.
  988. if (empty($sandbox['records'])) {
  989. watchdog('metatag', 'Update 7018: No {metatag} records needed to have the revision_id value fixed.', array(), WATCHDOG_INFO);
  990. if (drupal_is_cli()) {
  991. drupal_set_message(t('Update 7018: No {metatag} records needed to have the revision_id value fixed.'));
  992. }
  993. return t('No {metatag} records needed to have the revision_id value fixed.');
  994. }
  995. // Total records that must be visited.
  996. $sandbox['max'] = count($sandbox['records']);
  997. // A place to store messages during the run.
  998. $sandbox['messages'] = array();
  999. // An initial record of the number of records to be updated.
  1000. watchdog('metatag', 'Update 7018: !count records to update.', array('!count' => $sandbox['max']), WATCHDOG_INFO);
  1001. if (drupal_is_cli()) {
  1002. drupal_set_message(t('Update 7018: !count records to update.', array('!count' => $sandbox['max'])));
  1003. }
  1004. // Last record processed.
  1005. $sandbox['current_record'] = -1;
  1006. // Because a lot of other processing happens on the first iteration, just do
  1007. // one.
  1008. $limit = 1;
  1009. }
  1010. // Work out which entities support languages and revisions.
  1011. $has_language = array();
  1012. $has_revisions = array();
  1013. foreach (entity_get_info() as $entity_type => $info) {
  1014. $has_language[$entity_type] = FALSE;
  1015. $has_revisions[$entity_type] = FALSE;
  1016. if (!empty($info['entity keys']['language'])) {
  1017. $has_language[$entity_type] = $info['entity keys']['language'];
  1018. }
  1019. if (!empty($info['entity keys']['revision'])) {
  1020. $has_revisions[$entity_type] = $info['entity keys']['revision'];
  1021. }
  1022. }
  1023. // Set default values.
  1024. for ($ctr = 0; $ctr < $limit; $ctr++) {
  1025. $sandbox['current_record']++;
  1026. if (empty($sandbox['records'][$sandbox['current_record']])) {
  1027. break;
  1028. }
  1029. // Shortcuts for later.
  1030. $entity_type = $sandbox['records'][$sandbox['current_record']]->entity_type;
  1031. $entity_id = $sandbox['records'][$sandbox['current_record']]->entity_id;
  1032. // Make sure to load the correct language record.
  1033. $language = $sandbox['records'][$sandbox['current_record']]->language;
  1034. $conditions = array();
  1035. // Some entities don't include a language value.
  1036. if (!empty($has_language[$entity_type])) {
  1037. $conditions['language'] = $language;
  1038. }
  1039. $records = entity_load($entity_type, array($entity_id), $conditions);
  1040. // Fix this record.
  1041. if (!empty($records)) {
  1042. $entity = reset($records);
  1043. // Speed up the handling of entities that don't support revisions.
  1044. $revision_id = 0;
  1045. if (!empty($has_revisions[$entity_type])) {
  1046. list($entity_id, $revision_id, $bundle) = entity_extract_ids($entity_type, $entity);
  1047. $revision_id = intval($revision_id);
  1048. }
  1049. // Don't bother updating records if the revision_id is 0.
  1050. if (!empty($revision_id)) {
  1051. $exists = db_query("SELECT entity_id
  1052. FROM {metatag}
  1053. WHERE entity_type = :entity_type
  1054. AND entity_id = :entity_id
  1055. AND revision_id = :revision_id
  1056. AND language = :language",
  1057. array(
  1058. ':entity_type' => $entity_type,
  1059. ':entity_id' => $entity_id,
  1060. ':revision_id' => $revision_id,
  1061. ':language' => $language,
  1062. ))->fetchObject();
  1063. // There isn't already a record for the revision_id, so update the
  1064. // metatag record.
  1065. if (!$exists) {
  1066. db_update('metatag')
  1067. ->fields(array('revision_id' => $revision_id))
  1068. ->condition('entity_type', $entity_type)
  1069. ->condition('entity_id', $entity_id)
  1070. ->condition('revision_id', 0)
  1071. ->condition('language', $language)
  1072. ->execute();
  1073. }
  1074. // The record exists, so delete the old one under the grounds that the
  1075. // one with a revision_id is newer.
  1076. // Disclaimer: this is completely arbitrary, without providing a UI to
  1077. // let the site maintainer/builder choose which of the two records to
  1078. // keep, we're stuck with a bad scenario. Thankfully this should not
  1079. // happen very often and would only affect sites that were running a
  1080. // dev release. Also, sorry :(
  1081. else {
  1082. db_delete('metatag')
  1083. ->condition('entity_type', $entity_type)
  1084. ->condition('entity_id', $entity_id)
  1085. ->condition('revision_id', 0)
  1086. ->condition('language', $language)
  1087. ->execute();
  1088. }
  1089. // Nodes can have multiple revisions, so create new {metatag} records
  1090. // for each of the other revisions.
  1091. if ($entity_type == 'node') {
  1092. $revisions = node_revision_list($entity);
  1093. if (count($revisions) > 1) {
  1094. $metatags = db_query("SELECT data
  1095. FROM {metatag}
  1096. WHERE entity_type = :entity_type
  1097. AND entity_id = :entity_id
  1098. AND language = :language",
  1099. array(
  1100. ':entity_type' => $entity_type,
  1101. ':entity_id' => $entity_id,
  1102. ':language' => $language,
  1103. ));
  1104. if (!empty($metatags) && isset($metatags->data) && !empty($metatags->data)) {
  1105. foreach ($revisions as $vid => $revision) {
  1106. // Only one record per nid/vid/langcode, thank you.
  1107. if ($vid != $revision_id) {
  1108. // Check that there isn't already a record for this revision.
  1109. $exists = db_query("SELECT entity_id
  1110. FROM {metatag}
  1111. WHERE entity_type = :entity_type
  1112. AND entity_id = :entity_id
  1113. AND revision_id = :revision_id
  1114. AND language = :language",
  1115. array(
  1116. ':entity_type' => $entity_type,
  1117. ':entity_id' => $entity_id,
  1118. ':revision_id' => $vid,
  1119. ':language' => $language,
  1120. ))->fetchObject();
  1121. if (!$exists) {
  1122. $node = node_load($entity_id, $vid);
  1123. $record = new StdClass();
  1124. $record->entity_type = $entity_type;
  1125. $record->entity_id = $entity_id;
  1126. $record->revision_id = $vid;
  1127. $record->language = $language;
  1128. $record->data = $metatags->data;
  1129. drupal_write_record('metatag', $record);
  1130. }
  1131. }
  1132. }
  1133. }
  1134. }
  1135. }
  1136. // Other entity types.
  1137. else {
  1138. drupal_set_message(t('Metatag records for @type objects have not been checked for revisions.', array('@type' => $entity_type)), 'status', FALSE);
  1139. }
  1140. }
  1141. }
  1142. // Update our progress information.
  1143. $sandbox['progress']++;
  1144. }
  1145. // Set the "finished" status, to tell batch engine whether this function
  1146. // needs to run again. If you set a float, this will indicate the progress of
  1147. // the batch so the progress bar will update.
  1148. $sandbox['#finished'] = ($sandbox['progress'] >= $sandbox['max']) ? TRUE : ($sandbox['progress'] / $sandbox['max']);
  1149. if ($sandbox['#finished']) {
  1150. // Clear all caches so the fixed data will be reloaded.
  1151. cache_clear_all('*', 'cache_metatag', TRUE);
  1152. // A final log of the number of records that were converted.
  1153. watchdog('metatag', 'Update 7018: !count records were updated in total.', array('!count' => $sandbox['progress']), WATCHDOG_INFO);
  1154. if (drupal_is_cli()) {
  1155. drupal_set_message(t('Update 7018: !count records were updated.', array('!count' => $sandbox['progress'])));
  1156. }
  1157. // hook_update_N() may optionally return a string which will be displayed
  1158. // to the user.
  1159. return t('Fixed the revision_id values for !count {metatag} records.', array('!count' => $sandbox['progress']));
  1160. }
  1161. }
  1162. /**
  1163. * Clear the entity_cache bins.
  1164. */
  1165. function metatag_update_7019() {
  1166. if (module_exists('entitycache')) {
  1167. foreach (drupal_get_schema() as $table_name => $spec) {
  1168. if (strpos($table_name, 'cache_entity_') === 0) {
  1169. cache_clear_all('*', $table_name, TRUE);
  1170. drupal_set_message(t("Cleared the @table cache bin", array('@table' => $table_name)));
  1171. }
  1172. }
  1173. }
  1174. else {
  1175. drupal_set_message(t("The EntityCache module is not installed, nothing to do."));
  1176. }
  1177. }
  1178. /**
  1179. * Clear the Metatag cache.
  1180. */
  1181. function metatag_update_7020() {
  1182. cache_clear_all('*', 'cache_metatag', TRUE);
  1183. return t('All Metatag caches cleared.');
  1184. }
  1185. /**
  1186. * Clear the existing Metatag cache so all unwanted 403/404 paths can be
  1187. * purged.
  1188. */
  1189. function metatag_update_7021() {
  1190. cache_clear_all('*', 'cache_metatag', TRUE);
  1191. return t('All Metatag caches cleared.');
  1192. }
  1193. /**
  1194. * A minor bit of tidy-up after update 7015.
  1195. */
  1196. function metatag_update_7022() {
  1197. variable_del('metatag_skip_update_7015');
  1198. }
  1199. /**
  1200. * Clear the Metatag cache because $cid_parts was changed.
  1201. */
  1202. function metatag_update_7023() {
  1203. cache_clear_all('*', 'cache_metatag', TRUE);
  1204. return t('All Metatag caches cleared.');
  1205. }
  1206. /**
  1207. * Rename the 'twitter:image' meta tag to 'twitter:image:src', part 1.
  1208. */
  1209. function metatag_update_7024() {
  1210. // Find all {metatag} records that contained an entry for the old meta tag.
  1211. $records = db_query("SELECT entity_type, entity_id, revision_id, language, data
  1212. FROM {metatag}
  1213. WHERE data LIKE '%twitter:image%'");
  1214. // This message will be returned if nothing needed to be updated.
  1215. $none_message = t('No Metatag entity records needed to have the "twitter:image" meta tag fixed.');
  1216. if ($records->rowCount() == 0) {
  1217. drupal_set_message($none_message);
  1218. }
  1219. else {
  1220. $keys = array('entity_type', 'entity_id', 'revision_id', 'language');
  1221. // Loop over the values and correct them.
  1222. $counter = 0;
  1223. foreach ($records as $record) {
  1224. $record->data = unserialize($record->data);
  1225. if (isset($record->data['twitter:image'])) {
  1226. $record->data['twitter:image:src'] = $record->data['twitter:image'];
  1227. unset($record->data['twitter:image']);
  1228. drupal_write_record('metatag', $record, $keys);
  1229. $counter++;
  1230. }
  1231. }
  1232. if ($counter == 0) {
  1233. drupal_set_message($none_message);
  1234. }
  1235. else {
  1236. drupal_set_message(t('Converted the "twitter:image" meta tag for @count entity records to the correct "twitter:image:src" meta tag.', array('@count' => $counter)));
  1237. }
  1238. }
  1239. }
  1240. /**
  1241. * Replaced by update 7030.
  1242. */
  1243. function metatag_update_7025() {
  1244. // Do nothing.
  1245. }
  1246. /**
  1247. * Rename the 'copyright' meta tag to 'rights', part 1.
  1248. */
  1249. function metatag_update_7026() {
  1250. // Find all {metatag} records that contained an entry for the old meta tag.
  1251. $records = db_query("SELECT entity_type, entity_id, revision_id, language, data
  1252. FROM {metatag}
  1253. WHERE data LIKE '%copyright%'");
  1254. // This message will be returned if nothing needed to be updated.
  1255. $none_message = t('No Metatag entity records needed to have the "copyright" meta tag fixed.');
  1256. if ($records->rowCount() == 0) {
  1257. drupal_set_message($none_message);
  1258. }
  1259. else {
  1260. $keys = array('entity_type', 'entity_id', 'revision_id', 'language');
  1261. // Loop over the values and correct them.
  1262. $counter = 0;
  1263. foreach ($records as $record) {
  1264. $record->data = unserialize($record->data);
  1265. if (isset($record->data['copyright'])) {
  1266. $record->data['rights'] = $record->data['copyright'];
  1267. unset($record->data['copyright']);
  1268. drupal_write_record('metatag', $record, $keys);
  1269. $counter++;
  1270. }
  1271. }
  1272. if ($counter == 0) {
  1273. drupal_set_message($none_message);
  1274. }
  1275. else {
  1276. drupal_set_message(t('Converted the "copyright" meta tag for @count entity records to the correct "rights" meta tag.', array('@count' => $counter)));
  1277. }
  1278. }
  1279. }
  1280. /**
  1281. * Replaced by update 7031.
  1282. */
  1283. function metatag_update_7027() {
  1284. // Do nothing.
  1285. }
  1286. /**
  1287. * Clear the menu cache so the new Advanced Settings page will be picked up.
  1288. */
  1289. function metatag_update_7028() {
  1290. variable_set('menu_rebuild_needed', TRUE);
  1291. }
  1292. /**
  1293. * Add an index to the {metatag} table to speed up some queries.
  1294. */
  1295. function metatag_update_7029() {
  1296. db_add_index('metatag', 'type_revision', array('entity_type', 'revision_id'));
  1297. drupal_set_message(t('Added an index to the main Metatag table that will hopefully improve performance a little.'));
  1298. }
  1299. /**
  1300. * Rename the 'twitter:image' meta tag to 'twitter:image:src', part 2.
  1301. */
  1302. function metatag_update_7030() {
  1303. // Find all {metatag_config} records that contained an entry for the old meta
  1304. // tag.
  1305. $records = db_query("SELECT cid, config
  1306. FROM {metatag_config}
  1307. WHERE config LIKE '%twitter:image%'");
  1308. // This message will be returned if nothing needed to be updated.
  1309. $none_message = t('No Metatag configuration records needed to have the "twitter:image" meta tag fixed. That said, there may be other configurations elsewhere that do need updating.');
  1310. // Loop over the values and correct them.
  1311. if ($records->rowCount() == 0) {
  1312. drupal_set_message($none_message);
  1313. }
  1314. else {
  1315. $keys = array('cid');
  1316. // Loop over the values and correct them.
  1317. $counter = 0;
  1318. foreach ($records as $record) {
  1319. $record->config = unserialize($record->config);
  1320. if (isset($record->config['twitter:image'])) {
  1321. $record->config['twitter:image:src'] = $record->config['twitter:image'];
  1322. unset($record->config['twitter:image']);
  1323. drupal_write_record('metatag_config', $record, $keys);
  1324. $counter++;
  1325. }
  1326. }
  1327. if ($counter == 0) {
  1328. drupal_set_message($none_message);
  1329. }
  1330. else {
  1331. drupal_set_message(t('Converted the "twitter:image" meta tag for @count configurations to the correct "twitter:image:src" meta tag.', array('@count' => $counter)));
  1332. }
  1333. }
  1334. }
  1335. /**
  1336. * Rename the 'copyright' meta tag to 'rights', part 2.
  1337. */
  1338. function metatag_update_7031() {
  1339. // Find all {metatag_config} records that contained an entry for the old meta
  1340. // tag.
  1341. $records = db_query("SELECT cid, config
  1342. FROM {metatag_config}
  1343. WHERE config LIKE '%copyright%'");
  1344. // This message will be returned if nothing needed to be updated.
  1345. $none_message = t('No Metatag configuration records needed to have the "copyright" meta tag fixed. That said, there may be other configurations elsewhere that do need updating.');
  1346. // Loop over the values and correct them.
  1347. if ($records->rowCount() == 0) {
  1348. drupal_set_message($none_message);
  1349. }
  1350. else {
  1351. $keys = array('cid');
  1352. // Loop over the values and correct them.
  1353. $counter = 0;
  1354. foreach ($records as $record) {
  1355. $record->config = unserialize($record->config);
  1356. if (isset($record->config['copyright'])) {
  1357. $record->config['rights'] = $record->config['copyright'];
  1358. unset($record->config['copyright']);
  1359. drupal_write_record('metatag_config', $record, $keys);
  1360. $counter++;
  1361. }
  1362. }
  1363. if ($counter == 0) {
  1364. drupal_set_message($none_message);
  1365. }
  1366. else {
  1367. drupal_set_message(t('Converted the "copyright" meta tag for @count configurations to the correct "rights" meta tag.', array('@count' => $counter)));
  1368. }
  1369. }
  1370. }
  1371. /**
  1372. * Clear the Metatag cache.
  1373. */
  1374. function metatag_update_7032() {
  1375. cache_clear_all('*', 'cache_metatag', TRUE);
  1376. return t('All Metatag caches cleared.');
  1377. }
  1378. /**
  1379. * These originally removed the 'author' meta tag, but it was subsequently
  1380. * decided that this was not the correct approach, that the meta tag should not
  1381. * be removed after all.
  1382. *
  1383. * @see https://www.drupal.org/node/2330823
  1384. */
  1385. function metatag_update_7033() {
  1386. }
  1387. function metatag_update_7034() {
  1388. }
  1389. function metatag_update_7035() {
  1390. }