diff --git a/metatag.install b/metatag.install index 93bf346..7695f1e 100644 --- a/metatag.install +++ b/metatag.install @@ -233,11 +233,131 @@ function metatag_update_7003() { } /** - * Update all records in the metatag table and assign each the correct language - * value, will also resolve problems created during the release of beta3. This - * might take a while, depending on how much data needs to be converted. + * Replaced by updates 7008 & 7009. */ -function metatag_update_7004(&$sandbox) { +function metatag_update_7004() { + // Nothing. +} + +/** + * Removing wrong metatag watchdog entries that break the admin/reports/dblog + * page. + */ +function metatag_update_7005() { + db_delete('watchdog') + ->condition('type', 'metatag') + ->condition('variables', serialize('info')) + ->execute(); +} + +/** + * Remove metatag records that were added by old versions of the module for + * entities that don't actually support meta tags. A more complete version of + * this will be added later on after it's (hopefully) guaranteed that all + * modules have updated to the correct API usage. + */ +function metatag_update_7006() { + $types = array( + // Core. + 'comment', + 'menu_link', + 'taxonomy_vocabulary', + // Some contrib entities. + 'mailchimp_list', + 'profile2', + 'profile2_type', + 'redirect', + 'rules_config', + 'wysiwyg_profile', + ); + foreach ($types as $type) { + $num_deleted = db_delete('metatag') + ->condition('entity_type', $type) + ->execute(); + if ($num_deleted > 0) { + 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' => $type))); + } + } +} + +/** + * Remove metatag records for objects that have been deleted; older versions of + * Metatag may have failed to purge these. + */ +function metatag_update_7007() { + $result = db_query("DELETE m + FROM {metatag} m + LEFT OUTER JOIN {node} n + ON m.entity_id=n.nid + WHERE m.entity_type='node' + AND n.nid IS NULL"); + if ($result->rowCount() > 0) { + drupal_set_message(t('Removed @count meta tag record(s) for nodes that had been purged.', array('@count' => $result->rowCount()))); + } + + $result = db_query("DELETE m + FROM {metatag} m + LEFT OUTER JOIN {users} u + ON m.entity_id=u.uid + WHERE m.entity_type='user' + AND u.uid IS NULL"); + if ($result->rowCount() > 0) { + drupal_set_message(t('Removed @count meta tag record(s) for users that had been purged.', array('@count' => $result->rowCount()))); + } + + $result = db_query("DELETE m + FROM {metatag} m + LEFT OUTER JOIN {taxonomy_term_data} t + ON m.entity_id=t.tid + WHERE m.entity_type='taxonomy_term' + AND t.tid IS NULL"); + if ($result->rowCount() > 0) { + drupal_set_message(t('Removed @count meta tag record(s) for taxonomy terms that had been purged.', array('@count' => $result->rowCount()))); + } +} + +/** + * Reset all metatag language values. + */ +function metatag_update_7008() { + // Reset all metatag language values to empty strings, to better identify + // what needs work. + $result = db_query("UPDATE {metatag} SET language = ''"); + if ($result->rowCount() > 0) { + drupal_set_message(t('Reset language values for @count Metatag records.', array('@count' => $result->rowCount()))); + } + + // Taxonomy entities don't support a 'language' option, so reset them to + // LANGUAGE_NONE. + $result = db_query("UPDATE {metatag} SET language = :language WHERE entity_type='taxonomy_term'", array(':language' => LANGUAGE_NONE)); + if ($result->rowCount() > 0) { + drupal_set_message(t('Fixed language values for @count taxonomy terms.', array('@count' => $result->rowCount()))); + } + + // Update User values. + $result = db_query("UPDATE {metatag} AS m INNER JOIN {users} u ON m.entity_id=u.uid SET m.language = u.language WHERE m.entity_type='user' AND u.language!=''"); + if ($result->rowCount() > 0) { + drupal_set_message(t('Fixed language values for @count users that had a language setting.', array('@count' => $result->rowCount()))); + } + $result = db_query("UPDATE {metatag} AS m INNER JOIN {users} u ON m.entity_id=u.uid SET m.language = :language WHERE m.entity_type='user' AND u.language=''", array(':language' => LANGUAGE_NONE)); + if ($result->rowCount() > 0) { + drupal_set_message(t('Fixed language values for @count users that didn\'t have a language setting.', array('@count' => $result->rowCount()))); + } + + // Update Node values. + $result = db_query("UPDATE {metatag} AS m INNER JOIN {node} n ON m.entity_id=n.nid AND m.entity_type='node' SET m.language = n.language"); + if ($result->rowCount() > 0) { + drupal_set_message(t('Fixed language values for @count nodes.', array('@count' => $result->rowCount()))); + } +} + +/** + * Fix the metatag language value for all entity records that don't already + * have a language fixed via update 7008; this should mainly cover custom + * entity types. It might take a while, depending on how much data needs to be + * converted. + */ +function metatag_update_7009(&$sandbox) { // Use the sandbox at your convenience to store the information needed // to track progression between successive calls to the function. if (!isset($sandbox['progress'])) { @@ -248,9 +368,10 @@ function metatag_update_7004(&$sandbox) { // way to do this, so we're going to cache all record keys and manually // step through them. $records = db_select('metatag', 'm') - ->fields('m', array('entity_type', 'entity_id', 'language')) - ->orderBy('entity_type', 'ASC') - ->orderBy('entity_id', 'ASC') + ->fields('m', array('entity_type', 'entity_id')) + ->condition('m.language', '') + ->orderBy('m.entity_type', 'ASC') + ->orderBy('m.entity_id', 'ASC') ->execute(); $sandbox['records'] = array(); foreach ($records as $record) { @@ -262,8 +383,11 @@ function metatag_update_7004(&$sandbox) { // A place to store messages during the run. $sandbox['messages'] = array(); - // An initial record of the number of records to be upgraded. - watchdog('metatag', 'Update 7004: !count records to upgrade.', array('!count' => $sandbox['max']), WATCHDOG_INFO); + // An initial record of the number of records to be updated. + watchdog('metatag', 'Update 7009: !count records to update.', array('!count' => $sandbox['max']), WATCHDOG_INFO); + if (drupal_is_cli()) { + drupal_set_message(t('Update 7009: !count records to update.', array('!count' => $sandbox['max']))); + } // Last record processed. $sandbox['current_record'] = -1; @@ -271,7 +395,10 @@ function metatag_update_7004(&$sandbox) { // If there's no data, don't bother with the extra work. if (empty($sandbox['max'])) { - return t('No records needed to be updated.'); + if (drupal_is_cli()) { + drupal_set_message(t('Update 7009: No records need updating.')); + } + return t('No records need updating.'); } // Proceed as normal. @@ -290,7 +417,7 @@ function metatag_update_7004(&$sandbox) { } // Set default values. - for ($x = 0; $x < $limit; $x += $increment) { + for ($ctr = 0; $ctr < $limit; $ctr += $increment) { $sandbox['current_record']++; if (empty($sandbox['records'][$sandbox['current_record']])) { break; @@ -299,7 +426,7 @@ function metatag_update_7004(&$sandbox) { // Shortcuts for later. $entity_type = $sandbox['records'][$sandbox['current_record']]->entity_type; $entity_id = $sandbox['records'][$sandbox['current_record']]->entity_id; - $language = $sandbox['records'][$sandbox['current_record']]->language; + $language = ''; // Load the entity. $entities = entity_load($entity_type, array($entity_id)); @@ -313,32 +440,17 @@ function metatag_update_7004(&$sandbox) { // new record because they thought the records were missing. try { // If there's a (non-empty) language value, use it. - if (!empty($entity->language)) { - // The language values are different. - if ($entity->language != $language) { - // Update the record with the entity's language value. - db_update('metatag') - ->fields(array('language' => $entity->language)) - ->condition('entity_type', $entity_type) - ->condition('entity_id', $entity_id) - ->condition('language', $language) - ->execute(); - } - // The language values are the same. - else { - // Do nothing. - } - } - // There's no language value. - else { - // Assign the global 'no language' value. - db_update('metatag') - ->fields(array('language' => LANGUAGE_NONE)) - ->condition('entity_type', $entity_type) - ->condition('entity_id', $entity_id) - ->condition('language', $language) - ->execute(); + $new_language = entity_language($entity_type, $entity); + if (empty($new_language)) { + $new_language = LANGUAGE_NONE; } + // Update the 'language' value. + db_update('metatag') + ->fields(array('language' => $new_language)) + ->condition('entity_type', $entity_type) + ->condition('entity_id', $entity_id) + ->condition('language', $language) + ->execute(); } catch (Exception $e) { // Delete the newer record. @@ -365,7 +477,7 @@ function metatag_update_7004(&$sandbox) { // A per-iterationl message, only record if not running via Drush. if (!drupal_is_cli()) { - watchdog('metatag', 'Update 7004: !count records were updated.', array('!count' => $x), WATCHDOG_INFO); + watchdog('metatag', 'Update 7009: !count records were updated.', array('!count' => $ctr), WATCHDOG_INFO); } // Set the "finished" status, to tell batch engine whether this function @@ -378,7 +490,10 @@ function metatag_update_7004(&$sandbox) { cache_clear_all('*', 'cache_metatag', TRUE); // A final log of the number of records that were converted. - watchdog('metatag', 'Update 7004: !count records were updated in total.', array('!count' => $sandbox['progress']), WATCHDOG_INFO); + watchdog('metatag', 'Update 7009: !count records were updated in total.', array('!count' => $sandbox['progress']), WATCHDOG_INFO); + if (drupal_is_cli()) { + drupal_set_message(t('Update 7009: !count records were updated.', array('!count' => $sandbox['progress']))); + } // hook_update_N() may optionally return a string which will be displayed // to the user. @@ -386,80 +501,3 @@ function metatag_update_7004(&$sandbox) { } } } - -/** - * Removing wrong metatag watchdog entries that break the admin/reports/dblog - * page. - */ -function metatag_update_7005() { - db_delete('watchdog') - ->condition('type', 'metatag') - ->condition('variables', serialize('info')) - ->execute(); -} - -/** - * Remove metatag records that were added by old versions of the module for - * entities that don't actually support meta tags. A more complete version of - * this will be added later on after it's (hopefully) guaranteed that all - * modules have updated to the correct API usage. - */ -function metatag_update_7006() { - $types = array( - // Core. - 'comment', - 'menu_link', - 'taxonomy_vocabulary', - // Some contrib entities. - 'mailchimp_list', - 'profile2', - 'profile2_type', - 'redirect', - 'rules_config', - 'wysiwyg_profile', - ); - foreach ($types as $type) { - $num_deleted = db_delete('metatag') - ->condition('entity_type', $type) - ->execute(); - if ($num_deleted > 0) { - 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' => $type))); - } - } -} - -/** - * Remove metatag records for objects that have been deleted; older versions of - * Metatag may have failed to purge these. - */ -function metatag_update_7007() { - $result = db_query("DELETE m - FROM {metatag} m - LEFT OUTER JOIN {node} n - ON m.entity_id=n.nid - WHERE m.entity_type='node' - AND n.nid IS NULL"); - if ($result->rowCount() > 0) { - drupal_set_message(t('Removed @count meta tag record(s) for nodes that had been purged.', array('@count' => $result->rowCount()))); - } - - $result = db_query("DELETE m - FROM {metatag} m - LEFT OUTER JOIN {users} u - ON m.entity_id=u.uid - WHERE m.entity_type='user' - AND u.uid IS NULL"); - if ($result->rowCount() > 0) { - drupal_set_message(t('Removed @count meta tag record(s) for users that had been purged.', array('@count' => $result->rowCount()))); - } - - $result = db_query("DELETE m - FROM {metatag} m - LEFT OUTER JOIN {taxonomy_term_data} t - ON m.entity_id=t.tid - WHERE m.entity_type='taxonomy_term' - AND t.tid IS NULL"); - if ($result->rowCount() > 0) { - drupal_set_message(t('Removed @count meta tag record(s) for taxonomy terms that had been purged.', array('@count' => $result->rowCount()))); - } -}