Commit 45603dfb authored by Maxime Roux's avatar Maxime Roux
Browse files

Issue #3262296 by MacSim, danflanagan8, apaderno: Get rid of the API key file...

Issue #3262296 by MacSim, danflanagan8, apaderno: Get rid of the API key file + follow coding standards
parent 8f47b9ae
Loading
Loading
Loading
Loading
+9 −16
Original line number Diff line number Diff line
@@ -34,32 +34,25 @@ you notify every other one in the same time.
REQUIREMENTS
------------

This module requires no modules outside of Drupal core but your project root
folder must be writable by `www-data`.
This module requires no modules outside of Drupal core.


INSTALLATION
------------

 * Install as you would normally install a contributed Drupal module. Visit
Install as you would normally install a contributed Drupal module. Visit
https://www.drupal.org/node/1897420 for further information.

 * If you're having a requirement error during the install it's most likely
   because your project root folder is not in the `www-data` group. Therefore
   Drupal won't be able to create the key file.


CONFIGURATION
-------------

 * Go to `/admin/config/services/index_now`, generate the key file and choose
   the default search engine you want to interact with.
   You can also exclude content types and/or taxonomy vocabularies for which
   you don't want to use Index Now.
Go to `/admin/config/services/index_now`, generate the API key and choose the
default search engine you want to interact with.
You can also exclude content types, taxonomy vocabularies and/or commerce
products types for which you don't want to use Index Now.
If you don't want to use that feature on an environment (ex: local / preprod)
   just don't generate the key file.
 * If you use git, you should add the following line in your `.gitignore` file:
   `index_now_key_*.txt`
just don't generate the API key.


SERVICE
+27 −26
Original line number Diff line number Diff line
@@ -5,35 +5,12 @@
 * Manage the install of the index_now module.
 */

/**
 * Implements hook_requirements().
 */
function index_now_requirements($phase) {
  $requirements = [];

  if (!is_writable(DRUPAL_ROOT)) {
    $requirements['apikey_file'] = [
      'description' => t("%root_path folder is not writable. Index Now won't be able to create the file which will contains the API key. Check the rights and try to install the module again.", [
        '%root_path' => DRUPAL_ROOT,
      ]),
      'severity' => REQUIREMENT_ERROR,
    ];
  }

  return $requirements;
}

/**
 * Implements hook_uninstall().
 */
function index_now_uninstall() {
  // Delete the API key file.
  $api_key = \Drupal::service('index_now.manager')->searchForAnApiKeyFile();

  if (!empty($api_key)) {
    $filepath = \Drupal::service('index_now.manager')->buildApiKeyFilePath($api_key);
    \Drupal::service('file_system')->unlink($filepath);
  }
  // Delete the API key.
  $api_key = \Drupal::service('state')->delete('index_now_api_key');
}

/**
@@ -52,11 +29,35 @@ function index_now_update_9001() {
  if (!empty($api_key)) {
    // Change the API key filename.
    $old_file = DRUPAL_ROOT . "/index_now_key.txt";
    $new_file = \Drupal::service('index_now.manager')->buildApiKeyFilePath($api_key);
    $new_file = DRUPAL_ROOT . '/index_now_key_' . $api_key . '.txt';
    if (file_exists($old_file)) {
      \Drupal::service('file_system')->move($old_file, $new_file);
    }
    return;
  }
}

/**
 * Implements hook_update_N().
 *
 * Search for an API key file and, if such a file exists,
 * store the key as as state then delete the file.
 */
function index_now_update_9002() {
  $mask = '/^index_now_key_[0-9a-f\-]+\.txt$/';
  $options = [
    'recurse' => FALSE,
    'key' => 'name',
  ];
  $files = \Drupal::service('file_system')
    ->scanDirectory(DRUPAL_ROOT, $mask, $options);

  if (!empty($files)) {
    $filename = current(array_keys($files));
    $api_key = substr($filename, 14);
    \Drupal::service('state')->set('index_now_api_key', $api_key);

    $filepath = DRUPAL_ROOT . '/' . $filename . '.txt';
    \Drupal::service('file_system')->unlink($filepath);
  }
}
+11 −12
Original line number Diff line number Diff line
@@ -89,32 +89,31 @@ function index_now_help($route_name, RouteMatchInterface $route_match) {
      $help .= t("Note: Since November 2021, compatibles search engines will immediately share all the submitted URLs with every other compatible search engine.") . '<br />';
      $help .= t("This means that if you choose to notify one search engine, you notify every other one in the same time.");
      $help .= '</p>';
      $help .= '<a href="https://www.indexnow.org/">' . t("Read more about Index Now project") . '</a>';
      $help .= t('<a href=":protocol_url">Read more about Index Now project</a>',[
        ':protocol_url' => 'https://www.indexnow.org',
      ]);
      // Requirements.
      $help .= '<h2>' . t("Requirements") . '</h2>';
      $help .= '<p>' . t("This module requires no modules outside of Drupal core but your project root folder must be writable by <em>www-data</em>.") . '</p>';
      $help .= '<p>' . t("This module requires no modules outside of Drupal core.") . '</p>';
      // Installation.
      $help .= '<h2>' . t("Installation") . '</h2>';
      $help .= '<p>';
      $help .= t("If you're having a requirement error during the install it's most likely because your project root folder is not in the <em>www-data</em> group.") . '<br />';
      $help .= t("Therefore Drupal won't be able to create the key file.");
      $help .= t('Install as you would normally install a contributed Drupal module. Visit <a href=":installation_help">:installation_help</a> for further information.', [
        ':installation_help' => 'https://www.drupal.org/node/1897420',
      ]);
      $help .= '</p>';
      // Configuration.
      $help .= '<h2>' . t("Configuration") . '</h2>';
      $help .= '<p>';
      $help .= t('Go to <a href=":config_page">:config_page</a>, generate the key file and choose the default search engine you want to interact with.', [
      $help .= t('Go to <a href=":config_page">:config_page</a>, generate the API key and choose the default search engine you want to interact with.', [
        ':config_page' => Url::fromRoute('index_now')->toString(),
      ]) . '<br />';
      $help .= t("You can also exclude content types and/or taxonomy vocabularies for which you don't want to use Index Now.") . '<br/>';
      $help .= t("If you don't want to use that feature on an environment (ex: local / preprod) just don't generate the key file.");
      $help .= '</p>';
      $help .= '<p>';
      $help .= t("If you use git, you should add the following line in your <em>.gitignore</em> file:");
      $help .= '<code>' . "index_now_key_*.txt" . '</code>';
      $help .= t("You can also exclude content types, taxonomy vocabularies, commerce products types for which you don't want to use Index Now.") . '<br/>';
      $help .= t("If you don't want to use that feature on an environment (ex: local / preprod) just don't generate the API key.");
      $help .= '</p>';
      // Service.
      $help .= '<h2>' . t("Service") . '</h2>';
      $help .= '<p>' . t("The module provides a service that can be used to index other URLs than just nodes and taxonomy terms pages.") . '</p>';
      $help .= '<p>' . t("The module provides a service that can be used to index other URLs than just nodes, taxonomy terms and commerce products pages.") . '</p>';
      $help .= '<p>';
      $help .= t("Here is how to use it:") . '<br/>';
      $help .= '<code>' . "\Drupal::service('index_now.indexnow')->sendRequest(YOUR_URL);" . '</code>';
+8 −0
Original line number Diff line number Diff line
@@ -5,3 +5,11 @@ index_now.settings:
    _title: 'Index Now Settings Form'
  requirements:
    _permission: 'configure index now'

index_now.api_key:
  path: '/index_now_api_key/{api_key}'
  defaults:
    _controller: '\Drupal\index_now\Controller\ApiKeyController::build'
  requirements:
    _access: 'TRUE'
    api_key: '[0-9a-f\-]+'
+6 −4
Original line number Diff line number Diff line
@@ -4,10 +4,12 @@ services:
    arguments:
      - '@http_client'
      - '@config.factory'
      - '@index_now.manager'
      - '@state'
      - '@messenger'
      - '@logger.factory'
  index_now.manager:
    class: Drupal\index_now\Service\IndexNowManager
  index_now.path_processor:
    class: Drupal\index_now\PathProcessor\IndexNowPathProcessor
    arguments:
      - '@file_system'
      - '@state'
    tags:
      - { name: path_processor_inbound }
Loading