| Commit | Line | Data |
|---|---|---|
| 90ed9220 | 1 | <?php |
| 582713d9 | 2 | // $Id$ |
| 63b47c8d | 3 | /** |
| 582713d9 EM |
4 | * @file views.install |
| 5 | * Contains install and update functions for Views. | |
| 63b47c8d | 6 | */ |
| 58e4ed44 | 7 | |
| 582713d9 | 8 | function views_install() { |
| 90ca6fe9 EM |
9 | if ($GLOBALS['db_type'] == 'pgsql') { |
| 10 | db_query('CREATE OR REPLACE FUNCTION first(anyelement, anyelement) RETURNS anyelement AS \'SELECT COALESCE($1, $2);\' LANGUAGE \'sql\';'); | |
| 11 | db_query("DROP AGGREGATE IF EXISTS first(anyelement)"); | |
| 12 | db_query("CREATE AGGREGATE first(sfunc = first, basetype = anyelement, stype = anyelement);"); | |
| 13 | } | |
| 582713d9 EM |
14 | drupal_install_schema('views'); |
| 15 | db_query("UPDATE {system} SET weight = 10 WHERE name = 'views'"); | |
| fad16eed | 16 | } |
| e2ef9c10 | 17 | |
| 582713d9 EM |
18 | function views_uninstall() { |
| 19 | drupal_uninstall_schema('views'); | |
| e2ef9c10 | 20 | } |
| b06399e3 EM |
21 | |
| 22 | /** | |
| 3fddb150 | 23 | * Implementation of hook_schemea |
| b06399e3 EM |
24 | */ |
| 25 | function views_schema() { | |
| 3fddb150 EM |
26 | // Currently, schema 1 is the only schema we have. As we make updates, |
| 27 | // we might either create a new schema 2 or make adjustments here. | |
| 28 | return views_schema_1(); | |
| 29 | } | |
| 30 | ||
| 31 | /** | |
| 32 | * Views 2's initial schema; separated for the purposes of updates. | |
| 33 | */ | |
| 34 | function views_schema_1() { | |
| b06399e3 | 35 | $schema['views_view'] = array( |
| cfdea9bb | 36 | 'description' => 'Stores the general data for a view.', |
| b06399e3 EM |
37 | 'fields' => array( |
| 38 | 'vid' => array( | |
| 58e4ed44 EM |
39 | 'type' => 'serial', |
| 40 | 'unsigned' => TRUE, | |
| 41 | 'not null' => TRUE, | |
| cfdea9bb | 42 | 'description' => 'The view ID of the field, defined by the database.', |
| b06399e3 EM |
43 | 'no export' => TRUE, |
| 44 | ), | |
| 45 | 'name' => array( | |
| 58e4ed44 EM |
46 | 'type' => 'varchar', |
| 47 | 'length' => '32', | |
| b06399e3 | 48 | 'default' => '', |
| 58e4ed44 | 49 | 'not null' => TRUE, |
| cfdea9bb | 50 | '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.', |
| b06399e3 EM |
51 | ), |
| 52 | 'description' => array( | |
| 58e4ed44 EM |
53 | 'type' => 'varchar', |
| 54 | 'length' => '255', | |
| b06399e3 | 55 | 'default' => '', |
| cfdea9bb | 56 | 'description' => 'A description of the view for the admin interface.', |
| b06399e3 | 57 | ), |
| 58e4ed44 EM |
58 | 'tag' => array( |
| 59 | 'type' => 'varchar', | |
| 60 | 'length' => '255', | |
| 61 | 'default' => '', | |
| cfdea9bb | 62 | 'description' => 'A tag used to group/sort views in the admin interface', |
| 58e4ed44 | 63 | ), |
| b06399e3 | 64 | 'view_php' => array( |
| 58e4ed44 | 65 | 'type' => 'blob', |
| cfdea9bb | 66 | 'description' => 'A chunk of PHP code that can be used to provide modifications to the view prior to building.', |
| b06399e3 EM |
67 | ), |
| 68 | 'base_table' => array( | |
| 58e4ed44 | 69 | 'type' => 'varchar', |
| 56848600 | 70 | 'length' => '64', |
| b06399e3 | 71 | 'default' => '', |
| 58e4ed44 | 72 | 'not null' => TRUE, |
| cfdea9bb | 73 | 'description' => 'What table this view is based on, such as node, user, comment, or term.', |
| b06399e3 EM |
74 | ), |
| 75 | 'is_cacheable' => array( | |
| 58e4ed44 | 76 | 'type' => 'int', |
| b06399e3 EM |
77 | 'default' => 0, |
| 78 | 'size' => 'tiny', | |
| cfdea9bb | 79 | 'description' => 'A boolean to indicate whether or not this view may have its query cached.', |
| b06399e3 EM |
80 | ), |
| 81 | ), | |
| 82 | 'primary key' => array('vid'), | |
| 9755f01b | 83 | 'unique keys' => array('name' => array('name')), |
| b06399e3 EM |
84 | ); |
| 85 | ||
| 86 | $schema['views_display'] = array( | |
| cfdea9bb | 87 | 'description' => 'Stores information about each display attached to a view.', |
| b06399e3 EM |
88 | 'fields' => array( |
| 89 | 'vid' => array( | |
| 58e4ed44 EM |
90 | 'type' => 'int', |
| 91 | 'unsigned' => TRUE, | |
| 92 | 'not null' => TRUE, | |
| b06399e3 | 93 | 'default' => 0, |
| cfdea9bb | 94 | 'description' => 'The view this display is attached to.', |
| b06399e3 EM |
95 | 'no export' => TRUE, |
| 96 | ), | |
| dc3ec4ee | 97 | 'id' => array( |
| 58e4ed44 EM |
98 | 'type' => 'varchar', |
| 99 | 'length' => '64', | |
| b06399e3 | 100 | 'default' => '', |
| 58e4ed44 | 101 | 'not null' => TRUE, |
| cfdea9bb | 102 | '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.', |
| b06399e3 | 103 | ), |
| dc3ec4ee | 104 | 'display_title' => array( |
| 58e4ed44 EM |
105 | 'type' => 'varchar', |
| 106 | 'length' => '64', | |
| b06399e3 | 107 | 'default' => '', |
| 58e4ed44 | 108 | 'not null' => TRUE, |
| cfdea9bb | 109 | 'description' => 'The title of the display, viewable by the administrator.', |
| dc3ec4ee EM |
110 | ), |
| 111 | 'display_plugin' => array( | |
| 112 | 'type' => 'varchar', | |
| 113 | 'length' => '64', | |
| 114 | 'default' => '', | |
| 115 | 'not null' => TRUE, | |
| cfdea9bb | 116 | 'description' => 'The type of the display. Usually page, block or embed, but is pluggable so may be other things.', |
| b06399e3 | 117 | ), |
| b06399e3 | 118 | 'position' => array( |
| 58e4ed44 | 119 | 'type' => 'int', |
| b06399e3 | 120 | 'default' => 0, |
| cfdea9bb | 121 | 'description' => 'The order in which this display is loaded.', |
| b06399e3 EM |
122 | ), |
| 123 | 'display_options' => array( | |
| 58e4ed44 | 124 | 'type' => 'blob', |
| cfdea9bb | 125 | 'description' => 'A serialized array of options for this display; it contains options that are generally only pertinent to that display plugin type.', |
| b06399e3 EM |
126 | 'serialize' => TRUE, |
| 127 | 'serialized default' => 'a:0:{}', | |
| 128 | ), | |
| b06399e3 EM |
129 | ), |
| 130 | 'indexes' => array('vid' => array('vid', 'position')), | |
| 131 | ); | |
| 132 | ||
| bb770a1b | 133 | $schema['cache_views'] = drupal_get_schema_unprocessed('system', 'cache'); |
| 84536563 EM |
134 | |
| 135 | $schema['cache_views_data'] = drupal_get_schema_unprocessed('system', 'cache'); | |
| 136 | $schema['cache_views_data']['description'] = 'Cache table for views to store pre-rendered queries, results, and display output.'; | |
| 137 | $schema['cache_views_data']['fields']['serialized']['default'] = 1; | |
| 138 | ||
| b06399e3 | 139 | |
| 58e4ed44 | 140 | $schema['views_object_cache'] = array( |
| cfdea9bb | 141 | 'description' => 'A special cache used to store objects that are being edited; it serves to save state in an ordinarily stateless environment.', |
| 58e4ed44 EM |
142 | 'fields' => array( |
| 143 | 'sid' => array( | |
| 144 | 'type' => 'varchar', | |
| 145 | 'length' => '64', | |
| cfdea9bb | 146 | 'description' => 'The session ID this cache object belongs to.', |
| 58e4ed44 EM |
147 | ), |
| 148 | 'name' => array( | |
| 149 | 'type' => 'varchar', | |
| 150 | 'length' => '32', | |
| cfdea9bb | 151 | 'description' => 'The name of the view this cache is attached to.', |
| 58e4ed44 EM |
152 | ), |
| 153 | 'obj' => array( | |
| 154 | 'type' => 'varchar', | |
| 155 | 'length' => '32', | |
| cfdea9bb | 156 | '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.', |
| 58e4ed44 EM |
157 | ), |
| 158 | 'updated' => array( | |
| 159 | 'type' => 'int', | |
| 160 | 'unsigned' => TRUE, | |
| 161 | 'not null' => TRUE, | |
| 162 | 'default' => 0, | |
| cfdea9bb | 163 | 'description' => 'The time this cache was created or updated.', |
| 58e4ed44 EM |
164 | ), |
| 165 | 'data' => array( | |
| c0215efe EM |
166 | 'type' => 'text', |
| 167 | 'size' => 'big', | |
| cfdea9bb | 168 | 'description' => 'Serialized data being stored.', |
| 58e4ed44 EM |
169 | 'serialize' => TRUE, |
| 170 | ), | |
| 171 | ), | |
| 172 | 'indexes' => array( | |
| 173 | 'sid_obj_name' => array('sid', 'obj', 'name'), | |
| 174 | 'updated' => array('updated'), | |
| 175 | ), | |
| 176 | ); | |
| b06399e3 EM |
177 | return $schema; |
| 178 | } | |
| 1008b7ff | 179 | |
| 3fddb150 EM |
180 | /** |
| 181 | * Update a site to Drupal 6! Contains a bit of special code to detect | |
| 182 | * if you've been running a beta version or something. | |
| 183 | */ | |
| 184 | function views_update_6000() { | |
| 0eeeee6a | 185 | $ret = array(); |
| 3fddb150 | 186 | if (db_table_exists('views_view')) { |
| 0eeeee6a | 187 | return $ret; |
| 3fddb150 EM |
188 | } |
| 189 | ||
| 691c8574 EM |
190 | // This has the beneficial effect of wiping out any Views 1 cache at the |
| 191 | // same time; not wiping that cache could easily cause problems with Views 2. | |
| 192 | if (db_table_exists('cache_views')) { | |
| 193 | db_drop_table($ret, 'cache_views'); | |
| 194 | } | |
| 195 | ||
| 3fddb150 EM |
196 | // This is mostly the same as drupal_install_schema, but it forces |
| 197 | // views_schema_1 rather than the default schema. This will be important | |
| 198 | // if we have table updates. | |
| 199 | $schema = views_schema_1(); | |
| d7f09343 | 200 | _drupal_initialize_schema('views', $schema); |
| 3fddb150 | 201 | |
| 3fddb150 EM |
202 | foreach ($schema as $name => $table) { |
| 203 | db_create_table($ret, $name, $table); | |
| 204 | } | |
| 205 | return $ret; | |
| 206 | } | |
| 4c9e5b42 EM |
207 | |
| 208 | function views_update_6001() { | |
| 209 | $ret = array(); | |
| 210 | $result = db_query("SELECT * FROM {blocks} WHERE module = 'views' AND delta LIKE '\$exp%'"); | |
| 211 | while ($block = db_fetch_object($result)) { | |
| 212 | $new = strtr($block->delta, '$', '-'); | |
| 213 | $ret[] = update_sql("UPDATE {blocks} SET delta = '" . db_escape_string($new) . "' WHERE module = 'views' AND delta = '" . db_escape_string($block->delta) . "'"); | |
| 214 | } | |
| ae4f31f3 | 215 | $ret[] = update_sql("UPDATE {blocks} SET delta = CONCAT(delta, '-block_1') WHERE module = 'views'"); |
| 4c9e5b42 EM |
216 | |
| 217 | return $ret; | |
| 218 | } | |
| c0215efe | 219 | |
| b117ead5 EM |
220 | // NOTE: Update 6002 removed because it did not always work. |
| 221 | /** | |
| 222 | * Add missing unique key. | |
| 223 | */ | |
| 224 | function views_update_6003() { | |
| 225 | $ret = array(); | |
| 226 | ||
| 227 | db_add_unique_key($ret, 'views_view', 'name', array('name')); | |
| 228 | ||
| 229 | return $ret; | |
| 230 | } | |
| 231 | ||
| c0215efe EM |
232 | /** |
| 233 | * Enlarge the views_object_cache.data column to prevent truncation and JS | |
| 234 | * errors. | |
| 235 | */ | |
| b117ead5 | 236 | function views_update_6004() { |
| c0215efe EM |
237 | $ret = array(); |
| 238 | ||
| 239 | $new_field = array( | |
| 240 | 'type' => 'text', | |
| 241 | 'size' => 'big', | |
| cfdea9bb | 242 | 'description' => 'Serialized data being stored.', |
| c0215efe EM |
243 | 'serialize' => TRUE, |
| 244 | ); | |
| 245 | ||
| b117ead5 EM |
246 | // Drop and re-add this field because there is a bug in |
| 247 | // db_change_field that causes this to fail when trying to cast the data. | |
| 248 | db_drop_field($ret, 'views_object_cache', 'data'); | |
| 249 | db_add_field($ret, 'views_object_cache', 'data', $new_field); | |
| 9755f01b EM |
250 | |
| 251 | return $ret; | |
| 252 | } | |
| 56848600 EM |
253 | |
| 254 | /** | |
| 255 | * Enlarge the base_table column | |
| 256 | */ | |
| 257 | function views_update_6005() { | |
| 258 | $ret = array(); | |
| 259 | ||
| 260 | $new_field = array( | |
| 261 | 'type' => 'varchar', | |
| 262 | 'length' => '64', | |
| 263 | 'default' => '', | |
| 264 | 'not null' => TRUE, | |
| 265 | 'description' => 'What table this view is based on, such as node, user, comment, or term.', | |
| 266 | ); | |
| 267 | db_change_field($ret, 'views_view', 'base_table', 'base_table', $new_field); | |
| 268 | return $ret; | |
| 269 | } | |
| 84536563 EM |
270 | |
| 271 | /** | |
| 272 | * Add the cache_views_data table to support standard caching. | |
| 273 | */ | |
| 274 | function views_update_6006() { | |
| 275 | $ret = array(); | |
| 276 | ||
| 277 | $table = drupal_get_schema_unprocessed('system', 'cache'); | |
| 278 | $table['description'] = 'Cache table for views to store pre-rendered queries, results, and display output.'; | |
| 279 | $table['fields']['serialized']['default'] = 1; | |
| 280 | ||
| 281 | db_create_table($ret, 'cache_views_data', $table); | |
| 282 | ||
| 283 | return $ret; | |
| 284 | } | |
| 90ca6fe9 EM |
285 | |
| 286 | /** | |
| 287 | * Add aggregate function to PostgreSQL so GROUP BY can be used to force only | |
| 288 | * one result to be returned for each item. | |
| 289 | */ | |
| 290 | function views_update_6007() { | |
| 291 | $ret = array(); | |
| 292 | if ($GLOBALS['db_type'] == 'pgsql') { | |
| 293 | $ret[] = update_sql('CREATE OR REPLACE FUNCTION first(anyelement, anyelement) RETURNS anyelement AS \'SELECT COALESCE($1, $2);\' LANGUAGE \'sql\';'); | |
| 294 | $ret[] = update_sql("DROP AGGREGATE IF EXISTS first(anyelement)"); | |
| 295 | $ret[] = update_sql("CREATE AGGREGATE first(sfunc = first, basetype = anyelement, stype = anyelement);"); | |
| 296 | } | |
| 297 | return $ret; | |
| 298 | } |