function yr_verdata_update_7000 in Yr Weatherdata 7.3
Same name and namespace in other branches
- 7 yr_verdata.install \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.
File
- ./
yr_verdata.install, line 125 - Install, update and uninstall functions for the yr_verdata module.
Code
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.');
}