4 * Contains install and update functions for Views.
8 * Implementation of hook_install().
10 function views_install() {
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);");
16 drupal_install_schema('views');
17 db_query("UPDATE {system} SET weight = 10 WHERE name = 'views'");
21 * Implementation of hook_uninstall().
23 function views_uninstall() {
24 drupal_uninstall_schema('views');
28 * Implementation of hook_schema().
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.
35 * To change the schema, add a new views_schema_N()
36 * function to match the associated views_update_N()
38 * @param $caller_function
39 * The name of the function that called us.
40 * Used internally, if requesting a specific schema version.
42 function views_schema($caller_function = FALSE
) {
44 static
$schemas = array();
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
;
51 // Generate a sorted list of available schema update functions.
52 if ($get_current || empty($schemas)) {
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;
64 sort($schemas, SORT_NUMERIC
);
66 // If a specific version was requested, drop any later
67 // updates from the sequence.
68 if ($caller_function) {
70 $schema = array_pop($schemas);
71 } while ($schemas && $caller_function != 'views_schema_'.
$schema);
76 // Call views_schema_<n>, for the highest available <n>.
77 if ($schema = array_pop($schemas)) {
78 $function = 'views_schema_'.
$schema;
86 * Views 2's initial schema.
87 * Called directly by views_update_6000() for updates from Drupal 5.
89 * Important: Do not edit this schema!
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.
94 * Please do document updates with comments in this function, however.
96 function views_schema_6000() {
97 $schema['views_view'] = array(
98 'description' => 'Stores the general data for a view.',
104 'description' => 'The view ID of the field, defined by the database.',
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.',
114 'description' => array(
118 'description' => 'A description of the view for the admin interface.',
124 'description' => 'A tag used to group/sort views in the admin interface',
128 'description' => 'A chunk of PHP code that can be used to provide modifications to the view prior to building.',
130 'base_table' => array(
132 'length' => '32', // Updated to '64' in views_schema_6005()
135 'description' => 'What table this view is based on, such as node, user, comment, or term.',
137 'is_cacheable' => array(
141 'description' => 'A boolean to indicate whether or not this view may have its query cached.',
144 'primary key' => array('vid'),
145 'unique key' => array('name' => array('name')), // Updated to 'unique keys' in views_schema_6003()
148 $schema['views_display'] = array(
149 'description' => 'Stores information about each display attached to a view.',
156 'description' => 'The view this display is attached to.',
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.',
166 'display_title' => array(
171 'description' => 'The title of the display, viewable by the administrator.',
173 'display_plugin' => array(
178 'description' => 'The type of the display. Usually page, block or embed, but is pluggable so may be other things.',
183 'description' => 'The order in which this display is loaded.',
185 'display_options' => array(
186 // Type corrected in update 6009
188 'description' => 'A serialized array of options for this display; it contains options that are generally only pertinent to that display plugin type.',
190 'serialized default' => 'a:0:{}',
193 // Added primary keys in views_schema_6008()
194 'indexes' => array('vid' => array('vid', 'position')),
197 $schema['cache_views'] = drupal_get_schema_unprocessed('system', 'cache');
199 $schema['views_object_cache'] = array(
200 'description' => 'A special cache used to store objects that are being edited; it serves to save state in an ordinarily stateless environment.',
205 'description' => 'The session ID this cache object belongs to.',
210 'description' => 'The name of the view this cache is attached to.',
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.',
222 'description' => 'The time this cache was created or updated.',
225 'type' => 'blob', // Updated to 'text' (with size => 'big') in views_schema_6004()
226 'description' => 'Serialized data being stored.',
231 'sid_obj_name' => array('sid', 'obj', 'name'),
232 'updated' => array('updated'),
236 // $schema['cache_views_data'] added in views_schema_6006()
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.
245 function views_update_6000() {
247 if (db_table_exists('views_view')) {
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');
257 // This is mostly the same as drupal_install_schema, but it forces
258 // views_schema_6000() rather than the default views_schema().
259 // This is important for processing subsequent table updates.
260 $schema = views_schema_6000();
261 _drupal_initialize_schema('views', $schema);
263 foreach ($schema as
$name => $table) {
264 db_create_table($ret, $name, $table);
270 * Remove '$' symbol in special blocks, as it is invalid for theming.
272 function views_update_6001() {
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
) .
"'");
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) {
285 $ret[] = update_sql("UPDATE {blocks} SET delta = '$new' WHERE bid = $block->bid");
291 // NOTE: Update 6002 removed because it did not always work.
292 // Update 6004 implements the change correctly.
295 * Add missing unique key.
297 function 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']);
303 function views_update_6003() {
305 db_add_unique_key($ret, 'views_view', 'name', array('name'));
310 * Enlarge the views_object_cache.data column to prevent truncation and JS
313 function 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';
319 function views_update_6004() {
325 'description' => 'Serialized data being stored.',
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);
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() {
353 'description' => 'What table this view is based on, such as node, user, comment, or term.',
355 db_change_field($ret, 'views_view', 'base_table', 'base_table', $new_field);
360 * Add the cache_views_data table to support standard caching.
362 function 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;
369 function views_update_6006() {
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;
376 db_create_table($ret, 'cache_views_data', $table);
382 * Add aggregate function to PostgreSQL so GROUP BY can be used to force only
383 * one result to be returned for each item.
385 function views_update_6007() {
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);");
396 * Add the primary key to views_display table.
398 function views_schema_6008() {
399 $schema = views_schema(__FUNCTION__
);
400 $schema['views_display']['primary key'] = array('vid', 'id');
405 * Add the primary key to the views_display table.
407 function views_update_6008() {
410 db_add_primary_key($ret, 'views_display', array('vid', 'id'));
416 * Enlarge the views_display.display_options field to accomodate a larger set
417 * of configurations (e. g. fields, filters, etc.) on a display.
419 function views_schema_6009() {
420 $schema = views_schema(__FUNCTION__
);
421 $schema['views_display']['fields']['display_options'] = array(
424 'description' => 'A serialized array of options for this display; it contains options that are generally only pertinent to that display plugin type.',
426 'serialized default' => 'a:0:{}',
431 function views_update_6009() {
434 $schema = views_schema_6009();
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']);
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']);
448 db_drop_field($ret, 'views_display', 'display_options_old');
451 db_change_field($ret, 'views_display', 'display_options', 'display_options', $schema['views_display']['fields']['display_options']);
458 * Remove the view_php field
460 function views_schema_6010() {
461 $schema = views_schema(__FUNCTION__
);
462 unset($schema['views_view']['fields']['view_php']);
463 unset($schema['views_view']['fields']['is_cacheable']);
468 * Remove the view_php and is_cacheable field
470 function views_update_6010() {
473 db_drop_field($ret, 'views_view', 'view_php');
474 db_drop_field($ret, 'views_view', 'is_cacheable');
481 * Correct the cache setting for exposed filter blocks.
483 * @see http://drupal.org/node/910864
485 function views_update_6011() {
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-%'");
496 * Add a human readable name.
498 function views_schema_6012() {
499 $schema = views_schema(__FUNCTION__
);
500 $schema['views_view']['fields']['human_name'] = array(
504 'description' => 'A human readable name used to be displayed in the admin interface',
509 function views_update_6012() {
516 'description' => 'A human readable name used to be displayed in the admin interface',
519 db_add_field($ret, 'views_view', 'human_name', $new_field);
524 function views_schema_6013() {
525 $schema = views_schema(__FUNCTION__
);
526 $schema['views_view']['fields']['core'] = array(
529 'description' => 'Stores the drupal core version of the view.',
535 * Add a drupal core version field.
537 function views_update_6013() {
542 'description' => 'Stores the drupal core version of the view.',
545 db_add_field($ret, 'views_view', 'core', $new_field);