Projet

Général

Profil

Paste
Télécharger (19,9 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / views / views.install @ b858700c

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
    $functions = get_defined_functions();
48
    foreach ($functions['user'] as $function) {
49
      if (strpos($function, 'views_schema_') === 0) {
50
        $version = substr($function, strlen('views_schema_'));
51
        if (is_numeric($version)) {
52
          $schemas[] = $version;
53
        }
54
      }
55
    }
56
    if ($schemas) {
57
      sort($schemas, SORT_NUMERIC);
58

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

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

    
75
  return array();
76
}
77

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

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

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

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

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

    
258
  return $schema;
259
}
260

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
497

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

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

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

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

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

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

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

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

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

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

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

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

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

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