You are here

xmlsitemap_node.install in XML sitemap 5

File

xmlsitemap_node/xmlsitemap_node.install
View source
<?php

/**
 * Implementation of hook_requirements().
 */
function xmlsitemap_node_requirements($phase) {
  $t = get_t();
  $requirements = array();
  if (in_array($GLOBALS['db_type'], array(
    'mysql',
    'mysqli',
  )) && version_compare(db_version(), '4.0.14') < 0) {
    $requirements['xmlsitemap_node_sql'] = array(
      'title' => $t('XML Sitemap: Node'),
      'value' => $t('Your MySQL version is too low. &ldquo;XML Sitemap: Node&rdquo; requires MySQL 4.0.14 or higher.'),
      'severity' => REQUIREMENT_ERROR,
    );
  }
  return $requirements;
}

/**
 * Implementation of hook_install().
 */
function xmlsitemap_node_install() {
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      db_query("CREATE TABLE {xmlsitemap_node} (\n        nid int,\n        pid int,\n        last_changed int(11),\n        previously_changed int(11),\n        last_comment int(11),\n        previous_comment int(11),\n        priority_override float,\n        PRIMARY KEY (nid)\n      ) /*!40100 DEFAULT CHARACTER SET utf8 */;");
      break;
    case 'pgsql':
      db_query("CREATE TABLE {xmlsitemap_node} (\n        nid integer,\n        pid integer,\n        last_changed integer,\n        previously_changed integer,\n        last_comment integer,\n        previous_comment integer,\n        priority_override real,\n        PRIMARY KEY (nid)\n      );");
      break;
  }
  _xmlsitemap_node_gsitemap_replace();
}

/**
 * Transfer data from Google Sitemap if it is installed.
 */
function _xmlsitemap_node_gsitemap_replace() {
  if (db_result(db_query("\n    SELECT 1 FROM {system}\n    WHERE type = 'module' AND name = 'gsitemap' AND (status = 1 OR schema_version >= 0)\n  "))) {
    switch ($GLOBALS['db_type']) {
      case 'mysql':
      case 'mysqli':
        db_query("\n          UPDATE {xmlsitemap_node} xn, {url_alias} ua SET xn.pid = ua.pid\n          WHERE xn.pid IS NULL AND ua.src = CONCAT('node/', CAST(xn.nid AS CHAR))\n        ");
        break;
      case 'pgsql':
        db_query("\n          UPDATE {xmlsitemap_node} SET pid = {url_alias}.pid FROM {url_alias}\n          WHERE {xmlsitemap_node}.pid IS NULL AND {url_alias}.src = CONCAT('node/', CAST(nid AS VARCHAR))\n        ");
        break;
    }
    db_query("\n      INSERT INTO {xmlsitemap_node} (nid, pid, last_changed, previously_changed, last_comment, previous_comment, priority_override)\n      SELECT nid, pid, last_changed, previously_changed, last_comment, previous_comment, priority_override FROM {gsitemap}\n    ");
    $settings = db_query("SELECT * FROM {variable} WHERE name LIKE 'gsitemap\\_%wt'");
    while ($variable = db_fetch_object($settings)) {
      $ts = strlen('gsitemap_');
      $tl = strlen('wt');
      $type = substr($variable->name, $ts, -$tl);
      if (node_get_types('type', $type) === FALSE) {
        variable_set("xmlsitemap_node_{$type}_priority", (double) unserialize($variable->value));
      }
      else {
        variable_set("xmlsitemap_node_type_priority_{$type}", (double) unserialize($variable->value));
      }
      variable_del($variable->name);
    }
    $variable = db_fetch_object(db_query("SELECT * FROM {variable} WHERE name = 'gsitemap_countcom'"));
    variable_set('xmlsitemap_node_count_comments', unserialize($variable->value));
    variable_del($variable->name);
  }
}

/**
 * Implementation of hook_enable().
 */
function xmlsitemap_node_enable() {
  $weight = db_result(db_query("SELECT weight FROM {system} WHERE type = 'module' AND name = 'pathauto'"));
  if ($weight !== FALSE) {
    db_query("UPDATE {system} SET weight = %d WHERE type = 'module' AND name = 'xmlsitemap_node'", ++$weight);
  }
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      db_query("\n        UPDATE {xmlsitemap_node} xn INNER JOIN {node} n ON xn.nid = n.nid LEFT JOIN {node_comment_statistics} s ON s.nid = xn.nid\n        SET xn.previously_changed = xn.last_changed, xn.last_changed = n.changed, xn.last_comment = s.last_comment_timestamp\n        WHERE xn.nid = n.nid AND (xn.last_changed <> n.changed OR xn.last_comment <> s.last_comment_timestamp)\n      ");
      break;
    case 'pgsql':
      db_query("\n        UPDATE {xmlsitemap_node} SET previously_changed = last_changed, last_changed = {node}.changed, last_comment = {node_comment_statistics}.last_comment_timestamp\n        FROM {node} LEFT JOIN {node_comment_statistics} ON {node_comment_statistics}.nid = {node}.nid\n        WHERE {xmlsitemap_node}.nid = {node}.nid AND (last_changed <> {node}.changed OR last_comment <> {node_comment_statistics}.last_comment_timestamp)\n      ");
      break;
  }
  db_query(_xmlsitemap_node_insert_query());
  db_query(_xmlsitemap_node_updatepid_query());
  xmlsitemap_update_sitemap();
}

/**
 * Build SQL query for populating the xmlsitemap_node table.
 * @return Query string
 */
function _xmlsitemap_node_insert_query() {
  return "\n    INSERT INTO {xmlsitemap_node} (nid, last_changed, last_comment, previous_comment)\n    SELECT n.nid, n.changed, s.last_comment_timestamp, MAX(c.timestamp) FROM {node} n\n    LEFT JOIN {node_comment_statistics} s ON s.nid = n.nid\n    LEFT OUTER JOIN {comments} c ON c.nid = n.nid AND c.timestamp < s.last_comment_timestamp\n    LEFT JOIN {xmlsitemap_node} xn ON xn.nid = n.nid\n    WHERE xn.nid IS NULL\n    GROUP BY n.nid, n.changed, s.last_comment_timestamp\n  ";
}

/**
 * Build SQL query for updating pids in xmlsitemap_node table.
 * @return Query string
 */
function _xmlsitemap_node_updatepid_query() {
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $query = "\n        UPDATE {xmlsitemap_node} xn INNER JOIN {url_alias} ua\n        ON ua.src = CONCAT('node/', CAST(xn.nid AS CHAR))\n        SET xn.pid = ua.pid\n        WHERE xn.pid IS NULL\n      ";
      break;
    case 'pgsql':
      $query = "\n        UPDATE {xmlsitemap_node}\n        SET pid = {url_alias}.pid\n        FROM {url_alias}\n        WHERE {url_alias}.src = CONCAT('node/', CAST(nid AS VARCHAR)) AND {xmlsitemap_node}.pid IS NULL\n      ";
      break;
  }
  return $query;
}

/**
 * Implementation of hook_disable().
 */
function xmlsitemap_node_disable() {
  xmlsitemap_update_sitemap();
}

/**
 * Implementation of hook_uninstall().
 */
function xmlsitemap_node_uninstall() {
  db_query("DROP TABLE {xmlsitemap_node}");
  $settings = db_query("SELECT name FROM {variable} WHERE name LIKE 'xmlsitemap\\_node\\_%'");
  while ($variable = db_fetch_array($settings)) {
    variable_del($variable->name);
  }
}

/**
 * Implementation of hook_update_N().
 * Fix scrambled values.
 */
function xmlsitemap_node_update_1() {
  $ret = array();
  $result = db_query("SELECT xn.nid FROM {xmlsitemap_node} xn LEFT JOIN {node} n ON n.nid = xn.nid WHERE n.nid IS NULL");
  $bad_nids = array();
  while ($node = db_fetch_object($result)) {
    $bad_nids[] = $node->nid;
  }
  if (!empty($bad_nids)) {
    $ret[] = update_sql("DELETE FROM {xmlsitemap_node} WHERE nid IN (" . db_escape_string(implode(', ', $bad_nids)) . ")");
    $ret[] = update_sql(_xmlsitemap_node_insert_query());
    xmlsitemap_update_sitemap();
  }
  return $ret;
}

/**
 * Implementation of hook_update_N().
 * Add missing nodes.
 */
function xmlsitemap_node_update_2() {
  $ret = array(
    update_sql(_xmlsitemap_node_insert_query()),
  );
  xmlsitemap_update_sitemap();
  return $ret;
}

/**
 * Implementation of hook_update_N().
 * Add missing URL aliases.
 * Update last-changed times.
 */
function xmlsitemap_node_update_3() {
  $ret = array(
    update_sql("UPDATE {xmlsitemap_node} SET pid = NULL WHERE pid = 0"),
  );
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("\n        UPDATE {xmlsitemap_node} xn\n        INNER JOIN {node} n ON n.nid = xn.nid\n        LEFT JOIN {url_alias} ua ON ua.src = CONCAT('node/', CAST(xn.nid AS CHAR)) AND xn.pid IS NULL\n        SET xn.last_changed = n.changed, xn.pid = ua.pid\n      ");
      break;
    case 'pgsql':
      $ret[] = update_sql("\n        UPDATE {xmlsitemap_node}\n        SET pid = {url_alias}.pid, last_changed = {node}.changed\n        FROM {node} LEFT JOIN {url_alias} ON {url_alias}.src = CONCAT('node/', CAST({xmlsitemap_node}.nid AS VARCHAR)) AND {xmlsitemap_node}.pid IS NULL\n        WHERE {xmlsitemap_node}.nid = {node}.nid\n      ");
      break;
  }
  $ret = array_merge($ret, xmlsitemap_node_update_2());
  return $ret;
}

Functions

Namesort descending Description
xmlsitemap_node_disable Implementation of hook_disable().
xmlsitemap_node_enable Implementation of hook_enable().
xmlsitemap_node_install Implementation of hook_install().
xmlsitemap_node_requirements Implementation of hook_requirements().
xmlsitemap_node_uninstall Implementation of hook_uninstall().
xmlsitemap_node_update_1 Implementation of hook_update_N(). Fix scrambled values.
xmlsitemap_node_update_2 Implementation of hook_update_N(). Add missing nodes.
xmlsitemap_node_update_3 Implementation of hook_update_N(). Add missing URL aliases. Update last-changed times.
_xmlsitemap_node_gsitemap_replace Transfer data from Google Sitemap if it is installed.
_xmlsitemap_node_insert_query Build SQL query for populating the xmlsitemap_node table.
_xmlsitemap_node_updatepid_query Build SQL query for updating pids in xmlsitemap_node table.