You are here

linkchecker.install in Link checker 5.2

Installation file for Link Checker module.

File

linkchecker.install
View source
<?php

/**
 * @file
 * Installation file for Link Checker module.
 */
define('LINKCHECKER_MINIMUM_MYSQL', '5.0');

/**
 * Implementation of hook_install().
 */
function linkchecker_install() {
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      db_query("CREATE TABLE {linkchecker_boxes} (\n        bid INT unsigned NOT NULL,\n        lid INT unsigned NOT NULL,\n        PRIMARY KEY (bid, lid)\n      ) /*!40100 DEFAULT CHARACTER SET utf8 */;");
      db_query("CREATE TABLE {linkchecker_nodes} (\n        nid INT unsigned NOT NULL,\n        lid INT unsigned NOT NULL,\n        PRIMARY KEY (nid, lid)\n      ) /*!40100 DEFAULT CHARACTER SET utf8 */;");
      db_query("CREATE TABLE {linkchecker_comments} (\n        cid INT unsigned NOT NULL,\n        lid INT unsigned NOT NULL,\n        PRIMARY KEY (cid ,lid)\n      ) /*!40100 DEFAULT CHARACTER SET utf8 */;");
      db_query("CREATE TABLE {linkchecker_links} (\n        lid INT unsigned NOT NULL,\n        token VARCHAR(32) NOT NULL,\n        url TEXT NOT NULL,\n        method varchar(4) NOT NULL default 'HEAD',\n        code INT NOT NULL default '-1',\n        error TEXT,\n        fail_count INT NOT NULL default '0',\n        last_checked INT NOT NULL default '0',\n        status INT NOT NULL default '1',\n        PRIMARY KEY (lid),\n        UNIQUE KEY token (token)\n      ) /*!40100 DEFAULT CHARACTER SET utf8 */;");
      break;

    // PgSQL is untested!
    case 'pgsql':
      db_query("CREATE TABLE {linkchecker_boxes} (\n        bid int_unsigned NOT NULL,\n        lid int_unsigned NOT NULL,\n        PRIMARY KEY (bid, lid)\n      );");
      db_query("CREATE TABLE {linkchecker_nodes} (\n        nid int_unsigned NOT NULL,\n        lid int_unsigned NOT NULL,\n        PRIMARY KEY (nid, lid)\n      );");
      db_query("CREATE TABLE {linkchecker_comments} (\n        cid int_unsigned NOT NULL,\n        lid int_unsigned NOT NULL,\n        PRIMARY KEY (cid, lid)\n      );");
      db_query("CREATE TABLE {linkchecker_links} (\n        lid int_unsigned NOT NULL,\n        token VARCHAR(32) NOT NULL,\n        url TEXT NOT NULL,\n        method VARCHAR(4) NOT NULL default 'HEAD',\n        code INTEGER NOT NULL default '-1',\n        error TEXT,\n        fail_count INTEGER NOT NULL default '0',\n        last_checked INTEGER NOT NULL default '0',\n        status INT NOT NULL default '1',\n        PRIMARY KEY (lid),\n        UNIQUE (token)\n      );");
      break;
  }
}

/**
 * Implementation of hook_uninstall().
 */
function linkchecker_uninstall() {
  db_query("DROP TABLE {linkchecker_boxes}");
  db_query("DROP TABLE {linkchecker_nodes}");
  db_query("DROP TABLE {linkchecker_comments}");
  db_query("DROP TABLE {linkchecker_links}");
  variable_del('linkchecker_action_status_code_301');
  variable_del('linkchecker_action_status_code_404');
  variable_del('linkchecker_check_links_interval');
  variable_del('linkchecker_check_links_max');
  variable_del('linkchecker_check_useragent');
  variable_del('linkchecker_cleanup_links_last');
  variable_del('linkchecker_disable_link_check_for_urls');
  variable_del('linkchecker_extract_from_a');
  variable_del('linkchecker_extract_from_audio');
  variable_del('linkchecker_extract_from_embed');
  variable_del('linkchecker_extract_from_iframe');
  variable_del('linkchecker_extract_from_img');
  variable_del('linkchecker_extract_from_object');
  variable_del('linkchecker_extract_from_source');
  variable_del('linkchecker_extract_from_video');
  variable_del('linkchecker_filter_blacklist');
  variable_del('linkchecker_fqdn_only');
  variable_del('linkchecker_ignore_response_codes');
  variable_del('linkchecker_scan_blocks');
  variable_del('linkchecker_scan_comments');
  variable_del('linkchecker_scan_nodetypes');
}

/**
 * Implementation of hook_requirements().
 */
function linkchecker_requirements($phase) {
  $requirements = array();
  $t = get_t();
  switch ($phase) {
    case 'install':

      /*      // Module cannot work without external HTTP requests.
            if (variable_get('drupal_http_request_fails', TRUE) && !linkchecker_check_http_request()) {
              $requirements['http requests'] = array(
                'title' => $t('HTTP request status'),
                'value' => $t('Fails'),
                'severity' => REQUIREMENT_ERROR,
                'description' => $t('Your system or network configuration does not allow Drupal to access web pages, resulting in reduced functionality. This could be due to your webserver configuration or PHP settings, and should be resolved in order to download information about available updates, fetch aggregator feeds, sign in via OpenID, or use other network-dependent services.'),
              );
            } */

      // MySQL version with subselect support is required.
      $version = db_version();
      if (in_array($GLOBALS['db_type'], array(
        'mysql',
        'mysqli',
      )) && version_compare($version, LINKCHECKER_MINIMUM_MYSQL) < 0) {
        $requirements['linkchecker_minimum_mysql'] = array(
          'title' => $t('MySQL database'),
          'value' => $version,
          'severity' => REQUIREMENT_ERROR,
          'description' => $t('Your MySQL Server is too old. Link Checker requires at least MySQL %version.', array(
            '%version' => LINKCHECKER_MINIMUM_MYSQL,
          )),
        );
      }
      break;
  }
  return $requirements;
}

/**
 * Backport of D6 system_check_http_request() function.
 *
 * http://api.drupal.org/api/function/system_check_http_request.
 */
function linkchecker_check_http_request() {

  // Try to get the content of the front page via drupal_http_request().
  $result = drupal_http_request(url('', array(
    'absolute' => TRUE,
  )));

  // We only care that we get a http response - this means that Drupal
  // can make a http request.
  $works = isset($result->code) && $result->code >= 100 && $result->code < 600;
  variable_set('drupal_http_request_fails', !$works);
  return $works;
}

/**
 * Upgrade module to new schema.
 */
function linkchecker_update_5200() {
  $ret = array();

  // Module functions are required. Make sure the module is loaded.
  drupal_load('module', 'linkchecker');

  // Remove obsolete tables no longer required.
  $ret[] = update_sql("DROP TABLE {linkchecker_tasks}");
  $ret[] = update_sql("DROP TABLE {linkchecker_results}");

  // Create new tables.
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("CREATE TABLE {linkchecker_boxes} (\n        bid INT NOT NULL,\n        lid INT NOT NULL,\n        PRIMARY KEY (bid, lid)\n      ) /*!40100 DEFAULT CHARACTER SET utf8 */;");
      $ret[] = update_sql("CREATE TABLE {linkchecker_nodes} (\n        nid INT NOT NULL,\n        lid INT NOT NULL,\n        PRIMARY KEY (nid, lid)\n      ) /*!40100 DEFAULT CHARACTER SET utf8 */;");
      $ret[] = update_sql("CREATE TABLE {linkchecker_links} (\n        lid INT NOT NULL auto_increment,\n        token VARCHAR(32) NOT NULL,\n        url TEXT NOT NULL,\n        method varchar(4) NOT NULL default 'HEAD',\n        code INT NOT NULL default '0',\n        error TEXT,\n        fail_count INT NOT NULL default '0',\n        last_checked INT NOT NULL default '0',\n        PRIMARY KEY (lid),\n        UNIQUE KEY token (token)\n      ) /*!40100 DEFAULT CHARACTER SET utf8 */;");
      break;

    // Install PgSQL tables with update_5204 level! PgSQL was broken in
    // all older versions so we can expect nobody have used it. This allows not
    // to reinvent the complete upgrade process with all steps. Fully untested!
    case 'pgsql':
      $ret[] = update_sql("CREATE TABLE {linkchecker_boxes} (\n        bid int_unsigned NOT NULL,\n        lid int_unsigned NOT NULL,\n        PRIMARY KEY (bid, lid)\n      );");
      $ret[] = update_sql("CREATE TABLE {linkchecker_nodes} (\n        nid int_unsigned NOT NULL,\n        lid int_unsigned NOT NULL,\n        PRIMARY KEY (nid, lid)\n      );");
      $ret[] = update_sql("CREATE TABLE {linkchecker_comments} (\n        cid int_unsigned NOT NULL,\n        lid int_unsigned NOT NULL,\n        PRIMARY KEY (cid, lid)\n      );");
      $ret[] = update_sql("CREATE TABLE {linkchecker_links} (\n        lid int_unsigned NOT NULL,\n        token VARCHAR(32) NOT NULL,\n        url TEXT NOT NULL,\n        method VARCHAR(4) NOT NULL default 'HEAD',\n        code INTEGER NOT NULL default '-1',\n        error TEXT,\n        fail_count INTEGER NOT NULL default '0',\n        last_checked INTEGER NOT NULL default '0',\n        PRIMARY KEY (lid),\n        UNIQUE (token)\n      );");
      break;
  }

  // Upgrade settings. Could be less code, but is easier to follow.
  $ignore_response_codes = preg_split('/(\\r\\n?|\\n)/', variable_get('linkchecker_ignore_responses', "200\n304\n401\n403"));

  // Filter all invalid responds codes and outdated error messages out.
  $ignore_response_codes = array_filter($ignore_response_codes, '_linkchecker_isvalid_response_code');

  // Make sure we have status code 200 and 304 in the ignore list.
  $ignore_response_codes = array_merge(array(
    '200',
    '304',
  ), $ignore_response_codes);
  $ignore_response_codes = array_unique($ignore_response_codes);
  variable_set('linkchecker_ignore_response_codes', implode("\n", $ignore_response_codes));
  $ret[] = array(
    'success' => TRUE,
    'query' => 'Ignored response codes have been upgraded to ' . implode(",", $ignore_response_codes),
  );

  // Remove obsolete settings.
  variable_del('linkchecker_ignore_responses');
  variable_del('linkchecker_rebuild');
  variable_del('linkchecker_maxtime');
  variable_del('linkchecker_socket_timeout');
  variable_del('linkchecker_max_links_per_node');
  variable_del('linkchecker_remove_after');
  variable_del('linkchecker_give_up');

  // D5 required job_queue.module. We need to make sure both is active.
  if (!module_exists('job_queue')) {
    module_disable(array(
      'linkchecker',
    ));
    drupal_set_message('The required job_queue module is missing. The linkchecker module has been disabled. Install job_queue module and re-enable linkchecker, please.', 'error');
  }
  return $ret;
}

/**
 * Install linkchecker_comments table.
 */
function linkchecker_update_5201() {
  $ret = array();

  // Create new tables.
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("CREATE TABLE {linkchecker_comments} (\n        cid INT NOT NULL,\n        lid INT NOT NULL,\n        PRIMARY KEY (cid ,lid)\n      ) /*!40100 DEFAULT CHARACTER SET utf8 */;");
      break;
  }
  return $ret;
}

/**
 * Remove unnecessary elements from custom submit buttons.
 */
function linkchecker_update_5202() {
  $ret = array();
  variable_del('linkchecker_analyze');
  variable_del('linkchecker_clear_analyze');
  return $ret;
}

/**
 * Change default value for linkchecker_links.code to -1.
 *
 * fsockopen may return 0 as an indication that the error occurred before
 * the connect() call. This is most likely due to a problem initializing the
 * socket.
 */
function linkchecker_update_5203() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {linkchecker_links} MODIFY COLUMN `code` INT NOT NULL DEFAULT -1");
      break;
  }
  return $ret;
}

/**
 * Make fields unsigned.
 */
function linkchecker_update_5204() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {linkchecker_links} CHANGE lid `lid` INT unsigned NOT NULL auto_increment");
      $ret[] = update_sql("ALTER TABLE {linkchecker_boxes} DROP PRIMARY KEY");
      $ret[] = update_sql("ALTER TABLE {linkchecker_boxes} CHANGE bid `bid` INT unsigned NOT NULL");
      $ret[] = update_sql("ALTER TABLE {linkchecker_boxes} CHANGE lid `lid` INT unsigned NOT NULL");
      $ret[] = update_sql("ALTER TABLE {linkchecker_boxes} ADD PRIMARY KEY (bid, lid)");
      $ret[] = update_sql("ALTER TABLE {linkchecker_comments} DROP PRIMARY KEY");
      $ret[] = update_sql("ALTER TABLE {linkchecker_comments} CHANGE cid `cid` INT unsigned NOT NULL");
      $ret[] = update_sql("ALTER TABLE {linkchecker_comments} CHANGE lid `lid` INT unsigned NOT NULL");
      $ret[] = update_sql("ALTER TABLE {linkchecker_comments} ADD PRIMARY KEY (cid, lid)");
      $ret[] = update_sql("ALTER TABLE {linkchecker_nodes} DROP PRIMARY KEY");
      $ret[] = update_sql("ALTER TABLE {linkchecker_nodes} CHANGE nid `nid` INT unsigned NOT NULL");
      $ret[] = update_sql("ALTER TABLE {linkchecker_nodes} CHANGE lid `lid` INT unsigned NOT NULL");
      $ret[] = update_sql("ALTER TABLE {linkchecker_nodes} ADD PRIMARY KEY (nid, lid)");
      break;
  }
  return $ret;
}

/**
 * Remove wrong auto_increment from linkchecker_links.lid.
 */
function linkchecker_update_5205() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {linkchecker_links} MODIFY COLUMN `lid` INTEGER UNSIGNED NOT NULL");
      break;
  }
  return $ret;
}

/**
 * Remove any references to nodes that are not published and also remove their comments references.
 */
function linkchecker_update_5206() {
  $ret = array();
  $ret[] = update_sql("DELETE FROM {linkchecker_nodes} WHERE nid IN (SELECT nid FROM {node} WHERE status = 0)");
  $ret[] = update_sql("DELETE FROM {linkchecker_comments} WHERE cid IN (SELECT c.cid FROM {node} n INNER JOIN {comments} c ON c.nid = n.nid WHERE n.status = 0)");
  return $ret;
}

/**
 * Add status column to links table.
 */
function linkchecker_update_5207() {
  $ret = array();
  $ret[] = update_sql("ALTER TABLE {linkchecker_links} ADD COLUMN `status` INTEGER NOT NULL DEFAULT 1");
  return $ret;
}

/**
 * Remove obsolete variable.
 */
function linkchecker_update_5208() {
  $ret = array();
  variable_del('linkchecker_cleanup_links_interval');
  return $ret;
}

/**
 * Update the permissions table, to reflect changes to hook_perm.
 */
function linkchecker_update_5209() {
  $ret = array();
  $res = db_query('SELECT rid, perm FROM {permission}');
  $perms = array();
  while ($p = db_fetch_object($res)) {
    $perm = $p->perm;
    $perm = preg_replace('/access linkchecker/', 'access broken links report', $perm);
    $perms[$p->rid] = $perm;
  }
  foreach ($perms as $rid => $renamed_permission) {
    db_query("UPDATE {permission} SET perm = '%s' WHERE rid = %d", $renamed_permission, $rid);
    $ret[] = array(
      'success' => TRUE,
      'query' => 'UPDATE {permission} SET perm = ' . check_plain($renamed_permission) . ' WHERE rid = ' . $rid,
    );
  }
  return $ret;
}

/**
 * Add status code 302 to the default list of ignored response codes.
 */
function linkchecker_update_5210() {
  $ret = array();
  $ignore_response_codes = preg_split('/(\\r\\n?|\\n)/', variable_get('linkchecker_ignore_response_codes', "200\n302\n304\n401\n403"));
  if (!in_array('302', $ignore_response_codes)) {
    $ignore_response_codes[] = '302';
    sort($ignore_response_codes);
    variable_set('linkchecker_ignore_response_codes', implode("\n", $ignore_response_codes));
    $ret[] = array(
      'success' => TRUE,
      'query' => 'Added the status code 302 to the list of ignored response codes.',
    );
  }
  else {
    $ret[] = array(
      'success' => TRUE,
      'query' => 'No action taken. The status code 302 was already on the list of ignored response codes.',
    );
  }
  return $ret;
}

/**
 * Remove obsolete variable.
 */
function linkchecker_update_5211() {
  $ret = array();
  variable_del('linkchecker_pathfilter_support');
  return $ret;
}

/**
 * Add RFC documenation domains back if they have been removed.
 */
function linkchecker_update_5212() {
  $ret = array();
  $linkchecker_disable_link_check_for_urls = array_filter(preg_split('/(\\r\\n?|\\n)/', variable_get('linkchecker_disable_link_check_for_urls', LINKCHECKER_RESERVED_DOCUMENTATION_DOMAINS)));
  variable_set('linkchecker_disable_link_check_for_urls', implode("\n", array_unique(array_merge(explode("\n", LINKCHECKER_RESERVED_DOCUMENTATION_DOMAINS), $linkchecker_disable_link_check_for_urls))));
  $ret[] = array(
    'success' => TRUE,
    'query' => 'Added RFC documenation domains back if they have been removed.',
  );
  return $ret;
}

Functions

Namesort descending Description
linkchecker_check_http_request Backport of D6 system_check_http_request() function.
linkchecker_install Implementation of hook_install().
linkchecker_requirements Implementation of hook_requirements().
linkchecker_uninstall Implementation of hook_uninstall().
linkchecker_update_5200 Upgrade module to new schema.
linkchecker_update_5201 Install linkchecker_comments table.
linkchecker_update_5202 Remove unnecessary elements from custom submit buttons.
linkchecker_update_5203 Change default value for linkchecker_links.code to -1.
linkchecker_update_5204 Make fields unsigned.
linkchecker_update_5205 Remove wrong auto_increment from linkchecker_links.lid.
linkchecker_update_5206 Remove any references to nodes that are not published and also remove their comments references.
linkchecker_update_5207 Add status column to links table.
linkchecker_update_5208 Remove obsolete variable.
linkchecker_update_5209 Update the permissions table, to reflect changes to hook_perm.
linkchecker_update_5210 Add status code 302 to the default list of ignored response codes.
linkchecker_update_5211 Remove obsolete variable.
linkchecker_update_5212 Add RFC documenation domains back if they have been removed.

Constants

Namesort descending Description
LINKCHECKER_MINIMUM_MYSQL @file Installation file for Link Checker module.