Spike PHPCoverage Details: system.install

Line #FrequencySource Line
1 <?php
2 // $Id: system.install,v 1.244 2008/03/21 08:52:25 dries Exp $
3 
4 /**
5  * Test and report Drupal installation requirements.
6  *
7  * @param $phase
8  *   The current system installation phase.
9  * @return
10  *   An array of system requirements.
11  */
12 function system_requirements($phase) {
13   $requirements = array();
14   // Ensure translations don't break at install time
15   $t = get_t();
16 
17   // Report Drupal version
18   if ($phase == 'runtime') {
19     $requirements['drupal'] = array(
20       'title' => $t('Drupal'),
21       'value' => VERSION,
22       'severity' => REQUIREMENT_INFO,
23       'weight' => -10,
24     );
25   }
26 
27   // Web server information.
28   $software = $_SERVER['SERVER_SOFTWARE'];
29   $requirements['webserver'] = array(
30     'title' => $t('Web server'),
31     'value' => $software,
32   );
33 
34   // Test PHP version
35   $requirements['php'] = array(
36     'title' => $t('PHP'),
37     'value' => ($phase == 'runtime') ? l(phpversion(), 'admin/reports/status/php') : phpversion(),
38   );
39   if (version_compare(phpversion(), DRUPAL_MINIMUM_PHP) < 0) {
40     $requirements['php']['description'] = $t('Your PHP installation is too old. Drupal requires at least PHP %version.', array('%version' => DRUPAL_MINIMUM_PHP));
41     $requirements['php']['severity'] = REQUIREMENT_ERROR;
42   }
43 
44   // Test PHP register_globals setting.
45   $requirements['php_register_globals'] = array(
46     'title' => $t('PHP register globals'),
47   );
48   $register_globals = trim(ini_get('register_globals'));
49   // Unfortunately, ini_get() may return many different values, and we can't
50   // be certain which values mean 'on', so we instead check for 'not off'
51   // since we never want to tell the user that their site is secure
52   // (register_globals off), when it is in fact on. We can only guarantee
53   // register_globals is off if the value returned is 'off', '', or 0.
54   if (!empty($register_globals) && strtolower($register_globals) != 'off') {
55     $requirements['php_register_globals']['description'] = $t('<em>register_globals</em> is enabled. Drupal requires this configuration directive to be disabled. Your site may not be secure when <em>register_globals</em> is enabled. The PHP manual has instructions for <a href="http://php.net/configuration.changes">how to change configuration settings</a>.');
56     $requirements['php_register_globals']['severity'] = REQUIREMENT_ERROR;
57     $requirements['php_register_globals']['value'] = $t("Enabled ('@value')", array('@value' => $register_globals));
58   }
59   else {
60     $requirements['php_register_globals']['value'] = $t('Disabled');
61   }
62 
63   // Test PHP memory_limit
64   $memory_limit = ini_get('memory_limit');
65   $requirements['php_memory_limit'] = array(
66     'title' => $t('PHP memory limit'),
67     'value' => $memory_limit,
68   );
69 
70   if ($memory_limit && parse_size($memory_limit) < parse_size(DRUPAL_MINIMUM_PHP_MEMORY_LIMIT)) {
71     $description = '';
72     if ($phase == 'install') {
73       $description = $t('Consider increasing your PHP memory limit to %memory_minimum_limit to help prevent errors in the installation process.', array('%memory_minimum_limit' => DRUPAL_MINIMUM_PHP_MEMORY_LIMIT));
74     }
75     elseif ($phase == 'update') {
76       $description = $t('Consider increasing your PHP memory limit to %memory_minimum_limit to help prevent errors in the update process.', array('%memory_minimum_limit' => DRUPAL_MINIMUM_PHP_MEMORY_LIMIT));
77     }
78     elseif ($phase == 'runtime') {
79       $description = $t('Depending on your configuration, Drupal can run with a %memory_limit PHP memory limit. However, a %memory_minimum_limit PHP memory limit or above is recommended, especially if your site uses additional custom or contributed modules.', array('%memory_limit' => $memory_limit, '%memory_minimum_limit' => DRUPAL_MINIMUM_PHP_MEMORY_LIMIT));
80     }
81 
82     if (!empty($description)) {
83       if ($php_ini_path = get_cfg_var('cfg_file_path')) {
84         $description .= ' '. $t('Increase the memory limit by editing the memory_limit parameter in the file %configuration-file and then restart your web server (or contact your system administrator or hosting provider for assistance).', array('%configuration-file' => $php_ini_path));
85       }
86       else {
87         $description .= ' '. $t('Contact your system administrator or hosting provider for assistance with increasing your PHP memory limit.');
88       }
89 
90       $requirements['php_memory_limit']['description'] = $description .' '. $t('See the <a href="@url">Drupal requirements</a> for more information.', array('@url' => 'http://drupal.org/requirements'));
91       $requirements['php_memory_limit']['severity'] = REQUIREMENT_WARNING;
92     }
93   }
94 
95   // Test DB version
96   global $db_type;
97   if (function_exists('db_status_report')) {
98     $requirements += db_status_report($phase);
99   }
100 
101   // Test settings.php file writability
102   if ($phase == 'runtime') {
103     $conf_dir = drupal_verify_install_file(conf_path(), FILE_NOT_WRITABLE, 'dir');
104     $conf_file = drupal_verify_install_file(conf_path() .'/settings.php', FILE_EXIST|FILE_READABLE|FILE_NOT_WRITABLE);
105     if (!$conf_dir || !$conf_file) {
106       $requirements['settings.php'] = array(
107         'value' => $t('Not protected'),
108         'severity' => REQUIREMENT_ERROR,
109         'description' => '',
110       );
111       if (!$conf_dir) {
112         $requirements['settings.php']['description'] .= $t('The directory %file is not protected from modifications and poses a security risk. You must change the directory\'s permissions to be non-writable. ', array('%file' => conf_path()));
113       }
114       if (!$conf_file) {
115         $requirements['settings.php']['description'] .= $t('The file %file is not protected from modifications and poses a security risk. You must change the file\'s permissions to be non-writable.', array('%file' => conf_path() .'/settings.php'));
116       }
117     }
118     else {
119       $requirements['settings.php'] = array(
120         'value' => $t('Protected'),
121       );
122     }
123     $requirements['settings.php']['title'] = $t('Configuration file');
124   }
125 
126   // Report cron status.
127   if ($phase == 'runtime') {
128     // Cron warning threshold defaults to two days.
129     $threshold_warning = variable_get('cron_threshold_warning', 172800);
130     // Cron error threshold defaults to two weeks.
131     $threshold_error = variable_get('cron_threshold_error', 1209600);
132     // Cron configuration help text.
133     $help = $t('For more information, see the online handbook entry for <a href="@cron-handbook">configuring cron jobs</a>.', array('@cron-handbook' => 'http://drupal.org/cron'));
134 
135     // Determine when cron last ran. If never, use the install time to
136     // determine the warning or error status.
137     $cron_last = variable_get('cron_last', NULL);
138     $never_run = FALSE;
139     if (!is_numeric($cron_last)) {
140       $never_run = TRUE;
141       $cron_last = variable_get('install_time', 0);
142     }
143 
144     // Determine severity based on time since cron last ran.
145     $severity = REQUIREMENT_OK;
146     if (time() - $cron_last > $threshold_error) {
147       $severity = REQUIREMENT_ERROR;
148     }
149     else if ($never_run || (time() - $cron_last > $threshold_warning)) {
150       $severity = REQUIREMENT_WARNING;
151     }
152 
153     // If cron hasn't been run, and the user is viewing the main
154     // administration page, instead of an error, we display a helpful reminder
155     // to configure cron jobs.
156     if ($never_run && $severity != REQUIREMENT_ERROR && $_GET['q'] == 'admin' && user_access('administer site configuration')) {
157       drupal_set_message($t('Cron has not run. Please visit the <a href="@status">status report</a> for more information.', array('@status' => url('admin/reports/status'))));
158     }
159 
160     // Set summary and description based on values determined above.
161     if ($never_run) {
162       $summary = $t('Never run');
163       $description = $t('Cron has not run.') .' '. $help;
164     }
165     else {
166       $summary = $t('Last run !time ago', array('!time' => format_interval(time() - $cron_last)));
167       $description = '';
168       if ($severity != REQUIREMENT_OK) {
169         $description = $t('Cron has not run recently.') .' '. $help;
170       }
171     }
172 
173     $description .= ' '. $t('You can <a href="@cron">run cron manually</a>.', array('@cron' => url('reports/status/run-cron')));
174     $description .= '<br />'. $t('To run cron from outside the site, go to <a href="!cron">!cron</a>', array('!cron' => url('cron.php', array('absolute' => true, 'query' => 'cron_key='. variable_get('cron_key', 'drupal')))));
175 
176     $requirements['cron'] = array(
177       'title' => $t('Cron maintenance tasks'),
178       'severity' => $severity,
179       'value' => $summary,
180       'description' => $description
181     );
182   }
183 
184   // Test files directory
185   $directory = file_directory_path();
186   $requirements['file system'] = array(
187     'title' => $t('File system'),
188   );
189 
190   // For installer, create the directory if possible.
191   if ($phase == 'install' && !is_dir($directory) && @mkdir($directory)) {
192     @chmod($directory, 0775); // Necessary for non-webserver users.
193   }
194 
195   $is_writable = is_writable($directory);
196   $is_directory = is_dir($directory);
197   if (!$is_writable || !$is_directory) {
198     $description = '';
199     $requirements['file system']['value'] = $t('Not writable');
200     if (!$is_directory) {
201       $error = $t('The directory %directory does not exist.', array('%directory' => $directory));
202     }
203     else {
204       $error = $t('The directory %directory is not writable.', array('%directory' => $directory));
205     }
206     // The files directory requirement check is done only during install and runtime.
207     if ($phase == 'runtime') {
208       $description = $error .' '. $t('You may need to set the correct directory at the <a href="@admin-file-system">file system settings page</a> or change the current directory\'s permissions so that it is writable.', array('@admin-file-system' => url('admin/settings/file-system')));
209     }
210     elseif ($phase == 'install') {
211       // For the installer UI, we need different wording. 'value' will
212       // be treated as version, so provide none there.
213       $description = $error .' '. $t('An automated attempt to create this directory failed, possibly due to a permissions problem. To proceed with the installation, either create the directory and modify its permissions manually, or ensure that the installer has the permissions to create it automatically. For more information, please see INSTALL.txt or the <a href="@handbook_url">on-line handbook</a>.', array('@handbook_url' => 'http://drupal.org/server-permissions'));
214       $requirements['file system']['value'] = '';
215     }
216     if (!empty($description)) {
217       $requirements['file system']['description'] = $description;
218       $requirements['file system']['severity'] = REQUIREMENT_ERROR;
219     }
220   }
221   else {
222     if (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PUBLIC) {
223       $requirements['file system']['value'] = $t('Writable (<em>public</em> download method)');
224     }
225     else {
226       $requirements['file system']['value'] = $t('Writable (<em>private</em> download method)');
227     }
228   }
229 
230   // See if updates are available in update.php.
231   if ($phase == 'runtime') {
232     $requirements['update'] = array(
233       'title' => $t('Database updates'),
234       'severity' => REQUIREMENT_OK,
235       'value' => $t('Up to date'),
236     );
237 
238     // Check installed modules.
239     foreach (module_list() as $module) {
240       $updates = drupal_get_schema_versions($module);
241       if ($updates !== FALSE) {
242         $default = drupal_get_installed_schema_version($module);
243         if (max($updates) > $default) {
244           $requirements['update']['severity'] = REQUIREMENT_ERROR;
245           $requirements['update']['value'] = $t('Out of date');
246           $requirements['update']['description'] = $t('Some modules have database schema updates to install. You should run the <a href="@update">database update script</a> immediately.', array('@update' => base_path() .'update.php'));
247           break;
248         }
249       }
250     }
251   }
252 
253   // Verify the update.php access setting
254   if ($phase == 'runtime') {
255     if (!empty($GLOBALS['update_free_access'])) {
256       $requirements['update access'] = array(
257         'value' => $t('Not protected'),
258         'severity' => REQUIREMENT_ERROR,
259         'description' => $t('The update.php script is accessible to everyone without authentication check, which is a security risk. You must change the $update_free_access value in your settings.php back to FALSE.'),
260       );
261     }
262     else {
263       $requirements['update access'] = array(
264         'value' => $t('Protected'),
265       );
266     }
267     $requirements['update access']['title'] = $t('Access to update.php');
268   }
269 
270   // Test Unicode library
271   include_once './includes/unicode.inc';
272   $requirements = array_merge($requirements, unicode_requirements());
273 
274   // Check for update status module.
275   if ($phase == 'runtime') {
276     if (!module_exists('update')) {
277       $requirements['update status'] = array(
278         'value' => $t('Not enabled'),
279         'severity' => REQUIREMENT_ERROR,
280         'description' => $t('Update notifications are not enabled. It is <strong>highly recommended</strong> that you enable the update status module from the <a href="@module">module administration page</a> in order to stay up-to-date on new releases. For more information please read the <a href="@update">Update status handbook page</a>.', array('@update' => 'http://drupal.org/handbook/modules/update', '@module' => url('admin/build/modules'))),
281       );
282     }
283     else {
284       $requirements['update status'] = array(
285         'value' => $t('Enabled'),
286       );
287       if (variable_get('drupal_http_request_fails', FALSE)) {
288         $requirements['http requests'] = array(
289           'title' => $t('HTTP request status'),
290           'value' => $t('Fails'),
291           'severity' => REQUIREMENT_ERROR,
292           'description' => $t('Your system or network configuration does not allow Drupal to access web pages, resulting in reduced functionality. This could be due to your webserver configuration or PHP settings, and should be resolved in order to download information about available updates, fetch aggregator feeds, sign in via OpenID, or use other network-dependent services.'),
293         );
294       }
295     }
296     $requirements['update status']['title'] = $t('Update notifications');
297   }
298 
299   return $requirements;
300 }
301 
302 /**
303  * Implementation of hook_install().
304  */
305 function system_install() {
3061  if ($GLOBALS['db_type'] == 'pgsql') {
307     // Create unsigned types.
308     db_query("CREATE DOMAIN int_unsigned integer CHECK (VALUE >= 0)");
309     db_query("CREATE DOMAIN smallint_unsigned smallint CHECK (VALUE >= 0)");
310     db_query("CREATE DOMAIN bigint_unsigned bigint CHECK (VALUE >= 0)");
311 
312     // Create functions.
313     db_query('CREATE OR REPLACE FUNCTION "greatest"(numeric, numeric) RETURNS numeric AS
314       \'SELECT CASE WHEN (($1 > $2) OR ($2 IS NULL)) THEN $1 ELSE $2 END;\'
315       LANGUAGE \'sql\''
316     );
317     db_query('CREATE OR REPLACE FUNCTION "greatest"(numeric, numeric, numeric) RETURNS numeric AS
318       \'SELECT greatest($1, greatest($2, $3));\'
319       LANGUAGE \'sql\''
320     );
321     if (!db_result(db_query("SELECT COUNT(*) FROM pg_proc WHERE proname = 'rand'"))) {
322       db_query('CREATE OR REPLACE FUNCTION "rand"() RETURNS float AS
323         \'SELECT random();\'
324         LANGUAGE \'sql\''
325       );
326     }
327 
328     if (!db_result(db_query("SELECT COUNT(*) FROM pg_proc WHERE proname = 'concat'"))) {
329       db_query('CREATE OR REPLACE FUNCTION "concat"(text, text) RETURNS text AS
330         \'SELECT $1 || $2;\'
331         LANGUAGE \'sql\''
332       );
333     }
334     db_query('CREATE OR REPLACE FUNCTION "if"(boolean, text, text) RETURNS text AS
335       \'SELECT CASE WHEN $1 THEN $2 ELSE $3 END;\'
336       LANGUAGE \'sql\''
337     );
338     db_query('CREATE OR REPLACE FUNCTION "if"(boolean, integer, integer) RETURNS integer AS
339       \'SELECT CASE WHEN $1 THEN $2 ELSE $3 END;\'
340       LANGUAGE \'sql\''
341     );
342   }
343 
344   // Create tables.
345   $modules = array('system', 'filter', 'block', 'user', 'node', 'comment', 'taxonomy');
3461  foreach ($modules as $module) {
3471    drupal_install_schema($module);
348   }
349 
350   // Load system theme data appropriately.
3511  system_theme_data();
352 
353   // Inserting uid 0 here confuses MySQL -- the next user might be created as
354   // uid 2 which is not what we want. So we insert the first user here, the
355   // anonymous user. uid is 1 here for now, but very soon it will be changed
356   // to 0.
3571  db_query("INSERT INTO {users} (name, mail) VALUES('%s', '%s')", '', '');
358   // We need some placeholders here as name and mail are uniques and data is
359   // presumed to be a serialized array. Install will change uid 1 immediately
360   // anyways. So we insert the superuser here, the uid is 2 here for now, but
361   // very soon it will be changed to 1.
3621  db_query("INSERT INTO {users} (name, mail, created, data) VALUES('%s', '%s', %d, '%s')", 'placeholder-for-uid-1', 'placeholder-for-uid-1', time(), serialize(array()));
363   // This sets the above two users uid 0 (anonymous). We avoid an explicit 0
364   // otherwise MySQL might insert the next auto_increment value.
3651  db_query("UPDATE {users} SET uid = uid - uid WHERE name = '%s'", '');
366   // This sets uid 1 (superuser). We skip uid 2 but that's not a big problem.
3671  db_query("UPDATE {users} SET uid = 1 WHERE name = '%s'", 'placeholder-for-uid-1');
368 
3691  db_query("INSERT INTO {role} (name) VALUES ('%s')", 'anonymous user');
3701  db_query("INSERT INTO {role} (name) VALUES ('%s')", 'authenticated user');
371 
3721  db_query("INSERT INTO {permission} (rid, perm, tid) VALUES (%d, '%s', %d)", 1, 'access content', 0);
3731  db_query("INSERT INTO {permission} (rid, perm, tid) VALUES (%d, '%s', %d)", 2, 'access comments, access content, post comments, post comments without approval', 0);
374 
3751  db_query("INSERT INTO {variable} (name, value) VALUES ('%s', '%s')", 'theme_default', 's:7:"garland";');
3761  db_query("UPDATE {system} SET status = %d WHERE type = '%s' AND name = '%s'", 1, 'theme', 'garland');
3771  db_query("INSERT INTO {blocks} (module, delta, theme, status, weight, region, pages, cache) VALUES ('%s', '%s', '%s', %d, %d, '%s', '%s', %d)", 'user', '0', 'garland', 1, 0, 'left', '', -1);
3781  db_query("INSERT INTO {blocks} (module, delta, theme, status, weight, region, pages, cache) VALUES ('%s', '%s', '%s', %d, %d, '%s', '%s', %d)", 'user', '1', 'garland', 1, 0, 'left', '', -1);
3791  db_query("INSERT INTO {blocks} (module, delta, theme, status, weight, region, pages, cache) VALUES ('%s', '%s', '%s', %d, %d, '%s', '%s', %d)", 'system', '0', 'garland', 1, 10, 'footer', '', -1);
380 
3811  db_query("INSERT INTO {node_access} (nid, gid, realm, grant_view, grant_update, grant_delete) VALUES (%d, %d, '%s', %d, %d, %d)", 0, 0, 'all', 1, 0, 0);
382 
383   // Add input formats.
3841  db_query("INSERT INTO {filter_formats} (name, roles, cache) VALUES ('%s', '%s', %d)", 'Filtered HTML', ',1,2,', 1);
3851  db_query("INSERT INTO {filter_formats} (name, roles, cache) VALUES ('%s', '%s', %d)", 'Full HTML', '', 1);
386 
387   // Enable filters for each input format.
388 
389   // Filtered HTML:
390   // URL filter.
3911  db_query("INSERT INTO {filters} (format, module, delta, weight) VALUES (%d, '%s', %d, %d)", 1, 'filter', 2, 0);
392   // HTML filter.
3931  db_query("INSERT INTO {filters} (format, module, delta, weight) VALUES (%d, '%s', %d, %d)", 1, 'filter', 0, 1);
394   // Line break filter.
3951  db_query("INSERT INTO {filters} (format, module, delta, weight) VALUES (%d, '%s', %d, %d)", 1, 'filter', 1, 2);
396   // HTML corrector filter.
3971  db_query("INSERT INTO {filters} (format, module, delta, weight) VALUES (%d, '%s', %d, %d)", 1, 'filter', 3, 10);
398 
399   // Full HTML:
400   // URL filter.
4011  db_query("INSERT INTO {filters} (format, module, delta, weight) VALUES (%d, '%s', %d, %d)", 2, 'filter', 2, 0);
402   // Line break filter.
4031  db_query("INSERT INTO {filters} (format, module, delta, weight) VALUES (%d, '%s', %d, %d)", 2, 'filter', 1, 1);
404   // HTML corrector filter.
4051  db_query("INSERT INTO {filters} (format, module, delta, weight) VALUES (%d, '%s', %d, %d)", 2, 'filter', 3, 10);
406 
4071  db_query("INSERT INTO {variable} (name, value) VALUES ('%s','%s')", 'filter_html_1', 'i:1;');
408 
4091  db_query("INSERT INTO {variable} (name, value) VALUES ('%s', '%s')", 'node_options_forum', 'a:1:{i:0;s:6:"status";}');
410 
4111  $cron_key = serialize(md5(mt_rand()));
412 
4131  db_query("INSERT INTO {variable} (name, value) VALUES ('%s', '%s')", 'cron_key', $cron_key);
414 }
415 
416 /**
417  * Implementation of hook_schema().
418  */
419 function system_schema() {
420   // NOTE: {variable} needs to be created before all other tables, as
421   // some database drivers, e.g. Oracle and DB2, will require variable_get()
422   // and variable_set() for overcoming some database specific limitations.
423   $schema['variable'] = array(
4241    'description' => t('Named variable/value pairs created by Drupal core or any other module or theme. All variables are cached in memory at the start of every Drupal request so developers should not be careless about what is stored here.'),
425     'fields' => array(
426       'name' => array(
427         'description' => t('The name of the variable.'),
428         'type' => 'varchar',
429         'length' => 128,
430         'not null' => TRUE,
431         'default' => '',
432       ),
433       'value' => array(
434         'description' => t('The value of the variable.'),
435         'type' => 'text',
436         'not null' => TRUE,
437         'size' => 'big',
438       ),
439     ),
440     'primary key' => array('name'),
441   );
442 
443   $schema['actions'] = array(
4441    'description' => t('Stores action information.'),
445     'fields' => array(
446       'aid' => array(
447         'description' => t('Primary Key: Unique actions ID.'),
448         'type' => 'varchar',
449         'length' => 255,
450         'not null' => TRUE,
451         'default' => '0',
452       ),
453       'type' => array(
454         'description' => t('The object that that action acts on (node, user, comment, system or custom types.)'),
455         'type' => 'varchar',
456         'length' => 32,
457         'not null' => TRUE,
458         'default' => '',
459       ),
460       'callback' => array(
461         'description' => t('The callback function that executes when the action runs.'),
462         'type' => 'varchar',
463         'length' => 255,
464         'not null' => TRUE,
465         'default' => '',
466       ),
467       'parameters' => array(
468         'description' => t('Parameters to be passed to the callback function.'),
469         'type' => 'text',
470         'not null' => TRUE,
471         'size' => 'big',
472       ),
473       'description' => array(
474         'description' => t('Description of the action.'),
475         'type' => 'varchar',
476         'length' => 255,
477         'not null' => TRUE,
478         'default' => '0',
479       ),
480     ),
481     'primary key' => array('aid'),
482   );
483 
484   $schema['actions_aid'] = array(
4851    'description' => t('Stores action IDs for non-default actions.'),
486     'fields' => array(
487       'aid' => array(
488         'description' => t('Primary Key: Unique actions ID.'),
489         'type' => 'serial',
490         'unsigned' => TRUE,
491         'not null' => TRUE,
492       ),
493     ),
494     'primary key' => array('aid'),
495   );
496 
497   $schema['batch'] = array(
4981    'description' => t('Stores details about batches (processes that run in multiple HTTP requests).'),
499     'fields' => array(
500       'bid' => array(
501         'description' => t('Primary Key: Unique batch ID.'),
502         'type' => 'serial',
503         'unsigned' => TRUE,
504         'not null' => TRUE,
505       ),
506       'token' => array(
507         'description' => t("A string token generated against the current user's session id and the batch id, used to ensure that only the user who submitted the batch can effectively access it."),
508         'type' => 'varchar',
509         'length' => 64,
510         'not null' => TRUE,
511       ),
512       'timestamp' => array(
513         'description' => t('A Unix timestamp indicating when this batch was submitted for processing. Stale batches are purged at cron time.'),
514         'type' => 'int',
515         'not null' => TRUE,
516       ),
517       'batch' => array(
518         'description' => t('A serialized array containing the processing data for the batch.'),
519         'type' => 'text',
520         'not null' => FALSE,
521         'size' => 'big',
522       ),
523     ),
524     'primary key' => array('bid'),
525     'indexes' => array(
526       'token' => array('token'),
527     ),
528   );
529 
530   $schema['cache'] = array(
5311    'description' => t('Generic cache table for caching things not separated out into their own tables. Contributed modules may also use this to store cached items.'),
532     'fields' => array(
533       'cid' => array(
534         'description' => t('Primary Key: Unique cache ID.'),
535         'type' => 'varchar',
536         'length' => 255,
537         'not null' => TRUE,
538         'default' => '',
539       ),
540       'data' => array(
541         'description' => t('A collection of data to cache.'),
542         'type' => 'blob',
543         'not null' => FALSE,
544         'size' => 'big',
545       ),
546       'expire' => array(
547         'description' => t('A Unix timestamp indicating when the cache entry should expire, or 0 for never.'),
548         'type' => 'int',
549         'not null' => TRUE,
550         'default' => 0,
551       ),
552       'created' => array(
553         'description' => t('A Unix timestamp indicating when the cache entry was created.'),
554         'type' => 'int',
555         'not null' => TRUE,
556         'default' => 0,
557       ),
558       'headers' => array(
559         'description' => t('Any custom HTTP headers to be added to cached data.'),
560         'type' => 'text',
561         'not null' => FALSE,
562       ),
563       'serialized' => array(
564         'description' => t('A flag to indicate whether content is serialized (1) or not (0).'),
565         'type' => 'int',
566         'size' => 'small',
567         'not null' => TRUE,
568         'default' => 0,
569       ),
570     ),
571     'indexes' => array(
572       'expire' => array('expire'),
573     ),
574     'primary key' => array('cid'),
575   );
576 
5771  $schema['cache_form'] = $schema['cache'];
5781  $schema['cache_form']['description'] = t('Cache table for the form system to store recently built forms and their storage data, to be used in subsequent page requests.');
5791  $schema['cache_page'] = $schema['cache'];
5801  $schema['cache_page']['description'] = t('Cache table used to store compressed pages for anonymous users, if page caching is enabled.');
5811  $schema['cache_menu'] = $schema['cache'];
5821  $schema['cache_menu']['description'] = t('Cache table for the menu system to store router information as well as generated link trees for various menu/page/user combinations.');
583 
584   $schema['files'] = array(
5851    'description' => t('Stores information for uploaded files.'),
586     'fields' => array(
587       'fid' => array(
588         'description' => t('Primary Key: Unique files ID.'),
589         'type' => 'serial',
590         'unsigned' => TRUE,
591         'not null' => TRUE,
592       ),
593       'uid' => array(
594         'description' => t('The {users}.uid of the user who is associated with the file.'),
595         'type' => 'int',
596         'unsigned' => TRUE,
597         'not null' => TRUE,
598         'default' => 0,
599       ),
600       'filename' => array(
601         'description' => t('Name of the file.'),
602         'type' => 'varchar',
603         'length' => 255,
604         'not null' => TRUE,
605         'default' => '',
606       ),
607       'filepath' => array(
608         'description' => t('Path of the file relative to Drupal root.'),
609         'type' => 'varchar',
610         'length' => 255,
611         'not null' => TRUE,
612         'default' => '',
613       ),
614       'filemime' => array(
615         'description' => t('The file MIME type.'),
616         'type' => 'varchar',
617         'length' => 255,
618         'not null' => TRUE,
619         'default' => '',
620       ),
621       'filesize' => array(
622         'description' => t('The size of the file in bytes.'),
623         'type' => 'int',
624         'unsigned' => TRUE,
625         'not null' => TRUE,
626         'default' => 0,
627       ),
628       'status' => array(
629         'description' => t('A flag indicating whether file is temporary (1) or permanent (0).'),
630         'type' => 'int',
631         'not null' => TRUE,
632         'default' => 0,
633       ),
634       'timestamp' => array(
635         'description' => t('UNIX timestamp for when the file was added.'),
636         'type' => 'int',
637         'unsigned' => TRUE,
638         'not null' => TRUE,
639         'default' => 0,
640       ),
641     ),
642     'indexes' => array(
643       'uid' => array('uid'),
644       'status' => array('status'),
645       'timestamp' => array('timestamp'),
646     ),
647     'primary key' => array('fid'),
648   );
649 
650   $schema['flood'] = array(
6511    'description' => t('Flood controls the threshold of events, such as the number of contact attempts.'),
652     'fields' => array(
653       'fid' => array(
654         'description' => t('Unique flood event ID.'),
655         'type' => 'serial',
656         'not null' => TRUE,
657       ),
658       'event' => array(
659         'description' => t('Name of event (e.g. contact).'),
660         'type' => 'varchar',
661         'length' => 64,
662         'not null' => TRUE,
663         'default' => '',
664       ),
665       'hostname' => array(
666         'description' => t('Hostname of the visitor.'),
667         'type' => 'varchar',
668         'length' => 128,
669         'not null' => TRUE,
670         'default' => '',
671       ),
672       'timestamp' => array(
673         'description' => t('Timestamp of the event.'),
674         'type' => 'int',
675         'not null' => TRUE,
676         'default' => 0,
677       ),
678     ),
679     'primary key' => array('fid'),
680     'indexes' => array(
681       'allow' => array('event', 'hostname', 'timestamp'),
682     ),
683   );
684 
685   $schema['history'] = array(
6861    'description' => t('A record of which {users} have read which {node}s.'),
687     'fields' => array(
688       'uid' => array(
689         'description' => t('The {users}.uid that read the {node} nid.'),
690         'type' => 'int',
691         'not null' => TRUE,
692         'default' => 0,
693       ),
694       'nid' => array(
695         'description' => t('The {node}.nid that was read.'),
696         'type' => 'int',
697         'not null' => TRUE,
698         'default' => 0,
699       ),
700       'timestamp' => array(
701         'description' => t('The Unix timestamp at which the read occurred.'),
702         'type' => 'int',
703         'not null' => TRUE,
704         'default' => 0,
705       ),
706     ),
707     'primary key' => array('uid', 'nid'),
708     'indexes' => array(
709       'nid' => array('nid'),
710     ),
711   );
712   $schema['menu_router'] = array(
7131    'description' => t('Maps paths to various callbacks (access, page and title)'),
714     'fields' => array(
715       'path' => array(
716         'description' => t('Primary Key: the Drupal path this entry describes'),
717         'type' => 'varchar',
718         'length' => 255,
719         'not null' => TRUE,
720         'default' => '',
721       ),
722       'load_functions' => array(
723         'description' => t('A serialized array of function names (like node_load) to be called to load an object corresponding to a part of the current path.'),
724         'type' => 'varchar',
725         'length' => 255,
726         'not null' => TRUE,
727         'default' => '',
728       ),
729       'to_arg_functions' => array(
730         'description' => t('A serialized array of function names (like user_current_to_arg) to be called to replace a part of the router path with another string.'),
731         'type' => 'varchar',
732         'length' => 255,
733         'not null' => TRUE,
734         'default' => '',
735       ),
736       'access_callback' => array(
737         'description' => t('The callback which determines the access to this router path. Defaults to user_access.'),
738         'type' => 'varchar',
739         'length' => 255,
740         'not null' => TRUE,
741         'default' => '',
742       ),
743       'access_arguments' => array(
744         'description' => t('A serialized array of arguments for the access callback.'),
745         'type' => 'text',
746         'not null' => FALSE,
747       ),
748       'page_callback' => array(
749         'description' => t('The name of the function that renders the page.'),
750         'type' => 'varchar',
751         'length' => 255,
752         'not null' => TRUE,
753         'default' => '',
754       ),
755       'page_arguments' => array(
756         'description' => t('A serialized array of arguments for the page callback.'),
757         'type' => 'text',
758         'not null' => FALSE,
759       ),
760       'fit' => array(
761         'description' => t('A numeric representation of how specific the path is.'),
762         'type' => 'int',
763         'not null' => TRUE,
764         'default' => 0,
765       ),
766       'number_parts' => array(
767         'description' => t('Number of parts in this router path.'),
768         'type' => 'int',
769         'not null' => TRUE,
770         'default' => 0,
771         'size' => 'small',
772       ),
773       'tab_parent' => array(
774         'description' => t('Only for local tasks (tabs) - the router path of the parent page (which may also be a local task).'),
775         'type' => 'varchar',
776         'length' => 255,
777         'not null' => TRUE,
778         'default' => '',
779       ),
780       'tab_root' => array(
781         'description' => t('Router path of the closest non-tab parent page. For pages that are not local tasks, this will be the same as the path.'),
782         'type' => 'varchar',
783         'length' => 255,
784         'not null' => TRUE,
785         'default' => '',
786       ),
787       'title' => array(
788         'description' => t('The title for the current page, or the title for the tab if this is a local task.'),
789         'type' => 'varchar',
790         'length' => 255,
791         'not null' => TRUE,
792         'default' => '',
793       ),
794       'title_callback' => array(
795         'description' => t('A function which will alter the title. Defaults to t()'),
796         'type' => 'varchar',
797         'length' => 255,
798         'not null' => TRUE,
799         'default' => '',
800       ),
801       'title_arguments' => array(
802         'description' => t('A serialized array of arguments for the title callback. If empty, the title will be used as the sole argument for the title callback.'),
803         'type' => 'varchar',
804         'length' => 255,
805         'not null' => TRUE,
806         'default' => '',
807       ),
808       'type' => array(
809         'description' => t('Numeric representation of the type of the menu item, like MENU_LOCAL_TASK.'),
810         'type' => 'int',
811         'not null' => TRUE,
812         'default' => 0,
813       ),
814       'block_callback' => array(
815         'description' => t('Name of a function used to render the block on the system administration page for this item.'),
816         'type' => 'varchar',
817         'length' => 255,
818         'not null' => TRUE,
819         'default' => '',
820       ),
821       'description' => array(
822         'description' => t('A description of this item.'),
823         'type' => 'text',
824         'not null' => TRUE,
825       ),
826       'position' => array(
827         'description' => t('The position of the block (left or right) on the system administration page for this item.'),
828         'type' => 'varchar',
829         'length' => 255,
830         'not null' => TRUE,
831         'default' => '',
832       ),
833       'weight' => array(
834         'description' => t('Weight of the element. Lighter weights are higher up, heavier weights go down.'),
835         'type' => 'int',
836         'not null' => TRUE,
837         'default' => 0,
838       ),
839       'file' => array(
840         'description' => t('The file to include for this element, usually the page callback function lives in this file.'),
841         'type' => 'text',
842         'size' => 'medium',
843       ),
844     ),
845     'indexes' => array(
846       'fit' => array('fit'),
847       'tab_parent' => array('tab_parent'),
848     ),
849     'primary key' => array('path'),
850   );
851 
852   $schema['menu_links'] = array(
8531    'description' => t('Contains the individual links within a menu.'),
854     'fields' => array(
855      'menu_name' => array(
856         'description' => t("The menu name. All links with the same menu name (such as 'navigation') are part of the same menu."),
857         'type' => 'varchar',
858         'length' => 32,
859         'not null' => TRUE,
860         'default' => '',
861       ),
862       'mlid' => array(
863         'description' => t('The menu link ID (mlid) is the integer primary key.'),
864         'type' => 'serial',
865         'unsigned' => TRUE,
866         'not null' => TRUE,
867       ),
868       'plid' => array(
869         'description' => t('The parent link ID (plid) is the mlid of the link above in the hierarchy, or zero if the link is at the top level in its menu.'),
870         'type' => 'int',
871         'unsigned' => TRUE,
872         'not null' => TRUE,
873         'default' => 0,
874       ),
875       'link_path' => array(
876         'description' => t('The Drupal path or external path this link points to.'),
877         'type' => 'varchar',
878         'length' => 255,
879         'not null' => TRUE,
880         'default' => '',
881       ),
882       'router_path' => array(
883         'description' => t('For links corresponding to a Drupal path (external = 0), this connects the link to a {menu_router}.path for joins.'),
884         'type' => 'varchar',
885         'length' => 255,
886         'not null' => TRUE,
887         'default' => '',
888       ),
889       'link_title' => array(
890       'description' => t('The text displayed for the link, which may be modified by a title callback stored in {menu_router}.'),
891         'type' => 'varchar',
892         'length' => 255,
893         'not null' => TRUE,
894         'default' => '',
895       ),
896       'options' => array(
897         'description' => t('A serialized array of options to be passed to the url() or l() function, such as a query string or HTML attributes.'),
898         'type' => 'text',
899         'not null' => FALSE,
900       ),
901       'module' => array(
902         'description' => t('The name of the module that generated this link.'),
903         'type' => 'varchar',
904         'length' => 255,
905         'not null' => TRUE,
906         'default' => 'system',
907       ),
908       'hidden' => array(
909         'description' => t('A flag for whether the link should be rendered in menus. (1 = a disabled menu item that may be shown on admin screens, -1 = a menu callback, 0 = a normal, visible link)'),
910         'type' => 'int',
911         'not null' => TRUE,
912         'default' => 0,
913         'size' => 'small',
914       ),
915       'external' => array(
916         'description' => t('A flag to indicate if the link points to a full URL starting with a protocol, like http:// (1 = external, 0 = internal).'),
917         'type' => 'int',
918         'not null' => TRUE,
919         'default' => 0,
920         'size' => 'small',
921       ),
922       'has_children' => array(
923         'description' => t('Flag indicating whether any links have this link as a parent (1 = children exist, 0 = no children).'),
924         'type' => 'int',
925         'not null' => TRUE,
926         'default' => 0,
927         'size' => 'small',
928       ),
929       'expanded' => array(
930         'description' => t('Flag for whether this link should be rendered as expanded in menus - expanded links always have their child links displayed, instead of only when the link is in the active trail (1 = expanded, 0 = not expanded)'),
931         'type' => 'int',
932         'not null' => TRUE,
933         'default' => 0,
934         'size' => 'small',
935       ),
936       'weight' => array(
937         'description' => t('Link weight among links in the same menu at the same depth.'),
938         'type' => 'int',
939         'not null' => TRUE,
940         'default' => 0,
941       ),
942       'depth' => array(
943         'description' => t('The depth relative to the top level. A link with plid == 0 will have depth == 1.'),
944         'type' => 'int',
945         'not null' => TRUE,
946         'default' => 0,
947         'size' => 'small',
948       ),
949       'customized' => array(
950         'description' => t('A flag to indicate that the user has manually created or edited the link (1 = customized, 0 = not customized).'),
951         'type' => 'int',
952         'not null' => TRUE,
953         'default' => 0,
954         'size' => 'small',
955       ),
956       'p1' => array(
957         'description' => t('The first mlid in the materialized path. If N = depth, then pN must equal the mlid. If depth > 1 then p(N-1) must equal the plid. All pX where X > depth must equal zero. The columns p1 .. p9 are also called the parents.'),
958         'type' => 'int',
959         'unsigned' => TRUE,
960         'not null' => TRUE,
961         'default' => 0,
962       ),
963       'p2' => array(
964         'description' => t('The second mlid in the materialized path. See p1.'),
965         'type' => 'int',
966         'unsigned' => TRUE,
967         'not null' => TRUE,
968         'default' => 0,
969       ),
970       'p3' => array(
971         'description' => t('The third mlid in the materialized path. See p1.'),
972         'type' => 'int',
973         'unsigned' => TRUE,
974         'not null' => TRUE,
975         'default' => 0,
976       ),
977       'p4' => array(
978         'description' => t('The fourth mlid in the materialized path. See p1.'),
979         'type' => 'int',
980         'unsigned' => TRUE,
981         'not null' => TRUE,
982         'default' => 0,
983       ),
984       'p5' => array(
985         'description' => t('The fifth mlid in the materialized path. See p1.'),
986         'type' => 'int',
987         'unsigned' => TRUE,
988         'not null' => TRUE,
989         'default' => 0,
990       ),
991       'p6' => array(
992         'description' => t('The sixth mlid in the materialized path. See p1.'),
993         'type' => 'int',
994         'unsigned' => TRUE,
995         'not null' => TRUE,
996         'default' => 0,
997       ),
998       'p7' => array(
999         'description' => t('The seventh mlid in the materialized path. See p1.'),
1000         'type' => 'int',
1001         'unsigned' => TRUE,
1002         'not null' => TRUE,
1003         'default' => 0,
1004       ),
1005       'p8' => array(
1006         'description' => t('The eighth mlid in the materialized path. See p1.'),
1007         'type' => 'int',
1008         'unsigned' => TRUE,
1009         'not null' => TRUE,
1010         'default' => 0,
1011       ),
1012       'p9' => array(
1013         'description' => t('The ninth mlid in the materialized path. See p1.'),
1014         'type' => 'int',
1015         'unsigned' => TRUE,
1016         'not null' => TRUE,
1017         'default' => 0,
1018       ),
1019       'updated' => array(
1020         'description' => t('Flag that indicates that this link was generated during the update from Drupal 5.'),
1021         'type' => 'int',
1022         'not null' => TRUE,
1023         'default' => 0,
1024         'size' => 'small',
1025       ),
1026     ),
1027     'indexes' => array(
1028       'path_menu' => array(array('link_path', 128), 'menu_name'),
1029       'menu_plid_expand_child' => array('menu_name', 'plid', 'expanded', 'has_children'),
1030       'menu_parents' => array('menu_name', 'p1', 'p2', 'p3', 'p4', 'p5', 'p6', 'p7', 'p8', 'p9'),
1031       'router_path' => array(array('router_path', 128)),
1032     ),
1033     'primary key' => array('mlid'),
1034   );
1035 
1036   $schema['sessions'] = array(
10371    'description' => t("Drupal's session handlers read and write into the sessions table. Each record represents a user session, either anonymous or authenticated."),
1038     'fields' => array(
1039       'uid' => array(
1040         'description' => t('The {users}.uid corresponding to a session, or 0 for anonymous user.'),
1041         'type' => 'int',
1042         'unsigned' => TRUE,
1043         'not null' => TRUE,
1044       ),
1045       'sid' => array(
1046         'description' => t("Primary key: A session ID. The value is generated by PHP's Session API."),
1047         'type' => 'varchar',
1048         'length' => 64,
1049         'not null' => TRUE,
1050         'default' => '',
1051       ),
1052       'hostname' => array(
1053         'description' => t('The IP address that last used this session ID (sid).'),
1054         'type' => 'varchar',
1055         'length' => 128,
1056         'not null' => TRUE,
1057         'default' => '',
1058       ),
1059       'timestamp' => array(
1060         'description' => t('The Unix timestamp when this session last requested a page. Old records are purged by PHP automatically.'),
1061         'type' => 'int',
1062         'not null' => TRUE,
1063         'default' => 0,
1064       ),
1065       'cache' => array(
1066         'description' => t("The time of this user's last post. This is used when the site has specified a minimum_cache_lifetime. See cache_get()."),
1067         'type' => 'int',
1068         'not null' => TRUE,
1069         'default' => 0,
1070       ),
1071       'session' => array(
1072         'description' => t('The serialized contents of $_SESSION, an array of name/value pairs that persists across page requests by this session ID. Drupal loads $_SESSION from here at the start of each request and saves it at the end.'),
1073         'type' => 'text',
1074         'not null' => FALSE,
1075         'size' => 'big',
1076       ),
1077     ),
1078     'primary key' => array('sid'),
1079     'indexes' => array(
1080       'timestamp' => array('timestamp'),
1081       'uid' => array('uid'),
1082     ),
1083   );
1084 
1085   $schema['system'] = array(
10861    'description' => t("A list of all modules, themes, and theme engines that are or have been installed in Drupal's file system."),
1087     'fields' => array(
1088       'filename' => array(
1089         'description' => t('The path of the primary file for this item, relative to the Drupal root; e.g. modules/node/node.module.'),
1090         'type' => 'varchar',
1091         'length' => 255,
1092         'not null' => TRUE,
1093         'default' => '',
1094       ),
1095       'name' => array(
1096         'description' => t('The name of the item; e.g. node.'),
1097         'type' => 'varchar',
1098         'length' => 255,
1099         'not null' => TRUE,
1100         'default' => '',
1101       ),
1102       'type' => array(
1103         'description' => t('The type of the item, either module, theme, or theme_engine.'),
1104         'type' => 'varchar',
1105         'length' => 255,
1106         'not null' => TRUE,
1107         'default' => '',
1108       ),
1109       'owner' => array(
1110         'description' => t("A theme's 'parent'. Can be either a theme or an engine."),
1111         'type' => 'varchar',
1112         'length' => 255,
1113         'not null' => TRUE,
1114         'default' => '',
1115       ),
1116       'status' => array(
1117         'description' => t('Boolean indicating whether or not this item is enabled.'),
1118         'type' => 'int',
1119         'not null' => TRUE,
1120         'default' => 0,
1121       ),
1122       'throttle' => array(
1123         'description' => t('Boolean indicating whether this item is disabled when the throttle.module disables throttleable items.'),
1124         'type' => 'int',
1125         'not null' => TRUE,
1126         'default' => 0,
1127         'size' => 'tiny',
1128       ),
1129       'bootstrap' => array(
1130         'description' => t("Boolean indicating whether this module is loaded during Drupal's early bootstrapping phase (e.g. even before the page cache is consulted)."),
1131         'type' => 'int',
1132         'not null' => TRUE,
1133         'default' => 0,
1134       ),
1135       'schema_version' => array(
1136         'description' => t("The module's database schema version number. -1 if the module is not installed (its tables do not exist); 0 or the largest N of the module's hook_update_N() function that has either been run or existed when the module was first installed."),
1137         'type' => 'int',
1138         'not null' => TRUE,
1139         'default' => -1,
1140         'size' => 'small',
1141       ),
1142       'weight' => array(
1143         'description' => t("The order in which this module's hooks should be invoked relative to other modules. Equal-weighted modules are ordered by name."),
1144         'type' => 'int',
1145         'not null' => TRUE,
1146         'default' => 0,
1147       ),
1148       'info' => array(
1149         'description' => t("A serialized array containing information from the module's .info file; keys can include name, description, package, version, core, dependencies, dependents, and php."),
1150         'type' => 'text',
1151         'not null' => FALSE,
1152       ),
1153     ),
1154     'primary key' => array('filename'),
1155     'indexes' => array(
1156       'modules' => array(array('type', 12), 'status', 'weight', 'filename'),
1157       'bootstrap' => array(array('type', 12), 'status', 'bootstrap', 'weight', 'filename'),
1158     ),
1159   );
1160 
1161   $schema['url_alias'] = array(
11621    'description' => t('A list of URL aliases for Drupal paths; a user may visit either the source or destination path.'),
1163     'fields' => array(
1164       'pid' => array(
1165         'description' => t('A unique path alias identifier.'),
1166         'type' => 'serial',
1167         'unsigned' => TRUE,
1168         'not null' => TRUE,
1169       ),
1170       'src' => array(
1171         'description' => t('The Drupal path this alias is for; e.g. node/12.'),
1172         'type' => 'varchar',
1173         'length' => 128,
1174         'not null' => TRUE,
1175         'default' => '',
1176       ),
1177       'dst' => array(
1178         'description' => t('The alias for this path; e.g. title-of-the-story.'),
1179         'type' => 'varchar',
1180         'length' => 128,
1181         'not null' => TRUE,
1182         'default' => '',
1183       ),
1184       'language' => array(
1185         'description' => t('The language this alias is for; if blank, the alias will be used for unknown languages. Each Drupal path can have an alias for each supported language.'),
1186         'type' => 'varchar',
1187         'length' => 12,
1188         'not null' => TRUE,
1189         'default' => '',
1190       ),
1191     ),
1192     'unique keys' => array(
1193       'dst_language' => array('dst', 'language'),
1194     ),
1195     'primary key' => array('pid'),
1196     'indexes' => array(
1197       'src' => array('src'),
1198     ),
1199   );
1200 
12011  return $schema;
1202 }
1203 
1204 // Updates for core.
1205 
1206 function system_update_last_removed() {
1207   return 1021;
1208 }
1209 
1210 /**
1211  * @defgroup updates-5.x-extra Extra system updates for 5.x
1212  * @{
1213  */
1214 
1215 /**
1216  * Add index on users created column.
1217  */
1218 function system_update_1022() {
1219   $ret = array();
1220   db_add_index($ret, 'users', 'created', array('created'));
1221   // Also appears as system_update_6004(). Ensure we don't update twice.
1222   variable_set('system_update_1022', TRUE);
1223   return $ret;
1224 }
1225 
1226 /**
1227  * @} End of "defgroup updates-5.x-extra"
1228  */
1229 
1230 /**
1231  * @defgroup updates-5.x-to-6.x System updates from 5.x to 6.x
1232  * @{
1233  */
1234 
1235 /**
1236  * Remove auto_increment from {boxes} to allow adding custom blocks with
1237  * visibility settings.
1238  */
1239 function system_update_6000() {
1240   $ret = array();
1241   switch ($GLOBALS['db_type']) {
1242     case 'mysql':
1243     case 'mysqli':
1244       $max = (int)db_result(db_query('SELECT MAX(bid) FROM {boxes}'));
1245       $ret[] = update_sql('ALTER TABLE {boxes} CHANGE COLUMN bid bid int NOT NULL');
1246       $ret[] = update_sql("REPLACE INTO {sequences} VALUES ('{boxes}_bid', $max)");
1247       break;
1248   }
1249   return $ret;
1250 }
1251 
1252 /**
1253  * Add version id column to {term_node} to allow taxonomy module to use revisions.
1254  */
1255 function system_update_6001() {
1256   $ret = array();
1257 
1258   // Add vid to term-node relation.  The schema says it is unsigned.
1259   db_add_field($ret, 'term_node', 'vid', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0));
1260   db_drop_primary_key($ret, 'term_node');
1261   db_add_primary_key($ret, 'term_node', array('vid', 'tid', 'nid'));
1262   db_add_index($ret, 'term_node', 'vid', array('vid'));
1263 
1264   db_query('UPDATE {term_node} t SET vid = (SELECT vid FROM {node} n WHERE t.nid = n.nid)');
1265   return $ret;
1266 }
1267 
1268 /**
1269  * Increase the maximum length of variable names from 48 to 128.
1270  */
1271 function system_update_6002() {
1272   $ret = array();
1273   db_drop_primary_key($ret, 'variable');
1274   db_change_field($ret, 'variable', 'name', 'name', array('type' => 'varchar', 'length' => 128, 'not null' => TRUE, 'default' => ''));
1275   db_add_primary_key($ret, 'variable', array('name'));
1276   return $ret;
1277 }
1278 
1279 /**
1280  * Add index on comments status column.
1281  */
1282 function system_update_6003() {
1283   $ret = array();
1284   db_add_index($ret, 'comments', 'status', array('status'));
1285   return $ret;
1286 }
1287 
1288 /**
1289  * This update used to add an index on users created column (#127941).
1290  * However, system_update_1022() does the same thing.  This update
1291  * tried to detect if 1022 had already run but failed to do so,
1292  * resulting in an "index already exists" error.
1293  *
1294  * Adding the index here is never necessary.  Sites installed before
1295  * 1022 will run 1022, getting the update.  Sites installed on/after 1022
1296  * got the index when the table was first created.  Therefore, this
1297  * function is now a no-op.
1298  */
1299 function system_update_6004() {
1300   return array();
1301 }
1302 
1303 /**
1304  * Add language to url_alias table and modify indexes.
1305  */
1306 function system_update_6005() {
1307   $ret = array();
1308   switch ($GLOBALS['db_type']) {
1309     case 'pgsql':
1310       db_add_column($ret, 'url_alias', 'language', 'varchar(12)', array('default' => "''", 'not null' => TRUE));
1311 
1312       // As of system.install:1.85 (before the new language
1313       // subsystem), new installs got a unique key named
1314       // url_alias_dst_key on url_alias.dst.  Unfortunately,
1315       // system_update_162 created a unique key inconsistently named
1316       // url_alias_dst_idx on url_alias.dst (keys should have the _key
1317       // suffix, indexes the _idx suffix).  Therefore, sites installed
1318       // before system_update_162 have a unique key with a different
1319       // name than sites installed after system_update_162().  Now, we
1320       // want to drop the unique key on dst which may have either one
1321       // of two names and create a new unique key on (dst, language).
1322       // There is no way to know which key name exists so we have to
1323       // drop both, causing an SQL error.  Thus, we just hide the
1324       // error and only report the update_sql results that work.
1325       $err = error_reporting(0);
1326       $ret1 = update_sql('DROP INDEX {url_alias}_dst_idx');
1327       if ($ret1['success']) {
1328   $ret[] = $ret1;
1329       }
1330       $ret1 = array();
1331       db_drop_unique_key($ret, 'url_alias', 'dst');
1332       foreach ($ret1 as $r) {
1333   if ($r['success']) {
1334     $ret[] = $r;
1335   }
1336       }
1337       error_reporting($err);
1338 
1339       $ret[] = update_sql('CREATE UNIQUE INDEX {url_alias}_dst_language_idx ON {url_alias}(dst, language)');
1340       break;
1341     case 'mysql':
1342     case 'mysqli':
1343       $ret[] = update_sql("ALTER TABLE {url_alias} ADD language varchar(12) NOT NULL default ''");
1344       $ret[] = update_sql("ALTER TABLE {url_alias} DROP INDEX dst");
1345       $ret[] = update_sql("ALTER TABLE {url_alias} ADD UNIQUE dst_language (dst, language)");
1346       break;
1347   }
1348   return $ret;
1349 }
1350 
1351 /**
1352  * Drop useless indices on node_counter table.
1353  */
1354 function system_update_6006() {
1355   $ret = array();
1356   switch ($GLOBALS['db_type']) {
1357     case 'pgsql':
1358       $ret[] = update_sql('DROP INDEX {node_counter}_daycount_idx');
1359       $ret[] = update_sql('DROP INDEX {node_counter}_totalcount_idx');
1360       $ret[] = update_sql('DROP INDEX {node_counter}_timestamp_idx');
1361       break;
1362     case 'mysql':
1363     case 'mysqli':
1364       $ret[] = update_sql("ALTER TABLE {node_counter} DROP INDEX daycount");
1365       $ret[] = update_sql("ALTER TABLE {node_counter} DROP INDEX totalcount");
1366       $ret[] = update_sql("ALTER TABLE {node_counter} DROP INDEX timestamp");
1367       break;
1368   }
1369   return $ret;
1370 }
1371 
1372 /**
1373  * Change the severity column in the watchdog table to the new values.
1374  */
1375 function system_update_6007() {
1376   $ret = array();
1377   $ret[] = update_sql("UPDATE {watchdog} SET severity = ". WATCHDOG_NOTICE ." WHERE severity = 0");
1378   $ret[] = update_sql("UPDATE {watchdog} SET severity = ". WATCHDOG_WARNING ." WHERE severity = 1");
1379   $ret[] = update_sql("UPDATE {watchdog} SET severity = ". WATCHDOG_ERROR ." WHERE severity = 2");
1380   return $ret;
1381 }
1382 
1383 /**
1384  * Add info files to themes.  The info and owner columns are added by
1385  * update_fix_d6_requirements() in update.php to avoid a large number
1386  * of error messages from update.php.  All we need to do here is copy
1387  * description to owner and then drop description.
1388  */
1389 function system_update_6008() {
1390   $ret = array();
1391   $ret[] = update_sql('UPDATE {system} SET owner = description');
1392   db_drop_field($ret, 'system', 'description');
1393 
1394   // Rebuild system table contents.
1395   module_rebuild_cache();
1396   system_theme_data();
1397 
1398   return $ret;
1399 }
1400 
1401 /**
1402  * The PHP filter is now a separate module.
1403  */
1404 function system_update_6009() {
1405   $ret = array();
1406 
1407   // If any input format used the Drupal 5 PHP filter.
1408   if (db_result(db_query("SELECT COUNT(format) FROM {filters} WHERE module = 'filter' AND delta = 1"))) {
1409     // Enable the PHP filter module.
1410     $ret[] = update_sql("UPDATE {system} SET status = 1 WHERE name = 'php' AND type = 'module'");
1411     // Update the input filters.
1412     $ret[] = update_sql("UPDATE {filters} SET delta = 0, module = 'php' WHERE module = 'filter' AND delta = 1");
1413   }
1414 
1415   // With the removal of the PHP evaluator filter, the deltas of the line break
1416   // and URL filter have changed.
1417   $ret[] = update_sql("UPDATE {filters} SET delta = 1 WHERE module = 'filter' AND delta = 2");
1418   $ret[] = update_sql("UPDATE {filters} SET delta = 2 WHERE module = 'filter' AND delta = 3");
1419 
1420   return $ret;
1421 }
1422 
1423 /**
1424  * Add variable replacement for watchdog messages.
1425  *
1426  * The variables field is NOT NULL and does not have a default value.
1427  * Existing log messages should not be translated in the new system,
1428  * so we insert 'N;' (serialize(NULL)) as the temporary default but
1429  * then remove the default value to match the schema.
1430  */
1431 function system_update_6010() {
1432   $ret = array();
1433   db_add_field($ret, 'watchdog', 'variables', array('type' => 'text', 'size' => 'big', 'not null' => TRUE, 'initial' => 'N;'));
1434   return $ret;
1435 }
1436 
1437 /**
1438  * Add language support to nodes
1439  */
1440 function system_update_6011() {
1441   $ret = array();
1442   switch ($GLOBALS['db_type']) {
1443     case 'pgsql':
1444       db_add_column($ret, 'node', 'language', 'varchar(12)', array('default' => "''", 'not null' => TRUE));
1445       break;
1446     case 'mysql':
1447     case 'mysqli':
1448       $ret[] = update_sql("ALTER TABLE {node} ADD language varchar(12) NOT NULL default ''");
1449       break;
1450   }
1451   return $ret;
1452 }
1453 
1454 /**
1455  * Add serialized field to cache tables.  This is now handled directly
1456  * by update.php, so this function is a no-op.
1457  */
1458 function system_update_6012() {
1459   return array();
1460 }
1461 
1462 /**
1463  * Rebuild cache data for theme system changes
1464  */
1465 function system_update_6013() {
1466   // Rebuild system table contents.
1467   module_rebuild_cache();
1468   system_theme_data();
1469 
1470   return array(array('success' => TRUE, 'query' => 'Cache rebuilt.'));
1471 }
1472 
1473 /**
1474  * Record that the installer is done, so it is not
1475  * possible to run the installer on upgraded sites.
1476  */
1477 function system_update_6014() {
1478   variable_set('install_task', 'done');
1479 
1480   return array(array('success' => TRUE, 'query' => "variable_set('install_task')"));
1481 }
1482 
1483 /**
1484  * Add the form cache table.
1485  */
1486 function system_update_6015() {
1487   $ret = array();
1488 
1489   switch ($GLOBALS['db_type']) {
1490     case 'pgsql':
1491       $ret[] = update_sql("CREATE TABLE {cache_form} (
1492         cid varchar(255) NOT NULL default '',
1493         data bytea,
1494         expire int NOT NULL default '0',
1495         created int NOT NULL default '0',
1496         headers text,
1497         serialized smallint NOT NULL default '0',
1498         PRIMARY KEY (cid)
1499     )");
1500       $ret[] = update_sql("CREATE INDEX {cache_form}_expire_idx ON {cache_form} (expire)");
1501       break;
1502     case 'mysql':
1503     case 'mysqli':
1504       $ret[] = update_sql("CREATE TABLE {cache_form} (
1505         cid varchar(255) NOT NULL default '',
1506         data longblob,
1507         expire int NOT NULL default '0',
1508         created int NOT NULL default '0',
1509         headers text,
1510         serialized int(1) NOT NULL default '0',
1511         PRIMARY KEY (cid),
1512         INDEX expire (expire)
1513       ) /*!40100 DEFAULT CHARACTER SET UTF8 */ ");
1514       break;
1515   }
1516 
1517   return $ret;
1518 }
1519 
1520 /**
1521  * Make {node}'s primary key be nid, change nid,vid to a unique key.
1522  * Add primary keys to block, filters, flood, permission, and term_relation.
1523  */
1524 function system_update_6016() {
1525   $ret = array();
1526 
1527   switch ($GLOBALS['db_type']) {
1528     case 'pgsql':
1529       $ret[] = update_sql("ALTER TABLE {node} ADD CONSTRAINT {node}_nid_vid_key UNIQUE (nid, vid)");
1530       db_add_column($ret, 'blocks', 'bid', 'serial');
1531       $ret[] = update_sql("ALTER TABLE {blocks} ADD PRIMARY KEY (bid)");
1532       db_add_column($ret, 'filters', 'fid', 'serial');
1533       $ret[] = update_sql("ALTER TABLE {filters} ADD PRIMARY KEY (fid)");
1534       db_add_column($ret, 'flood', 'fid', 'serial');
1535       $ret[] = update_sql("ALTER TABLE {flood} ADD PRIMARY KEY (fid)");
1536       db_add_column($ret, 'permission', 'pid', 'serial');
1537       $ret[] = update_sql("ALTER TABLE {permission} ADD PRIMARY KEY (pid)");
1538       db_add_column($ret, 'term_relation', 'trid', 'serial');
1539       $ret[] = update_sql("ALTER TABLE {term_relation} ADD PRIMARY KEY (trid)");
1540       db_add_column($ret, 'term_synonym', 'tsid', 'serial');
1541       $ret[] = update_sql("ALTER TABLE {term_synonym} ADD PRIMARY KEY (tsid)");
1542       break;
1543     case 'mysql':
1544     case 'mysqli':
1545       $ret[] = update_sql('ALTER TABLE {node} ADD UNIQUE KEY nid_vid (nid, vid)');
1546       $ret[] = update_sql("ALTER TABLE {blocks} ADD bid int NOT NULL AUTO_INCREMENT PRIMARY KEY");
1547       $ret[] = update_sql("ALTER TABLE {filters} ADD fid int NOT NULL AUTO_INCREMENT PRIMARY KEY");
1548       $ret[] = update_sql("ALTER TABLE {flood} ADD fid int NOT NULL AUTO_INCREMENT PRIMARY KEY");
1549       $ret[] = update_sql("ALTER TABLE {permission} ADD pid int NOT NULL AUTO_INCREMENT PRIMARY KEY");
1550       $ret[] = update_sql("ALTER TABLE {term_relation} ADD trid int NOT NULL AUTO_INCREMENT PRIMARY KEY");
1551       $ret[] = update_sql("ALTER TABLE {term_synonym} ADD tsid int NOT NULL AUTO_INCREMENT PRIMARY KEY");
1552       break;
1553   }
1554 
1555   return $ret;
1556 }
1557 
1558 /**
1559  * Rename settings related to user.module email notifications.
1560  */
1561 function system_update_6017() {
1562   $ret = array();
1563   // Maps old names to new ones.
1564   $var_names = array(
1565     'admin'    => 'register_admin_created',
1566     'approval' => 'register_pending_approval',
1567     'welcome'  => 'register_no_approval_required',
1568     'pass'     => 'password_reset',
1569   );
1570   foreach ($var_names as $old => $new) {
1571     foreach (array('_subject', '_body') as $suffix) {
1572       $old_name = 'user_mail_'. $old . $suffix;
1573       $new_name = 'user_mail_'. $new . $suffix;
1574       if ($old_val = variable_get($old_name, FALSE)) {
1575         variable_set($new_name, $old_val);
1576         variable_del($old_name);
1577         $ret[] = array('success' => TRUE, 'query' => "variable_set($new_name)");
1578         $ret[] = array('success' => TRUE, 'query' => "variable_del($old_name)");
1579         if ($old_name == 'user_mail_approval_body') {
1580           drupal_set_message('Saving an old value of the welcome message body for users that are pending administrator approval. However, you should consider modifying this text, since Drupal can now be configured to automatically notify users and send them their login information when their accounts are approved. See the <a href="'. url('admin/user/settings') .'">User settings</a> page for details.');
1581         }
1582       }
1583     }
1584   }
1585   return $ret;
1586 }
1587 
1588 /**
1589  * Add HTML corrector to HTML formats or replace the old module if it was in use.
1590  */
1591 function system_update_6018() {
1592   $ret = array();
1593 
1594   // Disable htmlcorrector.module, if it exists and replace its filter.
1595   if (module_exists('htmlcorrector')) {
1596     module_disable(array('htmlcorrector'));
1597     $ret[] = update_sql("UPDATE {filter_formats} SET module = 'filter', delta = 3 WHERE module = 'htmlcorrector'");
1598     $ret[] = array('success' => TRUE, 'query' => 'HTML Corrector module was disabled; this functionality has now been added to core.');
1599     return $ret;
1600   }
1601 
1602   // Otherwise, find any format with 'HTML' in its name and add the filter at the end.
1603   $result = db_query("SELECT format, name FROM {filter_formats} WHERE name LIKE '%HTML%'");
1604   while ($format = db_fetch_object($result)) {
1605     $weight = db_result(db_query("SELECT MAX(weight) FROM {filters} WHERE format = %d", $format->format));
1606     db_query("INSERT INTO {filters} (format, module, delta, weight) VALUES (%d, '%s', %d, %d)", $format->format, 'filter', 3, max(10, $weight + 1));
1607     $ret[] = array('success' => TRUE, 'query' => "HTML corrector filter added to the '". $format->name ."' input format.");
1608   }
1609 
1610   return $ret;
1611 }
1612 
1613 /**
1614  * Reconcile small differences in the previous, manually created mysql
1615  * and pgsql schemas so they are the same and can be represented by a
1616  * single schema structure.
1617  *
1618  * Note that the mysql and pgsql cases make different changes.  This
1619  * is because each schema needs to be tweaked in different ways to
1620  * conform to the new schema structure.  Also, since they operate on
1621  * tables defined by many optional core modules which may not ever
1622  * have been installed, they must test each table for existence.  If
1623  * the modules are first installed after this update exists the tables
1624  * will be created from the schema structure and will start out
1625  * correct.
1626  */
1627 function system_update_6019() {
1628   $ret = array();
1629 
1630   switch ($GLOBALS['db_type']) {
1631     case 'pgsql':
1632       // Remove default ''.
1633       if (db_table_exists('aggregator_feed')) {
1634         db_field_set_no_default($ret, 'aggregator_feed', 'description');
1635         db_field_set_no_default($ret, 'aggregator_feed', 'image');
1636       }
1637       db_field_set_no_default($ret, 'blocks', 'pages');
1638       if (db_table_exists('contact')) {
1639         db_field_set_no_default($ret, 'contact', 'recipients');
1640         db_field_set_no_default($ret, 'contact', 'reply');
1641       }
1642       db_field_set_no_default($ret, 'watchdog', 'location');
1643       db_field_set_no_default($ret, 'node_revisions', 'body');
1644       db_field_set_no_default($ret, 'node_revisions', 'teaser');
1645       db_field_set_no_default($ret, 'node_revisions', 'log');
1646 
1647       // Update from pgsql 'float' (which means 'double precision') to
1648       // schema 'float' (which in pgsql means 'real').
1649       if (db_table_exists('search_index')) {
1650         db_change_field($ret, 'search_index', 'score', 'score', array('type' => 'float'));
1651       }
1652       if (db_table_exists('search_total')) {
1653         db_change_field($ret, 'search_total', 'count', 'count', array('type' => 'float'));
1654       }
1655 
1656       // Replace unique index dst_language with a unique constraint.  The
1657       // result is the same but the unique key fits our current schema
1658       // structure.  Also, the postgres documentation implies that
1659       // unique constraints are preferable to unique indexes.  See
1660       // http://www.postgresql.org/docs/8.2/interactive/indexes-unique.html.
1661       if (db_table_exists('url_alias')) {
1662         db_drop_index($ret, 'url_alias', 'dst_language');
1663         db_add_unique_key($ret, 'url_alias', 'dst_language',
1664           array('dst', 'language'));
1665       }
1666 
1667       // Fix term_node pkey: mysql and pgsql code had different orders.
1668       if (db_table_exists('term_node')) {
1669         db_drop_primary_key($ret, 'term_node');
1670         db_add_primary_key($ret, 'term_node', array('vid', 'tid', 'nid'));
1671       }
1672 
1673       // Make boxes.bid unsigned.
1674       db_drop_primary_key($ret, 'boxes');
1675       db_change_field($ret, 'boxes', 'bid', 'bid', array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE), array('primary key' => array('bid')));
1676 
1677       // Fix primary key
1678       db_drop_primary_key($ret, 'node');
1679       db_add_primary_key($ret, 'node', array('nid'));
1680 
1681       break;
1682 
1683     case 'mysql':
1684     case 'mysqli':
1685       // Rename key 'link' to 'url'.
1686       if (db_table_exists('aggregator_feed')) {
1687         db_drop_unique_key($ret, 'aggregator_feed', 'link');
1688         db_add_unique_key($ret, 'aggregator_feed', 'url', array('url'));
1689       }
1690 
1691       // Change to size => small.
1692       if (db_table_exists('boxes')) {
1693         db_change_field($ret, 'boxes', 'format', 'format', array('type' => 'int', 'size' => 'small', 'not null' => TRUE, 'default' => 0));
1694       }
1695 
1696       // Change to size => small.
1697       // Rename index 'lid' to 'nid'.
1698       if (db_table_exists('comments')) {
1699         db_change_field($ret, 'comments', 'format', 'format', array('type' => 'int', 'size' => 'small', 'not null' => TRUE, 'default' => 0));
1700         db_drop_index($ret, 'comments', 'lid');
1701         db_add_index($ret, 'comments', 'nid', array('nid'));
1702       }
1703 
1704       // Change to size => small.
1705       db_change_field($ret, 'cache', 'serialized', 'serialized', array('type' => 'int', 'size' => 'small', 'not null' => TRUE, 'default' => 0));
1706       db_change_field($ret, 'cache_filter', 'serialized', 'serialized', array('type' => 'int', 'size' => 'small', 'not null' => TRUE, 'default' => 0));
1707       db_change_field($ret, 'cache_page', 'serialized', 'serialized', array('type' => 'int', 'size' => 'small', 'not null' => TRUE, 'default' => 0));
1708       db_change_field($ret, 'cache_form', 'serialized', 'serialized', array('type' => 'int', 'size' => 'small', 'not null' => TRUE, 'default' => 0));
1709 
1710       // Remove default => 0, set auto increment.
1711       $new_uid = 1 + db_result(db_query('SELECT MAX(uid) FROM {users}'));
1712       $ret[] = update_sql('UPDATE {users} SET uid = '. $new_uid .' WHERE uid = 0');
1713       db_drop_primary_key($ret, 'users');
1714       db_change_field($ret, 'users', 'uid', 'uid', array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE), array('primary key' => array('uid')));
1715       $ret[] = update_sql('UPDATE {users} SET uid = 0 WHERE uid = '. $new_uid);
1716 
1717       // Special field names.
1718       $map = array('node_revisions' => 'vid');
1719       // Make sure these tables have proper auto_increment fields.
1720       foreach (array('boxes', 'files', 'node', 'node_revisions') as $table) {
1721         $field = isset($map[$table]) ? $map[$table] : $table[0] .'id';
1722         db_drop_primary_key($ret, $table);
1723         db_change_field($ret, $table, $field, $field, array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE), array('primary key' => array($field)));
1724       }
1725 
1726       break;
1727   }
1728 
1729   return $ret;
1730 }
1731 
1732 /**
1733  * Create the tables for the new menu system.
1734  */
1735 function system_update_6020() {
1736   $ret = array();
1737 
1738   $schema['menu_router'] = array(
1739     'fields' => array(
1740       'path'             => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''),
1741       'load_functions'   => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''),
1742       'to_arg_functions' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''),
1743       'access_callback'  => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''),
1744       'access_arguments' => array('type' => 'text', 'not null' => FALSE),
1745       'page_callback'    => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''),
1746       'page_arguments'   => array('type' => 'text', 'not null' => FALSE),
1747       'fit'              => array('type' => 'int', 'not null' => TRUE, 'default' => 0),
1748       'number_parts'     => array('type' => 'int', 'not null' => TRUE, 'default' => 0, 'size' => 'small'),
1749       'tab_parent'       => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''),
1750       'tab_root'         => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''),
1751       'title'            => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''),
1752       'title_callback'   => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''),
1753       'title_arguments'  => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''),
1754       'type'             => array('type' => 'int', 'not null' => TRUE, 'default' => 0),
1755       'block_callback'   => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''),
1756       'description'      => array('type' => 'text', 'not null' => TRUE),
1757       'position'         => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''),
1758       'weight'           => array('type' => 'int', 'not null' => TRUE, 'default' => 0),
1759       'file'             => array('type' => 'text', 'size' => 'medium')
1760     ),
1761     'indexes' => array(
1762       'fit'        => array('fit'),
1763       'tab_parent' => array('tab_parent')
1764     ),
1765     'primary key' => array('path'),
1766   );
1767 
1768   $schema['menu_links'] = array(
1769     'fields' => array(
1770       'menu_name'    => array('type' => 'varchar', 'length' => 32, 'not null' => TRUE, 'default' => ''),
1771       'mlid'         => array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE),
1772       'plid'         => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
1773       'link_path'    => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''),
1774       'router_path'  => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''),
1775       'link_title'   => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''),
1776       'options'      => array('type' => 'text', 'not null' => FALSE),
1777       'module'       => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => 'system'),
1778       'hidden'       => array('type' => 'int', 'not null' => TRUE, 'default' => 0, 'size' => 'small'),
1779       'external'     => array('type' => 'int', 'not null' => TRUE, 'default' => 0, 'size' => 'small'),
1780       'has_children' => array('type' => 'int', 'not null' => TRUE, 'default' => 0, 'size' => 'small'),
1781       'expanded'     => array('type' => 'int', 'not null' => TRUE, 'default' => 0, 'size' => 'small'),
1782       'weight'       => array('type' => 'int', 'not null' => TRUE, 'default' => 0),
1783       'depth'        => array('type' => 'int', 'not null' => TRUE, 'default' => 0, 'size' => 'small'),
1784       'customized'   => array('type' => 'int', 'not null' => TRUE, 'default' => 0, 'size' => 'small'),
1785       'p1'           => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
1786       'p2'           => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
1787       'p3'           => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
1788       'p4'           => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
1789       'p5'           => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
1790       'p6'           => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
1791       'p7'           => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
1792       'p8'           => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
1793       'p9'           => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
1794       'updated'      => array('type' => 'int', 'not null' => TRUE, 'default' => 0, 'size' => 'small'),
1795     ),
1796     'indexes' => array(
1797       'path_menu'              => array(array('link_path', 128), 'menu_name'),
1798       'menu_plid_expand_child' => array('menu_name', 'plid', 'expanded', 'has_children'),
1799       'menu_parents'           => array('menu_name', 'p1', 'p2', 'p3', 'p4', 'p5', 'p6', 'p7', 'p8', 'p9'),
1800       'router_path'            => array(array('router_path', 128)),
1801     ),
1802     'primary key' => array('mlid'),
1803   );
1804 
1805   foreach ($schema as $name => $table) {
1806     db_create_table($ret, $name, $table);
1807   }
1808   return $ret;
1809 }
1810 
1811 /**
1812  * Migrate the menu items from the old menu system to the new menu_links table.
1813  */
1814 function system_update_6021() {
1815   $ret = array('#finished' => 0);
1816   $menus = array(
1817     'navigation' => array(
1818       'menu_name' => 'navigation',
1819       'title' => 'Navigation',
1820       'description' => 'The navigation menu is provided by Drupal and is the main interactive menu for any site. It is usually the only menu that contains personalized links for authenticated users, and is often not even visible to anonymous users.',
1821     ),
1822     'primary-links' => array(
1823       'menu_name' => 'primary-links',
1824       'title' => 'Primary links',
1825       'description' => 'Primary links are often used at the theme layer to show the major sections of a site. A typical representation for primary links would be tabs along the top.',
1826     ),
1827     'secondary-links' => array(
1828       'menu_name' => 'secondary-links',
1829       'title' => 'Secondary links',
1830       'description' => 'Secondary links are often used for pages like legal notices, contact details, and other secondary navigation items that play a lesser role than primary links.',
1831     ),
1832   );
1833   // Multi-part update
1834   if (!isset($_SESSION['system_update_6021'])) {
1835     db_add_field($ret, 'menu', 'converted', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0, 'size' => 'tiny'));
1836     $_SESSION['system_update_6021_max'] = db_result(db_query('SELECT COUNT(*) FROM {menu}'));
1837     $_SESSION['menu_menu_map'] = array(1 => 'navigation');
1838     // 0 => FALSE is for new menus, 1 => FALSE is for the navigation.
1839     $_SESSION['menu_item_map'] = array(0 => FALSE, 1 => FALSE);
1840     $table = array(
1841       'fields' => array(
1842         'menu_name'   => array('type' => 'varchar', 'length' => 32, 'not null' => TRUE, 'default' => ''),
1843         'title'       => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''),
1844         'description' => array('type' => 'text', 'not null' => FALSE),
1845       ),
1846       'primary key' => array('menu_name'),
1847     );
1848     db_create_table($ret, 'menu_custom', $table);
1849     db_query("INSERT INTO {menu_custom} (menu_name, title, description) VALUES ('%s', '%s', '%s')", $menus['navigation']);
1850     $_SESSION['system_update_6021'] = 0;
1851   }
1852 
1853   $limit = 50;
1854   while ($limit-- && ($item = db_fetch_array(db_query_range('SELECT * FROM {menu} WHERE converted = 0', 0, 1)))) {
1855     // If it's not a menu...
1856     if ($item['pid']) {
1857       // Let's climb up until we find an item with a converted parent.
1858       $item_original = $item;
1859       while ($item && !isset($_SESSION['menu_item_map'][$item['pid']])) {
1860         $item = db_fetch_array(db_query('SELECT * FROM {menu} WHERE mid = %d', $item['pid']));
1861       }
1862       // This can only occur if the menu entry is a leftover in the menu table.
1863       // These do not appear in Drupal 5 anyways, so we skip them.
1864       if (!$item) {
1865         db_query('UPDATE {menu} SET converted = %d WHERE mid = %d', 1, $item_original['mid']);
1866         $_SESSION['system_update_6021']++;
1867         continue;
1868       }
1869     }
1870     // We need to recheck because item might have changed.
1871     if ($item['pid']) {
1872       // Fill the new fields.
1873       $item['link_title'] = $item['title'];
1874       $item['link_path'] = drupal_get_normal_path($item['path']);
1875       // We know the parent is already set. If it's not FALSE then it's an item.
1876       if ($_SESSION['menu_item_map'][$item['pid']]) {
1877         // The new menu system parent link id.
1878         $item['plid'] = $_SESSION['menu_item_map'][$item['pid']]['mlid'];
1879         // The new menu system menu name.
1880         $item['menu_name'] = $_SESSION['menu_item_map'][$item['pid']]['menu_name'];
1881       }
1882       else {
1883         // This a top level element.
1884         $item['plid'] = 0;
1885         // The menu name is stored among the menus.
1886         $item['menu_name'] = $_SESSION['menu_menu_map'][$item['pid']];
1887       }
1888       // Is the element visible in the menu block?
1889       $item['hidden'] = !($item['type'] & MENU_VISIBLE_IN_TREE);
1890       // Is it a custom(ized) element?
1891       if ($item['type'] & (MENU_CREATED_BY_ADMIN | MENU_MODIFIED_BY_ADMIN)) {
1892         $item['customized'] = TRUE;
1893       }
1894       // Items created via the menu module need to be assigned to it.
1895       if ($item['type'] & MENU_CREATED_BY_ADMIN) {
1896         $item['module'] = 'menu';
1897         $item['router_path'] = '';
1898         $item['updated'] = TRUE;
1899       }
1900       else {
1901         $item['module'] = 'system';
1902         $item['router_path'] = $item['path'];
1903         $item['updated'] = FALSE;
1904       }
1905       // Save the link.
1906       menu_link_save($item);
1907       $_SESSION['menu_item_map'][$item['mid']] = array('mlid' => $item['mlid'], 'menu_name' => $item['menu_name']);
1908     }
1909     elseif (!isset($_SESSION['menu_menu_map'][$item['mid']])) {
1910       $item['menu_name'] = 'menu-'. preg_replace('/[^a-zA-Z0-9]/', '-', strtolower($item['title']));
1911       $item['menu_name'] = substr($item['menu_name'], 0, 20);
1912       $original_menu_name = $item['menu_name'];
1913       $i = 0;
1914       while (db_result(db_query("SELECT menu_name FROM {menu_custom} WHERE menu_name = '%s'", $item['menu_name']))) {
1915         $item['menu_name'] = $original_menu_name . ($i++);
1916       }
1917       if ($item['path']) {
1918         // Another bunch of bogus entries. Apparently, these are leftovers
1919         // from Drupal 4.7 .
1920         $_SESSION['menu_bogus_menus'][] = $item['menu_name'];
1921       }
1922       else {
1923         // Add this menu to the list of custom menus.
1924         db_query("INSERT INTO {menu_custom} (menu_name, title, description) VALUES ('%s', '%s', '')", $item['menu_name'], $item['title']);
1925       }
1926       $_SESSION['menu_menu_map'][$item['mid']] = $item['menu_name'];
1927       $_SESSION['menu_item_map'][$item['mid']] = FALSE;
1928     }
1929     db_query('UPDATE {menu} SET converted = %d WHERE mid = %d', 1, $item['mid']);
1930     $_SESSION['system_update_6021']++;
1931   }
1932 
1933   if ($_SESSION['system_update_6021'] >= $_SESSION['system_update_6021_max']) {
1934     if (!empty($_SESSION['menu_bogus_menus'])) {
1935       // Remove entries in bogus menus. This is secure because we deleted
1936       // every non-alpanumeric character from the menu name.
1937       $ret[] = update_sql("DELETE FROM {menu_links} WHERE menu_name IN ('". implode("', '", $_SESSION['menu_bogus_menus']) ."')");
1938     }
1939 
1940     $menu_primary_menu = variable_get('menu_primary_menu', 0);
1941     // Ensure that we wind up with a system menu named 'primary-links'.
1942     if (isset($_SESSION['menu_menu_map'][2])) {
1943       // The primary links menu that ships with Drupal 5 has mid = 2.  If this
1944       // menu hasn't been deleted by the site admin, we use that.
1945       $updated_primary_links_menu = 2;
1946     }
1947     elseif (isset($_SESSION['menu_menu_map'][$menu_primary_menu]) && $menu_primary_menu > 1) {
1948       // Otherwise, we use the menu that is currently assigned to the primary
1949       // links region of the theme, as long as it exists and isn't the
1950       // Navigation menu.
1951       $updated_primary_links_menu = $menu_primary_menu;
1952     }
1953     else {
1954       // As a last resort, create 'primary-links' as a new menu.
1955       $updated_primary_links_menu = 0;
1956       db_query("INSERT INTO {menu_custom} (menu_name, title, description) VALUES ('%s', '%s', '%s')", $menus['primary-links']);
1957     }
1958 
1959     if ($updated_primary_links_menu) {
1960       // Change the existing menu name to 'primary-links'.
1961       $replace = array('%new_name' => 'primary-links', '%desc' => $menus['primary-links']['description'], '%old_name' => $_SESSION['menu_menu_map'][$updated_primary_links_menu]);
1962       $ret[] = update_sql(strtr("UPDATE {menu_custom} SET menu_name = '%new_name', description = '%desc' WHERE menu_name = '%old_name'", $replace));
1963       $ret[] = update_sql("UPDATE {menu_links} SET menu_name = 'primary-links' WHERE menu_name = '". $_SESSION['menu_menu_map'][$updated_primary_links_menu] ."'");
1964       $_SESSION['menu_menu_map'][$updated_primary_links_menu] = 'primary-links';
1965     }
1966 
1967     $menu_secondary_menu = variable_get('menu_secondary_menu', 0);
1968     // Ensure that we wind up with a system menu named 'secondary-links'.
1969     if (isset($_SESSION['menu_menu_map'][$menu_secondary_menu]) && $menu_secondary_menu > 1 && $menu_secondary_menu != $updated_primary_links_menu) {
1970       // We use the menu that is currently assigned to the secondary links
1971       // region of the theme, as long as (a) it exists, (b) it isn't the
1972       // Navigation menu, (c) it isn't the same menu we assigned as the
1973       // system 'primary-links' menu above, and (d) it isn't the same menu
1974       // assigned to the primary links region of the theme.
1975       $updated_secondary_links_menu = $menu_secondary_menu;
1976     }
1977     else {
1978       // Otherwise, create 'secondary-links' as a new menu.
1979       $updated_secondary_links_menu = 0;
1980       db_query("INSERT INTO {menu_custom} (menu_name, title, description) VALUES ('%s', '%s', '%s')", $menus['secondary-links']);
1981     }
1982 
1983     if ($updated_secondary_links_menu) {
1984       // Change the existing menu name to 'secondary-links'.
1985       $replace = array('%new_name' => 'secondary-links', '%desc' => $menus['secondary-links']['description'], '%old_name' => $_SESSION['menu_menu_map'][$updated_secondary_links_menu]);
1986       $ret[] = update_sql(strtr("UPDATE {menu_custom} SET menu_name = '%new_name', description = '%desc' WHERE menu_name = '%old_name'", $replace));
1987       $ret[] = update_sql("UPDATE {menu_links} SET menu_name = 'secondary-links' WHERE menu_name = '". $_SESSION['menu_menu_map'][$updated_secondary_links_menu] ."'");
1988       $_SESSION['menu_menu_map'][$updated_secondary_links_menu] = 'secondary-links';
1989     }
1990 
1991     // Update menu OTF preferences.
1992     $mid = variable_get('menu_parent_items', 0);
1993     $menu_name = ($mid && isset($_SESSION['menu_menu_map'][$mid])) ? $_SESSION['menu_menu_map'][$mid] : 'navigation';
1994     variable_set('menu_default_node_menu', $menu_name);
1995     variable_del('menu_parent_items');
1996 
1997     // Update the source of the primary and secondary links.
1998     $menu_name = ($menu_primary_menu && isset($_SESSION['menu_menu_map'][$menu_primary_menu])) ? $_SESSION['menu_menu_map'][$menu_primary_menu] : '';
1999     variable_set('menu_primary_links_source', $menu_name);
2000     variable_del('menu_primary_menu');
2001 
2002     $menu_name = ($menu_secondary_menu && isset($_SESSION['menu_menu_map'][$menu_secondary_menu])) ? $_SESSION['menu_menu_map'][$menu_secondary_menu] : '';
2003     variable_set('menu_secondary_links_source', $menu_name);
2004     variable_del('menu_secondary_menu');
2005 
2006     // Skip the navigation menu - it is handled by the user module.
2007     unset($_SESSION['menu_menu_map'][1]);
2008     // Update the deltas for all menu module blocks.
2009     foreach ($_SESSION['menu_menu_map'] as $mid => $menu_name) {
2010       // This is again secure because we deleted every non-alpanumeric
2011       // character from the menu name.
2012       $ret[] = update_sql("UPDATE {blocks} SET delta = '". $menu_name ."' WHERE module = 'menu' AND delta = '". $mid ."'");
2013       $ret[] = update_sql("UPDATE {blocks_roles} SET delta = '". $menu_name ."' WHERE module = 'menu' AND delta = '". $mid ."'");
2014     }
2015     $ret[] = array('success' => TRUE, 'query' => 'Relocated '. $_SESSION['system_update_6021'] .' existing items to the new menu system.');
2016     $ret[] = update_sql("DROP TABLE {menu}");
2017     unset($_SESSION['system_update_6021'], $_SESSION['system_update_6021_max'], $_SESSION['menu_menu_map'], $_SESSION['menu_item_map'], $_SESSION['menu_bogus_menus']);
2018     // Create the menu overview links - also calls menu_rebuild(). If menu is
2019     // disabled, then just call menu_rebuild.
2020     if (function_exists('menu_enable')) {
2021       menu_enable();
2022     }
2023     else {
2024       menu_rebuild();
2025     }
2026     $ret['#finished'] = 1;
2027   }
2028   else {
2029     $ret['#finished'] = $_SESSION['system_update_6021'] / $_SESSION['system_update_6021_max'];
2030   }
2031   return $ret;
2032 }
2033 
2034 /**
2035  * Update files tables to associate files to a uid by default instead of a nid.
2036  * Rename file_revisions to upload since it should only be used by the upload
2037  * module used by upload to link files to nodes.
2038  */
2039 function system_update_6022() {
2040   $ret = array();
2041 
2042   // Rename the nid field to vid, add status and timestamp fields, and indexes.
2043   db_drop_index($ret, 'files', 'nid');
2044   db_change_field($ret, 'files', 'nid', 'uid', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0));
2045   db_add_field($ret, 'files', 'status', array('type' => 'int', 'not null' => TRUE, 'default' => 0));
2046   db_add_field($ret, 'files', 'timestamp', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0));
2047   db_add_index($ret, 'files', 'uid', array('uid'));
2048   db_add_index($ret, 'files', 'status', array('status'));
2049   db_add_index($ret, 'files', 'timestamp', array('timestamp'));
2050 
2051   // Rename the file_revisions table to upload then add nid column. Since we're
2052   // changing the table name we need to drop and re-add the indexes and
2053   // the primary key so both mysql and pgsql end up with the correct index
2054   // names.
2055   db_drop_primary_key($ret, 'file_revisions');
2056   db_drop_index($ret, 'file_revisions', 'vid');
2057   db_rename_table($ret, 'file_revisions', 'upload');
2058   db_add_field($ret, 'upload', 'nid', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0));
2059   db_add_index($ret, 'upload', 'nid', array('nid'));
2060   db_add_primary_key($ret, 'upload', array('vid', 'fid'));
2061   db_add_index($ret, 'upload', 'fid', array('fid'));
2062 
2063   // The nid column was renamed to uid. Use the old nid to find the node's uid.
2064   update_sql('UPDATE {files} SET uid = (SELECT n.uid FROM {node} n WHERE {files}.uid = n.nid)');
2065   update_sql('UPDATE {upload} SET nid = (SELECT r.nid FROM {node_revisions} r WHERE {upload}.vid = r.vid)');
2066 
2067   // Mark all existing files as FILE_STATUS_PERMANENT.
2068   $ret[] = update_sql('UPDATE {files} SET status = 1');
2069 
2070   return $ret;
2071 }
2072 
2073 function system_update_6023() {
2074   $ret = array();
2075 
2076   // nid is DEFAULT 0
2077   db_drop_index($ret, 'node_revisions', 'nid');
2078   db_change_field($ret, 'node_revisions', 'nid', 'nid', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0));
2079   db_add_index($ret, 'node_revisions', 'nid', array('nid'));
2080   return $ret;
2081 }
2082 
2083 /**
2084  * Add translation fields to nodes used by translation module.
2085  */
2086 function system_update_6024() {
2087   $ret = array();
2088   db_add_field($ret, 'node', 'tnid', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0));
2089   db_add_field($ret, 'node', 'translate', array('type' => 'int', 'not null' => TRUE, 'default' => 0));
2090   db_add_index($ret, 'node', 'tnid', array('tnid'));
2091   db_add_index($ret, 'node', 'translate', array('translate'));
2092   return $ret;
2093 }
2094 
2095 /**
2096  * Increase the maximum length of node titles from 128 to 255.
2097  */
2098 function system_update_6025() {
2099   $ret = array();
2100   db_drop_index($ret, 'node', 'node_title_type');
2101   db_change_field($ret, 'node', 'title', 'title', array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''));
2102   db_add_index($ret, 'node', 'node_title_type', array('title', array('type', 4)));
2103   db_change_field($ret, 'node_revisions', 'title', 'title', array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''));
2104   return $ret;
2105 }
2106 
2107 /**
2108  * Display warning about new Update status module.
2109  */
2110 function system_update_6026() {
2111   $ret = array();
2112 
2113   // Notify user that new update module exists.
2114   drupal_set_message('Drupal can check periodically for important bug fixes and security releases using the new update status module. This module can be turned on from the <a href="'. url('admin/build/modules') .'">modules administration page</a>. For more information please read the <a href="http://drupal.org/handbook/modules/update">Update status handbook page</a>.');
2115 
2116   return $ret;
2117 }
2118 
2119 /**
2120  * Add block cache.
2121  */
2122 function system_update_6027() {
2123   $ret = array();
2124 
2125   // Create the blocks.cache column.
2126   db_add_field($ret, 'blocks', 'cache', array('type' => 'int', 'not null' => TRUE, 'default' => 1, 'size' => 'tiny'));
2127 
2128   // The cache_block table is created in update_fix_d6_requirements() since
2129   // calls to cache_clear_all() would otherwise cause warnings.
2130 
2131   // Fill in the values for the new 'cache' column in the {blocks} table.
2132   foreach (module_list() as $module) {
2133     if ($module_blocks = module_invoke($module, 'block', 'list')) {
2134       foreach ($module_blocks as $delta => $block) {
2135         if (isset($block['cache'])) {
2136           db_query("UPDATE {blocks} SET cache = %d WHERE module = '%s' AND delta = %d", $block['cache'], $module, $delta);
2137         }
2138       }
2139     }
2140   }
2141 
2142   return $ret;
2143 }
2144 
2145 /**
2146  * Add the node load cache table.
2147  */
2148 function system_update_6028() {
2149   // Removed node_load cache to discuss it more for Drupal 7.
2150   return array();
2151 }
2152 
2153 /**
2154  * Enable the dblog module on sites that upgrade, since otherwise
2155  * watchdog logging will stop unexpectedly.
2156  */
2157 function system_update_6029() {
2158   // The watchdog table is now owned by dblog, which is not yet
2159   // "installed" according to the system table, but the table already
2160   // exists.  We set the module as "installed" here to avoid an error
2161   // later.
2162   //
2163   // Although not the case for the initial D6 release, it is likely
2164   // that dblog.install will have its own update functions eventually.
2165   // However, dblog did not exist in D5 and this update is part of the
2166   // initial D6 release, so we know that dblog is not installed yet.
2167   // It is therefore correct to install it as version 0.  If
2168   // dblog updates exist, the next run of update.php will get them.
2169   drupal_set_installed_schema_version('dblog', 0);
2170   module_enable(array('dblog'));
2171   menu_rebuild();
2172   return array(array('success' => TRUE, 'query' => "'dblog' module enabled."));
2173 }
2174 
2175 /**
2176  * Add the tables required by actions.inc.
2177  */
2178 function system_update_6030() {
2179   $ret = array();
2180 
2181   // Rename the old contrib actions table if it exists so the contrib version
2182   // of the module can do something with the old data.
2183   if (db_table_exists('actions')) {
2184     db_rename_table($ret, 'actions', 'actions_old_contrib');
2185   }
2186 
2187   $schema['actions'] = array(
2188     'fields' => array(
2189       'aid' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => '0'),
2190       'type' => array('type' => 'varchar', 'length' => 32, 'not null' => TRUE, 'default' => ''),
2191       'callback' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''),
2192       'parameters' => array('type' => 'text', 'not null' => TRUE, 'size' => 'big'),
2193       'description' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => '0'),
2194     ),
2195     'primary key' => array('aid'),
2196   );
2197 
2198   $schema['actions_aid'] = array(
2199     'fields' => array(
2200       'aid' => array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE),
2201     ),
2202     'primary key' => array('aid'),
2203   );
2204 
2205   db_create_table($ret, 'actions', $schema['actions']);
2206   db_create_table($ret, 'actions_aid', $schema['actions_aid']);
2207 
2208   return $ret;
2209 }
2210 
2211 /**
2212  * Ensure that installer cannot be run again after updating from Drupal 5.x to 6.x
2213  * Actually, this is already done by system_update_6014(), so this is now a no-op.
2214  */
2215 function system_update_6031() {
2216   return array();
2217 }
2218 
2219 /**
2220  * profile_fields.name used to be nullable but is part of a unique key
2221  * and so shouldn't be.
2222  */
2223 function system_update_6032() {
2224   $ret = array();
2225   if (db_table_exists('profile_fields')) {
2226     db_drop_unique_key($ret, 'profile_fields', 'name');
2227     db_change_field($ret, 'profile_fields', 'name', 'name', array('type' => 'varchar', 'length' => 128, 'not null' => TRUE, 'default' => ''));
2228     db_add_unique_key($ret, 'profile_fields', 'name', array('name'));
2229   }
2230   return $ret;
2231 }
2232 
2233 /**
2234  * Change node_comment_statistics to be not autoincrement.
2235  */
2236 function system_update_6033() {
2237   $ret = array();
2238   if (db_table_exists('node_comment_statistics')) {
2239     // On pgsql but not mysql, db_change_field() drops all keys
2240     // involving the changed field, which in this case is the primary
2241     // key.  The normal approach is explicitly drop the pkey, change the
2242     // field, and re-create the pkey.
2243     //
2244     // Unfortunately, in this case that won't work on mysql; we CANNOT
2245     // drop the pkey because on mysql auto-increment fields must be
2246     // included in at least one key or index.
2247     //
2248     // Since we cannot drop the pkey before db_change_field(), after
2249     // db_change_field() we may or may not still have a pkey.  The
2250     // simple way out is to re-create the pkey only when using pgsql.
2251     // Realistic requirements trump idealistic purity.
2252     db_change_field($ret, 'node_comment_statistics', 'nid', 'nid', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0));
2253     if ($GLOBALS['db_type'] == 'pgsql') {
2254       db_add_primary_key($ret, 'node_comment_statistics', array('nid'));
2255     }
2256   }
2257   return $ret;
2258 }
2259 
2260 /**
2261  * Rename permission "administer access control" to "administer permissions".
2262  */
2263 function system_update_6034() {
2264   $ret = array();
2265   $result = db_query("SELECT rid, perm FROM {permission} ORDER BY rid");
2266   while ($role = db_fetch_object($result)) {
2267     $renamed_permission = preg_replace('/administer access control/', 'administer permissions', $role->perm);
2268     if ($renamed_permission != $role->perm) {
2269       $ret[] = update_sql("UPDATE {permission} SET perm = '$renamed_permission' WHERE rid = $role->rid");
2270     }
2271   }
2272   return $ret;
2273 }
2274 
2275 /**
2276  * Change index on system table for better performance.
2277  */
2278 function system_update_6035() {
2279   $ret = array();
2280   db_drop_index($ret, 'system', 'weight');
2281   db_add_index($ret, 'system', 'modules', array(array('type', 12), 'status', 'weight', 'filename'));
2282   db_add_index($ret, 'system', 'bootstrap', array(array('type', 12), 'status', 'bootstrap', 'weight', 'filename'));
2283   return $ret;
2284 }
2285 
2286 /**
2287  * Change the search schema and indexing.
2288  *
2289  * The table data is preserved where possible in MYSQL and MYSQLi using
2290  * ALTER IGNORE. Other databases don't support that, so for them the
2291  * tables are dropped and re-created, and will need to be re-indexed
2292  * from scratch.
2293  */
2294 function system_update_6036() {
2295   $ret = array();
2296   if (db_table_exists('search_index')) {
2297     // Create the search_dataset.reindex column.
2298     db_add_field($ret, 'search_dataset', 'reindex', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0));
2299 
2300     // Drop the search_index.from fields which are no longer used.
2301     db_drop_index($ret, 'search_index', 'from_sid_type');
2302     db_drop_field($ret, 'search_index', 'fromsid');
2303     db_drop_field($ret, 'search_index', 'fromtype');
2304 
2305     // Drop the search_dataset.sid_type index, so that it can be made unique.
2306     db_drop_index($ret, 'search_dataset', 'sid_type');
2307 
2308     // Create the search_node_links Table.
2309     $search_node_links_schema = array(
2310       'fields' => array(
2311         'sid'      => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
2312         'type'     => array('type' => 'varchar', 'length' => 16, 'not null' => TRUE, 'default' => ''),
2313         'nid'      => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
2314         'caption'    => array('type' => 'text', 'size' => 'big', 'not null' => FALSE),
2315       ),
2316       'primary key' => array('sid', 'type', 'nid'),
2317       'indexes' => array('nid' => array('nid')),
2318     );
2319     db_create_table($ret, 'search_node_links', $search_node_links_schema);
2320 
2321     // with the change to search_dataset.reindex, the search queue is handled differently,
2322     // and this is no longer needed
2323     variable_del('node_cron_last');
2324 
2325     // Add a unique index for the search_index.
2326     if ($GLOBALS['db_type'] == 'mysql' || $GLOBALS['db_type'] == 'mysqli') {
2327       // Since it's possible that some existing sites have duplicates,
2328       // create the index using the IGNORE keyword, which ignores duplicate errors.
2329       // However, pgsql doesn't support it
2330       $ret[] = update_sql("ALTER IGNORE TABLE {search_index} ADD UNIQUE KEY word_sid_type (word, sid, type)");
2331       $ret[] = update_sql("ALTER IGNORE TABLE {search_dataset} ADD UNIQUE KEY sid_type (sid, type)");
2332 
2333       // Everything needs to be reindexed.
2334       $ret[] = update_sql("UPDATE {search_dataset} SET reindex = 1");
2335     }
2336     else {
2337       // Delete the existing tables if there are duplicate values
2338       if (db_result(db_query("SELECT sid FROM {search_dataset} GROUP BY sid, type HAVING COUNT(*) > 1")) || db_result(db_query("SELECT sid FROM {search_index} GROUP BY word, sid, type HAVING COUNT(*) > 1"))) {
2339         $ret[] = update_sql('DELETE FROM {search_dataset}');
2340         $ret[] = update_sql('DELETE FROM {search_index}');
2341         $ret[] = update_sql('DELETE FROM {search_total}');
2342       }
2343       else {
2344         // Everything needs to be reindexed.
2345         $ret[] = update_sql("UPDATE {search_dataset} SET reindex = 1");
2346       }
2347 
2348       // create the new indexes
2349       db_add_unique_key($ret, 'search_index', 'word_sid_type', array('word', 'sid', 'type'));
2350       db_add_unique_key($ret, 'search_dataset', 'sid_type', array('sid', 'type'));
2351     }
2352   }
2353   return $ret;
2354 }
2355 
2356 /**
2357  * Create consistent empty region for disabled blocks.
2358  */
2359 function system_update_6037() {
2360   $ret = array();
2361   db_change_field($ret, 'blocks', 'region', 'region', array('type' => 'varchar', 'length' => 64, 'not null' => TRUE, 'default' => ''));
2362   $ret[] = update_sql("UPDATE {blocks} SET region = '' WHERE status = 0");
2363   return $ret;
2364 }
2365 
2366 /**
2367  * Ensure that "Account" is not used as a Profile category.
2368  */
2369 function system_update_6038() {
2370   $ret = array();
2371   if (db_table_exists('profile_fields')) {
2372     $ret[] = update_sql("UPDATE {profile_fields} SET category = 'Account settings' WHERE LOWER(category) = 'account'");
2373     if ($affectedrows = db_affected_rows()) {
2374       drupal_set_message('There were '. $affectedrows .' profile fields that used a reserved category name. They have been assigned to the category "Account settings".');
2375     }
2376   }
2377   return $ret;
2378 }
2379 
2380 /**
2381  * Rename permissions "edit foo content" to "edit any foo content".
2382  * Also update poll module permission "create polls" to "create
2383  * poll content".
2384  */
2385 function system_update_6039() {
2386   $ret = array();
2387   $result = db_query("SELECT rid, perm FROM {permission} ORDER BY rid");
2388   while ($role = db_fetch_object($result)) {
2389     $renamed_permission = preg_replace('/(?<=^|,\ )edit\ ([a-zA-Z0-9_\-]+)\ content(?=,|$)/', 'edit any $1 content', $role->perm);
2390     $renamed_permission = preg_replace('/(?<=^|,\ )create\ polls(?=,|$)/', 'create poll content', $renamed_permission);
2391     if ($renamed_permission != $role->perm) {
2392       $ret[] = update_sql("UPDATE {permission} SET perm = '$renamed_permission' WHERE rid = $role->rid");
2393     }
2394   }
2395   return $ret;
2396 }
2397 
2398 /**
2399  * Add a weight column to the upload table.
2400  */
2401 function system_update_6040() {
2402   $ret = array();
2403   if (db_table_exists('upload')) {
2404     db_add_field($ret, 'upload', 'weight', array('type' => 'int', 'not null' => TRUE, 'default' => 0, 'size' => 'tiny'));
2405   }
2406   return $ret;
2407 }
2408 
2409 /**
2410  * Change forum vocabulary not to be required by default and set the weight of the forum.module 1 higher than the taxonomy.module.
2411  */
2412 function system_update_6041() {
2413   $weight = intval((db_result(db_query("SELECT weight FROM {system} WHERE name = 'taxonomy'"))) + 1);
2414   $ret = array();
2415   $vid = intval(variable_get('forum_nav_vocabulary', ''));
2416   if (db_table_exists('vocabulary') && $vid) {
2417     $ret[] = update_sql("UPDATE {vocabulary} SET required = 0 WHERE vid = " . $vid);
2418     $ret[] = update_sql("UPDATE {system} SET weight = ". $weight ." WHERE name = 'forum'");
2419   }
2420   return $ret;
2421 }
2422 
2423 /**
2424  * Upgrade recolored theme stylesheets to new array structure.
2425  */
2426 function system_update_6042() {
2427   foreach (list_themes() as $theme) {
2428     $stylesheet = variable_get('color_'. $theme->name .'_stylesheet', NULL);
2429     if (!empty($stylesheet)) {
2430       variable_set('color_'. $theme->name .'_stylesheets', array($stylesheet));
2431       variable_del('color_'. $theme->name .'_stylesheet');
2432     }
2433   }
2434   return array();
2435 }
2436 
2437 /**
2438  * Update table indices to make them more rational and useful.
2439  */
2440 function system_update_6043() {
2441   $ret = array();
2442   // Required modules first.
2443   // Add new system module indexes.
2444   db_add_index($ret, 'flood', 'allow', array('event', 'hostname', 'timestamp'));
2445   db_add_index($ret, 'history', 'nid', array('nid'));
2446   // Change length of theme field in {blocks} to be consistent with module, and
2447   // to avoid a MySQL error regarding a too-long index.  Also add new indices.
2448   db_change_field($ret, 'blocks', 'theme', 'theme', array('type' => 'varchar', 'length' => 64, 'not null' => TRUE, 'default' => ''),array(
2449                   'unique keys' => array('tmd' => array('theme', 'module', 'delta'),),
2450                   'indexes' => array('list' => array('theme', 'status', 'region', 'weight', 'module'),),));
2451   db_add_index($ret, 'blocks_roles', 'rid', array('rid'));
2452   // Improve filter module indices.
2453   db_drop_index($ret, 'filters', 'weight');
2454   db_add_unique_key($ret, 'filters', 'fmd', array('format', 'module', 'delta'));
2455   db_add_index($ret, 'filters', 'list', array('format', 'weight', 'module', 'delta'));
2456   // Drop unneeded keys form the node table.
2457   db_drop_index($ret, 'node', 'status');
2458   db_drop_unique_key($ret, 'node', 'nid_vid');
2459   // Improve user module indices.
2460   db_add_index($ret, 'users', 'mail', array('mail'));
2461   db_add_index($ret, 'users_roles', 'rid', array('rid'));
2462 
2463   // Optional modules - need to check if the tables exist.
2464   // Alter aggregator module's tables primary keys to make them more useful.
2465   if (db_table_exists('aggregator_category_feed')) {
2466     db_drop_primary_key($ret, 'aggregator_category_feed');
2467     db_add_primary_key($ret, 'aggregator_category_feed', array('cid', 'fid'));
2468     db_add_index($ret, 'aggregator_category_feed', 'fid', array('fid'));
2469   }
2470   if (db_table_exists('aggregator_category_item')) {
2471     db_drop_primary_key($ret, 'aggregator_category_item');
2472     db_add_primary_key($ret, 'aggregator_category_item', array('cid', 'iid'));
2473     db_add_index($ret, 'aggregator_category_item', 'iid', array('iid'));
2474   }
2475   // Alter contact module's table to add an index.
2476   if (db_table_exists('contact')) {
2477     db_add_index($ret, 'contact', 'list', array('weight', 'category'));
2478   }
2479   // Alter locale table to add a primary key, drop an index.
2480   if (db_table_exists('locales_target')) {
2481     db_add_primary_key($ret, 'locales_target', array('language', 'lid', 'plural'));
2482   }
2483   // Alter a poll module table to add a primary key.
2484   if (db_table_exists('poll_votes')) {
2485     db_drop_index($ret, 'poll_votes', 'nid');
2486     db_add_primary_key($ret, 'poll_votes', array('nid', 'uid', 'hostname'));
2487   }
2488   // Alter a profile module table to add a primary key.
2489   if (db_table_exists('profile_values')) {
2490     db_drop_index($ret, 'profile_values', 'uid');
2491     db_drop_index($ret, 'profile_values', 'fid');
2492     db_change_field($ret,'profile_values' ,'fid', 'fid', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0,), array('indexes' => array('fid' => array('fid'),)));
2493     db_change_field($ret,'profile_values' ,'uid', 'uid', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0,));
2494     db_add_primary_key($ret, 'profile_values', array('uid', 'fid'));
2495   }
2496   // Alter a statistics module table to add an index.
2497   if (db_table_exists('accesslog')) {
2498     db_add_index($ret, 'accesslog', 'uid', array('uid'));
2499   }
2500   // Alter taxonomy module's tables.
2501   if (db_table_exists('term_data')) {
2502     db_drop_index($ret, 'term_data', 'vid');
2503     db_add_index($ret, 'term_data', 'vid_name', array('vid', 'name'));
2504     db_add_index($ret, 'term_data', 'taxonomy_tree', array('vid', 'weight', 'name'));
2505   }
2506   if (db_table_exists('term_node')) {
2507     db_drop_primary_key($ret, 'term_node');
2508     db_drop_index($ret, 'term_node', 'tid');
2509     db_add_primary_key($ret, 'term_node', array('tid', 'vid'));
2510   }
2511   if (db_table_exists('term_relation')) {
2512     db_drop_index($ret, 'term_relation', 'tid1');
2513     db_add_unique_key($ret, 'term_relation', 'tid1_tid2', array('tid1', 'tid2'));
2514   }
2515   if (db_table_exists('term_synonym')) {
2516     db_drop_index($ret, 'term_synonym', 'name');
2517     db_add_index($ret, 'term_synonym', 'name_tid', array('name', 'tid'));
2518   }
2519   if (db_table_exists('vocabulary')) {
2520     db_add_index($ret, 'vocabulary', 'list', array('weight', 'name'));
2521   }
2522   if (db_table_exists('vocabulary_node_types')) {
2523     db_drop_primary_key($ret, 'vocabulary_node_types');
2524     db_add_primary_key($ret, 'vocabulary_node_types', array('type', 'vid'));
2525     db_add_index($ret, 'vocabulary_node_types', 'vid', array('vid'));
2526   }
2527   // If we updated in RC1 or before ensure we don't update twice.
2528   variable_set('system_update_6043_RC2', TRUE);
2529 
2530   return $ret;
2531 }
2532 
2533 /**
2534  * RC1 to RC2 index cleanup.
2535  */
2536 function system_update_6044() {
2537   $ret = array();
2538 
2539   // Delete invalid entries in {term_node} after system_update_6001.
2540   $ret[] = update_sql("DELETE FROM {term_node} WHERE vid = 0");
2541 
2542   // Only execute the rest of this function if 6043 was run in RC1 or before.
2543   if (variable_get('system_update_6043_RC2', FALSE)) {
2544     variable_del('system_update_6043_RC2');
2545     return $ret;
2546   }
2547 
2548   // User module indices.
2549   db_drop_unique_key($ret, 'users', 'mail');
2550   db_add_index($ret, 'users', 'mail', array('mail'));
2551 
2552   // Optional modules - need to check if the tables exist.
2553   // Alter taxonomy module's tables.
2554   if (db_table_exists('term_data')) {
2555     db_drop_unique_key($ret, 'term_data', 'vid_name');
2556     db_add_index($ret, 'term_data', 'vid_name', array('vid', 'name'));
2557   }
2558   if (db_table_exists('term_synonym')) {
2559     db_drop_unique_key($ret, 'term_synonym', 'name_tid', array('name', 'tid'));
2560     db_add_index($ret, 'term_synonym', 'name_tid', array('name', 'tid'));
2561   }
2562 
2563   return $ret;
2564 }
2565 
2566 /**
2567  * Update blog, book and locale module permissions.
2568  *
2569  * Blog module got "edit own blog" replaced with the more granular "create
2570  * blog entries", "edit own blog entries" and "delete own blog entries"
2571  * permissions. We grant create and edit to previously privileged users, but
2572  * delete is not granted to be in line with other permission changes in Drupal 6.
2573  *
2574  * Book module's "edit book pages" was upgraded to the bogus "edit book content"
2575  * in Drupal 6 RC1 instead of "edit any book content", which would be correct.
2576  *
2577  * Locale module introduced "administer languages" and "translate interface"
2578  * in place of "administer locales".
2579  *
2580  * Modeled after system_update_6039().
2581  */
2582 function system_update_6045() {
2583   $ret = array();
2584   $result = db_query("SELECT rid, perm FROM {permission} ORDER BY rid");
2585   while ($role = db_fetch_object($result)) {
2586     $renamed_permission = preg_replace('/(?<=^|,\ )edit\ own\ blog(?=,|$)/', 'create blog entries, edit own blog entries', $role->perm);
2587     $renamed_permission = preg_replace('/(?<=^|,\ )edit\ book\ content(?=,|$)/', 'edit any book content', $renamed_permission);
2588     $renamed_permission = preg_replace('/(?<=^|,\ )administer\ locales(?=,|$)/', 'administer languages, translate interface', $renamed_permission);
2589     if ($renamed_permission != $role->perm) {
2590       $ret[] = update_sql("UPDATE {permission} SET perm = '$renamed_permission' WHERE rid = $role->rid");
2591     }
2592   }
2593 
2594   // Notify user that delete permissions may have been changed. This was in
2595   // effect since system_update_6039(), but there was no user notice.
2596   drupal_set_message('Drupal now has separate edit and delete permissions. Previously, users who were able to edit content were automatically allowed to delete it. For added security, delete permissions for individual core content types have been <strong>removed</strong> from all roles on your site (only roles with the "administer nodes" permission can now delete these types of content). If you would like to reenable any individual delete permissions, you can do this at the <a href="'. url('admin/user/permissions', array('fragment' => 'module-node')) .'">permissions page</a>.');
2597   return $ret;
2598 }
2599 
2600 /**
2601  * Ensure that the file_directory_path variable is set (using the old 5.x
2602  * default, if necessary), so that the changed 6.x default won't break
2603  * existing sites.
2604  */
2605 function system_update_6046() {
2606   $ret = array();
2607   if (!variable_get('file_directory_path', FALSE)) {
2608     variable_set('file_directory_path', 'files');
2609     $ret[] = array('success' => TRUE, 'query' => "variable_set('file_directory_path')");
2610   }
2611   return $ret;
2612 }
2613 
2614 /**
2615  * Fix cache mode for blocks inserted in system_install() in fresh installs of previous RC.
2616  */
2617 function system_update_6047() {
2618   $ret = array();
2619   $ret[] = update_sql("UPDATE {blocks} SET cache = -1 WHERE module = 'user' AND delta IN ('0', '1')");
2620   $ret[] = update_sql("UPDATE {blocks} SET cache = -1 WHERE module = 'system' AND delta = '0'");
2621   return $ret;
2622 }
2623 
2624 /**
2625  * @} End of "defgroup updates-5.x-to-6.x"
2626  * The next series of updates should start at 7000.
2627  */
2628 
2629 /**
2630  * @defgroup updates-6.x-to-7.x System updates from 6.x to 7.x
2631  * @{
2632  */
2633 
2634 /**
2635  * Rename blog and forum permissions to be consistent with other content types.
2636  */
2637 function system_update_7000() {
2638   $ret = array();
2639   $result = db_query("SELECT rid, perm FROM {permission} ORDER BY rid");
2640   while ($role = db_fetch_object($result)) {
2641     $renamed_permission = preg_replace('/(?<=^|,\ )create\ blog\ entries(?=,|$)/', 'create blog content', $role->perm);
2642     $renamed_permission = preg_replace('/(?<=^|,\ )edit\ own\ blog\ entries(?=,|$)/', 'edit own blog content', $role->perm);
2643     $renamed_permission = preg_replace('/(?<=^|,\ )edit\ any\ blog\ entry(?=,|$)/', 'edit any blog content', $role->perm);
2644     $renamed_permission = preg_replace('/(?<=^|,\ )delete\ own\ blog\ entries(?=,|$)/', 'delete own blog content', $role->perm);
2645     $renamed_permission = preg_replace('/(?<=^|,\ )delete\ any\ blog\ entry(?=,|$)/', 'delete any blog content', $role->perm);
2646 
2647     $renamed_permission = preg_replace('/(?<=^|,\ )create\ forum\ topics(?=,|$)/', 'create forum content', $role->perm);
2648     $renamed_permission = preg_replace('/(?<=^|,\ )delete\ any\ forum\ topic(?=,|$)/', 'delete any forum content', $role->perm);
2649     $renamed_permission = preg_replace('/(?<=^|,\ )delete\ own\ forum\ topics(?=,|$)/', 'delete own forum content', $role->perm);
2650     $renamed_permission = preg_replace('/(?<=^|,\ )edit\ any\ forum\ topic(?=,|$)/', 'edit any forum content', $role->perm);
2651     $renamed_permission = preg_replace('/(?<=^|,\ )edit\ own\ forum\ topics(?=,|$)/', 'edit own forum content', $role->perm);
2652 
2653     if ($renamed_permission != $role->perm) {
2654       $ret[] = update_sql("UPDATE {permission} SET perm = '$renamed_permission' WHERE rid = $role->rid");
2655     }
2656   }
2657 
2658   return $ret;
2659 }
2660 
2661 /**
2662  * Generate a cron key and save it in the variables table
2663  */
2664 function system_update_7001() {
2665   $ret = array();
2666   variable_set('cron_key', md5(mt_rand()));
2667   $ret[] = array('success' => TRUE, 'query' => "variable_set('cron_key')");
2668   return $ret;
2669 }
2670 
2671 
2672 /**
2673  * @} End of "defgroup updates-6.x-to-7.x"
2674  * The next series of updates should start at 8000.
2675  */
2676 
2677