You are here

robotstxt.install in RobotsTxt 7

Same filename and directory in other branches
  1. 8 robotstxt.install
  2. 5 robotstxt.install
  3. 6 robotstxt.install

Install, update and uninstall functions for the robotstxt module.

File

robotstxt.install
View source
<?php

/**
 * @file
 * Install, update and uninstall functions for the robotstxt module.
 */

/**
 * Implements hook_install().
 */
function robotstxt_install() {
  $content = '';

  // List of candidates for import.
  $files = array(
    DRUPAL_ROOT . '/robots.txt',
    DRUPAL_ROOT . '/sites/default/default.robots.txt',
    drupal_get_path('module', 'robotstxt') . '/robots.txt',
  );
  foreach ($files as $file) {
    if (file_exists($file) && is_readable($file)) {
      $content = file_get_contents($file);
      break;
    }
  }
  variable_set('robotstxt', $content);
}

/**
 * Implements hook_uninstall().
 */
function robotstxt_uninstall() {
  variable_del('robotstxt');
}

/**
 * Implements hook_requirements().
 */
function robotstxt_requirements($phase) {
  $requirements = array();
  $t = get_t();
  switch ($phase) {
    case 'runtime':

      // Module cannot work without Clean URLs.
      if (!variable_get('clean_url', 0)) {
        $requirements['robotstxt_cleanurl'] = array(
          'title' => $t('RobotsTxt'),
          'severity' => REQUIREMENT_ERROR,
          'value' => $t('<a href="!clean_url">Clean URLs</a> are mandatory for this module.', array(
            '!clean_url' => url('admin/config/search/clean-urls'),
          )),
        );
      }

      // Webservers prefer the robots.txt file on disk and does not allow menu
      // path overwrite.
      if (file_exists(DRUPAL_ROOT . '/robots.txt')) {
        $requirements['robotstxt_file'] = array(
          'title' => $t('RobotsTxt'),
          'severity' => REQUIREMENT_WARNING,
          'value' => $t('RobotsTxt module works only if you remove the existing robots.txt file in your website root.'),
        );
      }
  }
  return $requirements;
}

/**
 * Automatically add the 'administer robots.txt' permission to granted users.
 */
function robotstxt_update_6100() {
  $roles = user_roles(FALSE, 'administer site configuration');
  foreach ($roles as $rid => $role) {
    _update_7000_user_role_grant_permissions($rid, array(
      'administer robots.txt',
    ), 'robotstxt');
  }
  return t("Added 'administer robots.txt' permission to all roles with 'administer site configuration' permission.");
}

/**
 * Rename menu path 'logout' to 'user/logout' for consistency.
 */
function robotstxt_update_7000() {

  // Case #337820.
  $robotstxt = variable_get('robotstxt');
  $robotstxt = str_replace('Disallow: /logout/', 'Disallow: /user/logout/', $robotstxt);
  $robotstxt = str_replace('Disallow: /?q=logout/', 'Disallow: /?q=user/logout/', $robotstxt);
  variable_set('robotstxt', $robotstxt);
  return t("Renamed menu path 'logout' to 'user/logout'.");
}

/**
 * Allow crawling of 'sites' folder by search engines, don't disallow it.
 */
function robotstxt_update_7100() {

  // Case #494462.
  $robotstxt = variable_get('robotstxt');
  $robotstxt = preg_replace("/Disallow:\\s\\/sites\\/(\r\n?|\n)/", '', $robotstxt);
  variable_set('robotstxt', $robotstxt);
  return t("Removed 'sites' folder from crawling exclusion list.");
}

/**
 * Allow crawling of 'contact' folder by search engines, don't disallow it.
 */
function robotstxt_update_7101() {

  // Case #905576.
  $robotstxt = variable_get('robotstxt');
  $robotstxt = preg_replace("/Disallow:\\s\\/(\\?q=)?contact\\/(\r\n?|\n)/", '', $robotstxt);
  variable_set('robotstxt', $robotstxt);
  return t("Removed 'contact' folder from crawling exclusion list.");
}

/**
 * Add 3 missing exclusions introduced by latest robots.txt update.
 */
function robotstxt_update_7102() {
  $robotstxt = variable_get('robotstxt');
  $robotstxt = preg_replace("/Disallow:\\s\\/INSTALL.pgsql.txt(\r\n?|\n)Disallow:\\s\\/install.php(\r\n?|\n)/", "Disallow: /INSTALL.pgsql.txt\nDisallow: /INSTALL.sqlite.txt\nDisallow: /install.php\n", $robotstxt);
  $robotstxt = preg_replace("/Disallow:\\s\\/comment\\/reply\\/(\r\n?|\n)Disallow:\\s\\/node\\/add\\/(\r\n?|\n)/", "Disallow: /comment/reply/\nDisallow: /filter/tips/\nDisallow: /node/add/\n", $robotstxt);
  $robotstxt = preg_replace("/Disallow:\\s\\/\\?q=comment\\/reply\\/(\r\n?|\n)Disallow:\\s\\/\\?q=node\\/add\\/(\r\n?|\n)/", "Disallow: /?q=comment/reply/\nDisallow: /?q=filter/tips/\nDisallow: /?q=node/add/\n", $robotstxt);
  variable_set('robotstxt', $robotstxt);
  return t("Added 'INSTALL.sqlite.txt' and '/filter/tips/' exclusions.");
}

/**
 * Update robots.txt syntax checking link.
 */
function robotstxt_update_7103() {
  $robotstxt = variable_get('robotstxt');
  $robotstxt = str_replace('http://www.sxw.org.uk/computing/robots/check.html', 'http://www.frobee.com/robots-txt-check', $robotstxt);
  variable_set('robotstxt', $robotstxt);
  return t('Updated syntax checking link.');
}

/**
 * Update robots.txt link.
 */
function robotstxt_update_7104() {
  $robotstxt = variable_get('robotstxt');
  $robotstxt = str_replace('http://www.robotstxt.org/wc/robots.html', 'http://www.robotstxt.org/robotstxt.html', $robotstxt);
  variable_set('robotstxt', $robotstxt);
  return t('Updated robotstxt.org link.');
}

Functions

Namesort descending Description
robotstxt_install Implements hook_install().
robotstxt_requirements Implements hook_requirements().
robotstxt_uninstall Implements hook_uninstall().
robotstxt_update_6100 Automatically add the 'administer robots.txt' permission to granted users.
robotstxt_update_7000 Rename menu path 'logout' to 'user/logout' for consistency.
robotstxt_update_7100 Allow crawling of 'sites' folder by search engines, don't disallow it.
robotstxt_update_7101 Allow crawling of 'contact' folder by search engines, don't disallow it.
robotstxt_update_7102 Add 3 missing exclusions introduced by latest robots.txt update.
robotstxt_update_7103 Update robots.txt syntax checking link.
robotstxt_update_7104 Update robots.txt link.