| [ Index ] |
PHP Cross Reference of Drupal 6 (gatewave) |
[Summary view] [Print] [Text view]
1 <?php 2 // $Id: views.install,v 1.50.2.4 2010/06/16 20:37:29 merlinofchaos Exp $ 3 /** 4 * @file views.install 5 * Contains install and update functions for Views. 6 */ 7 8 /** 9 * Implementation of hook_install(). 10 */ 11 function views_install() { 12 if ($GLOBALS['db_type'] == 'pgsql') { 13 db_query('CREATE OR REPLACE FUNCTION first(anyelement, anyelement) RETURNS anyelement AS \'SELECT COALESCE($1, $2);\' LANGUAGE \'sql\';'); 14 db_query("DROP AGGREGATE IF EXISTS first(anyelement)"); 15 db_query("CREATE AGGREGATE first(sfunc = first, basetype = anyelement, stype = anyelement);"); 16 } 17 drupal_install_schema('views'); 18 db_query("UPDATE {system} SET weight = 10 WHERE name = 'views'"); 19 } 20 21 /** 22 * Implementation of hook_uninstall(). 23 */ 24 function views_uninstall() { 25 drupal_uninstall_schema('views'); 26 } 27 28 /** 29 * Implementation of hook_schema(). 30 * 31 * Generate the current version of the database schema from 32 * the sequence of schema update functions. Uses a similar 33 * method to install.inc's drupal_get_schema_versions() to 34 * establish the update sequence. 35 * 36 * To change the schema, add a new views_schema_N() 37 * function to match the associated views_update_N() 38 * 39 * @param $caller_function 40 * The name of the function that called us. 41 * Used internally, if requesting a specific schema version. 42 */ 43 function views_schema($caller_function = FALSE) { 44 static $get_current; 45 static $schemas = array(); 46 47 // If called with no arguments, get the latest version of the schema. 48 if (!isset($get_current)) { 49 $get_current = $caller_function ? FALSE : TRUE; 50 } 51 52 // Generate a sorted list of available schema update functions. 53 if ($get_current || empty($schemas)) { 54 $get_current = FALSE; 55 $functions = get_defined_functions(); 56 foreach ($functions['user'] as $function) { 57 if (strpos($function, 'views_schema_') === 0) { 58 $version = substr($function, strlen('views_schema_')); 59 if (is_numeric($version)) { 60 $schemas[] = $version; 61 } 62 } 63 } 64 if ($schemas) { 65 sort($schemas, SORT_NUMERIC); 66 67 // If a specific version was requested, drop any later 68 // updates from the sequence. 69 if ($caller_function) { 70 do { 71 $schema = array_pop($schemas); 72 } while ($schemas && $caller_function != 'views_schema_'. $schema); 73 } 74 } 75 } 76 77 // Call views_schema_<n>, for the highest available <n>. 78 if ($schema = array_pop($schemas)) { 79 $function = 'views_schema_'. $schema; 80 return $function(); 81 } 82 83 return array(); 84 } 85 86 /** 87 * Views 2's initial schema. 88 * Called directly by views_update_6000() for updates from Drupal 5. 89 * 90 * Important: Do not edit this schema! 91 * 92 * Updates to the views schema must be provided as views_schema_6xxx() functions, 93 * which views_schema() automatically sees and applies. See below for examples. 94 * 95 * Please do document updates with comments in this function, however. 96 */ 97 function views_schema_6000() { 98 $schema['views_view'] = array( 99 'description' => 'Stores the general data for a view.', 100 'fields' => array( 101 'vid' => array( 102 'type' => 'serial', 103 'unsigned' => TRUE, 104 'not null' => TRUE, 105 'description' => 'The view ID of the field, defined by the database.', 106 'no export' => TRUE, 107 ), 108 'name' => array( 109 'type' => 'varchar', 110 'length' => '32', 111 'default' => '', 112 'not null' => TRUE, 113 '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 ), 115 'description' => array( 116 'type' => 'varchar', 117 'length' => '255', 118 'default' => '', 119 'description' => 'A description of the view for the admin interface.', 120 ), 121 'tag' => array( 122 'type' => 'varchar', 123 'length' => '255', 124 'default' => '', 125 'description' => 'A tag used to group/sort views in the admin interface', 126 ), 127 'view_php' => array( 128 'type' => 'blob', 129 'description' => 'A chunk of PHP code that can be used to provide modifications to the view prior to building.', 130 ), 131 'base_table' => array( 132 'type' => 'varchar', 133 'length' => '32', // Updated to '64' in views_schema_6005() 134 'default' => '', 135 'not null' => TRUE, 136 'description' => 'What table this view is based on, such as node, user, comment, or term.', 137 ), 138 'is_cacheable' => array( 139 'type' => 'int', 140 'default' => 0, 141 'size' => 'tiny', 142 'description' => 'A boolean to indicate whether or not this view may have its query cached.', 143 ), 144 ), 145 'primary key' => array('vid'), 146 'unique key' => array('name' => array('name')), // Updated to 'unique keys' in views_schema_6003() 147 ); 148 149 $schema['views_display'] = array( 150 'description' => 'Stores information about each display attached to a view.', 151 'fields' => array( 152 'vid' => array( 153 'type' => 'int', 154 'unsigned' => TRUE, 155 'not null' => TRUE, 156 'default' => 0, 157 'description' => 'The view this display is attached to.', 158 'no export' => TRUE, 159 ), 160 'id' => array( 161 'type' => 'varchar', 162 'length' => '64', 163 'default' => '', 164 'not null' => TRUE, 165 '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 ), 167 'display_title' => array( 168 'type' => 'varchar', 169 'length' => '64', 170 'default' => '', 171 'not null' => TRUE, 172 'description' => 'The title of the display, viewable by the administrator.', 173 ), 174 'display_plugin' => array( 175 'type' => 'varchar', 176 'length' => '64', 177 'default' => '', 178 'not null' => TRUE, 179 'description' => 'The type of the display. Usually page, block or embed, but is pluggable so may be other things.', 180 ), 181 'position' => array( 182 'type' => 'int', 183 'default' => 0, 184 'description' => 'The order in which this display is loaded.', 185 ), 186 'display_options' => array( 187 // Type corrected in update 6009 188 'type' => 'blob', 189 'description' => 'A serialized array of options for this display; it contains options that are generally only pertinent to that display plugin type.', 190 'serialize' => TRUE, 191 'serialized default' => 'a:0:{}', 192 ), 193 ), 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 update_sql($ret, "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 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Thu Mar 24 11:18:33 2011 | Cross-referenced by PHPXref 0.7 |