Projet

Général

Profil

Paste
Télécharger (20,2 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / views / views.install @ 651307cd

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
 * Generate the current version of the database schema from
24
 * the sequence of schema update functions. Uses a similar
25
 * method to install.inc's drupal_get_schema_versions() to
26
 * establish the update sequence.
27
 *
28
 * To change the schema, add a new views_schema_N()
29
 * function to match the associated views_update_N()
30
 *
31
 * @param $caller_function
32
 *   The name of the function that called us.
33
 *   Used internally, if requesting a specific schema version.
34
 */
35
function views_schema($caller_function = FALSE) {
36
  static $get_current;
37
  static $schemas = array();
38

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

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

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

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

    
74
  return array();
75
}
76

    
77
/**
78
 * Views 2's initial schema.
79
 * Called directly by views_update_6000() for updates from Drupal 5.
80
 *
81
 * Important: Do not edit this schema!
82
 *
83
 * Updates to the views schema must be provided as views_schema_6xxx() functions,
84
 * which views_schema() automatically sees and applies. See below for 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', // Updated to '64' in views_schema_6005()
152
        'default' => '',
153
        'not null' => TRUE,
154
        'description' => 'What table this view is based on, such as node, user, comment, or term.',
155
      ),
156
      'is_cacheable' => array(
157
        'type' => 'int',
158
        'default' => 0,
159
        'size' => 'tiny',
160
        'description' => 'A boolean to indicate whether or not this view may have its query cached.',
161
      ),
162
    ),
163
    'primary key' => array('vid'),
164
    'unique key' => array('name' => array('name')), // Updated to 'unique keys' in views_schema_6003()
165
  );
166

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

    
216
  $schema['cache_views'] = drupal_get_schema_unprocessed('system', 'cache');
217

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

    
255
  // $schema['cache_views_data'] added in views_schema_6006()
256

    
257
  return $schema;
258
}
259

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

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

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

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

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

    
298
// NOTE: Update 6002 removed because it did not always work.
299
// Update 6004 implements the change correctly.
300

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

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

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

    
338
/**
339
 * Enlarge the base_table column
340
 */
341
function views_schema_6005() {
342
  $schema = views_schema(__FUNCTION__);
343
  $schema['views_view']['fields']['base_table']['length'] = 64;
344
  return $schema;
345
}
346
function views_update_6005() {
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('views_view', 'base_table', 'base_table', $new_field);
355
}
356

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

    
372
  db_create_table('cache_views_data', $table);
373
}
374

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

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

    
396
/**
397
 * Add the primary key to the views_display table.
398
 */
399
function views_update_6008() {
400
  db_add_primary_key('views_display', array('vid', 'id'));
401
}
402

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

    
419
function views_update_6009() {
420
  $schema = views_schema_6009();
421

    
422
  if (Database::getConnection()->databaseType() == 'pgsql') {
423
    db_query('ALTER TABLE {views_display} RENAME "display_options" TO "display_options_old"');
424
    db_add_field('views_display', 'display_options', $schema['views_display']['fields']['display_options']);
425

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

    
438
    db_drop_field('views_display', 'display_options_old');
439
  }
440
  else {
441
    db_change_field('views_display', 'display_options', 'display_options', $schema['views_display']['fields']['display_options']);
442
  }
443
}
444

    
445
/**
446
 * Remove the view_php field
447
 */
448
function views_schema_6010() {
449
  $schema = views_schema(__FUNCTION__);
450
  unset($schema['views_view']['fields']['view_php']);
451
  unset($schema['views_view']['fields']['is_cacheable']);
452
  return $schema;
453
}
454

    
455
/**
456
 * Remove the view_php and is_cacheable field
457
 */
458
function views_update_6010() {
459
  db_drop_field('views_view', 'view_php');
460
  db_drop_field('views_view', 'is_cacheable');
461
}
462

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

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

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

    
496

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

    
511
function views_update_6013() {
512
  // Check because D6 installs may already have added this.
513
  if (!db_field_exists('views_view', 'human_name')) {
514

    
515
    $new_field = array(
516
      'type' => 'varchar',
517
      'length' => '255',
518
      'default' => '',
519
      'description' => 'A human readable name used to be displayed in the admin interface',
520
    );
521

    
522
    db_add_field('views_view', 'human_name', $new_field);
523
  }
524
}
525

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

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

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

    
568
  // Rename the views_hide_help_message variable from negative to positive.
569
  $hide_help = variable_get('views_hide_help_message');
570
  if (isset($hide_help)) {
571
    variable_set('views_ui_show_advanced_help_warning', !$hide_help);
572
    variable_del('views_hide_help_message');
573
  }
574

    
575
  // Rename the unused views_no_hover_links variable.
576
  variable_del('views_no_hover_links');
577
}
578

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

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

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

    
614
function views_schema_7301() {
615
  $schema = views_schema(__FUNCTION__);
616
  $schema['views_view']['fields']['name']['length'] = 128;
617
  return $schema;
618
}
619

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

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