views.install 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  1. <?php
  2. /**
  3. * @file
  4. * Contains install and update functions for Views.
  5. */
  6. /**
  7. * Implements hook_install().
  8. */
  9. function views_install() {
  10. if (Database::getConnection()->databaseType() == 'pgsql') {
  11. db_query('CREATE OR REPLACE FUNCTION first(anyelement, anyelement) RETURNS anyelement AS \'SELECT COALESCE($1, $2);\' LANGUAGE \'sql\';');
  12. db_query("DROP AGGREGATE IF EXISTS first(anyelement)");
  13. db_query("CREATE AGGREGATE first(sfunc = first, basetype = anyelement, stype = anyelement);");
  14. }
  15. db_query("UPDATE {system} SET weight = 10 WHERE name = 'views'");
  16. }
  17. /**
  18. * Implements hook_schema().
  19. */
  20. function views_schema($caller_function = NULL) {
  21. // Generate the current version of the database schema from the sequence of
  22. // schema update functions. Uses a similar method to install.inc's
  23. // drupal_get_schema_versions() to establish the update sequence.
  24. //
  25. // To change the schema, add a new views_schema_N() function to match the
  26. // associated views_update_N().
  27. //
  28. // @param string $caller_function
  29. // The name of the function that called us. Used internally, if requesting a
  30. // specific schema version.
  31. static $get_current;
  32. static $schemas = array();
  33. // If called with no arguments, get the latest version of the schema.
  34. if (!isset($get_current)) {
  35. $get_current = $caller_function ? FALSE : TRUE;
  36. }
  37. // Generate a sorted list of available schema update functions.
  38. if ($get_current || empty($schemas)) {
  39. $get_current = FALSE;
  40. // Provide a worst-case scenario range.
  41. $start_schema = 6000;
  42. $end_schema = 7999;
  43. for ($i = $start_schema; $i <= $end_schema; $i++) {
  44. if (function_exists('views_schema_' . $i)) {
  45. $schemas[] = $i;
  46. }
  47. }
  48. if ($schemas) {
  49. sort($schemas, SORT_NUMERIC);
  50. // If a specific version was requested, drop any later updates from the
  51. // sequence.
  52. if ($caller_function) {
  53. do {
  54. $schema = array_pop($schemas);
  55. } while ($schemas && $caller_function != 'views_schema_' . $schema);
  56. }
  57. }
  58. }
  59. // Call views_schema_<n>, for the highest available <n>.
  60. if ($schema = array_pop($schemas)) {
  61. $function = 'views_schema_' . $schema;
  62. return $function();
  63. }
  64. return array();
  65. }
  66. /**
  67. * Views 2's initial schema.
  68. *
  69. * Called directly by views_update_6000() for updates from Drupal 5.
  70. *
  71. * Important: Do not edit this schema!
  72. *
  73. * Updates to the views schema must be provided as views_schema_6xxx()
  74. * functions, which views_schema() automatically sees and applies. See below for
  75. * examples.
  76. *
  77. * Please do document updates with comments in this function, however.
  78. */
  79. function views_schema_6000() {
  80. $schema['views_view'] = array(
  81. 'description' => 'Stores the general data for a view.',
  82. 'export' => array(
  83. 'identifier' => 'view',
  84. 'bulk export' => TRUE,
  85. 'primary key' => 'vid',
  86. 'default hook' => 'views_default_views',
  87. 'admin_title' => 'human_name',
  88. 'admin_description' => 'description',
  89. 'api' => array(
  90. 'owner' => 'views',
  91. 'api' => 'views_default',
  92. 'minimum_version' => '2',
  93. 'current_version' => '3.0',
  94. ),
  95. 'object' => 'view',
  96. // the callback to load the displays.
  97. 'subrecords callback' => 'views_load_display_records',
  98. // the variable that holds enabled/disabled status.
  99. 'status' => 'views_defaults',
  100. // CRUD callbacks.
  101. 'create callback' => 'views_new_view',
  102. 'save callback' => 'views_save_view',
  103. 'delete callback' => 'views_delete_view',
  104. 'export callback' => 'views_export_view',
  105. 'status callback' => 'views_export_status',
  106. 'cache defaults' => TRUE,
  107. 'default cache bin' => 'cache_views',
  108. ),
  109. 'fields' => array(
  110. 'vid' => array(
  111. 'type' => 'serial',
  112. 'unsigned' => TRUE,
  113. 'not null' => TRUE,
  114. 'description' => 'The view ID of the field, defined by the database.',
  115. 'no export' => TRUE,
  116. ),
  117. 'name' => array(
  118. 'type' => 'varchar',
  119. 'length' => '32',
  120. 'default' => '',
  121. 'not null' => TRUE,
  122. '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.',
  123. ),
  124. 'description' => array(
  125. 'type' => 'varchar',
  126. 'length' => '255',
  127. 'default' => '',
  128. 'description' => 'A description of the view for the admin interface.',
  129. ),
  130. 'tag' => array(
  131. 'type' => 'varchar',
  132. 'length' => '255',
  133. 'default' => '',
  134. 'description' => 'A tag used to group/sort views in the admin interface',
  135. ),
  136. 'view_php' => array(
  137. 'type' => 'blob',
  138. 'description' => 'A chunk of PHP code that can be used to provide modifications to the view prior to building.',
  139. ),
  140. 'base_table' => array(
  141. 'type' => 'varchar',
  142. 'length' => '32',
  143. // Updated to '64' in views_schema_6005()
  144. 'default' => '',
  145. 'not null' => TRUE,
  146. 'description' => 'What table this view is based on, such as node, user, comment, or term.',
  147. ),
  148. 'is_cacheable' => array(
  149. 'type' => 'int',
  150. 'default' => 0,
  151. 'size' => 'tiny',
  152. 'description' => 'A boolean to indicate whether or not this view may have its query cached.',
  153. ),
  154. ),
  155. 'primary key' => array('vid'),
  156. 'unique key' => array('name' => array('name')),
  157. // Updated to 'unique keys' in views_schema_6003()
  158. );
  159. $schema['views_display'] = array(
  160. 'description' => 'Stores information about each display attached to a view.',
  161. 'fields' => array(
  162. 'vid' => array(
  163. 'type' => 'int',
  164. 'unsigned' => TRUE,
  165. 'not null' => TRUE,
  166. 'default' => 0,
  167. 'description' => 'The view this display is attached to.',
  168. 'no export' => TRUE,
  169. ),
  170. 'id' => array(
  171. 'type' => 'varchar',
  172. 'length' => '64',
  173. 'default' => '',
  174. 'not null' => TRUE,
  175. '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.',
  176. ),
  177. 'display_title' => array(
  178. 'type' => 'varchar',
  179. 'length' => '64',
  180. 'default' => '',
  181. 'not null' => TRUE,
  182. 'description' => 'The title of the display, viewable by the administrator.',
  183. ),
  184. 'display_plugin' => array(
  185. 'type' => 'varchar',
  186. 'length' => '64',
  187. 'default' => '',
  188. 'not null' => TRUE,
  189. 'description' => 'The type of the display. Usually page, block or embed, but is pluggable so may be other things.',
  190. ),
  191. 'position' => array(
  192. 'type' => 'int',
  193. 'default' => 0,
  194. 'description' => 'The order in which this display is loaded.',
  195. ),
  196. 'display_options' => array(
  197. // Type corrected in update 6009
  198. 'type' => 'blob',
  199. 'description' => 'A serialized array of options for this display; it contains options that are generally only pertinent to that display plugin type.',
  200. 'serialize' => TRUE,
  201. 'serialized default' => 'a:0:{}',
  202. ),
  203. ),
  204. // Added primary keys in views_schema_6008()
  205. 'indexes' => array('vid' => array('vid', 'position')),
  206. );
  207. $schema['cache_views'] = drupal_get_schema_unprocessed('system', 'cache');
  208. $schema['views_object_cache'] = array(
  209. 'description' => 'A special cache used to store objects that are being edited; it serves to save state in an ordinarily stateless environment.',
  210. 'fields' => array(
  211. 'sid' => array(
  212. 'type' => 'varchar',
  213. 'length' => '64',
  214. 'description' => 'The session ID this cache object belongs to.',
  215. ),
  216. 'name' => array(
  217. 'type' => 'varchar',
  218. 'length' => '32',
  219. 'description' => 'The name of the view this cache is attached to.',
  220. ),
  221. 'obj' => array(
  222. 'type' => 'varchar',
  223. 'length' => '32',
  224. '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.',
  225. ),
  226. 'updated' => array(
  227. 'type' => 'int',
  228. 'unsigned' => TRUE,
  229. 'not null' => TRUE,
  230. 'default' => 0,
  231. 'description' => 'The time this cache was created or updated.',
  232. ),
  233. 'data' => array(
  234. 'type' => 'blob',
  235. // Updated to 'text' (with size => 'big') in views_schema_6004()
  236. 'description' => 'Serialized data being stored.',
  237. 'serialize' => TRUE,
  238. ),
  239. ),
  240. 'indexes' => array(
  241. 'sid_obj_name' => array('sid', 'obj', 'name'),
  242. 'updated' => array('updated'),
  243. ),
  244. );
  245. // $schema['cache_views_data'] added in views_schema_6006().
  246. return $schema;
  247. }
  248. /**
  249. * Update a site to Drupal 6! Contains a bit of special code to detect
  250. * if you've been running a beta version or something.
  251. */
  252. function views_update_6000() {
  253. if (db_table_exists('views_view')) {
  254. return;
  255. }
  256. // This has the beneficial effect of wiping out any Views 1 cache at the
  257. // same time; not wiping that cache could easily cause problems with Views 2.
  258. if (db_table_exists('cache_views')) {
  259. db_drop_table('cache_views');
  260. }
  261. // This is mostly the same as drupal_install_schema, but it forces
  262. // views_schema_6000() rather than the default views_schema().
  263. // This is important for processing subsequent table updates.
  264. $schema = views_schema_6000();
  265. _drupal_schema_initialize($schema, 'views');
  266. foreach ($schema as $name => $table) {
  267. db_create_table($name, $table);
  268. }
  269. }
  270. /**
  271. * Remove '$' symbol in special blocks, as it is invalid for theming.
  272. */
  273. function views_update_6001() {
  274. $result = db_query("SELECT * FROM {blocks} WHERE module = 'views' AND delta LIKE '\$exp%'");
  275. foreach ($result as $block) {
  276. $new = strtr($block->delta, '$', '-');
  277. update_sql("UPDATE {blocks} SET delta = '" . db_escape_string($new) . "' WHERE module = 'views' AND delta = '" . db_escape_string($block->delta) . "'");
  278. }
  279. update_sql("UPDATE {blocks} SET delta = CONCAT(delta, '-block_1') WHERE module = 'views'");
  280. }
  281. /**
  282. * NOTE: Update 6002 removed because it did not always work.
  283. * Update 6004 implements the change correctly.
  284. */
  285. /**
  286. * Add missing unique key.
  287. */
  288. function views_schema_6003() {
  289. $schema = views_schema(__FUNCTION__);
  290. $schema['views_view']['unique keys'] = array('name' => array('name'));
  291. unset($schema['views_view']['unique key']);
  292. return $schema;
  293. }
  294. function views_update_6003() {
  295. db_add_unique_key('views_view', 'name', array('name'));
  296. }
  297. /**
  298. * Enlarge the views_object_cache.data column to prevent truncation and JS
  299. * errors.
  300. */
  301. function views_schema_6004() {
  302. $schema = views_schema(__FUNCTION__);
  303. $schema['views_object_cache']['fields']['data']['type'] = 'text';
  304. $schema['views_object_cache']['fields']['data']['size'] = 'big';
  305. return $schema;
  306. }
  307. function views_update_6004() {
  308. $new_field = array(
  309. 'type' => 'text',
  310. 'size' => 'big',
  311. 'description' => 'Serialized data being stored.',
  312. 'serialize' => TRUE,
  313. );
  314. // Drop and re-add this field because there is a bug in
  315. // db_change_field that causes this to fail when trying to cast the data.
  316. db_drop_field('views_object_cache', 'data');
  317. db_add_field('views_object_cache', 'data', $new_field);
  318. }
  319. /**
  320. * Enlarge the base_table column.
  321. */
  322. function views_schema_6005() {
  323. $schema = views_schema(__FUNCTION__);
  324. $schema['views_view']['fields']['base_table']['length'] = 64;
  325. return $schema;
  326. }
  327. function views_update_6005() {
  328. $new_field = array(
  329. 'type' => 'varchar',
  330. 'length' => '64',
  331. 'default' => '',
  332. 'not null' => TRUE,
  333. 'description' => 'What table this view is based on, such as node, user, comment, or term.',
  334. );
  335. db_change_field('views_view', 'base_table', 'base_table', $new_field);
  336. }
  337. /**
  338. * Add the cache_views_data table to support standard caching.
  339. */
  340. function views_schema_6006() {
  341. $schema = views_schema(__FUNCTION__);
  342. $schema['cache_views_data'] = drupal_get_schema_unprocessed('system', 'cache');
  343. $schema['cache_views_data']['description'] = 'Cache table for views to store pre-rendered queries, results, and display output.';
  344. $schema['cache_views_data']['fields']['serialized']['default'] = 1;
  345. return $schema;
  346. }
  347. function views_update_6006() {
  348. $table = drupal_get_schema_unprocessed('system', 'cache');
  349. $table['description'] = 'Cache table for views to store pre-rendered queries, results, and display output.';
  350. $table['fields']['serialized']['default'] = 1;
  351. db_create_table('cache_views_data', $table);
  352. }
  353. /**
  354. * Add aggregate function to PostgreSQL so GROUP BY can be used to force only
  355. * one result to be returned for each item.
  356. */
  357. function views_update_6007() {
  358. if (Database::getConnection()->databaseType() == 'pgsql') {
  359. db_query('CREATE OR REPLACE FUNCTION first(anyelement, anyelement) RETURNS anyelement AS \'SELECT COALESCE($1, $2);\' LANGUAGE \'sql\';');
  360. db_query("DROP AGGREGATE IF EXISTS first(anyelement)");
  361. db_query("CREATE AGGREGATE first(sfunc = first, basetype = anyelement, stype = anyelement);");
  362. }
  363. }
  364. /**
  365. * Add the primary key to views_display table.
  366. */
  367. function views_schema_6008() {
  368. $schema = views_schema(__FUNCTION__);
  369. $schema['views_display']['primary key'] = array('vid', 'id');
  370. return $schema;
  371. }
  372. /**
  373. * Add the primary key to the views_display table.
  374. */
  375. function views_update_6008() {
  376. db_add_primary_key('views_display', array('vid', 'id'));
  377. }
  378. /**
  379. * Enlarge the views_display.display_options field to accommodate a larger set
  380. * of configurations (e. g. fields, filters, etc.) on a display.
  381. */
  382. function views_schema_6009() {
  383. $schema = views_schema(__FUNCTION__);
  384. $schema['views_display']['fields']['display_options'] = array(
  385. 'type' => 'text',
  386. 'size' => 'big',
  387. 'description' => 'A serialized array of options for this display; it contains options that are generally only pertinent to that display plugin type.',
  388. 'serialize' => TRUE,
  389. 'serialized default' => 'a:0:{}',
  390. );
  391. return $schema;
  392. }
  393. function views_update_6009() {
  394. $schema = views_schema_6009();
  395. if (Database::getConnection()->databaseType() == 'pgsql') {
  396. db_query('ALTER TABLE {views_display} RENAME "display_options" TO "display_options_old"');
  397. db_add_field('views_display', 'display_options', $schema['views_display']['fields']['display_options']);
  398. $sql = "SELECT vid, id, display_options_old FROM {views_display}";
  399. $result = db_query($sql);
  400. foreach ($result as $row) {
  401. $row['display_options_old'] = $row['display_options_old'];
  402. $sql = "UPDATE {views_display} SET display_options = :display_optons WHERE vid = :vid AND id = :id";
  403. db_query($sql, array(
  404. ':display_optons' => $row['display_options_old'],
  405. ':vid' => $row['vid'],
  406. ':id' => $row['id'],
  407. ));
  408. }
  409. db_drop_field('views_display', 'display_options_old');
  410. }
  411. else {
  412. db_change_field('views_display', 'display_options', 'display_options', $schema['views_display']['fields']['display_options']);
  413. }
  414. }
  415. /**
  416. * Remove the view_php field.
  417. */
  418. function views_schema_6010() {
  419. $schema = views_schema(__FUNCTION__);
  420. unset($schema['views_view']['fields']['view_php']);
  421. unset($schema['views_view']['fields']['is_cacheable']);
  422. return $schema;
  423. }
  424. /**
  425. * Remove the view_php and is_cacheable field.
  426. */
  427. function views_update_6010() {
  428. db_drop_field('views_view', 'view_php');
  429. db_drop_field('views_view', 'is_cacheable');
  430. }
  431. /**
  432. * Remove views_object_cache table and move the data to ctools_object_cache.
  433. */
  434. function views_schema_6011() {
  435. $schema = views_schema(__FUNCTION__);
  436. unset($schema['views_object_cache']);
  437. return $schema;
  438. }
  439. /**
  440. * Remove views_object_cache table and move the data to ctools_object_cache.
  441. */
  442. function views_update_6011() {
  443. $caches = db_query("SELECT * FROM {views_object_cache}");
  444. foreach ($caches as $item) {
  445. drupal_write_record('ctools_object_cache', $item);
  446. }
  447. db_drop_table('views_object_cache');
  448. }
  449. /**
  450. * Correct the cache setting for exposed filter blocks.
  451. *
  452. * @see http://drupal.org/node/910864
  453. */
  454. function views_update_6012() {
  455. // There is only one simple query to run.
  456. db_update('blocks')
  457. ->condition('module', 'views')
  458. ->condition('delta', db_like('-exp-') . '%', 'LIKE')
  459. ->fields(array('cache' => DRUPAL_NO_CACHE));
  460. }
  461. /**
  462. * Add a human readable name.
  463. */
  464. function views_schema_6013() {
  465. $schema = views_schema(__FUNCTION__);
  466. $schema['views_view']['fields']['human_name'] = array(
  467. 'type' => 'varchar',
  468. 'length' => '255',
  469. 'default' => '',
  470. 'description' => 'A human readable name used to be displayed in the admin interface',
  471. );
  472. return $schema;
  473. }
  474. function views_update_6013() {
  475. // Check because D6 installs may already have added this.
  476. if (!db_field_exists('views_view', 'human_name')) {
  477. $new_field = array(
  478. 'type' => 'varchar',
  479. 'length' => '255',
  480. 'default' => '',
  481. 'description' => 'A human readable name used to be displayed in the admin interface',
  482. );
  483. db_add_field('views_view', 'human_name', $new_field);
  484. }
  485. }
  486. function views_schema_6014() {
  487. $schema = views_schema(__FUNCTION__);
  488. $schema['views_view']['fields']['core'] = array(
  489. 'type' => 'int',
  490. 'default' => 0,
  491. 'description' => 'Stores the drupal core version of the view.',
  492. );
  493. return $schema;
  494. }
  495. /**
  496. * Add a drupal core version field.
  497. */
  498. function views_update_6014() {
  499. // Check because D6 installs may already have added this.
  500. if (!db_field_exists('views_view', 'core')) {
  501. $new_field = array(
  502. 'type' => 'int',
  503. 'default' => 0,
  504. 'description' => 'Stores the drupal core version of the view.',
  505. );
  506. db_add_field('views_view', 'core', $new_field);
  507. }
  508. }
  509. /**
  510. * Rename some system variables.
  511. */
  512. function views_update_7000() {
  513. // Views now lets users turn off query details on live preview.
  514. $query_on_top = variable_get('views_ui_query_on_top');
  515. if (isset($query_on_top)) {
  516. variable_set('views_ui_show_sql_query', TRUE);
  517. if ($query_on_top) {
  518. variable_set('views_ui_show_sql_query_where', 'above');
  519. }
  520. else {
  521. variable_set('views_ui_show_sql_query_where', 'below');
  522. }
  523. variable_del('views_ui_query_on_top');
  524. }
  525. // Rename the views_hide_help_message variable from negative to positive.
  526. $hide_help = variable_get('views_hide_help_message');
  527. if (isset($hide_help)) {
  528. variable_set('views_ui_show_advanced_help_warning', !$hide_help);
  529. variable_del('views_hide_help_message');
  530. }
  531. // Rename the unused views_no_hover_links variable.
  532. variable_del('views_no_hover_links');
  533. }
  534. /**
  535. * Fix missing items from Views administrative breadcrumb.
  536. */
  537. function views_update_7001() {
  538. $depth = db_select('menu_links')
  539. ->fields('menu_links', array('depth'))
  540. ->condition('link_path', 'admin/structure/views/view/%')
  541. ->execute()
  542. ->fetchField();
  543. if ($depth == 3) {
  544. db_delete('menu_links')
  545. ->condition('link_path', 'admin/structure/views/%', 'LIKE')
  546. ->execute();
  547. cache_clear_all(NULL, 'cache_menu');
  548. menu_rebuild();
  549. }
  550. }
  551. function views_schema_7300() {
  552. return views_schema_6013();
  553. }
  554. /**
  555. * Make sure the human_name field is added to the views_view table.
  556. *
  557. * If you updated from 6.x-2.x-dev to 7.x-3.x you already had schema
  558. * version 6014, so update 6013 never was nor will be run. As a result,
  559. * the human_name field is missing from the database.
  560. *
  561. * This will add the human_name field if it doesn't already exist.
  562. */
  563. function views_update_7300() {
  564. views_update_6013();
  565. }
  566. function views_schema_7301() {
  567. $schema = views_schema(__FUNCTION__);
  568. $schema['views_view']['fields']['name']['length'] = 128;
  569. return $schema;
  570. }
  571. /**
  572. * Enlarge the name column.
  573. */
  574. function views_update_7301() {
  575. $new_field = array(
  576. 'type' => 'varchar',
  577. 'length' => '128',
  578. 'default' => '',
  579. 'not null' => TRUE,
  580. '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.',
  581. );
  582. db_change_field('views_view', 'name', 'name', $new_field);
  583. }
  584. /**
  585. * Remove headers field from cache tables.
  586. *
  587. * @see system_update_7054()
  588. */
  589. function views_update_7302() {
  590. if (db_field_exists('cache_views', 'headers')) {
  591. db_drop_field('cache_views', 'headers');
  592. }
  593. if (db_field_exists('cache_views_data', 'headers')) {
  594. db_drop_field('cache_views_data', 'headers');
  595. }
  596. }