filter.install 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. <?php
  2. /**
  3. * @file
  4. * Install, update, and uninstall functions for the Filter module.
  5. */
  6. /**
  7. * Implements hook_schema().
  8. */
  9. function filter_schema() {
  10. $schema['filter'] = array(
  11. 'description' => 'Table that maps filters (HTML corrector) to text formats (Filtered HTML).',
  12. 'fields' => array(
  13. 'format' => array(
  14. 'type' => 'varchar',
  15. 'length' => 255,
  16. 'not null' => TRUE,
  17. 'description' => 'Foreign key: The {filter_format}.format to which this filter is assigned.',
  18. ),
  19. 'module' => array(
  20. 'type' => 'varchar',
  21. 'length' => 64,
  22. 'not null' => TRUE,
  23. 'default' => '',
  24. 'description' => 'The origin module of the filter.',
  25. ),
  26. 'name' => array(
  27. 'type' => 'varchar',
  28. 'length' => 32,
  29. 'not null' => TRUE,
  30. 'default' => '',
  31. 'description' => 'Name of the filter being referenced.',
  32. ),
  33. 'weight' => array(
  34. 'type' => 'int',
  35. 'not null' => TRUE,
  36. 'default' => 0,
  37. 'description' => 'Weight of filter within format.',
  38. ),
  39. 'status' => array(
  40. 'type' => 'int',
  41. 'not null' => TRUE,
  42. 'default' => 0,
  43. 'description' => 'Filter enabled status. (1 = enabled, 0 = disabled)',
  44. ),
  45. 'settings' => array(
  46. 'type' => 'blob',
  47. 'not null' => FALSE,
  48. 'size' => 'big',
  49. 'serialize' => TRUE,
  50. 'description' => 'A serialized array of name value pairs that store the filter settings for the specific format.',
  51. ),
  52. ),
  53. 'primary key' => array('format', 'name'),
  54. 'indexes' => array(
  55. 'list' => array('weight', 'module', 'name'),
  56. ),
  57. );
  58. $schema['filter_format'] = array(
  59. 'description' => 'Stores text formats: custom groupings of filters, such as Filtered HTML.',
  60. 'fields' => array(
  61. 'format' => array(
  62. 'type' => 'varchar',
  63. 'length' => 255,
  64. 'not null' => TRUE,
  65. 'description' => 'Primary Key: Unique machine name of the format.',
  66. ),
  67. 'name' => array(
  68. 'type' => 'varchar',
  69. 'length' => 255,
  70. 'not null' => TRUE,
  71. 'default' => '',
  72. 'description' => 'Name of the text format (Filtered HTML).',
  73. 'translatable' => TRUE,
  74. ),
  75. 'cache' => array(
  76. 'type' => 'int',
  77. 'not null' => TRUE,
  78. 'default' => 0,
  79. 'size' => 'tiny',
  80. 'description' => 'Flag to indicate whether format is cacheable. (1 = cacheable, 0 = not cacheable)',
  81. ),
  82. 'status' => array(
  83. 'type' => 'int',
  84. 'unsigned' => TRUE,
  85. 'not null' => TRUE,
  86. 'default' => 1,
  87. 'size' => 'tiny',
  88. 'description' => 'The status of the text format. (1 = enabled, 0 = disabled)',
  89. ),
  90. 'weight' => array(
  91. 'type' => 'int',
  92. 'not null' => TRUE,
  93. 'default' => 0,
  94. 'description' => 'Weight of text format to use when listing.',
  95. ),
  96. ),
  97. 'primary key' => array('format'),
  98. 'unique keys' => array(
  99. 'name' => array('name'),
  100. ),
  101. 'indexes' => array(
  102. 'status_weight' => array('status', 'weight'),
  103. ),
  104. );
  105. $schema['cache_filter'] = drupal_get_schema_unprocessed('system', 'cache');
  106. $schema['cache_filter']['description'] = 'Cache table for the Filter module to store already filtered pieces of text, identified by text format and hash of the text.';
  107. return $schema;
  108. }
  109. /**
  110. * Implements hook_install().
  111. */
  112. function filter_install() {
  113. // All sites require at least one text format (the fallback format) that all
  114. // users have access to, so add it here. We initialize it as a simple, safe
  115. // plain text format with very basic formatting, but it can be modified by
  116. // installation profiles to have other properties.
  117. $plain_text_format = array(
  118. 'format' => 'plain_text',
  119. 'name' => 'Plain text',
  120. 'weight' => 10,
  121. 'filters' => array(
  122. // Escape all HTML.
  123. 'filter_html_escape' => array(
  124. 'weight' => 0,
  125. 'status' => 1,
  126. ),
  127. // URL filter.
  128. 'filter_url' => array(
  129. 'weight' => 1,
  130. 'status' => 1,
  131. ),
  132. // Line break filter.
  133. 'filter_autop' => array(
  134. 'weight' => 2,
  135. 'status' => 1,
  136. ),
  137. ),
  138. );
  139. $plain_text_format = (object) $plain_text_format;
  140. filter_format_save($plain_text_format);
  141. // Set the fallback format to plain text.
  142. variable_set('filter_fallback_format', $plain_text_format->format);
  143. }
  144. /**
  145. * Implements hook_update_dependencies().
  146. */
  147. function filter_update_dependencies() {
  148. // filter_update_7005() migrates role permissions and therefore must run
  149. // after the {role} and {role_permission} tables are properly set up, which
  150. // happens in user_update_7007().
  151. $dependencies['filter'][7005] = array(
  152. 'user' => 7007,
  153. );
  154. return $dependencies;
  155. }
  156. /**
  157. * @addtogroup updates-6.x-to-7.x
  158. * @{
  159. */
  160. /**
  161. * Upgrade the {filter_formats} table and rename it to {filter_format}.
  162. */
  163. function filter_update_7000() {
  164. db_rename_table('filter_formats', 'filter_format');
  165. // Add the new {filter_format}.status and {filter_format}.weight column.
  166. db_add_field('filter_format', 'status', array(
  167. 'type' => 'int',
  168. 'unsigned' => TRUE,
  169. 'not null' => TRUE,
  170. 'default' => 1,
  171. 'size' => 'tiny',
  172. 'description' => 'The status of the text format. (1 = enabled, 0 = disabled)',
  173. ));
  174. db_add_field('filter_format', 'weight', array(
  175. 'type' => 'int',
  176. 'not null' => TRUE,
  177. 'default' => 0,
  178. 'description' => 'Weight of text format to use when listing.',
  179. ), array(
  180. 'indexes' => array(
  181. 'status_weight' => array('status', 'weight'),
  182. ),
  183. ));
  184. }
  185. /**
  186. * Break out "escape HTML filter" option to its own filter.
  187. */
  188. function filter_update_7001() {
  189. $result = db_query("SELECT format FROM {filter_format}")->fetchCol();
  190. $insert = db_insert('filters')->fields(array('format', 'module', 'delta', 'weight'));
  191. foreach ($result as $format_id) {
  192. // Deprecated constants FILTER_HTML_STRIP = 1 and FILTER_HTML_ESCAPE = 2.
  193. if (variable_get('filter_html_' . $format_id, 1) == 2) {
  194. $insert->values(array(
  195. 'format' => $format_id,
  196. 'module' => 'filter',
  197. 'delta' => 4,
  198. 'weight' => 0,
  199. ));
  200. }
  201. variable_del('filter_html_' . $format_id);
  202. }
  203. $insert->execute();
  204. }
  205. /**
  206. * Upgrade the {filter} table for core filters.
  207. */
  208. function filter_update_7003() {
  209. // Duplicates the {filters} table since core cannot take care of the potential
  210. // contributed module filters.
  211. db_rename_table('filters', 'd6_upgrade_filter');
  212. // Creates the Drupal 7 filter table.
  213. $filter_table = array(
  214. 'description' => 'Table that maps filters (HTML corrector) to text formats (Filtered HTML).',
  215. 'fields' => array(
  216. 'format' => array(
  217. 'type' => 'int',
  218. 'not null' => TRUE,
  219. 'default' => 0,
  220. 'description' => 'Foreign key: The {filter_format}.format to which this filter is assigned.',
  221. ),
  222. 'module' => array(
  223. 'type' => 'varchar',
  224. 'length' => 64,
  225. 'not null' => TRUE,
  226. 'default' => '',
  227. 'description' => 'The origin module of the filter.',
  228. ),
  229. 'name' => array(
  230. 'type' => 'varchar',
  231. 'length' => 32,
  232. 'not null' => TRUE,
  233. 'default' => '',
  234. 'description' => 'Name of the filter being referenced.',
  235. ),
  236. 'weight' => array(
  237. 'type' => 'int',
  238. 'not null' => TRUE,
  239. 'default' => 0,
  240. 'description' => 'Weight of filter within format.',
  241. ),
  242. 'status' => array(
  243. 'type' => 'int',
  244. 'not null' => TRUE,
  245. 'default' => 0,
  246. 'description' => 'Filter enabled status. (1 = enabled, 0 = disabled)',
  247. ),
  248. 'settings' => array(
  249. 'type' => 'blob',
  250. 'not null' => FALSE,
  251. 'size' => 'big',
  252. 'serialize' => TRUE,
  253. 'description' => 'A serialized array of name value pairs that store the filter settings for the specific format.',
  254. ),
  255. ),
  256. 'primary key' => array('format', 'name'),
  257. 'indexes' => array(
  258. 'list' => array('weight', 'module', 'name'),
  259. ),
  260. );
  261. db_create_table('filter', $filter_table);
  262. // Get an array of the renamed filter deltas, organized by module.
  263. $renamed_deltas = array(
  264. 'filter' => array(
  265. '0' => 'filter_html',
  266. '1' => 'filter_autop',
  267. '2' => 'filter_url',
  268. '3' => 'filter_htmlcorrector',
  269. '4' => 'filter_html_escape',
  270. ),
  271. 'php' => array(
  272. '0' => 'php_code',
  273. ),
  274. );
  275. // Loop through each filter and make changes to the core filter table by
  276. // each record from the old to the new table.
  277. foreach ($renamed_deltas as $module => $deltas) {
  278. foreach ($deltas as $old_delta => $new_name) {
  279. $query = db_select('d6_upgrade_filter')
  280. ->fields('d6_upgrade_filter', array('format', 'weight'))
  281. ->condition('module', $module)
  282. ->condition('delta', $old_delta)
  283. ->distinct();
  284. foreach ($query->execute() as $record) {
  285. // Port the filter settings.
  286. $settings = array();
  287. if ($new_name == 'filter_html') {
  288. if ($setting = variable_get("allowed_html_{$record->format}", NULL)) {
  289. $settings['allowed_html'] = $setting;
  290. variable_del("allowed_html_{$record->format}");
  291. }
  292. if ($setting = variable_get("filter_html_help_{$record->format}", NULL)) {
  293. $settings['filter_html_help'] = $setting;
  294. variable_del("filter_html_help_{$record->format}");
  295. }
  296. if ($setting = variable_get("filter_html_nofollow_{$record->format}", NULL)) {
  297. $settings['filter_html_nofollow'] = $setting;
  298. variable_del("filter_html_nofollow_{$record->format}");
  299. }
  300. }
  301. elseif ($new_name == 'filter_url') {
  302. if ($setting = variable_get("filter_url_length_{$record->format}", NULL)) {
  303. $settings['filter_url_length'] = $setting;
  304. variable_del("filter_url_length_{$record->format}");
  305. }
  306. }
  307. db_insert('filter')
  308. ->fields(array(
  309. 'format' => $record->format,
  310. 'module' => $module,
  311. 'name' => $new_name,
  312. 'weight' => $record->weight,
  313. 'settings' => serialize($settings),
  314. 'status' => 1,
  315. ))
  316. ->execute();
  317. }
  318. db_delete('d6_upgrade_filter')
  319. ->condition('module', $module)
  320. ->condition('delta', $old_delta)
  321. ->execute();
  322. }
  323. }
  324. }
  325. /**
  326. * Integrate text formats with the user permissions system.
  327. *
  328. * This function converts text format role assignments to use the new text
  329. * format permissions introduced in Drupal 7, creates a fallback (plain text)
  330. * format that is available to all users, and explicitly sets the text format
  331. * in cases that used to rely on a single site-wide default.
  332. */
  333. function filter_update_7005() {
  334. // Move role data from the filter system to the user permission system.
  335. $all_roles = array_keys(user_roles());
  336. $default_format = variable_get('filter_default_format', 1);
  337. $result = db_query("SELECT * FROM {filter_format}");
  338. foreach ($result as $format) {
  339. // We need to assign the default format to all roles (regardless of what
  340. // was stored in the database) to preserve the behavior of the site at the
  341. // moment of the upgrade.
  342. $format_roles = ($format->format == $default_format ? $all_roles : explode(',', $format->roles));
  343. foreach ($format_roles as $format_role) {
  344. if (in_array($format_role, $all_roles)) {
  345. _update_7000_user_role_grant_permissions($format_role, array('use text format ' . $format->format), 'filter');
  346. }
  347. }
  348. }
  349. // Drop the roles field from the {filter_format} table.
  350. db_drop_field('filter_format', 'roles');
  351. // Add a fallback text format which outputs plain text and appears last on
  352. // the list for all users. Generate a unique name for it, starting with
  353. // "Plain text".
  354. $start_name = 'Plain text';
  355. $format_name = $start_name;
  356. while ($format = db_query('SELECT format FROM {filter_format} WHERE name = :name', array(':name' => $format_name))->fetchField()) {
  357. $id = empty($id) ? 2 : $id + 1;
  358. $format_name = $start_name . ' ' . $id;
  359. }
  360. // Insert the filter format.
  361. $format_id = db_insert('filter_format')
  362. ->fields(array(
  363. 'name' => $format_name,
  364. 'cache' => 1,
  365. 'weight' => 1,
  366. 'status' => 1,
  367. ))
  368. ->execute();
  369. // This format should output plain text, so we escape all HTML and apply the
  370. // line break and URL filters only.
  371. db_insert('filter')
  372. ->fields(array(
  373. 'format',
  374. 'name',
  375. 'weight',
  376. 'status',
  377. 'module',
  378. 'settings',
  379. ))
  380. ->values(array(
  381. 'format' => $format_id,
  382. 'name' => 'filter_html_escape',
  383. 'weight' => 0,
  384. 'status' => 1,
  385. 'module' => 'filter',
  386. 'settings' => serialize(array()),
  387. ))
  388. ->values(array(
  389. 'format' => $format_id,
  390. 'name' => 'filter_url',
  391. 'weight' => 1,
  392. 'status' => 1,
  393. 'module' => 'filter',
  394. 'settings' => serialize(array()),
  395. ))
  396. ->values(array(
  397. 'format' => $format_id,
  398. 'name' => 'filter_autop',
  399. 'weight' => 2,
  400. 'status' => 1,
  401. 'module' => 'filter',
  402. 'settings' => serialize(array()),
  403. ))
  404. ->execute();
  405. variable_set('filter_fallback_format', $format_id);
  406. drupal_set_message('A new <em>Plain text</em> format has been created which will be available to all users. You can configure this text format on the <a href="' . url('admin/config/content/formats/' . $format) . '">text format configuration page</a>.');
  407. // Move the former site-wide default text format to the top of the list, so
  408. // that it continues to be the default text format for all users.
  409. db_update('filter_format')
  410. ->fields(array('weight' => -1))
  411. ->condition('format', $default_format)
  412. ->execute();
  413. // We do not delete the 'filter_default_format' variable, since other modules
  414. // need it in their update functions; for an example, see user_update_7010().
  415. // @todo This variable can be deleted in Drupal 8.
  416. }
  417. /**
  418. * Grant usage of all text formats to user roles having the 'administer filters' permission.
  419. */
  420. function filter_update_7008() {
  421. // Build the list of permissions to grant.
  422. $permissions = array();
  423. foreach (db_query('SELECT format FROM {filter_format}')->fetchCol() as $format_id) {
  424. if ($format_id != variable_get('filter_fallback_format')) {
  425. $permissions[] = 'use text format ' . $format_id;
  426. }
  427. }
  428. // Grant text format permissions to all roles that can 'administer filters'.
  429. // Albeit anonymous users *should not* have the permission, we cannot presume
  430. // that they do not or must not.
  431. if ($roles = user_roles(FALSE, 'administer filters')) {
  432. foreach ($roles as $rid => $name) {
  433. _update_7000_user_role_grant_permissions($rid, $permissions, 'filter');
  434. }
  435. }
  436. }
  437. /**
  438. * Converts fields that store serialized variables from text to blob.
  439. */
  440. function filter_update_7009() {
  441. $schema = system_schema_cache_7054();
  442. db_drop_table('cache_filter');
  443. db_create_table('cache_filter', $schema);
  444. }
  445. /**
  446. * Change {filter_format}.format and {filter}.format into varchar.
  447. */
  448. function filter_update_7010() {
  449. db_change_field('filter_format', 'format', 'format', array(
  450. 'type' => 'varchar',
  451. 'length' => 255,
  452. 'not null' => TRUE,
  453. 'description' => 'Primary Key: Unique machine name of the format.',
  454. ));
  455. db_change_field('filter', 'format', 'format', array(
  456. 'type' => 'varchar',
  457. 'length' => 255,
  458. 'not null' => TRUE,
  459. 'description' => 'Foreign key: The {filter_format}.format to which this filter is assigned.',
  460. ));
  461. }
  462. /**
  463. * @} End of "addtogroup updates-6.x-to-7.x".
  464. */