| [ Index ] |
PHP Cross Reference of Drupal 6 (yi-drupal) |
[Summary view] [Print] [Text view]
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 'indexes' => array('vid' => array('vid', 'position')), 194 ); 195 196 $schema['cache_views'] = drupal_get_schema_unprocessed('system', 'cache'); 197 198 $schema['views_object_cache'] = array( 199 'description' => 'A special cache used to store objects that are being edited; it serves to save state in an ordinarily stateless environment.', 200 'fields' => array( 201 'sid' => array( 202 'type' => 'varchar', 203 'length' => '64', 204 'description' => 'The session ID this cache object belongs to.', 205 ), 206 'name' => array( 207 'type' => 'varchar', 208 'length' => '32', 209 'description' => 'The name of the view this cache is attached to.', 210 ), 211 'obj' => array( 212 'type' => 'varchar', 213 'length' => '32', 214 '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.', 215 ), 216 'updated' => array( 217 'type' => 'int', 218 'unsigned' => TRUE, 219 'not null' => TRUE, 220 'default' => 0, 221 'description' => 'The time this cache was created or updated.', 222 ), 223 'data' => array( 224 'type' => 'blob', // Updated to 'text' (with size => 'big') in views_schema_6004() 225 'description' => 'Serialized data being stored.', 226 'serialize' => TRUE, 227 ), 228 ), 229 'indexes' => array( 230 'sid_obj_name' => array('sid', 'obj', 'name'), 231 'updated' => array('updated'), 232 ), 233 ); 234 235 // $schema['cache_views_data'] added in views_schema_6006() 236 237 return $schema; 238 } 239 240 /** 241 * Update a site to Drupal 6! Contains a bit of special code to detect 242 * if you've been running a beta version or something. 243 */ 244 function views_update_6000() { 245 $ret = array(); 246 if (db_table_exists('views_view')) { 247 return $ret; 248 } 249 250 // This has the beneficial effect of wiping out any Views 1 cache at the 251 // same time; not wiping that cache could easily cause problems with Views 2. 252 if (db_table_exists('cache_views')) { 253 db_drop_table($ret, 'cache_views'); 254 } 255 256 // This is mostly the same as drupal_install_schema, but it forces 257 // views_schema_6000() rather than the default views_schema(). 258 // This is important for processing subsequent table updates. 259 $schema = views_schema_6000(); 260 _drupal_initialize_schema('views', $schema); 261 262 foreach ($schema as $name => $table) { 263 db_create_table($ret, $name, $table); 264 } 265 return $ret; 266 } 267 268 /** 269 * Remove '$' symbol in special blocks, as it is invalid for theming. 270 */ 271 function views_update_6001() { 272 $ret = array(); 273 $result = db_query("SELECT * FROM {blocks} WHERE module = 'views' AND delta LIKE '\$exp%'"); 274 while ($block = db_fetch_object($result)) { 275 $new = strtr($block->delta, '$', '-'); 276 $ret[] = update_sql("UPDATE {blocks} SET delta = '" . db_escape_string($new) . "' WHERE module = 'views' AND delta = '" . db_escape_string($block->delta) . "'"); 277 } 278 $result = db_query("SELECT * FROM {blocks} WHERE module = 'views'"); 279 while ($block = db_fetch_object($result)) { 280 $new = $block->delta .= '-block_1'; 281 if (strlen($new) >= 32) { 282 $new = md5($new); 283 } 284 $ret[] = update_sql("UPDATE {blocks} SET delta = '$new' WHERE bid = $block->bid"); 285 } 286 287 return $ret; 288 } 289 290 // NOTE: Update 6002 removed because it did not always work. 291 // Update 6004 implements the change correctly. 292 293 /** 294 * Add missing unique key. 295 */ 296 function views_schema_6003() { 297 $schema = views_schema(__FUNCTION__); 298 $schema['views_view']['unique keys'] = array('name' => array('name')); 299 unset($schema['views_view']['unique key']); 300 return $schema; 301 } 302 function views_update_6003() { 303 $ret = array(); 304 db_add_unique_key($ret, 'views_view', 'name', array('name')); 305 return $ret; 306 } 307 308 /** 309 * Enlarge the views_object_cache.data column to prevent truncation and JS 310 * errors. 311 */ 312 function views_schema_6004() { 313 $schema = views_schema(__FUNCTION__); 314 $schema['views_object_cache']['fields']['data']['type'] = 'text'; 315 $schema['views_object_cache']['fields']['data']['size'] = 'big'; 316 return $schema; 317 } 318 function views_update_6004() { 319 $ret = array(); 320 321 $new_field = array( 322 'type' => 'text', 323 'size' => 'big', 324 'description' => 'Serialized data being stored.', 325 'serialize' => TRUE, 326 ); 327 328 // Drop and re-add this field because there is a bug in 329 // db_change_field that causes this to fail when trying to cast the data. 330 db_drop_field($ret, 'views_object_cache', 'data'); 331 db_add_field($ret, 'views_object_cache', 'data', $new_field); 332 333 return $ret; 334 } 335 336 /** 337 * Enlarge the base_table column 338 */ 339 function views_schema_6005() { 340 $schema = views_schema(__FUNCTION__); 341 $schema['views_view']['fields']['base_table']['length'] = 64; 342 return $schema; 343 } 344 function views_update_6005() { 345 $ret = array(); 346 347 $new_field = array( 348 'type' => 'varchar', 349 'length' => '64', 350 'default' => '', 351 'not null' => TRUE, 352 'description' => 'What table this view is based on, such as node, user, comment, or term.', 353 ); 354 db_change_field($ret, 'views_view', 'base_table', 'base_table', $new_field); 355 return $ret; 356 } 357 358 /** 359 * Add the cache_views_data table to support standard caching. 360 */ 361 function 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 } 368 function views_update_6006() { 369 $ret = array(); 370 371 $table = drupal_get_schema_unprocessed('system', 'cache'); 372 $table['description'] = 'Cache table for views to store pre-rendered queries, results, and display output.'; 373 $table['fields']['serialized']['default'] = 1; 374 375 db_create_table($ret, 'cache_views_data', $table); 376 377 return $ret; 378 } 379 380 /** 381 * Add aggregate function to PostgreSQL so GROUP BY can be used to force only 382 * one result to be returned for each item. 383 */ 384 function views_update_6007() { 385 $ret = array(); 386 if ($GLOBALS['db_type'] == 'pgsql') { 387 $ret[] = update_sql('CREATE OR REPLACE FUNCTION first(anyelement, anyelement) RETURNS anyelement AS \'SELECT COALESCE($1, $2);\' LANGUAGE \'sql\';'); 388 $ret[] = update_sql("DROP AGGREGATE IF EXISTS first(anyelement)"); 389 $ret[] = update_sql("CREATE AGGREGATE first(sfunc = first, basetype = anyelement, stype = anyelement);"); 390 } 391 return $ret; 392 } 393 394 /** 395 * Add the primary key to views_display table. 396 */ 397 function views_schema_6008() { 398 $schema = views_schema(__FUNCTION__); 399 $schema['views_display']['primary key'] = array('vid', 'id'); 400 return $schema; 401 } 402 403 /** 404 * Add the primary key to the views_display table. 405 */ 406 function views_update_6008() { 407 $ret = array(); 408 409 db_add_primary_key($ret, 'views_display', array('vid', 'id')); 410 411 return $ret; 412 } 413 414 /** 415 * Enlarge the views_display.display_options field to accomodate a larger set 416 * of configurations (e. g. fields, filters, etc.) on a display. 417 */ 418 function views_schema_6009() { 419 $schema = views_schema(__FUNCTION__); 420 $schema['views_display']['fields']['display_options'] = array( 421 'type' => 'text', 422 'size' => 'big', 423 'description' => 'A serialized array of options for this display; it contains options that are generally only pertinent to that display plugin type.', 424 'serialize' => TRUE, 425 'serialized default' => 'a:0:{}', 426 ); 427 return $schema; 428 } 429 430 function views_update_6009() { 431 $ret = array(); 432 433 $schema = views_schema_6009(); 434 435 if ($GLOBALS['db_type'] == 'pgsql') { 436 $ret[] = update_sql('ALTER TABLE {views_display} RENAME "display_options" TO "display_options_old"'); 437 db_add_field($ret, 'views_display', 'display_options', $schema['views_display']['fields']['display_options']); 438 439 $sql = "SELECT vid, id, display_options_old FROM {views_display}"; 440 $result = db_query($sql); 441 while ($row = db_fetch_array($result)) { 442 $row['display_options_old'] = db_decode_blob($row['display_options_old']); 443 $sql = "UPDATE {views_display} SET display_options = '%s' WHERE vid = %d AND id = '%s'"; 444 db_query($sql, $row['display_options_old'], $row['vid'], $row['id']); 445 } 446 447 db_drop_field($ret, 'views_display', 'display_options_old'); 448 } 449 else { 450 db_change_field($ret, 'views_display', 'display_options', 'display_options', $schema['views_display']['fields']['display_options']); 451 } 452 453 return $ret; 454 } 455 456 /** 457 * Remove the view_php field 458 */ 459 function views_schema_6010() { 460 $schema = views_schema(__FUNCTION__); 461 unset($schema['views_view']['fields']['view_php']); 462 unset($schema['views_view']['fields']['is_cacheable']); 463 return $schema; 464 } 465 466 /** 467 * Remove the view_php and is_cacheable field 468 */ 469 function views_update_6010() { 470 $ret = array(); 471 472 db_drop_field($ret, 'views_view', 'view_php'); 473 db_drop_field($ret, 'views_view', 'is_cacheable'); 474 475 476 return $ret; 477 } 478 479 /** 480 * Correct the cache setting for exposed filter blocks. 481 * 482 * @see http://drupal.org/node/910864 483 */ 484 function views_update_6011() { 485 $ret = array(); 486 487 // There is only one simple query to run. 488 $ret[] = update_sql("UPDATE {blocks} SET cache = " . BLOCK_NO_CACHE . " WHERE module = 'views' AND delta LIKE '-exp-%'"); 489 490 return $ret; 491 } 492 493 function views_schema_6013() { 494 $schema = views_schema(__FUNCTION__); 495 $schema['views_view']['fields']['core'] = array( 496 'type' => 'int', 497 'default' => 0, 498 'description' => 'Stores the drupal core version of the view.', 499 ); 500 return $schema; 501 } 502 503 /** 504 * Add a drupal core version field. 505 */ 506 function views_update_6013() { 507 $ret = array(); 508 $new_field = array( 509 'type' => 'int', 510 'default' => 0, 511 'description' => 'Stores the drupal core version of the view.', 512 ); 513 514 db_add_field($ret, 'views_view', 'core', $new_field); 515 516 return $ret; 517 } 518
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Mon Jul 9 18:01:44 2012 | Cross-referenced by PHPXref 0.7 |