yr_verdata.install in Yr Weatherdata 7.3
Same filename and directory in other branches
Install, update and uninstall functions for the yr_verdata module.
File
yr_verdata.installView source
<?php
/**
* @file
* Install, update and uninstall functions for the yr_verdata module.
*/
/**
* Implementation of hook_requirements().
*/
function yr_verdata_requirements($phase) {
$requirements = array();
$t = get_t();
switch ($phase) {
case 'install':
if (!function_exists('curl_init')) {
$req = 'cURL';
$requirements['curl'] = array(
'title' => $req,
'description' => $t('@req is required for yr_verdata to function properly. This is normally built-in with PHP 5. Contact your server administrator.', array(
'@req' => $req,
)),
'severity' => REQUIREMENT_ERROR,
);
}
if (!function_exists('simplexml_load_file')) {
$req = 'SimpleXML';
$requirements['simplexml'] = array(
'title' => $req,
'description' => $t('@req is required for yr_verdata to function properly. This is normally built-in with PHP 5. Contact your server administrator.', array(
'@req' => $req,
)),
'severity' => REQUIREMENT_ERROR,
);
}
break;
}
return $requirements;
}
/**
* Implementation of hook_uninstall().
*/
function yr_verdata_uninstall() {
drupal_uninstall_schema('yr_verdata');
file_unmanaged_delete_recursive('public://yr_verdata');
}
/**
* Implementation of hook_schema().
*/
function yr_verdata_schema() {
$schema['yr_verdata'] = array(
'description' => 'Table for stored locations.',
'fields' => array(
'yid' => array(
'description' => 'The unique local identifier for a location.',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
),
'url' => array(
'description' => 'The url for the forecast at yr.no.',
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
),
'file' => array(
'description' => 'The filename for the cahced forecast. An md5 hash of the url.',
'type' => 'varchar',
'length' => 50,
'not null' => TRUE,
'default' => '',
),
'lang' => array(
'description' => 'The language we want the forecast in.',
'type' => 'varchar',
'length' => 8,
'not null' => TRUE,
'default' => '',
),
'name' => array(
'description' => 'The name of the location, for sorting purposes.',
'type' => 'varchar',
'length' => 100,
'not null' => TRUE,
'default' => '',
),
'region' => array(
'description' => 'The name of the region, for sorting purposes.',
'type' => 'varchar',
'length' => 100,
'not null' => TRUE,
'default' => '',
),
'country' => array(
'description' => 'The name of the country, for sorting purposes.',
'type' => 'varchar',
'length' => 100,
'not null' => TRUE,
'default' => '',
),
'weight' => array(
'description' => 'Used to sort locations in lists and blocks.',
'type' => 'int',
'length' => 1,
'not null' => TRUE,
'default' => 0,
),
'updated' => array(
'description' => 'The unix timestamp for the last time this location had its feed checked.',
'type' => 'int',
'length' => 12,
'not null' => TRUE,
'default' => 0,
),
),
'indexes' => array(
'region' => array(
'region',
),
'country' => array(
'country',
),
),
'primary key' => array(
'yid',
),
);
return $schema;
}
/**
* Implementation of hook_update_N().
* This updates from 6.x-1.x or 6.x-2.x to 7.x-1.x.
*/
function yr_verdata_update_7000(&$sandbox) {
// Check to see what schema we are updating from.
// If we are going from 6.x-2.x to 7.x-1.x, the schema is already ready for
// 7.x-1.x so we just update the schema version number.
// We use a variable, because the installed schema will return 6999 instead of the actual installed schema.
$version = variable_get('yr_verdata_62', 0);
if ($version == 1) {
// Updating from 6.x-2.x, no problem.
drupal_set_installed_schema_version('yr_verdata', 7000);
}
else {
// Updating from 6.x-1.x, modify the database table.
// Add the new fields.
db_add_field('yr_verdata', 'file', array(
'type' => 'varchar',
'not null' => TRUE,
'default' => '',
'length' => 50,
'description' => 'The filename for the cahced forecast. An md5 hash of the url.',
));
db_add_field('yr_verdata', 'lang', array(
'type' => 'varchar',
'not null' => TRUE,
'default' => '',
'length' => 8,
'description' => 'The language we want the forecast in.',
));
db_add_field('yr_verdata', 'name', array(
'type' => 'varchar',
'not null' => TRUE,
'default' => '',
'length' => 100,
'description' => 'The name of the location, for sorting purposes.',
));
db_add_field('yr_verdata', 'weight', array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'length' => 1,
'description' => 'Used to sort locations in lists and blocks.',
));
db_add_field('yr_verdata', 'updated', array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'length' => 12,
'description' => 'The unix timestamp for the last time this location had its feed checked.',
));
// Load everything into an array and update each entry with values for the new fields.
$result = db_query("SELECT * FROM {yr_verdata}");
$records = $result
->fetchAll();
// All existing data is in the $records array. Drop some fields and recreate them.
db_drop_field('yr_verdata', 'url');
db_drop_field('yr_verdata', 'region');
db_drop_field('yr_verdata', 'country');
// Create anew...
db_add_field('yr_verdata', 'url', array(
'type' => 'varchar',
'not null' => TRUE,
'default' => '',
'length' => 255,
'description' => 'The url for this location at yr.no.',
));
db_add_field('yr_verdata', 'region', array(
'type' => 'varchar',
'not null' => TRUE,
'default' => '',
'length' => 100,
'description' => 'The region name for this location.',
));
db_add_field('yr_verdata', 'country', array(
'type' => 'varchar',
'not null' => TRUE,
'default' => '',
'length' => 100,
'description' => 'The country name for this location.',
));
foreach ($records as $record) {
// Update each individual record.
// Resolve what the new values are.
$url = 'http://www.yr.no/' . $record->url;
// The url needs the http://www.yr.no/ at the start in the 7.x-1.x branch.
// Make sure we always have a trailing slash.
if (drupal_substr($url, -1) != '/') {
$url .= '/';
}
$file = md5($url);
// Figure out what language this is.
$components = explode('/', drupal_substr($url, 17, -1));
switch ($components[0]) {
case 'place':
// English
$lang = 'en';
break;
case 'sted':
// Norwegian Bokmål
$lang = 'nb';
break;
case 'stad':
// Norwegian Nynorsk
$lang = 'nn';
break;
case 'paikka':
// Kvääni
$lang = 'no-kv';
break;
case 'sadji':
// Sami
$lang = 'smi';
break;
}
$n = count($components) - 1;
// Get the name from the url.
db_update('yr_verdata')
->fields(array(
'url' => $url,
'file' => $file,
'lang' => $lang,
'name' => str_replace('_', ' ', trim($components[$n])),
))
->condition('yid', $record->yid, '=')
->execute();
}
// Add indexes.
db_add_index('yr_verdata', 'region', array(
'region',
));
db_add_index('yr_verdata', 'country', array(
'country',
));
// Now remove all the old shit.
db_drop_field('yr_verdata', 'location');
db_drop_field('yr_verdata', 'subregion');
}
// If there are any old files lying around, delete them. We don't need them and can get new ones at first cron run.
file_unmanaged_delete_recursive('public://yr_verdata');
return t('Updated yr_verdata. You should run cron to get fresh forecasts.');
}
/**
* Implementation of hook_update_N().
* This updates from 7.x-1.x to 7.x-3.x.
*/
function yr_verdata_update_7300(&$sandbox) {
// @todo Lage update funksjon(er).
}
Functions
Name![]() |
Description |
---|---|
yr_verdata_requirements | Implementation of hook_requirements(). |
yr_verdata_schema | Implementation of hook_schema(). |
yr_verdata_uninstall | Implementation of hook_uninstall(). |
yr_verdata_update_7000 | Implementation of hook_update_N(). This updates from 6.x-1.x or 6.x-2.x to 7.x-1.x. |
yr_verdata_update_7300 | Implementation of hook_update_N(). This updates from 7.x-1.x to 7.x-3.x. |