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