-
Notifications
You must be signed in to change notification settings - Fork 0
/
refresher.install
82 lines (73 loc) · 3.22 KB
/
refresher.install
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
/**
* @file
* Install, update, and uninstall functions for the refresher module.
*/
/**
* Example of batch-driven update function.
*
* Because some update functions may require the batch API, the $sandbox
* provides a place to store state. When $standbox['#finished'] == TRUE,
* calls to this update function are completed.
*
* The $sandbox param provides a way to store data during multiple invocations.
* When the $sandbox['#finished'] == 1, execution is complete.
*
* This dummy 'update' function changes no state in the system. It simply
* loads each node.
*
* To make this update function run again and again, execute the query
* "update system set schema_version = 0 where name = 'refresher';"
* and then run /update.php.
*
* @ingroup refresher
*/
function refresher_update_7100(&$sandbox) {
$ret = array();
// Use the sandbox at your convenience to store the information needed
// to track progression between successive calls to the function.
if (!isset($sandbox['progress'])) {
$sandbox['progress'] = 0; // The count of nodes visited so far.
// Total nodes that must be visited.
$sandbox['max'] = db_query('SELECT COUNT(nid) FROM {node}')->fetchField();
// A place to store messages during the run.
$sandbox['messages'] = array();
$sandbox['current_node'] = -1; // Last node read via the query.
}
// Process nodes by groups of 10 (arbitrary value).
// When a group is processed, the batch update engine determines
// whether it should continue processing in the same request or provide
// progress feedback to the user and wait for the next request.
$limit = 10;
// Retrieve the next group of nids.
$result = db_select('node', 'n')
->fields('n', array('nid'))
->orderBy('n.nid', 'ASC')
->where('n.nid > :nid', array(':nid' => $sandbox['current_node']))
->extend('PagerDefault')
->limit($limit)
->execute();
foreach ($result as $row) {
// Here we actually perform a dummy 'update' on the current node.
$node = db_query('SELECT nid FROM {node} WHERE nid = :nid', array(':nid' => $row->nid))->fetchField();
// Update our progress information.
$sandbox['progress']++;
$sandbox['current_node'] = $row->nid;
}
// Set the "finished" status, to tell batch engine whether this function
// needs to run again. If you set a float, this will indicate the progress
// of the batch so the progress bar will update.
$sandbox['#finished'] = ($sandbox['progress'] >= $sandbox['max']) ? TRUE : ($sandbox['progress'] / $sandbox['max']);
// Set up a per-run message; Make a copy of $sandbox so we can change it.
// This is simply a debugging stanza to illustrate how to capture status
// from each pass through hook_update_N().
$sandbox_status = $sandbox;
unset($sandbox_status['messages']); // Don't want them in the output.
$sandbox['messages'][] = t('$sandbox=') . print_r($sandbox_status, TRUE);
if ($sandbox['#finished']) {
// hook_update_N() may optionally return a string which will be displayed
// to the user.
$final_message = '<ul><li>' . implode('</li><li>', $sandbox['messages']) . "</li></ul>";
return t('The refresher demonstration update did what it was supposed to do: @message', array('@message' => $final_message));
}
}