~views-port/+junk/Views3-d6

1 by Earl Miles
Initial commit
1
<?php
2
// $Id: views.install,v 1.46.2.6 2009/06/04 17:21:04 merlinofchaos Exp $
3
/**
4
 * @file views.install
5
 * Contains install and update functions for Views.
6
 */
7
8
function views_install() {
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
  }
14
  drupal_install_schema('views');
15
  db_query("UPDATE {system} SET weight = 10 WHERE name = 'views'");
16
}
17
18
function views_uninstall() {
19
  drupal_uninstall_schema('views');
20
}
21
22
/**
23
 * Implementation of hook_schemea
24
 */
25
function views_schema() {
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() {
35
  $schema['views_view'] = array(
36
    'description' => 'Stores the general data for a view.',
37
    'fields' => array(
38
      'vid' => array(
39
        'type' => 'serial',
40
        'unsigned' => TRUE,
41
        'not null' => TRUE,
42
        'description' => 'The view ID of the field, defined by the database.',
43
        'no export' => TRUE,
44
      ),
45
      'name' => array(
46
        'type' => 'varchar',
47
        'length' => '32',
48
        'default' => '',
49
        'not null' => TRUE,
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.',
51
      ),
52
      'description' => array(
53
        'type' => 'varchar',
54
        'length' => '255',
55
        'default' => '',
56
        'description' => 'A description of the view for the admin interface.',
57
      ),
58
      'tag' => array(
59
        'type' => 'varchar',
60
        'length' => '255',
61
        'default' => '',
62
        'description' => 'A tag used to group/sort views in the admin interface',
63
      ),
64
      'view_php' => array(
65
        'type' => 'blob',
66
        'description' => 'A chunk of PHP code that can be used to provide modifications to the view prior to building.',
67
      ),
68
      'base_table' => array(
69
        'type' => 'varchar',
70
        'length' => '64',
71
        'default' => '',
72
        'not null' => TRUE,
73
        'description' => 'What table this view is based on, such as node, user, comment, or term.',
74
      ),
75
      'is_cacheable' => array(
76
        'type' => 'int',
77
        'default' => 0,
78
        'size' => 'tiny',
79
        'description' => 'A boolean to indicate whether or not this view may have its query cached.',
80
      ),
81
    ),
82
    'primary key' => array('vid'),
83
    'unique keys' => array('name' => array('name')),
84
  );
85
86
  $schema['views_display'] = array(
87
    'description' => 'Stores information about each display attached to a view.',
88
    'fields' => array(
89
      'vid' => array(
90
        'type' => 'int',
91
        'unsigned' => TRUE,
92
        'not null' => TRUE,
93
        'default' => 0,
94
        'description' => 'The view this display is attached to.',
95
        'no export' => TRUE,
96
      ),
97
      'id' => array(
98
        'type' => 'varchar',
99
        'length' => '64',
100
        'default' => '',
101
        'not null' => TRUE,
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.',
103
      ),
104
      'display_title' => array(
105
        'type' => 'varchar',
106
        'length' => '64',
107
        'default' => '',
108
        'not null' => TRUE,
109
        'description' => 'The title of the display, viewable by the administrator.',
110
      ),
111
      'display_plugin' => array(
112
        'type' => 'varchar',
113
        'length' => '64',
114
        'default' => '',
115
        'not null' => TRUE,
116
        'description' => 'The type of the display. Usually page, block or embed, but is pluggable so may be other things.',
117
      ),
118
      'position' => array(
119
        'type' => 'int',
120
        'default' => 0,
121
        'description' => 'The order in which this display is loaded.',
122
      ),
123
      'display_options' => array(
124
        'type' => 'blob',
125
        'description' => 'A serialized array of options for this display; it contains options that are generally only pertinent to that display plugin type.',
126
        'serialize' => TRUE,
127
        'serialized default' => 'a:0:{}',
128
      ),
129
    ),
130
    'indexes' => array('vid' => array('vid', 'position')),
131
  );
132
133
  $schema['cache_views'] = drupal_get_schema_unprocessed('system', 'cache');
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
139
140
  $schema['views_object_cache'] = array(
141
    'description' => 'A special cache used to store objects that are being edited; it serves to save state in an ordinarily stateless environment.',
142
    'fields' => array(
143
      'sid' => array(
144
        'type' => 'varchar',
145
        'length' => '64',
146
        'description' => 'The session ID this cache object belongs to.',
147
      ),
148
      'name' => array(
149
        'type' => 'varchar',
150
        'length' => '32',
151
        'description' => 'The name of the view this cache is attached to.',
152
      ),
153
      'obj' => array(
154
        'type' => 'varchar',
155
        'length' => '32',
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.',
157
      ),
158
      'updated' => array(
159
        'type' => 'int',
160
        'unsigned' => TRUE,
161
        'not null' => TRUE,
162
        'default' => 0,
163
        'description' => 'The time this cache was created or updated.',
164
      ),
165
      'data' => array(
166
        'type' => 'text',
167
        'size' => 'big',
168
        'description' => 'Serialized data being stored.',
169
        'serialize' => TRUE,
170
      ),
171
    ),
172
    'indexes' => array(
173
      'sid_obj_name' => array('sid', 'obj', 'name'),
174
      'updated' => array('updated'),
175
    ),
176
  );
177
  return $schema;
178
}
179
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() {
185
  $ret = array();
186
  if (db_table_exists('views_view')) {
187
    return $ret;
188
  }
189
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
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();
200
  _drupal_initialize_schema('views', $schema);
201
202
  foreach ($schema as $name => $table) {
203
    db_create_table($ret, $name, $table);
204
  }
205
  return $ret;
206
}
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
  }
215
  $ret[] = update_sql("UPDATE {blocks} SET delta = CONCAT(delta, '-block_1') WHERE module = 'views'");
216
217
  return $ret;
218
}
219
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
232
/**
233
 * Enlarge the views_object_cache.data column to prevent truncation and JS
234
 * errors.
235
 */
236
function views_update_6004() {
237
  $ret = array();
238
239
  $new_field = array(
240
    'type' => 'text',
241
    'size' => 'big',
242
    'description' => 'Serialized data being stored.',
243
    'serialize' => TRUE,
244
  );
245
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);
250
251
  return $ret;
252
}
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
}
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
}
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
}