#1302494 by defr: Don't alter the value_options directly for flatten the options...
[project/views.git] / views.install
1 <?php
2 /**
3 * @file views.install
4 * Contains install and update functions for Views.
5 */
6
7 /**
8 * Implementation of hook_install().
9 */
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);");
15 }
16 drupal_install_schema('views');
17 db_query("UPDATE {system} SET weight = 10 WHERE name = 'views'");
18 }
19
20 /**
21 * Implementation of hook_uninstall().
22 */
23 function views_uninstall() {
24 drupal_uninstall_schema('views');
25 }
26
27 /**
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.
41 */
42 function 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();
83 }
84
85 /**
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.
95 */
96 function views_schema_6000() {
97 $schema['views_view'] = array(
98 'description' => 'Stores the general data for a view.',
99 'fields' => array(
100 'vid' => array(
101 'type' => 'serial',
102 'unsigned' => TRUE,
103 'not null' => TRUE,
104 'description' => 'The view ID of the field, defined by the database.',
105 'no export' => TRUE,
106 ),
107 'name' => array(
108 'type' => 'varchar',
109 'length' => '32',
110 'default' => '',
111 'not null' => TRUE,
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.',
113 ),
114 'description' => array(
115 'type' => 'varchar',
116 'length' => '255',
117 'default' => '',
118 'description' => 'A description of the view for the admin interface.',
119 ),
120 'tag' => array(
121 'type' => 'varchar',
122 'length' => '255',
123 'default' => '',
124 'description' => 'A tag used to group/sort views in the admin interface',
125 ),
126 'view_php' => array(
127 'type' => 'blob',
128 'description' => 'A chunk of PHP code that can be used to provide modifications to the view prior to building.',
129 ),
130 'base_table' => array(
131 'type' => 'varchar',
132 'length' => '32', // Updated to '64' in views_schema_6005()
133 'default' => '',
134 'not null' => TRUE,
135 'description' => 'What table this view is based on, such as node, user, comment, or term.',
136 ),
137 'is_cacheable' => array(
138 'type' => 'int',
139 'default' => 0,
140 'size' => 'tiny',
141 'description' => 'A boolean to indicate whether or not this view may have its query cached.',
142 ),
143 ),
144 'primary key' => array('vid'),
145 'unique key' => array('name' => array('name')), // Updated to 'unique keys' in views_schema_6003()
146 );
147
148 $schema['views_display'] = array(
149 'description' => 'Stores information about each display attached to a view.',
150 'fields' => array(
151 'vid' => array(
152 'type' => 'int',
153 'unsigned' => TRUE,
154 'not null' => TRUE,
155 'default' => 0,
156 'description' => 'The view this display is attached to.',
157 'no export' => TRUE,
158 ),
159 'id' => array(
160 'type' => 'varchar',
161 'length' => '64',
162 'default' => '',
163 'not null' => TRUE,
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.',
165 ),
166 'display_title' => array(
167 'type' => 'varchar',
168 'length' => '64',
169 'default' => '',
170 'not null' => TRUE,
171 'description' => 'The title of the display, viewable by the administrator.',
172 ),
173 'display_plugin' => array(
174 'type' => 'varchar',
175 'length' => '64',
176 'default' => '',
177 'not null' => TRUE,
178 'description' => 'The type of the display. Usually page, block or embed, but is pluggable so may be other things.',
179 ),
180 'position' => array(
181 'type' => 'int',
182 'default' => 0,
183 'description' => 'The order in which this display is loaded.',
184 ),
185 'display_options' => array(
186 // Type corrected in update 6009
187 'type' => 'blob',
188 'description' => 'A serialized array of options for this display; it contains options that are generally only pertinent to that display plugin type.',
189 'serialize' => TRUE,
190 'serialized default' => 'a:0:{}',
191 ),
192 ),
193 // Added primary keys in views_schema_6008()
194 'indexes' => array('vid' => array('vid', 'position')),
195 );
196
197 $schema['cache_views'] = drupal_get_schema_unprocessed('system', 'cache');
198
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.',
201 'fields' => array(
202 'sid' => array(
203 'type' => 'varchar',
204 'length' => '64',
205 'description' => 'The session ID this cache object belongs to.',
206 ),
207 'name' => array(
208 'type' => 'varchar',
209 'length' => '32',
210 'description' => 'The name of the view this cache is attached to.',
211 ),
212 'obj' => array(
213 'type' => 'varchar',
214 'length' => '32',
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.',
216 ),
217 'updated' => array(
218 'type' => 'int',
219 'unsigned' => TRUE,
220 'not null' => TRUE,
221 'default' => 0,
222 'description' => 'The time this cache was created or updated.',
223 ),
224 'data' => array(
225 'type' => 'blob', // Updated to 'text' (with size => 'big') in views_schema_6004()
226 'description' => 'Serialized data being stored.',
227 'serialize' => TRUE,
228 ),
229 ),
230 'indexes' => array(
231 'sid_obj_name' => array('sid', 'obj', 'name'),
232 'updated' => array('updated'),
233 ),
234 );
235
236 // $schema['cache_views_data'] added in views_schema_6006()
237
238 return $schema;
239 }
240
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 */
245 function views_update_6000() {
246 $ret = array();
247 if (db_table_exists('views_view')) {
248 return $ret;
249 }
250
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
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);
262
263 foreach ($schema as $name => $table) {
264 db_create_table($ret, $name, $table);
265 }
266 return $ret;
267 }
268
269 /**
270 * Remove '$' symbol in special blocks, as it is invalid for theming.
271 */
272 function 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 }
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 }
285 $ret[] = update_sql("UPDATE {blocks} SET delta = '$new' WHERE bid = $block->bid");
286 }
287
288 return $ret;
289 }
290
291 // NOTE: Update 6002 removed because it did not always work.
292 // Update 6004 implements the change correctly.
293
294 /**
295 * Add missing unique key.
296 */
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']);
301 return $schema;
302 }
303 function views_update_6003() {
304 $ret = array();
305 db_add_unique_key($ret, 'views_view', 'name', array('name'));
306 return $ret;
307 }
308
309 /**
310 * Enlarge the views_object_cache.data column to prevent truncation and JS
311 * errors.
312 */
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';
317 return $schema;
318 }
319 function views_update_6004() {
320 $ret = array();
321
322 $new_field = array(
323 'type' => 'text',
324 'size' => 'big',
325 'description' => 'Serialized data being stored.',
326 'serialize' => TRUE,
327 );
328
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);
333
334 return $ret;
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 $ret = array();
347
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 }
358
359 /**
360 * Add the cache_views_data table to support standard caching.
361 */
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;
367 return $schema;
368 }
369 function views_update_6006() {
370 $ret = array();
371
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 }
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 */
385 function views_update_6007() {
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 }
394
395 /**
396 * Add the primary key to views_display table.
397 */
398 function 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 */
407 function views_update_6008() {
408 $ret = array();
409
410 db_add_primary_key($ret, 'views_display', array('vid', 'id'));
411
412 return $ret;
413 }
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 */
419 function 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
431 function views_update_6009() {
432 $ret = array();
433
434 $schema = views_schema_6009();
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 }
453
454 return $ret;
455 }
456
457 /**
458 * Remove the view_php field
459 */
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']);
464 return $schema;
465 }
466
467 /**
468 * Remove the view_php and is_cacheable field
469 */
470 function 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 }
479
480 /**
481 * Correct the cache setting for exposed filter blocks.
482 *
483 * @see http://drupal.org/node/910864
484 */
485 function 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-%'");
490
491 return $ret;
492 }
493
494
495 /**
496 * Add a human readable name.
497 */
498 function 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
509 function 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);
520
521 return $ret;
522 }
523
524 function 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 */
537 function 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