Issue #2096013 by kalman.hosszu | mgifford: Issue Tag labels have problems in issue...
[project/views.git] / views.install
CommitLineData
31dd0540 1<?php
72f907f2 2
31dd0540 3/**
72f907f2 4 * @file
31dd0540
TGGM
5 * Contains install and update functions for Views.
6 */
7
45a5d43e
DW
8/**
9 * Implements hook_install().
10 */
31dd0540 11function views_install() {
7133bba5 12 if (Database::getConnection()->databaseType() == 'pgsql') {
31dd0540
TGGM
13 db_query('CREATE OR REPLACE FUNCTION first(anyelement, anyelement) RETURNS anyelement AS \'SELECT COALESCE($1, $2);\' LANGUAGE \'sql\';');
14 db_query("DROP AGGREGATE IF EXISTS first(anyelement)");
15 db_query("CREATE AGGREGATE first(sfunc = first, basetype = anyelement, stype = anyelement);");
16 }
31dd0540
TGGM
17 db_query("UPDATE {system} SET weight = 10 WHERE name = 'views'");
18}
19
31dd0540 20/**
45a5d43e
DW
21 * Implements hook_schema().
22 *
23 * Generate the current version of the database schema from
24 * the sequence of schema update functions. Uses a similar
25 * method to install.inc's drupal_get_schema_versions() to
26 * establish the update sequence.
27 *
28 * To change the schema, add a new views_schema_N()
29 * function to match the associated views_update_N()
30 *
31 * @param $caller_function
32 * The name of the function that called us.
33 * Used internally, if requesting a specific schema version.
31dd0540 34 */
45a5d43e
DW
35function views_schema($caller_function = FALSE) {
36 static $get_current;
37 static $schemas = array();
38
39 // If called with no arguments, get the latest version of the schema.
40 if (!isset($get_current)) {
41 $get_current = $caller_function ? FALSE : TRUE;
42 }
43
44 // Generate a sorted list of available schema update functions.
45 if ($get_current || empty($schemas)) {
46 $get_current = FALSE;
47 $functions = get_defined_functions();
48 foreach ($functions['user'] as $function) {
49 if (strpos($function, 'views_schema_') === 0) {
50 $version = substr($function, strlen('views_schema_'));
51 if (is_numeric($version)) {
52 $schemas[] = $version;
53 }
54 }
55 }
56 if ($schemas) {
57 sort($schemas, SORT_NUMERIC);
58
59 // If a specific version was requested, drop any later
60 // updates from the sequence.
61 if ($caller_function) {
62 do {
63 $schema = array_pop($schemas);
64 } while ($schemas && $caller_function != 'views_schema_'. $schema);
65 }
66 }
67 }
68
69 // Call views_schema_<n>, for the highest available <n>.
70 if ($schema = array_pop($schemas)) {
71 $function = 'views_schema_'. $schema;
72 return $function();
73 }
74
75 return array();
31dd0540
TGGM
76}
77
78/**
45a5d43e
DW
79 * Views 2's initial schema.
80 * Called directly by views_update_6000() for updates from Drupal 5.
81 *
82 * Important: Do not edit this schema!
83 *
84 * Updates to the views schema must be provided as views_schema_6xxx() functions,
85 * which views_schema() automatically sees and applies. See below for examples.
86 *
87 * Please do document updates with comments in this function, however.
31dd0540 88 */
45a5d43e 89function views_schema_6000() {
31dd0540
TGGM
90 $schema['views_view'] = array(
91 'description' => 'Stores the general data for a view.',
ced80ad3
EM
92 'export' => array(
93 'identifier' => 'view',
94 'bulk export' => TRUE,
95 'primary key' => 'vid',
96 'default hook' => 'views_default_views',
97 'admin_title' => 'human_name',
98 'admin_description' => 'description',
99 'api' => array(
100 'owner' => 'views',
101 'api' => 'views_default',
102 'minimum_version' => '2',
103 'current_version' => '3.0',
104 ),
105 'object' => 'view',
106 // the callback to load the displays
107 'subrecords callback' => 'views_load_display_records',
108 // the variable that holds enabled/disabled status
109 'status' => 'views_defaults',
110 // CRUD callbacks
111 'create callback' => 'views_new_view',
112 'save callback' => 'views_save_view',
113 'delete callback' => 'views_delete_view',
114 'export callback' => 'views_export_view',
d5cc1829 115 'status callback' => 'views_export_status',
6034755f
EM
116 'cache defaults' => TRUE,
117 'default cache bin' => 'cache_views',
ced80ad3 118 ),
31dd0540
TGGM
119 'fields' => array(
120 'vid' => array(
121 'type' => 'serial',
122 'unsigned' => TRUE,
123 'not null' => TRUE,
124 'description' => 'The view ID of the field, defined by the database.',
125 'no export' => TRUE,
126 ),
127 'name' => array(
128 'type' => 'varchar',
129 'length' => '32',
130 'default' => '',
131 'not null' => TRUE,
132 '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.',
133 ),
134 'description' => array(
135 'type' => 'varchar',
136 'length' => '255',
137 'default' => '',
138 'description' => 'A description of the view for the admin interface.',
139 ),
140 'tag' => array(
141 'type' => 'varchar',
142 'length' => '255',
143 'default' => '',
144 'description' => 'A tag used to group/sort views in the admin interface',
145 ),
146 'view_php' => array(
147 'type' => 'blob',
148 'description' => 'A chunk of PHP code that can be used to provide modifications to the view prior to building.',
149 ),
150 'base_table' => array(
151 'type' => 'varchar',
45a5d43e 152 'length' => '32', // Updated to '64' in views_schema_6005()
31dd0540
TGGM
153 'default' => '',
154 'not null' => TRUE,
155 'description' => 'What table this view is based on, such as node, user, comment, or term.',
156 ),
157 'is_cacheable' => array(
158 'type' => 'int',
159 'default' => 0,
160 'size' => 'tiny',
161 'description' => 'A boolean to indicate whether or not this view may have its query cached.',
162 ),
163 ),
164 'primary key' => array('vid'),
45a5d43e 165 'unique key' => array('name' => array('name')), // Updated to 'unique keys' in views_schema_6003()
31dd0540
TGGM
166 );
167
168 $schema['views_display'] = array(
169 'description' => 'Stores information about each display attached to a view.',
170 'fields' => array(
171 'vid' => array(
172 'type' => 'int',
173 'unsigned' => TRUE,
174 'not null' => TRUE,
175 'default' => 0,
176 'description' => 'The view this display is attached to.',
177 'no export' => TRUE,
178 ),
179 'id' => array(
180 'type' => 'varchar',
181 'length' => '64',
182 'default' => '',
183 'not null' => TRUE,
184 '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.',
185 ),
186 'display_title' => array(
187 'type' => 'varchar',
188 'length' => '64',
189 'default' => '',
190 'not null' => TRUE,
191 'description' => 'The title of the display, viewable by the administrator.',
192 ),
193 'display_plugin' => array(
194 'type' => 'varchar',
195 'length' => '64',
196 'default' => '',
197 'not null' => TRUE,
198 'description' => 'The type of the display. Usually page, block or embed, but is pluggable so may be other things.',
199 ),
200 'position' => array(
201 'type' => 'int',
202 'default' => 0,
203 'description' => 'The order in which this display is loaded.',
204 ),
205 'display_options' => array(
45a5d43e 206 // Type corrected in update 6009
31dd0540
TGGM
207 'type' => 'blob',
208 'description' => 'A serialized array of options for this display; it contains options that are generally only pertinent to that display plugin type.',
209 'serialize' => TRUE,
210 'serialized default' => 'a:0:{}',
211 ),
212 ),
45a5d43e 213 // Added primary keys in views_schema_6008()
31dd0540
TGGM
214 'indexes' => array('vid' => array('vid', 'position')),
215 );
216
217 $schema['cache_views'] = drupal_get_schema_unprocessed('system', 'cache');
2dd76fe0 218
31dd0540
TGGM
219 $schema['views_object_cache'] = array(
220 'description' => 'A special cache used to store objects that are being edited; it serves to save state in an ordinarily stateless environment.',
221 'fields' => array(
222 'sid' => array(
223 'type' => 'varchar',
224 'length' => '64',
225 'description' => 'The session ID this cache object belongs to.',
226 ),
227 'name' => array(
228 'type' => 'varchar',
229 'length' => '32',
230 'description' => 'The name of the view this cache is attached to.',
231 ),
232 'obj' => array(
233 'type' => 'varchar',
234 'length' => '32',
235 '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.',
236 ),
237 'updated' => array(
238 'type' => 'int',
239 'unsigned' => TRUE,
240 'not null' => TRUE,
241 'default' => 0,
242 'description' => 'The time this cache was created or updated.',
243 ),
244 'data' => array(
45a5d43e 245 'type' => 'blob', // Updated to 'text' (with size => 'big') in views_schema_6004()
31dd0540
TGGM
246 'description' => 'Serialized data being stored.',
247 'serialize' => TRUE,
248 ),
249 ),
250 'indexes' => array(
251 'sid_obj_name' => array('sid', 'obj', 'name'),
252 'updated' => array('updated'),
253 ),
254 );
45a5d43e
DW
255
256 // $schema['cache_views_data'] added in views_schema_6006()
257
31dd0540
TGGM
258 return $schema;
259}
260
261/**
262 * Update a site to Drupal 6! Contains a bit of special code to detect
263 * if you've been running a beta version or something.
264 */
265function views_update_6000() {
31dd0540 266 if (db_table_exists('views_view')) {
09d99d77 267 return;
31dd0540
TGGM
268 }
269
270 // This has the beneficial effect of wiping out any Views 1 cache at the
271 // same time; not wiping that cache could easily cause problems with Views 2.
272 if (db_table_exists('cache_views')) {
09d99d77 273 db_drop_table('cache_views');
31dd0540
TGGM
274 }
275
276 // This is mostly the same as drupal_install_schema, but it forces
45a5d43e
DW
277 // views_schema_6000() rather than the default views_schema().
278 // This is important for processing subsequent table updates.
279 $schema = views_schema_6000();
c9b3013b 280 _drupal_schema_initialize($schema, 'views');
31dd0540
TGGM
281
282 foreach ($schema as $name => $table) {
09d99d77 283 db_create_table($name, $table);
31dd0540 284 }
31dd0540
TGGM
285}
286
45a5d43e
DW
287/**
288 * Remove '$' symbol in special blocks, as it is invalid for theming.
289 */
31dd0540 290function views_update_6001() {
31dd0540 291 $result = db_query("SELECT * FROM {blocks} WHERE module = 'views' AND delta LIKE '\$exp%'");
2dd76fe0 292 foreach ($result as $block) {
31dd0540 293 $new = strtr($block->delta, '$', '-');
09d99d77 294 update_sql("UPDATE {blocks} SET delta = '" . db_escape_string($new) . "' WHERE module = 'views' AND delta = '" . db_escape_string($block->delta) . "'");
31dd0540 295 }
09d99d77 296 update_sql("UPDATE {blocks} SET delta = CONCAT(delta, '-block_1') WHERE module = 'views'");
31dd0540
TGGM
297}
298
299// NOTE: Update 6002 removed because it did not always work.
45a5d43e
DW
300// Update 6004 implements the change correctly.
301
31dd0540
TGGM
302/**
303 * Add missing unique key.
304 */
45a5d43e
DW
305function views_schema_6003() {
306 $schema = views_schema(__FUNCTION__);
307 $schema['views_view']['unique keys'] = array('name' => array('name'));
308 unset($schema['views_view']['unique key']);
309 return $schema;
310}
31dd0540 311function views_update_6003() {
d75d41a3 312 db_add_unique_key('views_view', 'name', array('name'));
31dd0540
TGGM
313}
314
315/**
316 * Enlarge the views_object_cache.data column to prevent truncation and JS
317 * errors.
318 */
45a5d43e
DW
319function views_schema_6004() {
320 $schema = views_schema(__FUNCTION__);
321 $schema['views_object_cache']['fields']['data']['type'] = 'text';
322 $schema['views_object_cache']['fields']['data']['size'] = 'big';
323 return $schema;
324}
31dd0540 325function views_update_6004() {
31dd0540
TGGM
326 $new_field = array(
327 'type' => 'text',
328 'size' => 'big',
329 'description' => 'Serialized data being stored.',
330 'serialize' => TRUE,
331 );
332
333 // Drop and re-add this field because there is a bug in
334 // db_change_field that causes this to fail when trying to cast the data.
d75d41a3
DW
335 db_drop_field('views_object_cache', 'data');
336 db_add_field('views_object_cache', 'data', $new_field);
31dd0540
TGGM
337}
338
339/**
340 * Enlarge the base_table column
341 */
45a5d43e
DW
342function views_schema_6005() {
343 $schema = views_schema(__FUNCTION__);
344 $schema['views_view']['fields']['base_table']['length'] = 64;
345 return $schema;
346}
31dd0540 347function views_update_6005() {
31dd0540
TGGM
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 );
d75d41a3 355 db_change_field('views_view', 'base_table', 'base_table', $new_field);
31dd0540
TGGM
356}
357
358/**
359 * Add the cache_views_data table to support standard caching.
360 */
45a5d43e
DW
361function views_schema_6006() {
362 $schema = views_schema(__FUNCTION__);
363 $schema['cache_views_data'] = drupal_get_schema_unprocessed('system', 'cache');
364 $schema['cache_views_data']['description'] = 'Cache table for views to store pre-rendered queries, results, and display output.';
365 $schema['cache_views_data']['fields']['serialized']['default'] = 1;
366 return $schema;
367}
31dd0540 368function views_update_6006() {
31dd0540
TGGM
369 $table = drupal_get_schema_unprocessed('system', 'cache');
370 $table['description'] = 'Cache table for views to store pre-rendered queries, results, and display output.';
371 $table['fields']['serialized']['default'] = 1;
372
d75d41a3 373 db_create_table('cache_views_data', $table);
31dd0540
TGGM
374}
375
376/**
377 * Add aggregate function to PostgreSQL so GROUP BY can be used to force only
378 * one result to be returned for each item.
379 */
380function views_update_6007() {
7133bba5 381 if (Database::getConnection()->databaseType() == 'pgsql') {
09d99d77
DW
382 db_query('CREATE OR REPLACE FUNCTION first(anyelement, anyelement) RETURNS anyelement AS \'SELECT COALESCE($1, $2);\' LANGUAGE \'sql\';');
383 db_query("DROP AGGREGATE IF EXISTS first(anyelement)");
384 db_query("CREATE AGGREGATE first(sfunc = first, basetype = anyelement, stype = anyelement);");
31dd0540 385 }
31dd0540 386}
0dc102d6
EM
387
388/**
389 * Add the primary key to views_display table.
390 */
45a5d43e
DW
391function views_schema_6008() {
392 $schema = views_schema(__FUNCTION__);
393 $schema['views_display']['primary key'] = array('vid', 'id');
394 return $schema;
395}
396
b8e8a060
DW
397/**
398 * Add the primary key to the views_display table.
399 */
0dc102d6 400function views_update_6008() {
d75d41a3 401 db_add_primary_key('views_display', array('vid', 'id'));
0dc102d6 402}
b8e8a060
DW
403
404/**
405 * Enlarge the views_display.display_options field to accomodate a larger set
406 * of configurations (e. g. fields, filters, etc.) on a display.
407 */
408function views_schema_6009() {
409 $schema = views_schema(__FUNCTION__);
410 $schema['views_display']['fields']['display_options'] = array(
411 'type' => 'text',
412 'size' => 'big',
413 'description' => 'A serialized array of options for this display; it contains options that are generally only pertinent to that display plugin type.',
414 'serialize' => TRUE,
415 'serialized default' => 'a:0:{}',
416 );
417 return $schema;
418}
419
420function views_update_6009() {
b8e8a060 421 $schema = views_schema_6009();
24a3fa62 422
7133bba5 423 if (Database::getConnection()->databaseType() == 'pgsql') {
09d99d77 424 db_query('ALTER TABLE {views_display} RENAME "display_options" TO "display_options_old"');
d75d41a3 425 db_add_field('views_display', 'display_options', $schema['views_display']['fields']['display_options']);
24a3fa62
EM
426
427 $sql = "SELECT vid, id, display_options_old FROM {views_display}";
428 $result = db_query($sql);
d75d41a3 429 foreach ($result as $row) {
9ced4bc9 430 $row['display_options_old'] = $row['display_options_old'];
d75d41a3
DW
431 $sql = "UPDATE {views_display} SET display_options = :display_optons WHERE vid = :vid AND id = :id";
432 db_query($sql, array(
433 ':display_optons' => $row['display_options_old'],
434 ':vid' => $row['vid'],
435 ':id' => $row['id'],
436 ));
24a3fa62
EM
437 }
438
d75d41a3 439 db_drop_field('views_display', 'display_options_old');
24a3fa62
EM
440 }
441 else {
d75d41a3 442 db_change_field('views_display', 'display_options', 'display_options', $schema['views_display']['fields']['display_options']);
24a3fa62 443 }
24a3fa62 444}
fe4413b4
DW
445
446/**
447 * Remove the view_php field
448 */
449function views_schema_6010() {
450 $schema = views_schema(__FUNCTION__);
451 unset($schema['views_view']['fields']['view_php']);
452 unset($schema['views_view']['fields']['is_cacheable']);
453 return $schema;
454}
455
456/**
457 * Remove the view_php and is_cacheable field
458 */
459function views_update_6010() {
09d99d77
DW
460 db_drop_field('views_view', 'view_php');
461 db_drop_field('views_view', 'is_cacheable');
fe4413b4 462}
f0bb22cd
DW
463
464/**
465 * Remove views_object_cache table and move the data to ctools_object_cache.
466 */
467function views_schema_6011() {
468 $schema = views_schema(__FUNCTION__);
469 unset($schema['views_object_cache']);
470 return $schema;
471}
472
473/**
474 * Remove views_object_cache table and move the data to ctools_object_cache.
475 */
476function views_update_6011() {
e2a1e453 477 $caches = db_query("SELECT * FROM {views_object_cache}");
f0bb22cd
DW
478 foreach ($caches as $item) {
479 drupal_write_record('ctools_object_cache', $item);
480 }
09d99d77 481 db_drop_table('views_object_cache');
f0bb22cd 482}
793c0813
DW
483
484/**
485 * Correct the cache setting for exposed filter blocks.
486 *
487 * @see http://drupal.org/node/910864
488 */
489function views_update_6012() {
793c0813
DW
490 // There is only one simple query to run.
491 $update = db_update('blocks')
492 ->condition('module', 'views')
493 ->condition('delta', db_like('-exp-') . '%', 'LIKE')
494 ->fields(array('cache' => DRUPAL_NO_CACHE));
793c0813 495}
cb9425b2
DW
496
497
498/**
499 * Add a human readable name.
500 */
501function views_schema_6013() {
502 $schema = views_schema(__FUNCTION__);
503 $schema['views_view']['fields']['human_name'] = array(
504 'type' => 'varchar',
505 'length' => '255',
506 'default' => '',
507 'description' => 'A human readable name used to be displayed in the admin interface',
508 );
509 return $schema;
510}
511
512function views_update_6013() {
a63e6f37
DW
513 // Check because D6 installs may already have added this.
514 if (!db_field_exists('views_view', 'human_name')) {
cb9425b2 515
a63e6f37
DW
516 $new_field = array(
517 'type' => 'varchar',
518 'length' => '255',
519 'default' => '',
520 'description' => 'A human readable name used to be displayed in the admin interface',
521 );
522
523 db_add_field('views_view', 'human_name', $new_field);
524 }
cb9425b2 525}
d7bc4f2f
DW
526
527function views_schema_6014() {
528 $schema = views_schema(__FUNCTION__);
529 $schema['views_view']['fields']['core'] = array(
530 'type' => 'int',
531 'default' => 0,
532 'description' => 'Stores the drupal core version of the view.',
533 );
534 return $schema;
535}
536
537/**
538 * Add a drupal core version field.
539 */
540function views_update_6014() {
344b79a6
EM
541 // Check because D6 installs may already have added this.
542 if (!db_field_exists('views_view', 'core')) {
543 $new_field = array(
544 'type' => 'int',
545 'default' => 0,
546 'description' => 'Stores the drupal core version of the view.',
547 );
548 db_add_field('views_view', 'core', $new_field);
549 }
d7bc4f2f
DW
550}
551
d5e0737c
KS
552/**
553 * Rename some system variables.
554 */
555function views_update_7000() {
556 // Views now lets users turn off query details on live preview.
557 $query_on_top = variable_get('views_ui_query_on_top');
558 if (isset($query_on_top)) {
559 variable_set('views_ui_show_sql_query', TRUE);
560 if ($query_on_top) {
561 variable_set('views_ui_show_sql_query_where', 'above');
562 }
563 else {
564 variable_set('views_ui_show_sql_query_where', 'below');
565 }
566 variable_del('views_ui_query_on_top');
567 }
568
569 // Rename the views_hide_help_message variable from negative to positive.
570 $hide_help = variable_get('views_hide_help_message');
571 if (isset($hide_help)) {
572 variable_set('views_ui_show_advanced_help_warning', !$hide_help);
573 variable_del('views_hide_help_message');
574 }
575
015b94b5
BZ
576 // Rename the unused views_no_hover_links variable.
577 variable_del('views_no_hover_links');
d5e0737c 578}
77d4bd71
EM
579
580/**
581 * Fix missing items from Views administrative breadcrumb
582 */
583function views_update_7001() {
584 $depth = db_select('menu_links')
585 ->fields('menu_links', array('depth'))
586 ->condition('link_path', 'admin/structure/views/view/%')
587 ->execute()
588 ->fetchField();
589
590 if ($depth == 3) {
591 db_delete('menu_links')
592 ->condition('link_path', 'admin/structure/views/%', 'LIKE')
593 ->execute();
594 cache_clear_all(NULL, 'cache_menu');
595 menu_rebuild();
596 }
77d4bd71 597}
1eb36fb9
DW
598
599function views_schema_7300() {
600 return views_schema_6013();
601}
602/**
603 * Make sure the human_name field is added to the views_view table.
604 *
f9604821
DW
605 * If you updated from 6.x-2.x-dev to 7.x-3.x you already had schema
606 * version 6014, so update 6013 never was nor will be run. As a result,
607 * the human_name field is missing from the database.
1eb36fb9 608 *
f9604821 609 * This will add the human_name field if it doesn't already exist.
1eb36fb9
DW
610 */
611function views_update_7300() {
612 views_update_6013();
613}
25760946
DW
614
615function views_schema_7301() {
616 $schema = views_schema(__FUNCTION__);
617 $schema['views_view']['fields']['name']['length'] = 128;
618 return $schema;
619}
620
621/**
622 * Enlarge the name column
623 */
624function views_update_7301() {
625 $new_field = array(
626 'type' => 'varchar',
627 'length' => '128',
628 'default' => '',
629 'not null' => TRUE,
630 '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.',
631 );
632 db_change_field('views_view', 'name', 'name', $new_field);
633}