View source
<?php
function weight_schema() {
$schema['weight_settings'] = array(
'description' => 'Table for storing Weight configuration',
'fields' => array(
'type' => array(
'type' => 'varchar',
'length' => 32,
'not null' => TRUE,
'default' => '',
),
'weight_enabled' => array(
'type' => 'int',
'size' => 'tiny',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
'weight_range' => array(
'type' => 'int',
'size' => 'tiny',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 20,
),
'menu_weight' => array(
'type' => 'int',
'size' => 'tiny',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
'weight_default' => array(
'type' => 'int',
'size' => 'tiny',
'unsigned' => FALSE,
'not null' => TRUE,
'default' => 0,
),
'sync_translations' => array(
'type' => 'int',
'size' => 'tiny',
'unsigned' => FALSE,
'not null' => TRUE,
'default' => 0,
),
),
'primary key' => array(
'type',
),
'foreign keys' => array(
'node_type' => array(
'table' => 'node_type',
'columns' => array(
'type' => 'type',
),
),
),
);
$schema['weight_weights'] = array(
'fields' => array(
'entity_id' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
),
'entity_type' => array(
'type' => 'varchar',
'length' => 32,
'not null' => TRUE,
),
'weight' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
),
'primary key' => array(
'entity_id',
),
);
return $schema;
}
function weight_schema_7200() {
$schema['weight_settings'] = array(
'description' => 'Table for storing Weight configuration',
'fields' => array(
'type' => array(
'type' => 'varchar',
'length' => 32,
'not null' => TRUE,
'default' => '',
),
'weight_enabled' => array(
'type' => 'int',
'size' => 'tiny',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
'weight_range' => array(
'type' => 'int',
'size' => 'tiny',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 20,
),
'menu_weight' => array(
'type' => 'int',
'size' => 'tiny',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
'weight_default' => array(
'type' => 'int',
'size' => 'tiny',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
),
'primary key' => array(
'type',
),
'foreign keys' => array(
'node_type' => array(
'table' => 'node_type',
'columns' => array(
'type' => 'type',
),
),
),
);
$schema['weight_weights'] = array(
'fields' => array(
'entity_id' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
),
'entity_type' => array(
'type' => 'varchar',
'length' => 32,
'not null' => TRUE,
),
'weight' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
),
'primary key' => array(
'entity_id',
),
);
return $schema;
}
function weight_update_7200() {
$types = node_type_get_names();
$weight_types = variable_get('weight_node_types', array());
$range = variable_get('weight_range', 20);
$menu_weight = (int) variable_get('weight_use_menu', 0);
$default = (int) variable_get('weight_default', 0);
$schema = weight_schema_7200();
_drupal_schema_initialize($schema, 'weight');
foreach ($schema as $name => $table) {
db_create_table($name, $table);
}
if (empty($types)) {
return;
}
foreach ($types as $type => $name) {
if (in_array($type, $weight_types)) {
$enabled = 1;
}
else {
$enabled = 0;
}
$query = db_insert('weight_settings')
->fields(array(
'type' => $type,
'weight_enabled' => $enabled,
'weight_range' => $range,
'menu_weight' => $menu_weight,
'weight_default' => $default,
))
->execute();
}
}
function weight_update_7202(&$sandbox) {
if (!isset($sandbox['progress'])) {
$sandbox['progress'] = 0;
$sandbox['last'] = 0;
$sandbox['max'] = db_query("SELECT COUNT(nid) FROM {node}")
->fetchField();
}
$query = db_select('node', 'n');
$query
->fields('n', array(
'nid',
'type',
'sticky',
))
->condition('n.nid', $sandbox['last'], '>')
->orderBy('n.nid')
->range(0, 200);
$nodes = $query
->execute();
foreach ($nodes as $node) {
$values = _weight_update_7202_decode($node->sticky);
if ($values['weight'] != NULL) {
$query = db_insert('weight_weights')
->fields(array(
'entity_id' => $node->nid,
'entity_type' => 'node',
'weight' => $values['weight'],
))
->execute();
}
db_update('node')
->fields(array(
'sticky' => $values['sticky'],
))
->condition('nid', $node->nid)
->execute();
db_update('node_revision')
->fields(array(
'sticky' => $values['sticky'],
))
->condition('nid', $node->nid)
->execute();
$sandbox['progress']++;
$sandbox['last'] = $node->nid;
}
$sandbox['#finished'] = empty($sandbox['max']) ? 1 : $sandbox['progress'] / $sandbox['max'];
}
function weight_update_7203() {
variable_del('weight_node_types');
variable_del('weight_range');
variable_del('weight_use_menu');
variable_del('weight_default');
variable_del('weight_label');
variable_del('weight_position');
}
function weight_update_7204() {
$spec = array(
'type' => 'int',
'size' => 'tiny',
'unsigned' => FALSE,
'not null' => TRUE,
'default' => 0,
);
db_change_field('weight_settings', 'weight_default', 'weight_default', $spec);
}
function weight_update_7205(&$sandbox) {
if (!db_table_exists('weight_weights')) {
$types = array();
$defaults = array();
$result = db_query('SELECT type, weight_default FROM {weight_settings} WHERE weight_enabled=1');
foreach ($result as $row) {
$types[] = $row->type;
$defaults[$row->type] = $row->weight_default;
}
if (!empty($types)) {
if (!isset($sandbox['progress'])) {
if (!db_table_exists('weight_weights')) {
db_create_table('weight_weights', drupal_get_schema_unprocessed('weight', 'weight_weights'));
}
$sandbox['progress'] = 0;
$sandbox['last'] = 0;
$sandbox['max'] = db_query("SELECT COUNT(nid) FROM {node} WHERE type IN (:types)", array(
':types' => $types,
))
->fetchField();
}
$query = db_select('node', 'n');
$query
->fields('n', array(
'nid',
'type',
))
->condition('n.type', $types, 'IN')
->condition('n.nid', $sandbox['last'], '>')
->orderBy('n.nid')
->range(0, 200);
$nodes = $query
->execute();
foreach ($nodes as $node) {
db_insert('weight_weights')
->fields(array(
'entity_id' => $node->nid,
'entity_type' => 'node',
'weight' => $defaults[$node->type],
))
->execute();
$sandbox['progress']++;
$sandbox['last'] = $node->nid;
}
$sandbox['#finished'] = empty($sandbox['max']) ? 1 : $sandbox['progress'] / $sandbox['max'];
}
}
}
function weight_update_7206(&$sandbox) {
if (db_table_exists('field_data_weight')) {
if (!isset($sandbox['progress'])) {
$sandbox['progress'] = 0;
$sandbox['last'] = 0;
$sandbox['max'] = db_query("SELECT COUNT(entity_id) FROM {field_data_weight}\n WHERE language=:lang", array(
':lang' => LANGUAGE_NONE,
))
->fetchField();
}
$query = db_select('field_data_weight', 'fw');
$query
->fields('fw', array(
'entity_id',
'weight_value',
))
->condition('fw.language', LANGUAGE_NONE)
->condition('fw.entity_id', $sandbox['last'], '>')
->orderBy('fw.entity_id')
->range(0, 200);
$nodes = $query
->execute();
foreach ($nodes as $node) {
db_update('weight_weights')
->fields(array(
'entity_id' => $node->entity_id,
'entity_type' => 'node',
'weight' => $node->weight_value,
))
->condition('entity_id', $node->entity_id)
->execute();
$sandbox['progress']++;
$sandbox['last'] = $node->entity_id;
}
$sandbox['#finished'] = empty($sandbox['max']) ? 1 : $sandbox['progress'] / $sandbox['max'];
}
}
function weight_update_7207() {
if (field_info_field('weight')) {
field_delete_field('weight');
field_sync_field_status();
$limit = variable_get('field_purge_batch_size', 10);
field_purge_batch($limit);
}
}
function weight_update_7208() {
db_drop_field('weight_settings', 'id');
db_add_primary_key('weight_settings', array(
'type',
));
}
function weight_update_7209() {
if (db_field_exists('weight_settings', 'sync_translations')) {
return;
}
$spec = array(
'type' => 'int',
'size' => 'tiny',
'unsigned' => FALSE,
'not null' => TRUE,
'default' => 0,
);
db_add_field('weight_settings', 'sync_translations', $spec);
}
function weight_update_7210(&$sandbox) {
$types = array();
$defaults = array();
$result = db_query('SELECT type, weight_default FROM {weight_settings} WHERE weight_enabled=1');
foreach ($result as $row) {
$types[] = $row->type;
$defaults[$row->type] = $row->weight_default;
}
if (!empty($types)) {
$query = db_select('node', 'n');
$query
->leftJoin('weight_weights', 'w', "w.entity_type = 'node' AND w.entity_id = n.nid");
$query
->fields('n', array(
'nid',
'type',
))
->condition('n.type', $types, 'IN')
->isNull('w.entity_id');
if (!isset($sandbox['progress'])) {
$sandbox['progress'] = 0;
$sandbox['last'] = 0;
$sandbox['max'] = $query
->countQuery()
->execute()
->fetchField();
}
$query
->condition('n.nid', $sandbox['last'], '>')
->orderBy('n.nid')
->range(0, 200);
$nodes = $query
->execute();
foreach ($nodes as $node) {
db_insert('weight_weights')
->fields(array(
'entity_id' => $node->nid,
'entity_type' => 'node',
'weight' => $defaults[$node->type],
))
->execute();
$sandbox['progress']++;
$sandbox['last'] = $node->nid;
}
$sandbox['#finished'] = empty($sandbox['max']) ? 1 : $sandbox['progress'] / $sandbox['max'];
}
}
function weight_update_7211() {
$types = node_type_get_names();
$settings = _weight_get_settings();
$range = variable_get('weight_range', 20);
$menu_weight = (int) variable_get('weight_use_menu', 0);
$default = (int) variable_get('weight_default', 0);
foreach ($types as $type => $name) {
if (!array_key_exists($type, $settings)) {
$query = db_insert('weight_settings')
->fields(array(
'type' => $type,
'weight_enabled' => 0,
'weight_range' => $range,
'menu_weight' => $menu_weight,
'weight_default' => $default,
))
->execute();
}
}
}
function weight_update_7212() {
if (db_field_exists('weight_settings', 'id')) {
db_add_primary_key('weight_settings');
db_drop_field('weight_settings', 'id');
db_add_primary_key('weight_settings', array(
'type',
));
}
}
function _weight_update_7202_decode($sticky) {
$values = array();
if ($sticky == 0 || $sticky == 1) {
$values['weight'] = NULL;
$values['sticky'] = $sticky;
}
elseif ($sticky > 0) {
$values['weight'] = 100 - $sticky;
$values['sticky'] = 1;
}
else {
$values['weight'] = -($sticky + 100);
$values['sticky'] = 0;
}
return $values;
}