Commit 3d5b0bad authored by Fathima Nazeeha Asmat's avatar Fathima Nazeeha Asmat Committed by Damien McKenna
Browse files

Issue #3106383 by DamienMcKenna, fathima.asmat: Remove the NOYDIR option on ROBOTS tag.

parent a74412df
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -3,6 +3,8 @@ Metatag 8.x-1.x-dev, 2023-xx-xx
#3083743 by Eugene Bocharov, DamienMcKenna, bserem, gkannan25, osopolar,
  timohuisman, akalam, zcht, druhu, maticb, vuil: Add Google's new robots tags.
#3360836 by DamienMcKenna: Nodewords migration triggers wrong API.
#3106383 by DamienMcKenna, fathima.asmat: Remove the NOYDIR option on ROBOTS
  tag.


Metatag 8.x-1.23, 2023-05-12
+161 −0
Original line number Diff line number Diff line
@@ -183,3 +183,164 @@ function metatag_post_update_convert_author_data(&$sandbox) {

  return (string) t('There were no overridden Metatag records.');
}

/**
 * Remove 'noydir' option from meta tag entity fields.
 */
function metatag_post_update_remove_robots_noydir(&$sandbox) {
  $entity_type_manager = \Drupal::entityTypeManager();
  $database = \Drupal::database();

  // This whole top section only needs to be done the first time.
  if (!isset($sandbox['total_records'])) {
    $sandbox['records_processed'] = 0;
    $sandbox['total_records'] = 0;
    $sandbox['current_field'] = 0;
    $sandbox['current_record'] = 0;

    // Counter to enumerate the fields
    // so we can access them in the array
    // by number rather than name.
    $field_counter = 0;
    $field_storage_configs = $entity_type_manager
      ->getStorage('field_storage_config')
      ->loadByProperties(['type' => 'metatag']);

    foreach ($field_storage_configs as $field_storage) {
      $field_name = $field_storage->getName();

      // Get the individual fields (field instances) associated with bundles.
      $fields = $entity_type_manager->getStorage('field_config')
        ->loadByProperties(['field_name' => $field_name]);

      foreach ($fields as $field) {
        // Get the bundle this field is attached to.
        $bundle = $field->getTargetBundle();
        $entity_type_id = $field->getTargetEntityTypeId();
        $entity_type = $entity_type_manager->getDefinition($entity_type_id);

        // Determine the table and "value" field names.
        $table_mapping = $entity_type_manager->getStorage($entity_type_id)
          ->getTableMapping();
        $field_table = $table_mapping->getFieldTableName($field_name);
        $field_value_field = $table_mapping->getFieldColumnName($field_storage, 'value');

        $tables = [];
        $tables[] = $field_table;
        if ($entity_type->isRevisionable() && $field_storage->isRevisionable()) {
          if ($table_mapping->requiresDedicatedTableStorage($field_storage)) {
            $revision_table = $table_mapping->getDedicatedRevisionTableName($field_storage);
            $tables[] = $revision_table;
          }
          elseif ($table_mapping->allowsSharedTableStorage($field_storage)) {
            $revision_table = $entity_type->getRevisionDataTable() ?: $entity_type->getRevisionTable();
            $tables[] = $revision_table;
          }
        }
        if ($tables) {
          $tables = array_unique($tables);
          foreach ($tables as $table) {
            $query = $database->select($table);
            $query->addField($table, 'entity_id');
            $query->addField($table, 'revision_id');
            $query->addField($table, 'langcode');
            $query->addField($table, $field_value_field);
            $query->condition('bundle', $bundle, '=');
            $query->condition($field_value_field, '%noydir%', 'LIKE');
            $result = $query->execute();
            $records = $result->fetchAll();

            if (empty($records)) {
              continue;
            }

            // Fill in all the sandbox information
            // so we can batch the individual
            // record comparing and updating.
            $sandbox['fields'][$field_counter]['field_table'] = $table;
            $sandbox['fields'][$field_counter]['field_value_field'] = $field_value_field;
            $sandbox['fields'][$field_counter]['records'] = $records;

            $sandbox['total_records'] += count($records);
            $field_counter++;
          }
        }
      }
    }
  }

  if ($sandbox['total_records'] == 0) {
    // No partially overridden fields so we can skip the whole batch process.
    $sandbox['#finished'] = 1;
  }
  else {
    // Begin the batch processing of individual field records.
    $max_per_batch = 10;
    $counter = 1;

    $current_field = $sandbox['current_field'];
    $current_field_records = $sandbox['fields'][$current_field]['records'];
    $current_record = $sandbox['current_record'];

    $field_table = $sandbox['fields'][$current_field]['field_table'];
    $field_value_field = $sandbox['fields'][$current_field]['field_value_field'];

    // Loop through the field(s) and remove 'noydir'
    // from value where applicable.
    while ($counter <= $max_per_batch && isset($current_field_records[$current_record])) {
      $record = $current_field_records[$current_record];

      // Strip any empty tags or ones matching the field's defaults and leave
      // only the overridden tags in $new_tags.
      $tags = metatag_data_decode($record->$field_value_field);
      if (!empty($tags['robots'])) {
        $robots_array = explode(', ', $tags['robots']);
        $robots_array = array_diff($robots_array, ['noydir']);
        $tags['robots'] = implode(', ', $robots_array);

        $tags_string = serialize($tags);
        $database->update($field_table)
          ->fields([
            $field_value_field => $tags_string,
          ])
          ->condition('entity_id', $record->entity_id)
          ->condition('revision_id', $record->revision_id)
          ->condition('langcode', $record->langcode)
          ->execute();
      }
      $counter++;
      $current_record++;
    }

    // We ran out of records for the field
    // so start the next batch out with the
    // next field.
    if (!isset($current_field_records[$current_record])) {
      $current_field++;
      $current_record = 0;
    }

    // We have finished all the fields. All done.
    if (!isset($sandbox['fields'][$current_field])) {
      $sandbox['records_processed'] += $counter - 1;
      $sandbox['#finished'] = 1;
    }
    // Update the sandbox values to prepare for the next round.
    else {
      $sandbox['current_field'] = $current_field;
      $sandbox['current_record'] = $current_record;
      $sandbox['records_processed'] += $counter - 1;
      $sandbox['#finished'] = $sandbox['records_processed'] / $sandbox['total_records'];
    }
  }

  if ($sandbox['total_records'] > 0) {
    return (string) t('Processed @processed of @total overridden Metatag records.', [
      '@processed' => $sandbox['records_processed'],
      '@total' => $sandbox['total_records'],
    ]);
  }
  else {
    return (string) t("There were no overridden Metatag records.");
  }
}
+0 −1
Original line number Diff line number Diff line
@@ -158,7 +158,6 @@ class Robots extends MetaNameBase {
      'noarchive' => $this->t('noarchive - Prevents cached copies of this page from appearing in search results.'),
      'nosnippet' => $this->t('nosnippet - Prevents descriptions from appearing in search results, and prevents page caching.'),
      'noodp' => $this->t('noodp - Blocks the <a href=":opendirectory">Open Directory Project</a> description from appearing in search results.', [':opendirectory' => 'http://www.dmoz.org/']),
      'noydir' => $this->t('noydir - Prevents Yahoo! from listing this page in the <a href=":ydir">Yahoo! Directory</a>.', [':ydir' => 'http://dir.yahoo.com/']),
      'noimageindex' => $this->t('noimageindex - Prevent search engines from indexing images on this page.'),
      'notranslate' => $this->t('notranslate - Prevent search engines from offering to translate this page in search results.'),
    ];
+22 −2
Original line number Diff line number Diff line
@@ -216,7 +216,18 @@ $connection->insert('nodewords')
    'type' => '5',
    'id' => '23',
    'name' => 'robots',
    'content' => 'a:2:{s:5:"value";a:8:{s:8:"nofollow";s:8:"nofollow";s:9:"nosnippet";s:9:"nosnippet";s:5:"index";i:0;s:7:"noindex";i:0;s:6:"follow";i:0;s:9:"noarchive";i:0;s:5:"noodp";i:0;s:6:"noydir";i:0;}s:11:"use_default";i:0;}',
    'content' => serialize(array(
      'value' => array(
        'nofollow' => 'nofollow',
        'nosnippet' => 'nosnippet',
        'index' => 0,
        'noindex' => 0,
        'follow' => 0,
        'noarchive' => 0,
        'noodp' => 0,
      ),
      'use_default' => 0,
    )),
  ])

  // Values for taxonomy term tid 16.
@@ -283,7 +294,16 @@ $connection->insert('nodewords')
    'type' => '8',
    'id' => '2',
    'name' => 'robots',
    'content' => 'a:2:{s:5:"value";a:6:{s:9:"noarchive";i:0;s:8:"nofollow";i:0;s:7:"noindex";i:0;s:5:"noodp";i:0;s:9:"nosnippet";i:0;s:6:"noydir";i:0;}s:11:"use_default";i:0;}',
    'content' => serialize(array(
      'value' => array(
        'noarchive' => 0,
        'nofollow' => 0,
        'noindex' => 0,
        'noodp' => 0,
        'nosnippet' => 0,
      ),
      'use_default' => 0,
    )),
  ])
  ->execute();

+0 −2
Original line number Diff line number Diff line
@@ -86,7 +86,6 @@ $connection->insert('metatag')
          'noarchive' => 0,
          'nosnippet' => 0,
          'noodp' => 0,
          'noydir' => 0,
          'noimageindex' => 0,
          'notranslate' => 0,
        ],
@@ -110,7 +109,6 @@ $connection->insert('metatag')
          'noarchive' => 0,
          'nosnippet' => 0,
          'noodp' => 0,
          'noydir' => 0,
          'noimageindex' => 0,
          'notranslate' => 0,
        ],
Loading