View source
<?php
function date_install_load() {
include_once './' . drupal_get_path('module', 'content') . '/content.module';
include_once './' . drupal_get_path('module', 'content') . '/includes/content.admin.inc';
include_once './' . drupal_get_path('module', 'date_api') . '/date_api.module';
include_once './' . drupal_get_path('module', 'date') . '/date.module';
if (module_exists('views')) {
include_once './' . drupal_get_path('module', 'date') . '/date.views.inc';
}
}
function date_install() {
date_install_load();
content_notify('install', 'date');
$ret = array();
date_clear_all();
return $ret;
}
function date_uninstall() {
date_install_load();
content_notify('uninstall', 'date');
$ret = array();
date_clear_all(TRUE);
return $ret;
}
function date_enable() {
date_install_load();
content_notify('enable', 'date');
date_clear_all();
}
function date_disable() {
date_install_load();
content_notify('disable', 'date');
date_clear_all(TRUE);
}
function date_update_last_removed() {
return 7;
}
function date_update_5100() {
date_install_load();
$ret = array();
$fields = content_fields();
foreach ($fields as $field) {
if (strstr($field['type'], 'date')) {
$field_settings = unserialize(db_result(db_query("SELECT global_settings from {" . content_field_tablename() . "} where field_name = '%s'", $field['field_name'])));
$widget_settings = unserialize(db_result(db_query("SELECT widget_settings from {" . content_instance_tablename() . "} where field_name = '%s'", $field['field_name'])));
$input_format = $field_settings['input_format'];
$input_format_custom = $field_settings['input_format_custom'];
$widget_settings['input_format'] = $input_format;
$widget_settings['input_format_custom'] = $input_format_custom;
$replace = array(
'0000' => 'O',
'00:00' => 'P',
'name' => 'e',
'' => '',
);
if (!empty($field_settings['output_format_zone'])) {
$field_settings['output_format_date'] .= ' ' . $replace[$field_settings['output_format_zone']];
if (!empty($field_settings['output_format_custom'])) {
$field_settings['output_format_custom'] .= ' ' . $replace[$field_settings['output_format_zone']];
}
}
if (!empty($field_settings['output_format_zone_short'])) {
$field_settings['output_format_date_short'] .= ' ' . $replace[$field_settings['output_format_zone_short']];
if (!empty($field_settings['output_format_custom_short'])) {
$field_settings['output_format_custom_short'] .= ' ' . $replace[$field_settings['output_format_zone_short']];
}
}
if (!empty($field_settings['output_format_zone_medium'])) {
$field_settings['output_format_date_medium'] .= ' ' . $replace[$field_settings['output_format_zone_medium']];
if (!empty($field_settings['output_format_custom_medium'])) {
$field_settings['output_format_custom_medium'] .= ' ' . $replace[$field_settings['output_format_zone_medium']];
}
}
if (!empty($field_settings['output_format_zone_long'])) {
$field_settings['output_format_date_long'] .= ' ' . $replace[$field_settings['output_format_zone_long']];
if (!empty($field_settings['output_format_custom_long'])) {
$field_settings['output_format_custom_long'] .= ' ' . $replace[$field_settings['output_format_zone_long']];
}
}
$field_settings['timezone_db'] = 'UTC';
$text_parts = array();
if (isset($widget_settings['select_year']) && empty($widget_settings['select_year'])) {
$text_parts[] = 'year';
}
if (isset($widget_settings['select_month']) && empty($widget_settings['select_month'])) {
$text_parts[] = 'mon';
}
if (isset($widget_settings['select_day']) && empty($widget_settings['select_day'])) {
$text_parts[] = 'mday';
}
$widget_settings['text_parts'] = $text_parts;
$widget_settings['year_range'] = '-' . intval(isset($widget_settings['years_back']) ? $widget_settings['years_back'] : 3) . ':+' . intval(isset($widget_settings['years_forward']) ? $widget_settings['years_forward'] : 3);
$fields = array(
'granularity',
'field_timezone',
'timezone_db',
'tz_handling',
'todate',
'output_format_date',
'output_format_date_short',
'output_format_date_medium',
'output_format_date_long',
'output_format_custom',
'output_format_custom_short',
'output_format_custom_medium',
'output_format_custom_long',
);
foreach (array_keys($field_settings) as $setting) {
if (!in_array($setting, $fields)) {
unset($field_settings[$setting]);
}
}
$widgets = array(
'default_value',
'default_value_custom',
'default_value2',
'default_value_custom2',
'input_format',
'input_format_custom',
'increment',
'text_parts',
'year_range',
);
foreach (array_keys($widget_settings) as $setting) {
if (!in_array($setting, $widgets)) {
unset($widget_settings[$setting]);
}
}
db_query("UPDATE {" . content_field_tablename() . "} SET global_settings = '%s' WHERE field_name='%s'", serialize($field_settings), $field['field_name']);
db_query("UPDATE {" . content_instance_tablename() . "} SET widget_settings = '%s' WHERE field_name='%s'", serialize($widget_settings), $field['field_name']);
$ret[] = array(
'success' => TRUE,
'query' => 'field settings updated to ' . serialize($field_settings) . '<br />widget settings updated to ' . serialize($widget_settings),
);
}
}
content_clear_type_cache();
return $ret;
}
function date_update_5101() {
$ret = array();
return $ret;
}
function date_update_5102() {
date_install_load();
variable_del('date_version');
$ret[] = update_sql("UPDATE {system} SET weight = 1 WHERE name = 'date'");
return date_db_integrity('5103');
}
function date_update_5103() {
$ret = array();
return $ret;
}
function date_update_5104() {
date_install_load();
$ret = array();
cache_clear_all('date_browser_views', 'cache');
return $ret;
}
function date_update_5200() {
date_install_load();
$ret = array();
module_enable(array(
'date_timezone',
));
$ret[] = update_sql("UPDATE {system} SET weight = 1 WHERE name = 'date'");
cache_clear_all('date_formats', 'cache', TRUE);
$replace = array(
'Y' => 'year',
'M' => 'month',
'D' => 'day',
'H' => 'hour',
'N' => 'minute',
'S' => 'second',
);
$fields = content_fields();
foreach ($fields as $field) {
if (strstr($field['type'], 'date')) {
$field_settings = unserialize(db_result(db_query("SELECT global_settings from {" . content_field_tablename() . "} where field_name = '%s'", $field['field_name'])));
$granularity = array();
foreach ((array) $field_settings['granularity'] as $item) {
$granularity[] = strtr($item, $replace);
}
$field_settings['granularity'] = $granularity;
db_query("UPDATE {" . content_field_tablename() . "} SET global_settings = '%s' WHERE field_name='%s'", serialize($field_settings), $field['field_name']);
}
}
content_clear_type_cache();
return $ret;
}
function date_update_5201() {
date_install_load();
$ret = array();
if (db_result(db_query("SELECT COUNT(*) FROM {" . content_instance_tablename() . "} WHERE widget_type = 'date_js'"))) {
$result = db_query("SELECT field_name, widget_type, widget_settings from {" . content_instance_tablename() . "} where widget_type = 'date_js'");
while ($widget = db_fetch_array($result)) {
$widget_settings = unserialize($widget['widget_settings']);
$widget_settings['input_format'] = DATE_FORMAT_DATETIME;
$widget_settings['input_format_custom'] = '';
db_query("UPDATE {" . content_instance_tablename() . "} SET widget_type = 'date_popup', widget_settings = '%s' WHERE field_name = '%s'", serialize($widget_settings), $widget['field_name']);
}
}
content_clear_type_cache();
return $ret;
}
function date_update_5202() {
date_install_load();
return date_db_integrity('5202');
}
function date_update_5203() {
date_install_load();
return date_data_integrity('5203');
}
function date_update_5204() {
date_install_load();
$ret = array();
$fields = content_fields();
foreach ($fields as $field) {
if (($field['type'] == 'date' || $field['type'] == 'datestamp') && $field['tz_handling'] == 'date') {
$db_info = content_database_info($field);
$table = $db_info['table'];
$column = $db_info['columns']['timezone']['column'];
$results = db_query("SELECT DISTINCT {$column} FROM {$table} ORDER BY {$column}");
while ($row = db_fetch_object($results)) {
if (!empty($row->timezone_name)) {
$new = date_timezone_replacement($row->timezone_name);
if (!empty($new) && $new != $row->timezone_name) {
db_query("UPDATE {$table} SET {$column} = '%s' WHERE {$column} = '%s'", $new, $row->timezone_name);
}
}
}
}
}
return $ret;
}
function date_update_5205() {
date_install_load();
$ret = array();
$fields = content_fields();
foreach ($fields as $field) {
if (strstr($field['type'], 'date')) {
$field_settings = unserialize(db_result(db_query("SELECT global_settings from {" . content_field_tablename() . "} where field_name = '%s'", $field['field_name'])));
foreach ($field_settings as $key => $value) {
if ($key == 'tz_handling' && $value == 'gmt') {
$field_settings[$key] = 'utc';
}
}
db_query("UPDATE {" . content_field_tablename() . "} SET global_settings = '%s' WHERE field_name = '%s'", serialize($field_settings), $field['field_name']);
}
}
return $ret;
}
function date_db_integrity($name) {
$ret = array();
$fields = content_fields();
foreach ($fields as $field) {
$db_info = content_database_info($field);
if ($field['type'] == 'date' || $field['type'] == 'datestamp') {
$table = $db_info['table'];
$columns_start = $db_info['columns'];
$columns_end = $db_info['columns'];
$columns_start['value']['length'] = 90;
if ($field['todate']) {
if (!db_column_exists($table, $columns_start['value2']['column'])) {
unset($columns_start['value2']);
}
else {
$columns_start['value2']['length'] = 80;
}
}
if ($field['tz_handling'] == 'date') {
if (!db_column_exists($table, $columns_start['timezone']['column'])) {
unset($columns_start['timezone']);
}
else {
$columns_start['timezone']['length'] = 80;
}
if (!db_column_exists($table, $columns_start['offset']['column'])) {
unset($columns_start['offset']);
}
else {
$columns_start['offset']['length'] = 80;
}
if ($field['todate']) {
if (!db_column_exists($table, $columns_start['offset2']['column'])) {
unset($columns_start['offset2']);
}
else {
$columns_start['offset2']['length'] = 80;
}
}
}
if ($field['repeat'] == 1) {
if (!db_column_exists($table, $columns_start['rrule']['column'])) {
unset($columns_start['rrule']);
}
else {
$columns_start['rrule']['length'] = 80;
}
}
content_alter_db_field($field, $columns_start, $field, $columns_end);
$message = 'Date database integrity check. Updated table ' . $table . ' to set all columns to accept NULL values.';
$ret[] = array(
'success' => TRUE,
'query' => $message,
);
}
}
content_clear_type_cache();
return $ret;
}
function date_data_integrity($name, $force_recalc = FALSE) {
$debug = TRUE;
$ret = array();
$update_name = 'date_update_' . $name;
if (!module_exists('date_api')) {
drupal_install_modules(array(
'date_api',
));
}
if (!module_exists('content')) {
drupal_install_modules(array(
'content',
));
}
if (!isset($_SESSION[$update_name]) || empty($_SESSION[$update_name]['search'])) {
$tables = array();
$_SESSION[$update_name]['search'] = array();
$_SESSION[$update_name]['fields'] = array();
$fields = content_fields();
foreach ($fields as $field) {
if ($field['type'] == 'date' || $field['type'] == 'datestamp') {
$_SESSION[$update_name]['fields'][$field['field_name']] = $field;
$db_info = content_database_info($field);
$tables[] = $db_info['table'];
}
}
$_SESSION[$update_name]['counted'] = 0;
$tables = array_unique($tables);
foreach ($tables as $table) {
$result = db_query("SELECT nid, vid FROM {" . $table . "} ORDER BY nid, vid");
while ($row = db_fetch_array($result)) {
$_SESSION[$update_name]['search'][] = $row['nid'] . ':' . $row['vid'];
}
}
sort($_SESSION[$update_name]['search']);
$_SESSION[$update_name]['search_all'] = $_SESSION[$update_name]['search'];
$_SESSION[$update_name]['count_total'] = count($_SESSION[$update_name]['search']);
$_SESSION[$update_name]['counted'] = 0;
}
$_SESSION[$update_name]['changed'] = array();
$_SESSION[$update_name]['unchanged'] = array();
for ($i = 0; $i <= 5; $i++) {
$update = !empty($_SESSION[$update_name]['search']) ? array_shift($_SESSION[$update_name]['search']) : array();
if (!empty($update)) {
$ids = explode(':', $update);
$node = node_load($ids[0], $ids[1]);
$needs_fix = FALSE;
foreach ($_SESSION[$update_name]['fields'] as $field_name => $field) {
$items = (array) $node->{$field_name};
$add = array();
foreach ($items as $delta => $item) {
if ($field['type'] == 'date' && !$field['required'] && (substr($item['value'], 0, 10) == '0000-00-00' || substr($item['value'], 0, 10) == '0001-00-00')) {
$item['value'] = NULL;
$needs_fix = TRUE;
}
if ($field['type'] == 'datestamp' && !$field['required'] && empty($item['value'])) {
$item['value'] = NULL;
$needs_fix = TRUE;
}
$add[$delta]['value'] = $item['value'];
if ($field['todate']) {
if (empty($item['value2']) && !empty($item['value']) || $force_recalc) {
$needs_fix = TRUE;
}
$add[$delta]['value2'] = !empty($item['value2']) ? $item['value2'] : $item['value'];
}
if ($field['tz_handling'] == 'date') {
if (empty($item['offset']) || empty($item['timezone']) || $force_recalc) {
$needs_fix = TRUE;
$timezone = date_get_timezone($field['tz_handling'], $item['timezone']);
$date = date_make_date($item['value'], 'GMT', $field['type']);
if (!empty($date) && !empty($timezone)) {
date_timezone_set($date, timezone_open($timezone));
$add[$delta]['timezone'] = $field['required'] || $item['value'] ? $timezone : NULL;
$add[$delta]['offset'] = $field['required'] || $item['value'] ? date_offset_get($date) : NULL;
}
$date = date_make_date($item['value2'], 'GMT', $field['type']);
if (!empty($date) && !empty($timezone)) {
date_timezone_set($date, timezone_open($timezone));
$add[$delta]['offset2'] = $field['required'] || $item['value'] ? date_offset_get($date) : NULL;
}
}
}
}
if (!empty($add)) {
$node->{$field_name} = $add;
}
}
$_SESSION[$update_name]['counted']++;
if ($needs_fix) {
node_save($node);
$_SESSION[$update_name]['changed'][] = $node->nid . ($node->vid != $node->nid ? ':' . $node->vid : '');
}
else {
$_SESSION[$update_name]['unchanged'][] = $node->nid . ($node->vid != $node->nid ? ':' . $node->vid : '');
}
}
}
if ($_SESSION[$update_name]['counted'] && $_SESSION[$update_name]['count_total'] && $_SESSION[$update_name]['counted'] < $_SESSION[$update_name]['count_total']) {
$progress = floatval($_SESSION[$update_name]['counted'] / $_SESSION[$update_name]['count_total']);
if ($debug) {
$message = 'Date data integrity check. Total nodes checked: ' . $_SESSION[$update_name]['counted'] . '<br />Updated nodes: ' . implode(', ', $_SESSION[$update_name]['changed']) . '<br />Unchanged nodes: ' . implode(', ', $_SESSION[$update_name]['unchanged']);
}
if ($debug) {
return array(
'#finished' => $progress,
array(
'success' => TRUE,
'query' => $message,
),
);
}
else {
return array(
'#finished' => $progress,
);
}
}
else {
drupal_set_message(t('All nodes with date fields have been checked.'));
$message = 'Date data integrity check. Total nodes checked: ' . $_SESSION[$update_name]['counted'];
if ($debug) {
$message .= '<br />Updated nodes: ' . implode(', ', $_SESSION[$update_name]['changed']) . '<br />Unchanged nodes: ' . implode(', ', $_SESSION[$update_name]['unchanged']);
}
unset($_SESSION[$update_name]);
content_clear_type_cache();
db_query('DELETE FROM {cache_content}');
return array(
'#finished' => 1,
array(
'success' => TRUE,
'query' => $message,
),
);
}
}