You are here

weather.install in Weather 5

File

weather.install
View source
<?php

/*
 * Copyright (C) 2006 Tobias Toedter <t.toedter@gmx.net>
 *
 * This file is part of the Drupal Weather module.
 *
 * Weather is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Weather is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Weather; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

/**
 * Implementation of hook_install().
 * 
 * We use the install hook to clean up the
 * Drupal tables from old variables used by
 * previous versions of this module.
 */
function weather_install() {

  // these were used by the official module
  variable_del('weather_region');
  variable_del('weather_scale');
  variable_del('weather_mini_block');
  variable_del('weather_extended_forecast');
  variable_del('weather_xml_feed');
  variable_del('weather_recache_cache');
  variable_del('weather_enable_cron');

  // these were added by an inofficial module release for Drupal 4.7
  variable_del('weather_host');
  variable_del('weather_title');

  // these were used in version 1.x of this module, they are no longer
  // needed in the 2.x series
  variable_del('weather_country');
  variable_del('weather_icao');
  variable_del('weather_icao_name');
  variable_del('weather_place');
  variable_del('weather_units');
  variable_del('weather_use_cron');

  // icao: ICAO code of the METAR station
  // next_update_on: timestamp of next possible update
  // metar_raw: raw METAR data, not parsed
  $sql = "CREATE TABLE {weather} (\n    icao VARCHAR(4) DEFAULT '' NOT NULL,\n    next_update_on INTEGER DEFAULT 0 NOT NULL,\n    metar_raw VARCHAR(255) DEFAULT '' NOT NULL,\n    PRIMARY KEY (icao)\n  )";

  // ensure the utf8 character set on MySQL (syntax for 4.1 and above)
  if ($GLOBALS['db_type'] == 'mysql' or $GLOBALS['db_type'] == 'mysqli') {
    $sql .= " /*!40100 DEFAULT CHARACTER SET utf8 */";
  }
  db_query($sql);

  // the table stores the configuration of a custom user weather block
  // uid: user id
  // cid: configuration id, to enable multiple locations in one block
  // icao: ICAO code of the METAR station
  // real_name: the name to display for the ICAO code
  // units: units for display (metric, imperial)
  // weight: the weight of the location
  $sql = "CREATE TABLE {weather_config} (\n    uid INTEGER DEFAULT 0 NOT NULL,\n    cid INTEGER DEFAULT 0 NOT NULL,\n    icao VARCHAR(4) DEFAULT '' NOT NULL,\n    real_name VARCHAR(255) DEFAULT '' NOT NULL,\n    units VARCHAR(8) DEFAULT 'metric' NOT NULL,\n    weight INTEGER DEFAULT 0 NOT NULL,\n    PRIMARY KEY (uid, cid)\n  )";

  // ensure the utf8 character set on MySQL (syntax for 4.1 and above)
  if ($GLOBALS['db_type'] == 'mysql' or $GLOBALS['db_type'] == 'mysqli') {
    $sql .= " /*!40100 DEFAULT CHARACTER SET utf8 */";
  }
  db_query($sql);
}

/**
 * Implementation of hook_uninstall().
 */
function weather_uninstall() {
  variable_del('weather_country');
  variable_del('weather_icao');
  variable_del('weather_icao_name');
  variable_del('weather_units');
  variable_del('weather_use_cron');
  variable_del('weather_fetch');
  db_query('DROP TABLE {weather}');
  db_query('DROP TABLE {weather_config}');
}

/**
 * Implementation of hook_update_N().
 * 
 * We use the update hook to clean up the
 * Drupal tables from old variables used by
 * previous versions of this module.
 */
function weather_update_1() {
  $ret = array();

  // these were used by the official module
  variable_del('weather_region');
  variable_del('weather_scale');
  variable_del('weather_mini_block');
  variable_del('weather_extended_forecast');
  variable_del('weather_xml_feed');
  variable_del('weather_recache_cache');
  variable_del('weather_enable_cron');

  // these were added by an inofficial module release for Drupal 4.7
  variable_del('weather_host');
  variable_del('weather_title');

  // the table stores every METAR data that has been received
  // wid: weather id
  // icao: ICAO code of the METAR station
  // reported_on: timestamp of report
  // metar_raw: raw METAR data, not parsed
  $sql = "CREATE TABLE {weather} (\n    wid INTEGER DEFAULT 0 NOT NULL,\n    icao VARCHAR(4) DEFAULT '' NOT NULL,\n    reported_on INTEGER DEFAULT 0 NOT NULL,\n    metar_raw VARCHAR(255) DEFAULT '' NOT NULL,\n    PRIMARY KEY (wid)\n  )";

  // ensure the utf8 character set on MySQL (syntax for 4.1 and above)
  if ($GLOBALS['db_type'] == 'mysql' or $GLOBALS['db_type'] == 'mysqli') {
    $sql .= " /*!40100 DEFAULT CHARACTER SET utf8 */";
  }
  $ret[] = update_sql($sql);
  return $ret;
}
function weather_update_2() {
  $ret = array();

  // these were used in version 1.x of this module, they are no longer
  // needed in the 2.x series
  variable_del('weather_country');
  variable_del('weather_icao');
  variable_del('weather_icao_name');
  variable_del('weather_place');
  variable_del('weather_units');
  variable_del('weather_use_cron');

  // we don't need the wid column. We make icao the new primary key,
  // however, we must first remove double entries. To achieve this,
  // we simply empty the whole table.
  $ret[] = update_sql("DELETE FROM {weather}");

  // drop the primary key
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {weather} DROP PRIMARY KEY");
      $ret[] = update_sql("ALTER TABLE {weather} ADD PRIMARY KEY (icao)");
      $ret[] = update_sql("ALTER TABLE {weather} DROP wid");
      break;
    case 'pgsql':
      $ret[] = update_sql("ALTER TABLE {weather} DROP CONSTRAINT {weather}_pkey");
      $ret[] = update_sql("ALTER TABLE {weather} ADD PRIMARY KEY (icao)");
      $ret[] = update_sql("ALTER TABLE {weather} DROP COLUMN wid");
      break;
  }

  // remove the primary key from the sequence table
  $ret[] = update_sql("DELETE FROM {sequences} WHERE name='{weather_wid}'");

  // change the column reported_on to next_update_on,
  // which is the timestamp of next possible update
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {weather} CHANGE reported_on next_update_on INTEGER DEFAULT 0 NOT NULL");
      break;
    case 'pgsql':
      $ret[] = update_sql("ALTER TABLE {weather} RENAME COLUMN reported_on TO next_update_on");
      break;
  }

  // the table stores the configuration of a custom user weather block
  // uid: user id
  // icao: ICAO code of the METAR station
  // real_name: the name to display for the ICAO code
  // units: units for display (metric, imperial)
  $sql = "CREATE TABLE {weather_config} (\n    uid INTEGER DEFAULT 0 NOT NULL,\n    icao VARCHAR(4) DEFAULT '' NOT NULL,\n    real_name VARCHAR(255) DEFAULT '' NOT NULL,\n    units VARCHAR(8) DEFAULT 'metric' NOT NULL,\n    PRIMARY KEY (uid)\n  )";

  // ensure the utf8 character set on MySQL (syntax for 4.1 and above)
  if ($GLOBALS['db_type'] == 'mysql' or $GLOBALS['db_type'] == 'mysqli') {
    $sql .= " /*!40100 DEFAULT CHARACTER SET utf8 */";
  }
  $ret[] = update_sql($sql);
  return $ret;
}
function weather_update_3() {
  $ret = array();

  // add the configuration id to weather_config,
  // to enable multiple locations in one block.
  // unfortunately, postgresql does not seem to support
  // this well enough, so we just drop the table created
  // in update_2 and re-create it again.
  $sql = "DROP TABLE {weather_config}";
  $ret[] = update_sql($sql);

  // the table stores the configuration of a custom user weather block
  // uid: user id
  // cid: configuration id, to enable multiple locations in one block
  // icao: ICAO code of the METAR station
  // real_name: the name to display for the ICAO code
  // units: units for display (metric, imperial)
  // weight: the weight of the location
  $sql = "CREATE TABLE {weather_config} (\n    uid INTEGER DEFAULT 0 NOT NULL,\n    cid INTEGER DEFAULT 0 NOT NULL,\n    icao VARCHAR(4) DEFAULT '' NOT NULL,\n    real_name VARCHAR(255) DEFAULT '' NOT NULL,\n    units VARCHAR(8) DEFAULT 'metric' NOT NULL,\n    weight INTEGER DEFAULT 0 NOT NULL,\n    PRIMARY KEY (uid, cid)\n  )";

  // ensure the utf8 character set on MySQL (syntax for 4.1 and above)
  if ($GLOBALS['db_type'] == 'mysql' or $GLOBALS['db_type'] == 'mysqli') {
    $sql .= " /*!40100 DEFAULT CHARACTER SET utf8 */";
  }
  $ret[] = update_sql($sql);
  return $ret;
}

Functions

Namesort descending Description
weather_install Implementation of hook_install().
weather_uninstall Implementation of hook_uninstall().
weather_update_1 Implementation of hook_update_N().
weather_update_2
weather_update_3