4 * Contains install and update functions for Views.
8 * Implements hook_install().
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);");
16 db_query("UPDATE {system} SET weight = 10 WHERE name = 'views'");
20 * Implements hook_schema().
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.
27 * To change the schema, add a new views_schema_N()
28 * function to match the associated views_update_N()
30 * @param $caller_function
31 * The name of the function that called us.
32 * Used internally, if requesting a specific schema version.
34 function views_schema($caller_function = FALSE
) {
36 static
$schemas = array();
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
;
43 // Generate a sorted list of available schema update functions.
44 if ($get_current || empty($schemas)) {
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;
56 sort($schemas, SORT_NUMERIC
);
58 // If a specific version was requested, drop any later
59 // updates from the sequence.
60 if ($caller_function) {
62 $schema = array_pop($schemas);
63 } while ($schemas && $caller_function != 'views_schema_'.
$schema);
68 // Call views_schema_<n>, for the highest available <n>.
69 if ($schema = array_pop($schemas)) {
70 $function = 'views_schema_'.
$schema;
78 * Views 2's initial schema.
79 * Called directly by views_update_6000() for updates from Drupal 5.
81 * Important: Do not edit this schema!
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.
86 * Please do document updates with comments in this function, however.
88 function views_schema_6000() {
89 $schema['views_view'] = array(
90 'description' => 'Stores the general data for a view.',
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',
100 'api' => 'views_default',
101 'minimum_version' => '2',
102 'current_version' => '3.0',
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',
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',
122 'description' => 'The view ID of the field, defined by the database.',
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.',
132 'description' => array(
136 'description' => 'A description of the view for the admin interface.',
142 'description' => 'A tag used to group/sort views in the admin interface',
146 'description' => 'A chunk of PHP code that can be used to provide modifications to the view prior to building.',
148 'base_table' => array(
150 'length' => '32', // Updated to '64' in views_schema_6005()
153 'description' => 'What table this view is based on, such as node, user, comment, or term.',
155 'is_cacheable' => array(
159 'description' => 'A boolean to indicate whether or not this view may have its query cached.',
162 'primary key' => array('vid'),
163 'unique key' => array('name' => array('name')), // Updated to 'unique keys' in views_schema_6003()
166 $schema['views_display'] = array(
167 'description' => 'Stores information about each display attached to a view.',
174 'description' => 'The view this display is attached to.',
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.',
184 'display_title' => array(
189 'description' => 'The title of the display, viewable by the administrator.',
191 'display_plugin' => array(
196 'description' => 'The type of the display. Usually page, block or embed, but is pluggable so may be other things.',
201 'description' => 'The order in which this display is loaded.',
203 'display_options' => array(
204 // Type corrected in update 6009
206 'description' => 'A serialized array of options for this display; it contains options that are generally only pertinent to that display plugin type.',
208 'serialized default' => 'a:0:{}',
211 // Added primary keys in views_schema_6008()
212 'indexes' => array('vid' => array('vid', 'position')),
215 $schema['cache_views'] = drupal_get_schema_unprocessed('system', 'cache');
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.',
223 'description' => 'The session ID this cache object belongs to.',
228 'description' => 'The name of the view this cache is attached to.',
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.',
240 'description' => 'The time this cache was created or updated.',
243 'type' => 'blob', // Updated to 'text' (with size => 'big') in views_schema_6004()
244 'description' => 'Serialized data being stored.',
249 'sid_obj_name' => array('sid', 'obj', 'name'),
250 'updated' => array('updated'),
254 // $schema['cache_views_data'] added in views_schema_6006()
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.
263 function views_update_6000() {
264 if (db_table_exists('views_view')) {
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');
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');
280 foreach ($schema as
$name => $table) {
281 db_create_table($name, $table);
286 * Remove '$' symbol in special blocks, as it is invalid for theming.
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
) .
"'");
294 update_sql("UPDATE {blocks} SET delta = CONCAT(delta, '-block_1') WHERE module = 'views'");
297 // NOTE: Update 6002 removed because it did not always work.
298 // Update 6004 implements the change correctly.
301 * Add missing unique key.
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']);
309 function views_update_6003() {
310 db_add_unique_key('views_view', 'name', array('name'));
314 * Enlarge the views_object_cache.data column to prevent truncation and JS
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';
323 function views_update_6004() {
327 'description' => 'Serialized data being stored.',
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);
338 * Enlarge the base_table column
340 function views_schema_6005() {
341 $schema = views_schema(__FUNCTION__
);
342 $schema['views_view']['fields']['base_table']['length'] = 64;
345 function views_update_6005() {
351 'description' => 'What table this view is based on, such as node, user, comment, or term.',
353 db_change_field('views_view', 'base_table', 'base_table', $new_field);
357 * Add the cache_views_data table to support standard caching.
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;
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;
371 db_create_table('cache_views_data', $table);
375 * Add aggregate function to PostgreSQL so GROUP BY can be used to force only
376 * one result to be returned for each item.
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);");
387 * Add the primary key to views_display table.
389 function views_schema_6008() {
390 $schema = views_schema(__FUNCTION__
);
391 $schema['views_display']['primary key'] = array('vid', 'id');
396 * Add the primary key to the views_display table.
398 function views_update_6008() {
399 db_add_primary_key('views_display', array('vid', 'id'));
403 * Enlarge the views_display.display_options field to accomodate a larger set
404 * of configurations (e. g. fields, filters, etc.) on a display.
406 function views_schema_6009() {
407 $schema = views_schema(__FUNCTION__
);
408 $schema['views_display']['fields']['display_options'] = array(
411 'description' => 'A serialized array of options for this display; it contains options that are generally only pertinent to that display plugin type.',
413 'serialized default' => 'a:0:{}',
418 function views_update_6009() {
419 $schema = views_schema_6009();
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']);
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'],
437 db_drop_field('views_display', 'display_options_old');
440 db_change_field('views_display', 'display_options', 'display_options', $schema['views_display']['fields']['display_options']);
445 * Remove the view_php field
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']);
455 * Remove the view_php and is_cacheable field
457 function views_update_6010() {
458 db_drop_field('views_view', 'view_php');
459 db_drop_field('views_view', 'is_cacheable');
463 * Remove views_object_cache table and move the data to ctools_object_cache.
465 function views_schema_6011() {
466 $schema = views_schema(__FUNCTION__
);
467 unset($schema['views_object_cache']);
472 * Remove views_object_cache table and move the data to ctools_object_cache.
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);
479 db_drop_table('views_object_cache');
483 * Correct the cache setting for exposed filter blocks.
485 * @see http://drupal.org/node/910864
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
));
497 * Add a human readable name.
499 function views_schema_6013() {
500 $schema = views_schema(__FUNCTION__
);
501 $schema['views_view']['fields']['human_name'] = array(
505 'description' => 'A human readable name used to be displayed in the admin interface',
510 function views_update_6013() {
511 // Check because D6 installs may already have added this.
512 if (!db_field_exists('views_view', 'human_name')) {
518 'description' => 'A human readable name used to be displayed in the admin interface',
521 db_add_field('views_view', 'human_name', $new_field);
525 function views_schema_6014() {
526 $schema = views_schema(__FUNCTION__
);
527 $schema['views_view']['fields']['core'] = array(
530 'description' => 'Stores the drupal core version of the view.',
536 * Add a drupal core version field.
538 function views_update_6014() {
539 // Check because D6 installs may already have added this.
540 if (!db_field_exists('views_view', 'core')) {
544 'description' => 'Stores the drupal core version of the view.',
546 db_add_field('views_view', 'core', $new_field);
551 * Rename some system variables.
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
);
559 variable_set('views_ui_show_sql_query_where', 'above');
562 variable_set('views_ui_show_sql_query_where', 'below');
564 variable_del('views_ui_query_on_top');
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');
574 // Rename the unused views_no_hover_links variable.
575 variable_del('views_no_hover_links');
579 * Fix missing items from Views administrative breadcrumb
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/%')
589 db_delete('menu_links')
590 ->condition('link_path', 'admin/structure/views/%', 'LIKE')
592 cache_clear_all(NULL
, 'cache_menu');
597 function views_schema_7300() {
598 return views_schema_6013();
601 * Make sure the human_name field is added to the views_view table.
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.
607 * This will add the human_name field if it doesn't already exist.
609 function views_update_7300() {
613 function views_schema_7301() {
614 $schema = views_schema(__FUNCTION__
);
615 $schema['views_view']['fields']['name']['length'] = 128;
620 * Enlarge the name column
622 function views_update_7301() {
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.',
630 db_change_field('views_view', 'name', 'name', $new_field);