Issue #1845268 by Pol, d.clarke: Fixed Jump menu default value incorrectly set when...
[project/views.git] / views.install
CommitLineData
90ed9220 1<?php
63b47c8d 2/**
582713d9
EM
3 * @file views.install
4 * Contains install and update functions for Views.
63b47c8d 5 */
58e4ed44 6
e8b70879
EM
7/**
8 * Implementation of hook_install().
9 */
582713d9 10function views_install() {
d8f37d0d
EM
11 if ($GLOBALS['db_type'] == 'pgsql') {
12 db_query('CREATE OR REPLACE FUNCTION first(anyelement, anyelement) RETURNS anyelement AS \'SELECT COALESCE($1, $2);\' LANGUAGE \'sql\';');
13 db_query("DROP AGGREGATE IF EXISTS first(anyelement)");
14 db_query("CREATE AGGREGATE first(sfunc = first, basetype = anyelement, stype = anyelement);");
15 }
582713d9
EM
16 drupal_install_schema('views');
17 db_query("UPDATE {system} SET weight = 10 WHERE name = 'views'");
fad16eed 18}
e2ef9c10 19
e8b70879
EM
20/**
21 * Implementation of hook_uninstall().
22 */
582713d9
EM
23function views_uninstall() {
24 drupal_uninstall_schema('views');
e2ef9c10 25}
b06399e3
EM
26
27/**
e8b70879
EM
28 * Implementation of hook_schema().
29 *
30 * Generate the current version of the database schema from
31 * the sequence of schema update functions. Uses a similar
32 * method to install.inc's drupal_get_schema_versions() to
33 * establish the update sequence.
34 *
35 * To change the schema, add a new views_schema_N()
36 * function to match the associated views_update_N()
37 *
38 * @param $caller_function
39 * The name of the function that called us.
40 * Used internally, if requesting a specific schema version.
b06399e3 41 */
e8b70879
EM
42function views_schema($caller_function = FALSE) {
43 static $get_current;
44 static $schemas = array();
45
46 // If called with no arguments, get the latest version of the schema.
47 if (!isset($get_current)) {
48 $get_current = $caller_function ? FALSE : TRUE;
49 }
50
51 // Generate a sorted list of available schema update functions.
52 if ($get_current || empty($schemas)) {
53 $get_current = FALSE;
54 $functions = get_defined_functions();
55 foreach ($functions['user'] as $function) {
56 if (strpos($function, 'views_schema_') === 0) {
57 $version = substr($function, strlen('views_schema_'));
58 if (is_numeric($version)) {
59 $schemas[] = $version;
60 }
61 }
62 }
63 if ($schemas) {
64 sort($schemas, SORT_NUMERIC);
65
66 // If a specific version was requested, drop any later
67 // updates from the sequence.
68 if ($caller_function) {
69 do {
70 $schema = array_pop($schemas);
71 } while ($schemas && $caller_function != 'views_schema_'. $schema);
72 }
73 }
74 }
75
76 // Call views_schema_<n>, for the highest available <n>.
77 if ($schema = array_pop($schemas)) {
78 $function = 'views_schema_'. $schema;
79 return $function();
80 }
81
82 return array();
3fddb150
EM
83}
84
85/**
e8b70879
EM
86 * Views 2's initial schema.
87 * Called directly by views_update_6000() for updates from Drupal 5.
88 *
89 * Important: Do not edit this schema!
90 *
91 * Updates to the views schema must be provided as views_schema_6xxx() functions,
92 * which views_schema() automatically sees and applies. See below for examples.
93 *
94 * Please do document updates with comments in this function, however.
3fddb150 95 */
e8b70879 96function views_schema_6000() {
b06399e3 97 $schema['views_view'] = array(
cfdea9bb 98 'description' => 'Stores the general data for a view.',
b06399e3
EM
99 'fields' => array(
100 'vid' => array(
58e4ed44
EM
101 'type' => 'serial',
102 'unsigned' => TRUE,
103 'not null' => TRUE,
cfdea9bb 104 'description' => 'The view ID of the field, defined by the database.',
b06399e3
EM
105 'no export' => TRUE,
106 ),
107 'name' => array(
58e4ed44
EM
108 'type' => 'varchar',
109 'length' => '32',
b06399e3 110 'default' => '',
58e4ed44 111 'not null' => TRUE,
cfdea9bb 112 'description' => 'The unique name of the view. This is the primary field views are loaded from, and is used so that views may be internal and not necessarily in the database. May only be alphanumeric characters plus underscores.',
b06399e3
EM
113 ),
114 'description' => array(
58e4ed44
EM
115 'type' => 'varchar',
116 'length' => '255',
b06399e3 117 'default' => '',
cfdea9bb 118 'description' => 'A description of the view for the admin interface.',
b06399e3 119 ),
58e4ed44
EM
120 'tag' => array(
121 'type' => 'varchar',
122 'length' => '255',
123 'default' => '',
cfdea9bb 124 'description' => 'A tag used to group/sort views in the admin interface',
58e4ed44 125 ),
b06399e3 126 'view_php' => array(
58e4ed44 127 'type' => 'blob',
cfdea9bb 128 'description' => 'A chunk of PHP code that can be used to provide modifications to the view prior to building.',
b06399e3
EM
129 ),
130 'base_table' => array(
58e4ed44 131 'type' => 'varchar',
e8b70879 132 'length' => '32', // Updated to '64' in views_schema_6005()
b06399e3 133 'default' => '',
58e4ed44 134 'not null' => TRUE,
cfdea9bb 135 'description' => 'What table this view is based on, such as node, user, comment, or term.',
b06399e3
EM
136 ),
137 'is_cacheable' => array(
58e4ed44 138 'type' => 'int',
b06399e3
EM
139 'default' => 0,
140 'size' => 'tiny',
cfdea9bb 141 'description' => 'A boolean to indicate whether or not this view may have its query cached.',
b06399e3
EM
142 ),
143 ),
144 'primary key' => array('vid'),
e8b70879 145 'unique key' => array('name' => array('name')), // Updated to 'unique keys' in views_schema_6003()
b06399e3
EM
146 );
147
148 $schema['views_display'] = array(
cfdea9bb 149 'description' => 'Stores information about each display attached to a view.',
b06399e3
EM
150 'fields' => array(
151 'vid' => array(
58e4ed44
EM
152 'type' => 'int',
153 'unsigned' => TRUE,
154 'not null' => TRUE,
b06399e3 155 'default' => 0,
cfdea9bb 156 'description' => 'The view this display is attached to.',
b06399e3
EM
157 'no export' => TRUE,
158 ),
dc3ec4ee 159 'id' => array(
58e4ed44
EM
160 'type' => 'varchar',
161 'length' => '64',
b06399e3 162 'default' => '',
58e4ed44 163 'not null' => TRUE,
cfdea9bb 164 'description' => 'An identifier for this display; usually generated from the display_plugin, so should be something like page or page_1 or block_2, etc.',
b06399e3 165 ),
dc3ec4ee 166 'display_title' => array(
58e4ed44
EM
167 'type' => 'varchar',
168 'length' => '64',
b06399e3 169 'default' => '',
58e4ed44 170 'not null' => TRUE,
cfdea9bb 171 'description' => 'The title of the display, viewable by the administrator.',
dc3ec4ee
EM
172 ),
173 'display_plugin' => array(
174 'type' => 'varchar',
175 'length' => '64',
176 'default' => '',
177 'not null' => TRUE,
cfdea9bb 178 'description' => 'The type of the display. Usually page, block or embed, but is pluggable so may be other things.',
b06399e3 179 ),
b06399e3 180 'position' => array(
58e4ed44 181 'type' => 'int',
b06399e3 182 'default' => 0,
cfdea9bb 183 'description' => 'The order in which this display is loaded.',
b06399e3
EM
184 ),
185 'display_options' => array(
021cdeec 186 // Type corrected in update 6009
58e4ed44 187 'type' => 'blob',
cfdea9bb 188 'description' => 'A serialized array of options for this display; it contains options that are generally only pertinent to that display plugin type.',
b06399e3
EM
189 'serialize' => TRUE,
190 'serialized default' => 'a:0:{}',
191 ),
b06399e3 192 ),
021cdeec 193 // Added primary keys in views_schema_6008()
b06399e3
EM
194 'indexes' => array('vid' => array('vid', 'position')),
195 );
196
bb770a1b 197 $schema['cache_views'] = drupal_get_schema_unprocessed('system', 'cache');
b06399e3 198
58e4ed44 199 $schema['views_object_cache'] = array(
cfdea9bb 200 'description' => 'A special cache used to store objects that are being edited; it serves to save state in an ordinarily stateless environment.',
58e4ed44
EM
201 'fields' => array(
202 'sid' => array(
203 'type' => 'varchar',
204 'length' => '64',
cfdea9bb 205 'description' => 'The session ID this cache object belongs to.',
58e4ed44
EM
206 ),
207 'name' => array(
208 'type' => 'varchar',
209 'length' => '32',
cfdea9bb 210 'description' => 'The name of the view this cache is attached to.',
58e4ed44
EM
211 ),
212 'obj' => array(
213 'type' => 'varchar',
214 'length' => '32',
cfdea9bb 215 'description' => 'The name of the object this cache is attached to; this essentially represents the owner so that several sub-systems can use this cache.',
58e4ed44
EM
216 ),
217 'updated' => array(
218 'type' => 'int',
219 'unsigned' => TRUE,
220 'not null' => TRUE,
221 'default' => 0,
cfdea9bb 222 'description' => 'The time this cache was created or updated.',
58e4ed44
EM
223 ),
224 'data' => array(
e8b70879 225 'type' => 'blob', // Updated to 'text' (with size => 'big') in views_schema_6004()
cfdea9bb 226 'description' => 'Serialized data being stored.',
58e4ed44
EM
227 'serialize' => TRUE,
228 ),
229 ),
230 'indexes' => array(
231 'sid_obj_name' => array('sid', 'obj', 'name'),
232 'updated' => array('updated'),
233 ),
234 );
e8b70879
EM
235
236 // $schema['cache_views_data'] added in views_schema_6006()
237
b06399e3
EM
238 return $schema;
239}
1008b7ff 240
3fddb150
EM
241/**
242 * Update a site to Drupal 6! Contains a bit of special code to detect
243 * if you've been running a beta version or something.
244 */
245function views_update_6000() {
0eeeee6a 246 $ret = array();
3fddb150 247 if (db_table_exists('views_view')) {
0eeeee6a 248 return $ret;
3fddb150
EM
249 }
250
691c8574
EM
251 // This has the beneficial effect of wiping out any Views 1 cache at the
252 // same time; not wiping that cache could easily cause problems with Views 2.
253 if (db_table_exists('cache_views')) {
254 db_drop_table($ret, 'cache_views');
255 }
256
3fddb150 257 // This is mostly the same as drupal_install_schema, but it forces
e8b70879
EM
258 // views_schema_6000() rather than the default views_schema().
259 // This is important for processing subsequent table updates.
260 $schema = views_schema_6000();
d7f09343 261 _drupal_initialize_schema('views', $schema);
3fddb150 262
3fddb150
EM
263 foreach ($schema as $name => $table) {
264 db_create_table($ret, $name, $table);
265 }
266 return $ret;
267}
4c9e5b42 268
e8b70879
EM
269/**
270 * Remove '$' symbol in special blocks, as it is invalid for theming.
271 */
4c9e5b42
EM
272function views_update_6001() {
273 $ret = array();
274 $result = db_query("SELECT * FROM {blocks} WHERE module = 'views' AND delta LIKE '\$exp%'");
275 while ($block = db_fetch_object($result)) {
276 $new = strtr($block->delta, '$', '-');
277 $ret[] = update_sql("UPDATE {blocks} SET delta = '" . db_escape_string($new) . "' WHERE module = 'views' AND delta = '" . db_escape_string($block->delta) . "'");
278 }
6f8f6ac6
EM
279 $result = db_query("SELECT * FROM {blocks} WHERE module = 'views'");
280 while ($block = db_fetch_object($result)) {
281 $new = $block->delta .= '-block_1';
282 if (strlen($new) >= 32) {
283 $new = md5($new);
284 }
fb6921ad 285 $ret[] = update_sql("UPDATE {blocks} SET delta = '$new' WHERE bid = $block->bid");
6f8f6ac6 286 }
4c9e5b42
EM
287
288 return $ret;
289}
c0215efe 290
b117ead5 291// NOTE: Update 6002 removed because it did not always work.
e8b70879
EM
292// Update 6004 implements the change correctly.
293
b117ead5
EM
294/**
295 * Add missing unique key.
296 */
e8b70879
EM
297function views_schema_6003() {
298 $schema = views_schema(__FUNCTION__);
299 $schema['views_view']['unique keys'] = array('name' => array('name'));
300 unset($schema['views_view']['unique key']);
301 return $schema;
302}
b117ead5
EM
303function views_update_6003() {
304 $ret = array();
b117ead5 305 db_add_unique_key($ret, 'views_view', 'name', array('name'));
b117ead5
EM
306 return $ret;
307}
308
c0215efe
EM
309/**
310 * Enlarge the views_object_cache.data column to prevent truncation and JS
311 * errors.
312 */
e8b70879
EM
313function views_schema_6004() {
314 $schema = views_schema(__FUNCTION__);
315 $schema['views_object_cache']['fields']['data']['type'] = 'text';
316 $schema['views_object_cache']['fields']['data']['size'] = 'big';
317 return $schema;
318}
b117ead5 319function views_update_6004() {
c0215efe
EM
320 $ret = array();
321
322 $new_field = array(
323 'type' => 'text',
324 'size' => 'big',
cfdea9bb 325 'description' => 'Serialized data being stored.',
c0215efe
EM
326 'serialize' => TRUE,
327 );
328
b117ead5
EM
329 // Drop and re-add this field because there is a bug in
330 // db_change_field that causes this to fail when trying to cast the data.
331 db_drop_field($ret, 'views_object_cache', 'data');
332 db_add_field($ret, 'views_object_cache', 'data', $new_field);
9755f01b
EM
333
334 return $ret;
335}
c8d79e8f
EM
336
337/**
338 * Enlarge the base_table column
339 */
e8b70879
EM
340function views_schema_6005() {
341 $schema = views_schema(__FUNCTION__);
342 $schema['views_view']['fields']['base_table']['length'] = 64;
343 return $schema;
344}
c8d79e8f
EM
345function views_update_6005() {
346 $ret = array();
021cdeec 347
c8d79e8f
EM
348 $new_field = array(
349 'type' => 'varchar',
350 'length' => '64',
351 'default' => '',
352 'not null' => TRUE,
353 'description' => 'What table this view is based on, such as node, user, comment, or term.',
354 );
355 db_change_field($ret, 'views_view', 'base_table', 'base_table', $new_field);
356 return $ret;
357}
49cfa0a7
EM
358
359/**
360 * Add the cache_views_data table to support standard caching.
361 */
e8b70879
EM
362function views_schema_6006() {
363 $schema = views_schema(__FUNCTION__);
364 $schema['cache_views_data'] = drupal_get_schema_unprocessed('system', 'cache');
365 $schema['cache_views_data']['description'] = 'Cache table for views to store pre-rendered queries, results, and display output.';
366 $schema['cache_views_data']['fields']['serialized']['default'] = 1;
367 return $schema;
368}
02e6ee42 369function views_update_6006() {
49cfa0a7 370 $ret = array();
021cdeec 371
49cfa0a7
EM
372 $table = drupal_get_schema_unprocessed('system', 'cache');
373 $table['description'] = 'Cache table for views to store pre-rendered queries, results, and display output.';
374 $table['fields']['serialized']['default'] = 1;
375
376 db_create_table($ret, 'cache_views_data', $table);
377
378 return $ret;
379}
d8f37d0d
EM
380
381/**
382 * Add aggregate function to PostgreSQL so GROUP BY can be used to force only
383 * one result to be returned for each item.
384 */
02e6ee42 385function views_update_6007() {
d8f37d0d
EM
386 $ret = array();
387 if ($GLOBALS['db_type'] == 'pgsql') {
388 $ret[] = update_sql('CREATE OR REPLACE FUNCTION first(anyelement, anyelement) RETURNS anyelement AS \'SELECT COALESCE($1, $2);\' LANGUAGE \'sql\';');
389 $ret[] = update_sql("DROP AGGREGATE IF EXISTS first(anyelement)");
390 $ret[] = update_sql("CREATE AGGREGATE first(sfunc = first, basetype = anyelement, stype = anyelement);");
391 }
392 return $ret;
393}
2524ee71
EM
394
395/**
396 * Add the primary key to views_display table.
397 */
021cdeec
EM
398function views_schema_6008() {
399 $schema = views_schema(__FUNCTION__);
400 $schema['views_display']['primary key'] = array('vid', 'id');
401 return $schema;
402}
403
404/**
405 * Add the primary key to the views_display table.
406 */
2524ee71
EM
407function views_update_6008() {
408 $ret = array();
409
410 db_add_primary_key($ret, 'views_display', array('vid', 'id'));
411
412 return $ret;
413}
021cdeec
EM
414
415/**
416 * Enlarge the views_display.display_options field to accomodate a larger set
417 * of configurations (e. g. fields, filters, etc.) on a display.
418 */
419function views_schema_6009() {
420 $schema = views_schema(__FUNCTION__);
421 $schema['views_display']['fields']['display_options'] = array(
422 'type' => 'text',
423 'size' => 'big',
424 'description' => 'A serialized array of options for this display; it contains options that are generally only pertinent to that display plugin type.',
425 'serialize' => TRUE,
426 'serialized default' => 'a:0:{}',
427 );
428 return $schema;
429}
430
431function views_update_6009() {
432 $ret = array();
433
434 $schema = views_schema_6009();
a4598978
EM
435
436 if ($GLOBALS['db_type'] == 'pgsql') {
437 $ret[] = update_sql('ALTER TABLE {views_display} RENAME "display_options" TO "display_options_old"');
438 db_add_field($ret, 'views_display', 'display_options', $schema['views_display']['fields']['display_options']);
439
440 $sql = "SELECT vid, id, display_options_old FROM {views_display}";
441 $result = db_query($sql);
442 while ($row = db_fetch_array($result)) {
443 $row['display_options_old'] = db_decode_blob($row['display_options_old']);
444 $sql = "UPDATE {views_display} SET display_options = '%s' WHERE vid = %d AND id = '%s'";
445 db_query($sql, $row['display_options_old'], $row['vid'], $row['id']);
446 }
447
448 db_drop_field($ret, 'views_display', 'display_options_old');
449 }
450 else {
451 db_change_field($ret, 'views_display', 'display_options', 'display_options', $schema['views_display']['fields']['display_options']);
452 }
021cdeec
EM
453
454 return $ret;
a4598978 455}
da60d55e
EM
456
457/**
458 * Remove the view_php field
459 */
460function views_schema_6010() {
461 $schema = views_schema(__FUNCTION__);
462 unset($schema['views_view']['fields']['view_php']);
463 unset($schema['views_view']['fields']['is_cacheable']);
464 return $schema;
465}
466
467/**
468 * Remove the view_php and is_cacheable field
469 */
470function views_update_6010() {
471 $ret = array();
472
473 db_drop_field($ret, 'views_view', 'view_php');
474 db_drop_field($ret, 'views_view', 'is_cacheable');
475
476
477 return $ret;
478}
014b8da4
EM
479
480/**
481 * Correct the cache setting for exposed filter blocks.
482 *
483 * @see http://drupal.org/node/910864
484 */
485function views_update_6011() {
486 $ret = array();
487
488 // There is only one simple query to run.
489 $ret[] = update_sql("UPDATE {blocks} SET cache = " . BLOCK_NO_CACHE . " WHERE module = 'views' AND delta LIKE '-exp-%'");
915aec31 490
014b8da4
EM
491 return $ret;
492}
a4615a7f
EM
493
494
495/**
496 * Add a human readable name.
497 */
498function views_schema_6012() {
499 $schema = views_schema(__FUNCTION__);
500 $schema['views_view']['fields']['human_name'] = array(
501 'type' => 'varchar',
502 'length' => '255',
503 'default' => '',
504 'description' => 'A human readable name used to be displayed in the admin interface',
505 );
506 return $schema;
507}
508
509function views_update_6012() {
510 $ret = array();
511
512 $new_field = array(
513 'type' => 'varchar',
514 'length' => '255',
515 'default' => '',
516 'description' => 'A human readable name used to be displayed in the admin interface',
517 );
518
519 db_add_field($ret, 'views_view', 'human_name', $new_field);
ace41de4
DW
520
521 return $ret;
a4615a7f 522}
584acf9b
EM
523
524function views_schema_6013() {
525 $schema = views_schema(__FUNCTION__);
526 $schema['views_view']['fields']['core'] = array(
527 'type' => 'int',
528 'default' => 0,
529 'description' => 'Stores the drupal core version of the view.',
530 );
531 return $schema;
532}
533
534/**
535 * Add a drupal core version field.
536 */
537function views_update_6013() {
538 $ret = array();
539 $new_field = array(
540 'type' => 'int',
541 'default' => 0,
542 'description' => 'Stores the drupal core version of the view.',
543 );
544
545 db_add_field($ret, 'views_view', 'core', $new_field);
546
547 return $ret;
548}
549
06fd13bd
DW
550function views_schema_6300() {
551 return views_schema_6013();
552}
553/**
554 * Take sure the human_name field is added to the views_view table.
555 *
556 * If you updated from 6.x-2.x-dev to 6.x-3.x you had already schema
557 * version 6013, so the update 6012 isn't runned, and never was.
558 * As a result the human_name field is missing in the database.
559 *
560 * Add the human_name field if it doesn't exist before.
561 */
562function views_update_6300() {
563 $ret = array();
564 if (!db_column_exists('views_view', 'human_name')) {
565 $ret += views_update_6012();
566 }
567 return $ret;
568}