diff --git a/weight.install b/weight.install index d02891ffe9a45f4d06965884e1d602e4fc639d45..c5f2044c08457f1029816d7d3c9c4ccab8d5318c 100644 --- a/weight.install +++ b/weight.install @@ -329,6 +329,56 @@ function weight_update_7209() { db_add_field('weight_settings', 'sync_translations', $spec); } +/** + * Ensure all nodes of enabled types have a weight. + */ +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)) { + if (!isset($sandbox['progress'])) { + $sandbox['progress'] = 0; + $sandbox['last'] = 0; + $sandbox['max'] = db_query("SELECT COUNT(n.nid) FROM {node} n + LEFT OUTER JOIN {weight_weights} w ON w.entity_id = n.nid + WHERE type IN (:types) AND w.entity_id IS NULL", + array(':types' => $types))->fetchField(); + } + + $query = db_select('node', 'n'); + $query->leftJoin('weight_weights', 'w', 'w.entity_id = n.nid'); + $query + ->fields('n', array('nid', 'type')) + ->condition('n.type', $types, 'IN') + ->condition('n.nid', $sandbox['last'], '>') + ->isNull('w.entity_id') + ->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']); + } +} + /** * Decode weight from sticky column. */