Project

General

Profile

Paste
Download (20.2 KB) Statistics
| Branch: | Revision:

root / drupal7 / sites / all / modules / views / views.install @ 4003efde

1
<?php
2

    
3
/**
4
 * @file
5
 * Contains install and update functions for Views.
6
 */
7

    
8
/**
9
 * Implements hook_install().
10
 */
11
function views_install() {
12
  if (Database::getConnection()->databaseType() == '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
  db_query("UPDATE {system} SET weight = 10 WHERE name = 'views'");
18
}
19

    
20
/**
21
 * Implements hook_schema().
22
 */
23
function views_schema($caller_function = NULL) {
24
  // Generate the current version of the database schema from the sequence of
25
  // schema update functions. Uses a similar method to install.inc's
26
  // drupal_get_schema_versions() to establish the update sequence.
27
  //
28
  // To change the schema, add a new views_schema_N() function to match the
29
  // associated views_update_N().
30
  //
31
  // @param string $caller_function
32
  //   The name of the function that called us. Used internally, if requesting a
33
  //   specific schema version.
34
  static $get_current;
35
  static $schemas = array();
36

    
37
  // If called with no arguments, get the latest version of the schema.
38
  if (!isset($get_current)) {
39
    $get_current = $caller_function ? FALSE : TRUE;
40
  }
41

    
42
  // Generate a sorted list of available schema update functions.
43
  if ($get_current || empty($schemas)) {
44
    $get_current = FALSE;
45
    // Provide a worst-case scenario range.
46
    $start_schema = 6000;
47
    $end_schema = 7999;
48
    for ($i = $start_schema; $i <= $end_schema; $i++) {
49
      if (function_exists('views_schema_' . $i)) {
50
        $schemas[] = $i;
51
      }
52
    }
53
    if ($schemas) {
54
      sort($schemas, SORT_NUMERIC);
55

    
56
      // If a specific version was requested, drop any later updates from the
57
      // sequence.
58
      if ($caller_function) {
59
        do {
60
          $schema = array_pop($schemas);
61
        } while ($schemas && $caller_function != 'views_schema_' . $schema);
62
      }
63
    }
64
  }
65

    
66
  // Call views_schema_<n>, for the highest available <n>.
67
  if ($schema = array_pop($schemas)) {
68
    $function = 'views_schema_' . $schema;
69
    return $function();
70
  }
71

    
72
  return array();
73
}
74

    
75
/**
76
 * Views 2's initial schema.
77
 *
78
 * Called directly by views_update_6000() for updates from Drupal 5.
79
 *
80
 * Important: Do not edit this schema!
81
 *
82
 * Updates to the views schema must be provided as views_schema_6xxx()
83
 * functions, which views_schema() automatically sees and applies. See below for
84
 * examples.
85
 *
86
 * Please do document updates with comments in this function, however.
87
 */
88
function views_schema_6000() {
89
  $schema['views_view'] = array(
90
    'description' => 'Stores the general data for a view.',
91
    'export' => array(
92
      'identifier' => 'view',
93
      'bulk export' => TRUE,
94
      'primary key' => 'vid',
95
      'default hook' => 'views_default_views',
96
      'admin_title' => 'human_name',
97
      'admin_description' => 'description',
98
      'api' => array(
99
        'owner' => 'views',
100
        'api' => 'views_default',
101
        'minimum_version' => '2',
102
        'current_version' => '3.0',
103
      ),
104
      'object' => 'view',
105
      // the callback to load the displays.
106
      'subrecords callback' => 'views_load_display_records',
107
      // the variable that holds enabled/disabled status.
108
      'status' => 'views_defaults',
109
      // CRUD callbacks.
110
      'create callback' => 'views_new_view',
111
      'save callback' => 'views_save_view',
112
      'delete callback' => 'views_delete_view',
113
      'export callback' => 'views_export_view',
114
      'status callback' => 'views_export_status',
115
      'cache defaults' => TRUE,
116
      'default cache bin' => 'cache_views',
117
    ),
118
    'fields' => array(
119
      'vid' => array(
120
        'type' => 'serial',
121
        'unsigned' => TRUE,
122
        'not null' => TRUE,
123
        'description' => 'The view ID of the field, defined by the database.',
124
        'no export' => TRUE,
125
      ),
126
      'name' => array(
127
        'type' => 'varchar',
128
        'length' => '32',
129
        'default' => '',
130
        'not null' => TRUE,
131
        '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.',
132
      ),
133
      'description' => array(
134
        'type' => 'varchar',
135
        'length' => '255',
136
        'default' => '',
137
        'description' => 'A description of the view for the admin interface.',
138
      ),
139
      'tag' => array(
140
        'type' => 'varchar',
141
        'length' => '255',
142
        'default' => '',
143
        'description' => 'A tag used to group/sort views in the admin interface',
144
      ),
145
      'view_php' => array(
146
        'type' => 'blob',
147
        'description' => 'A chunk of PHP code that can be used to provide modifications to the view prior to building.',
148
      ),
149
      'base_table' => array(
150
        'type' => 'varchar',
151
        'length' => '32',
152
  // Updated to '64' in views_schema_6005()
153
        'default' => '',
154
        'not null' => TRUE,
155
        'description' => 'What table this view is based on, such as node, user, comment, or term.',
156
      ),
157
      'is_cacheable' => array(
158
        'type' => 'int',
159
        'default' => 0,
160
        'size' => 'tiny',
161
        'description' => 'A boolean to indicate whether or not this view may have its query cached.',
162
      ),
163
    ),
164
    'primary key' => array('vid'),
165
    'unique key' => array('name' => array('name')),
166
  // Updated to 'unique keys' in views_schema_6003()
167
  );
168

    
169
  $schema['views_display'] = array(
170
    'description' => 'Stores information about each display attached to a view.',
171
    'fields' => array(
172
      'vid' => array(
173
        'type' => 'int',
174
        'unsigned' => TRUE,
175
        'not null' => TRUE,
176
        'default' => 0,
177
        'description' => 'The view this display is attached to.',
178
        'no export' => TRUE,
179
      ),
180
      'id' => array(
181
        'type' => 'varchar',
182
        'length' => '64',
183
        'default' => '',
184
        'not null' => TRUE,
185
        '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.',
186
      ),
187
      'display_title' => array(
188
        'type' => 'varchar',
189
        'length' => '64',
190
        'default' => '',
191
        'not null' => TRUE,
192
        'description' => 'The title of the display, viewable by the administrator.',
193
      ),
194
      'display_plugin' => array(
195
        'type' => 'varchar',
196
        'length' => '64',
197
        'default' => '',
198
        'not null' => TRUE,
199
        'description' => 'The type of the display. Usually page, block or embed, but is pluggable so may be other things.',
200
      ),
201
      'position' => array(
202
        'type' => 'int',
203
        'default' => 0,
204
        'description' => 'The order in which this display is loaded.',
205
      ),
206
      'display_options' => array(
207
        // Type corrected in update 6009
208
        'type' => 'blob',
209
        'description' => 'A serialized array of options for this display; it contains options that are generally only pertinent to that display plugin type.',
210
        'serialize' => TRUE,
211
        'serialized default' => 'a:0:{}',
212
      ),
213
    ),
214
    // Added primary keys in views_schema_6008()
215
    'indexes' => array('vid' => array('vid', 'position')),
216
  );
217

    
218
  $schema['cache_views'] = drupal_get_schema_unprocessed('system', 'cache');
219

    
220
  $schema['views_object_cache'] = array(
221
    'description' => 'A special cache used to store objects that are being edited; it serves to save state in an ordinarily stateless environment.',
222
    'fields' => array(
223
      'sid' => array(
224
        'type' => 'varchar',
225
        'length' => '64',
226
        'description' => 'The session ID this cache object belongs to.',
227
      ),
228
      'name' => array(
229
        'type' => 'varchar',
230
        'length' => '32',
231
        'description' => 'The name of the view this cache is attached to.',
232
      ),
233
      'obj' => array(
234
        'type' => 'varchar',
235
        'length' => '32',
236
        '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.',
237
      ),
238
      'updated' => array(
239
        'type' => 'int',
240
        'unsigned' => TRUE,
241
        'not null' => TRUE,
242
        'default' => 0,
243
        'description' => 'The time this cache was created or updated.',
244
      ),
245
      'data' => array(
246
        'type' => 'blob',
247
  // Updated to 'text' (with size => 'big') in views_schema_6004()
248
        'description' => 'Serialized data being stored.',
249
        'serialize' => TRUE,
250
      ),
251
    ),
252
    'indexes' => array(
253
      'sid_obj_name' => array('sid', 'obj', 'name'),
254
      'updated' => array('updated'),
255
    ),
256
  );
257

    
258
  // $schema['cache_views_data'] added in views_schema_6006().
259
  return $schema;
260
}
261

    
262
/**
263
 * Update a site to Drupal 6! Contains a bit of special code to detect
264
 * if you've been running a beta version or something.
265
 */
266
function views_update_6000() {
267
  if (db_table_exists('views_view')) {
268
    return;
269
  }
270

    
271
  // This has the beneficial effect of wiping out any Views 1 cache at the
272
  // same time; not wiping that cache could easily cause problems with Views 2.
273
  if (db_table_exists('cache_views')) {
274
    db_drop_table('cache_views');
275
  }
276

    
277
  // This is mostly the same as drupal_install_schema, but it forces
278
  // views_schema_6000() rather than the default views_schema().
279
  // This is important for processing subsequent table updates.
280
  $schema = views_schema_6000();
281
  _drupal_schema_initialize($schema, 'views');
282

    
283
  foreach ($schema as $name => $table) {
284
    db_create_table($name, $table);
285
  }
286
}
287

    
288
/**
289
 * Remove '$' symbol in special blocks, as it is invalid for theming.
290
 */
291
function views_update_6001() {
292
  $result = db_query("SELECT * FROM {blocks} WHERE module = 'views' AND delta LIKE '\$exp%'");
293
  foreach ($result as $block) {
294
    $new = strtr($block->delta, '$', '-');
295
    update_sql("UPDATE {blocks} SET delta = '" . db_escape_string($new) . "' WHERE module = 'views' AND delta = '" . db_escape_string($block->delta) . "'");
296
  }
297
  update_sql("UPDATE {blocks} SET delta = CONCAT(delta, '-block_1') WHERE module = 'views'");
298
}
299

    
300
/**
301
 * NOTE: Update 6002 removed because it did not always work.
302
 * Update 6004 implements the change correctly.
303
 */
304

    
305
/**
306
 * Add missing unique key.
307
 */
308
function views_schema_6003() {
309
  $schema = views_schema(__FUNCTION__);
310
  $schema['views_view']['unique keys'] = array('name' => array('name'));
311
  unset($schema['views_view']['unique key']);
312
  return $schema;
313
}
314
function views_update_6003() {
315
  db_add_unique_key('views_view', 'name', array('name'));
316
}
317

    
318
/**
319
 * Enlarge the views_object_cache.data column to prevent truncation and JS
320
 * errors.
321
 */
322
function views_schema_6004() {
323
  $schema = views_schema(__FUNCTION__);
324
  $schema['views_object_cache']['fields']['data']['type'] = 'text';
325
  $schema['views_object_cache']['fields']['data']['size'] = 'big';
326
  return $schema;
327
}
328
function views_update_6004() {
329
  $new_field = array(
330
    'type' => 'text',
331
    'size' => 'big',
332
    'description' => 'Serialized data being stored.',
333
    'serialize' => TRUE,
334
  );
335

    
336
  // Drop and re-add this field because there is a bug in
337
  // db_change_field that causes this to fail when trying to cast the data.
338
  db_drop_field('views_object_cache', 'data');
339
  db_add_field('views_object_cache', 'data', $new_field);
340
}
341

    
342
/**
343
 * Enlarge the base_table column.
344
 */
345
function views_schema_6005() {
346
  $schema = views_schema(__FUNCTION__);
347
  $schema['views_view']['fields']['base_table']['length'] = 64;
348
  return $schema;
349
}
350
function views_update_6005() {
351
  $new_field = array(
352
    'type' => 'varchar',
353
    'length' => '64',
354
    'default' => '',
355
    'not null' => TRUE,
356
    'description' => 'What table this view is based on, such as node, user, comment, or term.',
357
  );
358
  db_change_field('views_view', 'base_table', 'base_table', $new_field);
359
}
360

    
361
/**
362
 * Add the cache_views_data table to support standard caching.
363
 */
364
function views_schema_6006() {
365
  $schema = views_schema(__FUNCTION__);
366
  $schema['cache_views_data'] = drupal_get_schema_unprocessed('system', 'cache');
367
  $schema['cache_views_data']['description'] = 'Cache table for views to store pre-rendered queries, results, and display output.';
368
  $schema['cache_views_data']['fields']['serialized']['default'] = 1;
369
  return $schema;
370
}
371
function views_update_6006() {
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('cache_views_data', $table);
377
}
378

    
379
/**
380
 * Add aggregate function to PostgreSQL so GROUP BY can be used to force only
381
 * one result to be returned for each item.
382
 */
383
function views_update_6007() {
384
  if (Database::getConnection()->databaseType() == 'pgsql') {
385
    db_query('CREATE OR REPLACE FUNCTION first(anyelement, anyelement) RETURNS anyelement AS \'SELECT COALESCE($1, $2);\' LANGUAGE \'sql\';');
386
    db_query("DROP AGGREGATE IF EXISTS first(anyelement)");
387
    db_query("CREATE AGGREGATE first(sfunc = first, basetype = anyelement, stype = anyelement);");
388
  }
389
}
390

    
391
/**
392
 * Add the primary key to views_display table.
393
 */
394
function views_schema_6008() {
395
  $schema = views_schema(__FUNCTION__);
396
  $schema['views_display']['primary key'] = array('vid', 'id');
397
  return $schema;
398
}
399

    
400
/**
401
 * Add the primary key to the views_display table.
402
 */
403
function views_update_6008() {
404
  db_add_primary_key('views_display', array('vid', 'id'));
405
}
406

    
407
/**
408
 * Enlarge the views_display.display_options field to accommodate a larger set
409
 * of configurations (e. g. fields, filters, etc.) on a display.
410
 */
411
function views_schema_6009() {
412
  $schema = views_schema(__FUNCTION__);
413
  $schema['views_display']['fields']['display_options'] = array(
414
    'type' => 'text',
415
    'size' => 'big',
416
    'description' => 'A serialized array of options for this display; it contains options that are generally only pertinent to that display plugin type.',
417
    'serialize' => TRUE,
418
    'serialized default' => 'a:0:{}',
419
  );
420
  return $schema;
421
}
422

    
423
function views_update_6009() {
424
  $schema = views_schema_6009();
425

    
426
  if (Database::getConnection()->databaseType() == 'pgsql') {
427
    db_query('ALTER TABLE {views_display} RENAME "display_options" TO "display_options_old"');
428
    db_add_field('views_display', 'display_options', $schema['views_display']['fields']['display_options']);
429

    
430
    $sql = "SELECT vid, id, display_options_old FROM {views_display}";
431
    $result = db_query($sql);
432
    foreach ($result as $row) {
433
      $row['display_options_old'] = $row['display_options_old'];
434
      $sql = "UPDATE {views_display} SET display_options = :display_optons WHERE vid = :vid AND id = :id";
435
      db_query($sql, array(
436
        ':display_optons' => $row['display_options_old'],
437
        ':vid' => $row['vid'],
438
        ':id' => $row['id'],
439
      ));
440
    }
441

    
442
    db_drop_field('views_display', 'display_options_old');
443
  }
444
  else {
445
    db_change_field('views_display', 'display_options', 'display_options', $schema['views_display']['fields']['display_options']);
446
  }
447
}
448

    
449
/**
450
 * Remove the view_php field.
451
 */
452
function views_schema_6010() {
453
  $schema = views_schema(__FUNCTION__);
454
  unset($schema['views_view']['fields']['view_php']);
455
  unset($schema['views_view']['fields']['is_cacheable']);
456
  return $schema;
457
}
458

    
459
/**
460
 * Remove the view_php and is_cacheable field.
461
 */
462
function views_update_6010() {
463
  db_drop_field('views_view', 'view_php');
464
  db_drop_field('views_view', 'is_cacheable');
465
}
466

    
467
/**
468
 * Remove views_object_cache table and move the data to ctools_object_cache.
469
 */
470
function views_schema_6011() {
471
  $schema = views_schema(__FUNCTION__);
472
  unset($schema['views_object_cache']);
473
  return $schema;
474
}
475

    
476
/**
477
 * Remove views_object_cache table and move the data to ctools_object_cache.
478
 */
479
function views_update_6011() {
480
  $caches = db_query("SELECT * FROM {views_object_cache}");
481
  foreach ($caches as $item) {
482
    drupal_write_record('ctools_object_cache', $item);
483
  }
484
  db_drop_table('views_object_cache');
485
}
486

    
487
/**
488
 * Correct the cache setting for exposed filter blocks.
489
 *
490
 * @see http://drupal.org/node/910864
491
 */
492
function views_update_6012() {
493
  // There is only one simple query to run.
494
  db_update('blocks')
495
    ->condition('module', 'views')
496
    ->condition('delta', db_like('-exp-') . '%', 'LIKE')
497
    ->fields(array('cache' => DRUPAL_NO_CACHE));
498
}
499

    
500

    
501
/**
502
 * Add a human readable name.
503
 */
504
function views_schema_6013() {
505
  $schema = views_schema(__FUNCTION__);
506
  $schema['views_view']['fields']['human_name'] = array(
507
    'type' => 'varchar',
508
    'length' => '255',
509
    'default' => '',
510
    'description' => 'A human readable name used to be displayed in the admin interface',
511
  );
512
  return $schema;
513
}
514

    
515
function views_update_6013() {
516
  // Check because D6 installs may already have added this.
517
  if (!db_field_exists('views_view', 'human_name')) {
518

    
519
    $new_field = array(
520
      'type' => 'varchar',
521
      'length' => '255',
522
      'default' => '',
523
      'description' => 'A human readable name used to be displayed in the admin interface',
524
    );
525

    
526
    db_add_field('views_view', 'human_name', $new_field);
527
  }
528
}
529

    
530
function views_schema_6014() {
531
  $schema = views_schema(__FUNCTION__);
532
  $schema['views_view']['fields']['core'] = array(
533
    'type' => 'int',
534
    'default' => 0,
535
    'description' => 'Stores the drupal core version of the view.',
536
  );
537
  return $schema;
538
}
539

    
540
/**
541
 * Add a drupal core version field.
542
 */
543
function views_update_6014() {
544
  // Check because D6 installs may already have added this.
545
  if (!db_field_exists('views_view', 'core')) {
546
    $new_field = array(
547
      'type' => 'int',
548
      'default' => 0,
549
      'description' => 'Stores the drupal core version of the view.',
550
    );
551
    db_add_field('views_view', 'core', $new_field);
552
  }
553
}
554

    
555
/**
556
 * Rename some system variables.
557
 */
558
function views_update_7000() {
559
  // Views now lets users turn off query details on live preview.
560
  $query_on_top = variable_get('views_ui_query_on_top');
561
  if (isset($query_on_top)) {
562
    variable_set('views_ui_show_sql_query', TRUE);
563
    if ($query_on_top) {
564
      variable_set('views_ui_show_sql_query_where', 'above');
565
    }
566
    else {
567
      variable_set('views_ui_show_sql_query_where', 'below');
568
    }
569
    variable_del('views_ui_query_on_top');
570
  }
571

    
572
  // Rename the views_hide_help_message variable from negative to positive.
573
  $hide_help = variable_get('views_hide_help_message');
574
  if (isset($hide_help)) {
575
    variable_set('views_ui_show_advanced_help_warning', !$hide_help);
576
    variable_del('views_hide_help_message');
577
  }
578

    
579
  // Rename the unused views_no_hover_links variable.
580
  variable_del('views_no_hover_links');
581
}
582

    
583
/**
584
 * Fix missing items from Views administrative breadcrumb.
585
 */
586
function views_update_7001() {
587
  $depth = db_select('menu_links')
588
    ->fields('menu_links', array('depth'))
589
    ->condition('link_path', 'admin/structure/views/view/%')
590
    ->execute()
591
    ->fetchField();
592

    
593
  if ($depth == 3) {
594
    db_delete('menu_links')
595
      ->condition('link_path', 'admin/structure/views/%', 'LIKE')
596
      ->execute();
597
    cache_clear_all(NULL, 'cache_menu');
598
    menu_rebuild();
599
  }
600
}
601

    
602
function views_schema_7300() {
603
  return views_schema_6013();
604
}
605
/**
606
 * Make sure the human_name field is added to the views_view table.
607
 *
608
 * If you updated from 6.x-2.x-dev to 7.x-3.x you already had schema
609
 * version 6014, so update 6013 never was nor will be run. As a result,
610
 * the human_name field is missing from the database.
611
 *
612
 * This will add the human_name field if it doesn't already exist.
613
 */
614
function views_update_7300() {
615
  views_update_6013();
616
}
617

    
618
function views_schema_7301() {
619
  $schema = views_schema(__FUNCTION__);
620
  $schema['views_view']['fields']['name']['length'] = 128;
621
  return $schema;
622
}
623

    
624
/**
625
 * Enlarge the name column.
626
 */
627
function views_update_7301() {
628
  $new_field = array(
629
    'type' => 'varchar',
630
    'length' => '128',
631
    'default' => '',
632
    'not null' => TRUE,
633
    '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.',
634
  );
635
  db_change_field('views_view', 'name', 'name', $new_field);
636
}
637

    
638
/**
639
 * Remove headers field from cache tables.
640
 *
641
 * @see system_update_7054()
642
 */
643
function views_update_7302() {
644
  if (db_field_exists('cache_views', 'headers')) {
645
    db_drop_field('cache_views', 'headers');
646
  }
647
  if (db_field_exists('cache_views_data', 'headers')) {
648
    db_drop_field('cache_views_data', 'headers');
649
  }
650
}