media.install 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231
  1. <?php
  2. /**
  3. * @file
  4. * Install, update and uninstall functions for the Media module.
  5. */
  6. /**
  7. * Implements hook_install().
  8. */
  9. function media_install() {
  10. // Make sure that we set the icon base directory variable if it is not
  11. // already set.
  12. $base = variable_get('media_icon_base_directory', NULL);
  13. if (!isset($base)) {
  14. $default_base = 'public://media-icons';
  15. variable_set('media_icon_base_directory', $default_base);
  16. }
  17. try {
  18. _media_install_copy_icons();
  19. }
  20. catch (Exception $e) {
  21. watchdog_exception('media', $e);
  22. }
  23. }
  24. /**
  25. * Copy the media file icons to files directory for use with image styles.
  26. */
  27. function _media_install_copy_icons() {
  28. $destination = variable_get('media_icon_base_directory', 'public://media-icons') . '/' . variable_get('media_icon_set', 'default');
  29. if (!file_prepare_directory($destination, FILE_MODIFY_PERMISSIONS || FILE_CREATE_DIRECTORY)) {
  30. throw new Exception("Unable to create directory $destination.");
  31. }
  32. // @todo If we ever add another default icon set, this should copy all images from one directory up.
  33. $source = drupal_get_path('module', 'media') . '/images/icons/' . variable_get('media_icon_set', 'default');
  34. $files = file_scan_directory($source, '/.*\.(png|jpg)$/');
  35. foreach ($files as $file) {
  36. $result = file_unmanaged_copy($file->uri, $destination, FILE_EXISTS_REPLACE);
  37. if (!$result) {
  38. throw new Exception("Unable to copy {$file->uri} to $destination.");
  39. }
  40. }
  41. }
  42. /**
  43. * Implements hook_uninstall().
  44. */
  45. function media_uninstall() {
  46. // Remove variables.
  47. variable_del('media_dialog_theme');
  48. variable_del('media_icon_base_directory');
  49. variable_del('media_icon_set');
  50. variable_del('media_show_deprecated_view_modes');
  51. // Dialog options.
  52. variable_del('media_dialogclass');
  53. variable_del('media_modal');
  54. variable_del('media_draggable');
  55. variable_del('media_resizable');
  56. variable_del('media_minwidth');
  57. variable_del('media_width');
  58. variable_del('media_height');
  59. variable_del('media_position');
  60. variable_del('media_zindex');
  61. variable_del('media_backgroundcolor');
  62. variable_del('media_opacity');
  63. // bulk edit option
  64. variable_del('media_bulk_upload_edit');
  65. }
  66. /**
  67. * Implements hook_update_dependencies().
  68. */
  69. function media_update_dependencies() {
  70. // media_update_7200() needs to convert old 'media' permissions to new 'file'
  71. // permissions, so it must run before file_entity_7208 which updates existing
  72. // 'file' permissions to be split per file type.
  73. $dependencies['file_entity'][7208] = array(
  74. 'media' => 7200,
  75. );
  76. // This update function requires field_update_7002() to run before it since
  77. // the field_bundle_settings variable has been split into separate variables
  78. // per entity type and bundle.
  79. $dependencies['media'][7016] = array(
  80. 'field' => 7002,
  81. 'rules' => 7205,
  82. );
  83. // Those updates require {file_type} table created.
  84. $dependencies['media'][7200] = array(
  85. 'file_entity' => 7207,
  86. );
  87. // Require {file_type}.mimetypes column before updating them.
  88. $dependencies['media'][7208] = array(
  89. 'file_entity' => 7210,
  90. );
  91. $dependencies['media'][7212] = array(
  92. 'file_entity' => 7210,
  93. );
  94. return $dependencies;
  95. }
  96. /**
  97. * Implements hook_requirements().
  98. */
  99. function media_requirements($phase) {
  100. $t = get_t();
  101. // Make sure that file_entity module is 2.x version.
  102. // We can't add this check in .info file because drupal.org testbot can't
  103. // handle it. See #1734648.
  104. $requirements = array();
  105. if ($phase == 'update') {
  106. $info = system_get_info('module', 'file_entity');
  107. $file_entity_version = $info['version'];
  108. $file_entity_minimum_date = 1495439635;
  109. if (isset($info['datestamp'])) {
  110. $file_entity_installed_date = $info['datestamp'];
  111. }
  112. else {
  113. if (strpos($file_entity_version, '7.x-3') !== FALSE) {
  114. // Good version, any number that passes the condition is good.
  115. $file_entity_installed_date = $file_entity_minimum_date + 1;
  116. }
  117. else {
  118. // Check version 2 release and parse into a number.
  119. // No need to check version 1 as that's checked below.
  120. $version_number = str_replace('7.x-2.', '', $file_entity_version);
  121. if (is_numeric($version_number)) {
  122. // Plain number, so good version.
  123. $file_entity_installed_date = $file_entity_minimum_date + 1;
  124. }
  125. else {
  126. // Bad version or release (beta, alpha, unstable...).
  127. $file_entity_installed_date = $file_entity_minimum_date - 1;
  128. }
  129. }
  130. }
  131. // Time of 2017-05-22 07:53:55 is most recent commit in 7.x-2.0.
  132. if ($file_entity_installed_date < $file_entity_minimum_date) {
  133. $description = $t('Media 2.10 or newer requires <a href="@url">File entity 2.0 or newer (2.4 or higher recommended)</a>. Please download the correct version and make sure you have deleted the file_entity folder inside the media module directory.', array('@url' => 'http://drupal.org/project/file_entity'));
  134. $requirements['file_entity']['description'] = $description;
  135. $requirements['file_entity']['severity'] = REQUIREMENT_ERROR;
  136. $requirements['file_entity']['value'] = $file_entity_version;
  137. $requirements['file_entity']['title'] = $t('Fieldable Files (file_entity) module 7.x-2.0 or newer is required by Media');
  138. drupal_set_message($description, 'error', TRUE);
  139. }
  140. if (strpos($info['version'], '7.x-2') === FALSE &&
  141. strpos($info['version'], '7.x-3') === FALSE) {
  142. $requirements['file_entity'] = array(
  143. 'title' => $t('File entity 2.x or 3.x required.'),
  144. 'value' => $t('Wrong version'),
  145. 'severity' => REQUIREMENT_ERROR,
  146. 'description' => $t('Media requires <a href="@url">File entity 2.x or 3.x</a>. Please download the correct version and make sure you have deleted the file_entity folder inside the media module directory.', array('@url' => 'http://drupal.org/project/file_entity')),
  147. );
  148. }
  149. }
  150. return $requirements;
  151. }
  152. /**
  153. * Deprecated update function.
  154. */
  155. function media_update_7000() {
  156. }
  157. /**
  158. * Deprecated update function.
  159. */
  160. function media_update_7001() {
  161. }
  162. /**
  163. * Create the media_type table from the media_types variable.
  164. */
  165. function media_update_7002() {
  166. if (db_table_exists('media_type')) {
  167. return;
  168. }
  169. $schema['media_type'] = array(
  170. 'description' => 'Stores the settings for media types.',
  171. 'fields' => array(
  172. 'name' => array(
  173. 'description' => 'The machine name of the media type.',
  174. 'type' => 'varchar',
  175. 'length' => 255,
  176. 'not null' => TRUE,
  177. 'default' => '',
  178. ),
  179. 'label' => array(
  180. 'description' => 'The label of the media type.',
  181. 'type' => 'varchar',
  182. 'length' => 255,
  183. 'not null' => TRUE,
  184. 'default' => '',
  185. ),
  186. 'base' => array(
  187. 'description' => 'If this is a base type (i.e. cannot be deleted)',
  188. 'type' => 'int',
  189. 'not null' => TRUE,
  190. 'default' => 0,
  191. 'size' => 'tiny',
  192. ),
  193. 'weight' => array(
  194. 'description' => 'Weight of media type. Determines which one wins when claiming a piece of media (first wins)',
  195. 'type' => 'int',
  196. 'not null' => TRUE,
  197. 'default' => 0,
  198. 'size' => 'normal',
  199. ),
  200. 'type_callback' => array(
  201. 'description' => 'Callback to determine if provided media is of this type.',
  202. 'type' => 'varchar',
  203. 'length' => 255,
  204. 'not null' => FALSE,
  205. 'default' => '',
  206. ),
  207. 'type_callback_args' => array(
  208. 'type' => 'text',
  209. 'not null' => FALSE,
  210. 'size' => 'big',
  211. 'serialize' => TRUE,
  212. 'description' => 'A serialized array of name value pairs that will be passed to the callback function',
  213. ),
  214. ),
  215. 'primary key' => array('name'),
  216. );
  217. db_create_table('media_type', $schema['media_type']);
  218. drupal_load('module', 'media');
  219. $old_types = variable_get('media_types', array());
  220. foreach ($old_types as $type) {
  221. // Was an error in the original creation.
  222. if (isset($type->callbacks)) {
  223. unset($type->callbacks);
  224. }
  225. $type->name = $type->machine_name;
  226. unset($type->machine_name);
  227. db_merge('media_type')
  228. ->key(array('name' => $type->name))
  229. ->fields((array) $type)
  230. ->execute();
  231. }
  232. variable_del('media_types');
  233. }
  234. /**
  235. * We now prefix media namespaced variables with media__, so fix old variables.
  236. */
  237. function media_update_7003() {
  238. drupal_load('module', 'media');
  239. foreach (media_variable_default() as $variable => $value) {
  240. if (($test = variable_get('media_' . $variable, TRUE)) == variable_get('media_' . $variable, FALSE)) {
  241. media_variable_set($variable, $test);
  242. variable_del('media_' . $variable);
  243. }
  244. }
  245. }
  246. /**
  247. * Empty update function to trigger a menu rebuild.
  248. */
  249. function media_update_7004() {
  250. }
  251. /**
  252. * Deprecated update function.
  253. */
  254. function media_update_7005() {
  255. }
  256. /**
  257. * Rename the file table to file_managed in case head2head was used.
  258. */
  259. function media_update_7006() {
  260. if (db_table_exists('file') && !db_table_exists('file_managed')) {
  261. db_rename_table('file', 'file_managed');
  262. }
  263. }
  264. /**
  265. * Deprecated update function.
  266. */
  267. function media_update_7007() {
  268. }
  269. /**
  270. * Empty function.
  271. */
  272. function media_update_7008() {
  273. }
  274. /**
  275. * Deprecated update function.
  276. */
  277. function media_update_7009() {
  278. }
  279. /**
  280. * Deprecated update function.
  281. */
  282. function media_update_7010() {
  283. }
  284. /**
  285. * Empty update function.
  286. */
  287. function media_update_7011() {
  288. }
  289. /**
  290. * Empty update function.
  291. */
  292. function media_update_7012() {
  293. }
  294. /**
  295. * Work around a core bug where text format cacheability is not updated.
  296. *
  297. * @see http://drupal.org/node/993230
  298. */
  299. function media_update_7013() {
  300. $formats = filter_formats();
  301. foreach ($formats as $format) {
  302. $format->filters = filter_list_format($format->format);
  303. // filter_format_save() expects filters to be an array, however
  304. // filter_list_format() gives us objects.
  305. foreach ($format->filters as $key => $value) {
  306. $format->filters[$key] = (array) $value;
  307. }
  308. filter_format_save($format);
  309. }
  310. }
  311. /**
  312. * Rename the media__dialog_get_theme_name variable to media__dialog_theme.
  313. */
  314. function media_update_7014() {
  315. if ($old_value = variable_get('media__dialog_get_theme_name')) {
  316. variable_del('media__dialog_get_theme_name');
  317. variable_set('media__dialog_theme', $old_value);
  318. }
  319. }
  320. /**
  321. * Empty update function to trigger a registry rebuild.
  322. */
  323. function media_update_7015() {
  324. }
  325. /**
  326. * Convert Media entities to File entities.
  327. *
  328. * This update function requires field_update_7002() to run before it since
  329. * the field_bundle_settings variable has been split into separate variables
  330. * per entity type and bundle.
  331. *
  332. * @see http://drupal.org/node/1418708
  333. * @see http://drupal.org/node/1211008
  334. */
  335. function media_update_7016() {
  336. // Allow File Entity module to take over the {file_managed}.type field. It
  337. // will create new indexes as it needs to, but it doesn't know about old ones,
  338. // so delete them.
  339. if (db_index_exists('file_managed', 'file_type')) {
  340. db_drop_index('file_managed', 'file_type');
  341. }
  342. module_enable(array('file_entity'));
  343. // Move all field instances from Media entity to File entity.
  344. $instances = field_read_instances(array('entity_type' => 'media'), array('include_inactive' => TRUE, 'include_deleted' => TRUE));
  345. foreach ($instances as $instance) {
  346. // Skip the old self-referencing file field. It will be deleted later in
  347. // this function.
  348. if ($instance['field_name'] === 'file') {
  349. continue;
  350. }
  351. // @todo Convert this to use _update_7000_field_read_fields()
  352. $fields = field_read_fields(array('id' => $instance['field_id']), array('include_inactive' => TRUE, 'include_deleted' => TRUE));
  353. $field = $fields[$instance['field_id']];
  354. // There is no API for updating the entity_type foreign key within field
  355. // data storage. We can do a direct db_update() for when the default SQL
  356. // storage back-end is being used, but must skip updating fields that use a
  357. // different storage type.
  358. if ($field['storage']['type'] !== 'field_sql_storage' || !module_exists('field_sql_storage') || !$field['storage']['active']) {
  359. $messages[] = t('Cannot update field %id (%field_name) because it does not use the field_sql_storage storage type.', array(
  360. '%id' => $field['id'],
  361. '%field_name' => $field['field_name'],
  362. ));
  363. continue;
  364. }
  365. // Update the data tables.
  366. $table_name = _field_sql_storage_tablename($field);
  367. $revision_name = _field_sql_storage_revision_tablename($field);
  368. db_update($table_name)
  369. ->fields(array('entity_type' => 'file'))
  370. ->condition('entity_type', 'media')
  371. ->condition('bundle', $instance['bundle'])
  372. ->execute();
  373. db_update($revision_name)
  374. ->fields(array('entity_type' => 'file'))
  375. ->condition('entity_type', 'media')
  376. ->condition('bundle', $instance['bundle'])
  377. ->execute();
  378. // Once all the data has been updated, update the {field_config_instance}
  379. // record.
  380. db_update('field_config_instance')
  381. ->fields(array('entity_type' => 'file'))
  382. ->condition('id', $instance['id'])
  383. ->execute();
  384. }
  385. // Update the field_bundle_settings configuration variable: move media bundle
  386. // settings to file bundles, and move settings of the old self-referencing
  387. // file field to the new file pseudo-field.
  388. foreach ($instances as $instance) {
  389. if ($instance['field_name'] === 'file' && !$instance['deleted']) {
  390. $file_settings = field_bundle_settings('file', $instance['bundle']);
  391. $media_settings = field_bundle_settings('media', $instance['bundle']);
  392. $file_settings = array_merge($file_settings, $media_settings);
  393. if (isset($instance['widget']['weight'])) {
  394. $file_settings['extra_fields']['form']['file']['weight'] = $instance['widget']['weight'];
  395. }
  396. if (isset($instance['display'])) {
  397. foreach ($instance['display'] as $view_mode => $display) {
  398. if (isset($display['weight'])) {
  399. $file_settings['extra_fields']['display']['file'][$view_mode]['weight'] = $display['weight'];
  400. }
  401. if (isset($display['type'])) {
  402. $file_settings['extra_fields']['display']['file'][$view_mode]['visible'] = ($display['type'] != 'hidden');
  403. }
  404. }
  405. }
  406. field_bundle_settings('file', $instance['bundle'], $file_settings);
  407. }
  408. }
  409. // Delete old media bundle settings.
  410. db_delete('variable')
  411. ->condition('name', db_like('field_bundle_settings_media__') . '%', 'LIKE')
  412. ->execute();
  413. // Copy field formatter settings of old self-referencing file field to file
  414. // pseudo-field formatter settings.
  415. $file_displays = variable_get('file_displays', array());
  416. foreach ($instances as $instance) {
  417. if ($instance['field_name'] === 'file' && !$instance['deleted']) {
  418. if (isset($instance['display'])) {
  419. foreach ($instance['display'] as $view_mode => $display) {
  420. if (isset($display['type']) && $display['type'] != 'hidden') {
  421. $file_formatter = 'file_field_' . $display['type'];
  422. $file_displays[$instance['bundle']][$view_mode][$file_formatter]['status'] = TRUE;
  423. if (isset($display['settings'])) {
  424. $file_displays[$instance['bundle']][$view_mode][$file_formatter]['settings'] = $display['settings'];
  425. }
  426. }
  427. }
  428. }
  429. }
  430. }
  431. variable_set('file_displays', $file_displays);
  432. // Delete the old self-referencing file field instances. If all instances are
  433. // deleted, field_delete_instance() will delete the field too.
  434. foreach ($instances as $instance) {
  435. if ($instance['field_name'] === 'file' && !$instance['deleted']) {
  436. field_delete_instance($instance);
  437. }
  438. }
  439. field_cache_clear();
  440. }
  441. /**
  442. * Move file display configuration.
  443. *
  444. * Move file display configurations from the 'file_displays' variable to the
  445. * {file_display} table.
  446. */
  447. function media_update_7017() {
  448. // If the {file_display} table doesn't exist, then the File Entity module's
  449. // update functions will automatically take care of migrating the
  450. // configurations. However, if file_entity_update_7001() has already run
  451. // prior to media_update_7016(), run it again in order to capture those
  452. // configurations too.
  453. if (db_table_exists('file_display') && function_exists('file_entity_update_7001')) {
  454. module_load_include('install', 'file_entity', 'file_entity');
  455. file_entity_update_7001();
  456. }
  457. }
  458. /**
  459. * Empty update function to trigger a menu rebuild.
  460. */
  461. function media_update_7018() {
  462. }
  463. /**
  464. * Update old view mode formaters.
  465. *
  466. * Update old per-view-mode media field formatters to the generic media
  467. * formatter with a setting.
  468. */
  469. function media_update_7019() {
  470. $instances = array();
  471. $fields = field_read_fields(array('type' => 'media'), array('include_inactive' => TRUE));
  472. foreach ($fields as $field) {
  473. $instances = array_merge($instances, field_read_instances(array('field_id' => $field['id']), array('include_inactive' => TRUE)));
  474. }
  475. foreach ($instances as $instance) {
  476. $update_instance = FALSE;
  477. foreach ($instance['display'] as $view_mode => $display) {
  478. if (in_array($display['type'], array('media_link', 'media_preview', 'media_small', 'media_large', 'media_original'))) {
  479. $update_instance = TRUE;
  480. $instance['display'][$view_mode]['type'] = 'media';
  481. $instance['display'][$view_mode]['settings'] = array('file_view_mode' => $display['type']);
  482. }
  483. }
  484. if ($update_instance) {
  485. field_update_instance($instance);
  486. }
  487. }
  488. }
  489. /**
  490. * Delete the wysiwyg_allowed_types variable if it is the same as default.
  491. */
  492. function media_update_7020() {
  493. if (variable_get('media__wysiwyg_allowed_types') == array('image', 'video')) {
  494. variable_del('media__wysiwyg_allowed_types');
  495. }
  496. }
  497. /**
  498. * Rerun media_update_7002() due to a typo that would prevent table creation.
  499. */
  500. function media_update_7021() {
  501. media_update_7002();
  502. }
  503. /**
  504. * Replace 'view media' perm from all users having the role with 'view file'.
  505. */
  506. function media_update_7200() {
  507. $perms = user_permission_get_modules();
  508. if (!isset($perms['view files'])) {
  509. throw new DrupalUpdateException('The File Entity module needs to be upgraded before continuing.');
  510. }
  511. else {
  512. $roles = user_roles(FALSE, 'view media');
  513. $permissions = array(
  514. 'view media' => FALSE,
  515. 'view files' => TRUE,
  516. );
  517. foreach ($roles as $rid => $role) {
  518. user_role_change_permissions($rid, $permissions);
  519. }
  520. $roles = user_roles(FALSE, 'edit media');
  521. $permissions = array(
  522. 'edit media' => FALSE,
  523. 'edit any files' => TRUE,
  524. );
  525. if (function_exists('file_entity_list_permissions')) {
  526. unset($permissions['edit any files']);
  527. foreach (file_entity_permissions_get_configured_types() as $type) {
  528. $permissions += file_entity_list_permissions($type);
  529. }
  530. }
  531. foreach ($roles as $rid => $role) {
  532. user_role_change_permissions($rid, $permissions);
  533. }
  534. $roles = user_roles(FALSE, 'administer media');
  535. $permissions = array(
  536. 'administer media' => FALSE,
  537. 'administer files' => TRUE,
  538. );
  539. foreach ($roles as $rid => $role) {
  540. user_role_change_permissions($rid, $permissions);
  541. }
  542. }
  543. }
  544. /**
  545. * Handle existing media fields.
  546. *
  547. * Enable the new Media Field module if this site uses "media" fields. File
  548. * fields are now preferred for storing media.
  549. */
  550. function media_update_7201() {
  551. $fields = field_info_fields();
  552. foreach ($fields as $field) {
  553. if ($field['type'] == 'media') {
  554. // This update function may run even if Media is not enabled. Don't enable
  555. // Media Field if its dependencies aren't already enabled.
  556. module_enable(array('mediafield'), FALSE);
  557. // Update entries in file_usage so that they are associated with Media
  558. // Field rather than Media.
  559. // @TODO This update function may conflict with
  560. // http://drupal.org/node/1268116
  561. db_update('file_usage')
  562. ->condition('module', 'media')
  563. ->fields(array('module' => 'mediafield'))
  564. ->execute();
  565. return t('The "Media" field type has been moved to the new "Media Field" module. This site uses media fields, so the Media Field module has been enabled.');
  566. }
  567. }
  568. return t('The "Media" field type has been moved to the new "Media Field" module. File fields can be used to store media.');
  569. }
  570. /**
  571. * Enable the Views module if it is not already enabled.
  572. */
  573. function media_update_7202() {
  574. module_enable(array('views'));
  575. if (!module_exists('views')) {
  576. throw new DrupalUpdateException('The <a href="https://drupal.org/project/views">Views module</a> must be downloaded and available for Media updates to proceed.');
  577. }
  578. }
  579. /**
  580. * Empty update function to trigger cache clear.
  581. */
  582. function media_update_7203() {
  583. // Do nothing.
  584. }
  585. /**
  586. * Update old Media view modes to the new File Entity ones.
  587. */
  588. function media_update_7204() {
  589. $view_mode_updates = array(
  590. 'media_preview' => 'preview',
  591. 'media_small' => 'teaser',
  592. 'media_large' => 'full',
  593. );
  594. // Update the media__wysiwyg_default_view_mode variable.
  595. $wysiwyg_default_view_mode = variable_get('media__wysiwyg_default_view_mode');
  596. if (isset($wysiwyg_default_view_mode) && isset($view_mode_updates[$wysiwyg_default_view_mode])) {
  597. $wysiwyg_default_view_mode = $view_mode_updates[$wysiwyg_default_view_mode];
  598. variable_set('media__wysiwyg_default_view_mode', $wysiwyg_default_view_mode);
  599. }
  600. // Update view mode references in the 'field_bundle_settings' variable.
  601. $field_bundle_settings = variable_get('field_bundle_settings');
  602. if (!empty($field_bundle_settings['file'])) {
  603. foreach ($field_bundle_settings['file'] as $file_type => $info) {
  604. // Per-bundle information about the view modes.
  605. foreach ($view_mode_updates as $old_view_mode => $new_view_mode) {
  606. if (isset($info['view_modes'][$old_view_mode])) {
  607. $field_bundle_settings['file'][$file_type]['view_modes'][$new_view_mode] = $info['view_modes'][$old_view_mode];
  608. unset($field_bundle_settings['file'][$file_type]['view_modes'][$old_view_mode]);
  609. }
  610. // The File Entity module defaults to not use custom settings for the
  611. // new view modes, but the Media module used to default to using custom
  612. // settings, so if this variable is not defined, use the prior default.
  613. if (!isset($field_bundle_settings['file'][$file_type]['view_modes'][$new_view_mode]['custom_settings'])) {
  614. $field_bundle_settings['file'][$file_type]['view_modes'][$new_view_mode]['custom_settings'] = TRUE;
  615. }
  616. }
  617. // Settings for the "extra fields" configured on the Manage Display page.
  618. if (!empty($info['extra_fields']['display'])) {
  619. foreach ($info['extra_fields']['display'] as $extra_field_name => $extra_field_info) {
  620. foreach ($view_mode_updates as $old_view_mode => $new_view_mode) {
  621. if (isset($extra_field_info[$old_view_mode])) {
  622. $field_bundle_settings['file'][$file_type]['extra_fields']['display'][$extra_field_name][$new_view_mode] = $extra_field_info[$old_view_mode];
  623. unset($field_bundle_settings['file'][$file_type]['extra_fields']['display'][$extra_field_name][$old_view_mode]);
  624. }
  625. }
  626. }
  627. }
  628. }
  629. }
  630. variable_set('field_bundle_settings', $field_bundle_settings);
  631. // Move settings for fields attached to files from the old view modes to the
  632. // new ones.
  633. $instances = field_read_instances(array('entity_type' => 'file'));
  634. foreach ($instances as $instance) {
  635. $updated = FALSE;
  636. foreach ($view_mode_updates as $old_view_mode => $new_view_mode) {
  637. if (isset($instance['display'][$old_view_mode])) {
  638. $instance['display'][$new_view_mode] = $instance['display'][$old_view_mode];
  639. unset($instance['display'][$old_view_mode]);
  640. $updated = TRUE;
  641. }
  642. }
  643. if ($updated) {
  644. field_update_instance($instance);
  645. }
  646. }
  647. // Move "Manage file display" settings from old view modes to new ones.
  648. $file_display_names = db_query('SELECT name FROM {file_display}')->fetchCol();
  649. foreach ($file_display_names as $old_file_display_name) {
  650. list($file_type, $view_mode, $formatter) = explode('__', $old_file_display_name, 3);
  651. if (isset($view_mode_updates[$view_mode])) {
  652. $view_mode = $view_mode_updates[$view_mode];
  653. $new_file_display_name = implode('__', array($file_type, $view_mode, $formatter));
  654. db_delete('file_display')->condition('name', $new_file_display_name)->execute();
  655. db_update('file_display')->fields(array('name' => $new_file_display_name))->condition('name', $old_file_display_name)->execute();
  656. }
  657. }
  658. // Update file/image/media fields that use a formatter that reference an old
  659. // file view modes to reference the new ones.
  660. foreach (field_read_instances() as $instance) {
  661. if (!empty($instance['display'])) {
  662. $updated = FALSE;
  663. foreach ($instance['display'] as $instance_view_mode => $display) {
  664. if (isset($display['settings']['file_view_mode']) && isset($view_mode_updates[$display['settings']['file_view_mode']])) {
  665. $instance['display'][$instance_view_mode]['settings']['file_view_mode'] = $view_mode_updates[$display['settings']['file_view_mode']];
  666. $updated = TRUE;
  667. }
  668. }
  669. if ($updated) {
  670. field_update_instance($instance);
  671. }
  672. }
  673. }
  674. // Update formatter settings that reference the old view modes within saved
  675. // Views.
  676. if (db_table_exists('views_display')) {
  677. $result = db_select('views_display', 'v')->fields('v', array('vid', 'id', 'display_options'))->execute();
  678. foreach ($result as $record) {
  679. if (!empty($record->display_options)) {
  680. $display_options = unserialize($record->display_options);
  681. if (_media_update_7204_update_views_display_options($display_options, $view_mode_updates)) {
  682. db_update('views_display')
  683. ->fields(array('display_options' => serialize($display_options)))
  684. ->condition('vid', $record->vid)
  685. ->condition('id', $record->id)
  686. ->execute();
  687. }
  688. }
  689. }
  690. }
  691. // Update formatter settings that reference the old view modes within unsaved
  692. // Views in the CTools object cache. Objects in the CTools cache are instances
  693. // of classes, so the Views module must be enabled to unserialize it
  694. // correctly.
  695. if (db_table_exists('ctools_object_cache') && module_exists('views')) {
  696. $result = db_select('ctools_object_cache', 'c')->fields('c', array('sid', 'name', 'obj', 'data'))->condition('obj', 'view')->execute();
  697. foreach ($result as $record) {
  698. $view = unserialize($record->data);
  699. if (!empty($view->display)) {
  700. $updated = FALSE;
  701. foreach ($view->display as $display_name => $display) {
  702. if (!empty($display->display_options) && _media_update_7204_update_views_display_options($display->display_options, $view_mode_updates)) {
  703. $updated = TRUE;
  704. }
  705. }
  706. if ($updated) {
  707. db_update('ctools_object_cache')
  708. ->fields(array('data' => serialize($view)))
  709. ->condition('sid', $record->sid)
  710. ->condition('name', $record->name)
  711. ->condition('obj', $record->obj)
  712. ->execute();
  713. }
  714. }
  715. }
  716. }
  717. // Clear caches that might contain stale Views displays.
  718. if (module_exists('views')) {
  719. cache_clear_all('*', 'cache_views', TRUE);
  720. cache_clear_all('*', 'cache_views_data', TRUE);
  721. }
  722. if (module_exists('block')) {
  723. cache_clear_all('*', 'cache_block', TRUE);
  724. }
  725. cache_clear_all('*', 'cache_page', TRUE);
  726. // We still have the old media_link and media_original view modes that must be
  727. // supported for now.
  728. // @TODO: Make this apply only to updates from Media 1.x.
  729. // @see media_entity_info_alter()
  730. variable_set('media__show_deprecated_view_modes', TRUE);
  731. }
  732. /**
  733. * Drop the unused {media_list_type} table.
  734. */
  735. function media_update_7205() {
  736. if (db_table_exists('media_list_type')) {
  737. db_drop_table('media_list_type');
  738. return t('Dropped the unused {media_list_type} table.');
  739. }
  740. }
  741. /**
  742. * Move default file display configurations to the database.
  743. */
  744. function media_update_7206() {
  745. module_load_include('inc', 'file_entity', 'file_entity.file_api');
  746. module_load_include('inc', 'ctools', 'includes/export');
  747. $default_image_styles = array(
  748. 'preview' => 'square_thumbnail',
  749. 'teaser' => 'medium',
  750. 'full' => 'large',
  751. );
  752. // Only needed by sites that updated from Media 1.x.
  753. // @see media_entity_info_alter()
  754. if (variable_get('media__show_deprecated_view_modes')) {
  755. $default_image_styles['media_original'] = '';
  756. }
  757. // Clear out the ctools cache so that the old default implementations
  758. // are removed.
  759. ctools_export_load_object_reset('file_display');
  760. foreach ($default_image_styles as $view_mode => $image_style) {
  761. $existing_displays = file_displays_load('image', $view_mode, TRUE);
  762. // Only insert default config into the database if no existing
  763. // configuration is found.
  764. if (!isset($existing_displays['file_image'])) {
  765. $display_name = 'image__' . $view_mode . '__file_image';
  766. $display = array(
  767. 'api_version' => 1,
  768. 'name' => $display_name,
  769. 'status' => 1,
  770. 'weight' => 5,
  771. 'settings' => array('image_style' => $image_style),
  772. 'export_type' => NULL,
  773. );
  774. file_display_save((object) $display);
  775. }
  776. }
  777. }
  778. /**
  779. * Trigger cache clear.
  780. *
  781. * Empty update function to trigger cache clear after changing access callbacks
  782. * to file_entity_access.
  783. */
  784. function media_update_7207() {
  785. // Do nothing.
  786. }
  787. /**
  788. * Drop the media_types table and migrate files to file_entity types.
  789. */
  790. function media_update_7208() {
  791. // Reset static cache to ensure our new file types are recognized
  792. drupal_static_reset('ctools_export_load_object_table_exists');
  793. if (!db_table_exists('media_type')) {
  794. // No types to migrate.
  795. return;
  796. }
  797. // @see http://drupal.org/node/1292382
  798. if (!function_exists('file_type_get_enabled_types')) {
  799. throw new DrupalUpdateException('The File Entity module needs to be upgraded before continuing.');
  800. }
  801. else {
  802. $existing_types = db_select('media_type', 'mt')
  803. ->orderBy('weight')
  804. ->fields('mt')
  805. ->execute()
  806. // Will key by the name field.
  807. ->fetchAllAssoc('name');
  808. foreach ($existing_types as &$type) {
  809. $type->type_callback_args = unserialize($type->type_callback_args);
  810. }
  811. include_once DRUPAL_ROOT . '/includes/file.mimetypes.inc';
  812. $mapping = file_mimetype_mapping();
  813. // We do not migrate this type, since there is no way to handle its weight.
  814. unset($existing_types['default']);
  815. foreach ($existing_types as $type) {
  816. $extensions = isset($type->type_callback_args['extensions']) ? $type->type_callback_args['extensions'] : array();
  817. $mimetypes = isset($type->type_callback_args['mimetypes']) ? $type->type_callback_args['mimetypes'] : array();
  818. // Add mimetypes by extensions.
  819. foreach ($extensions as $extension) {
  820. if (isset($mapping['extensions'][$extension])) {
  821. $type->mimetypes[] = $mapping['mimetypes'][$mapping['extensions'][$extension]];
  822. }
  823. }
  824. // Add rest mimetypes.
  825. foreach ($mimetypes as $mimetype) {
  826. // Mimetype is a regex pattern.
  827. foreach ($mapping['mimetypes'] as $mapping_mimetype) {
  828. if (preg_match($mimetype, $mapping_mimetype) && !in_array($mapping_mimetype, $type->mimetypes)) {
  829. $type->mimetypes[] = $mapping_mimetype;
  830. }
  831. }
  832. }
  833. $type->streams = isset($type->type_callback_args['streams']) ? $type->type_callback_args['streams'] : array();
  834. $type->type = $type->name;
  835. // Merge existing type with new ones.
  836. if ($new_type = file_type_load($type->name)) {
  837. $new_type->mimetypes = array_merge($type->mimetypes, $new_type->mimetypes);
  838. if (!empty($new_type->streams)) {
  839. $new_type->streams = array_merge($type->streams, $new_type->streams);
  840. }
  841. else{
  842. $new_type->streams = $type->streams;
  843. }
  844. }
  845. else {
  846. $new_type = $type;
  847. }
  848. file_type_save($new_type);
  849. }
  850. db_drop_table('media_type');
  851. // Special treatment for old media application type to new file_entity
  852. // document one. Add some more mimetypes to document.
  853. $document_type = file_type_load('document');
  854. if (!$document_type) {
  855. return;
  856. }
  857. foreach ($mapping['mimetypes'] as $mimetype) {
  858. $is_document = strpos($mimetype, 'document') !== FALSE || strpos($mimetype, 'application/vnd.ms-') !== FALSE;
  859. if ($is_document && !in_array($mimetype, $document_type->mimetypes)) {
  860. $document_type->mimetypes[] = $mimetype;
  861. }
  862. }
  863. file_type_save($document_type);
  864. }
  865. }
  866. /**
  867. * Enable the hidden media_migrate_file_types module to provide a UI to update
  868. * {file_managed}.type with the new file types provided by file_entity.
  869. */
  870. function media_update_7209() {
  871. drupal_load('module', 'media_migrate_file_types');
  872. if (_media_migrate_file_types_get_migratable_file_types()) {
  873. module_enable(array('media_migrate_file_types'));
  874. }
  875. }
  876. /**
  877. * Delete deceprated media__type_icon_directory variable.
  878. */
  879. function media_update_7210() {
  880. variable_del('media__type_icon_directory');
  881. }
  882. /**
  883. * Save a square_thumbnail image style in the database for legacy support if one
  884. * does not already exist.
  885. */
  886. function media_update_7211() {
  887. $default_style = array(
  888. 'name' => 'square_thumbnail'
  889. );
  890. // Clear the image cache to remove any old image styles that only exist in
  891. // code.
  892. cache_clear_all('image_styles', 'cache_image', TRUE);
  893. // Check if the square_thumbnail image style exists.
  894. // The style will only exist if the user has customized it, otherwise it would
  895. // have been removed by clearing the image style cache.
  896. $existing_style = image_style_load('square_thumbnail');
  897. // Save a square_thumbnail image style in the database for legacy support.
  898. // This is only necessary if a square_thumbnail image style doesn't already
  899. // exist.
  900. if (empty($existing_style)) {
  901. $style = image_style_save($default_style);
  902. $effect = array(
  903. 'name' => 'image_scale_and_crop',
  904. 'data' => array(
  905. 'width' => 100,
  906. 'height' => 100,
  907. 'weight' => 0,
  908. ),
  909. 'isid' => $style['isid'],
  910. );
  911. image_effect_save($effect);
  912. }
  913. return t('Saved a square_thumbnail image style in the database for legacy support if one did not already exist.');
  914. }
  915. /**
  916. * Utility function for update 7204. Updates display options within Views.
  917. */
  918. function _media_update_7204_update_views_display_options(&$display_options, $view_mode_updates) {
  919. $updated = FALSE;
  920. // Update fields that use a formatter with a file_view_mode formatter setting.
  921. if (!empty($display_options['fields'])) {
  922. foreach ($display_options['fields'] as $field_name => $field_display) {
  923. if (isset($field_display['settings']['file_view_mode']) && isset($view_mode_updates[$field_display['settings']['file_view_mode']])) {
  924. $display_options['fields'][$field_name]['settings']['file_view_mode'] = $view_mode_updates[$field_display['settings']['file_view_mode']];
  925. $updated = TRUE;
  926. }
  927. }
  928. }
  929. // Update Views that display files directly using a row plugin with a view
  930. // mode setting.
  931. if (isset($display_options['row_plugin']) && $display_options['row_plugin'] === 'file' && isset($display_options['row_options']['view_mode']) && isset($view_mode_updates[$display_options['row_options']['view_mode']])) {
  932. $display_options['row_options']['view_mode'] = $view_mode_updates[$display_options['row_options']['view_mode']];
  933. $updated = TRUE;
  934. }
  935. return $updated;
  936. }
  937. /**
  938. * Re-create application file type for legacy reasons.
  939. */
  940. function media_update_7212() {
  941. module_load_include('inc', 'file_entity', 'file_entity.file_api');
  942. if (!file_type_load('application')) {
  943. $application = (object) array(
  944. 'api_version' => 1,
  945. 'type' => 'application',
  946. 'label' => t('Application'),
  947. 'description' => t('Multipurpose type - kept to support older sites.'),
  948. 'mimetypes' => array(),
  949. 'streams' => array(
  950. 'public',
  951. ),
  952. );
  953. file_type_save($application);
  954. $application = file_type_load('application');
  955. file_type_disable($application);
  956. }
  957. }
  958. /**
  959. * Remove the obsolete file_extensions variable.
  960. */
  961. function media_update_7213() {
  962. $media_file_extensions = explode(' ', variable_get('media__file_extensions'));
  963. $file_entity_file_extensions = explode(' ', variable_get('file_entity_default_allowed_extensions', 'jpg jpeg gif png txt doc docx xls xlsx pdf ppt pptx pps ppsx odt ods odp mp3 mov mp4 m4a m4v mpeg avi ogg oga ogv weba webp webm'));
  964. // Preserve any custom file extensions.
  965. if (array_diff($media_file_extensions, $file_entity_file_extensions)) {
  966. $combined_file_extensions = array_unique(array_merge($file_entity_file_extensions, $media_file_extensions));
  967. variable_set('file_entity_default_allowed_extensions', implode(' ' , $combined_file_extensions));
  968. }
  969. variable_del('media__file_extensions');
  970. }
  971. /**
  972. * Drop the legacy {media_filter_usage} table.
  973. */
  974. function media_update_7214() {
  975. if (db_table_exists('media_filter_usage')) {
  976. db_drop_table('media_filter_usage');
  977. }
  978. }
  979. /**
  980. * Skipped to run media_update_7217().
  981. */
  982. function media_update_7216() {
  983. // Do nothing.
  984. }
  985. /**
  986. * Copy file type icons to public files directory.
  987. */
  988. function media_update_7217() {
  989. // Remove any trailing slashes from the icon base directory variable.
  990. $dir = variable_get('media__icon_base_directory');
  991. if (!empty($dir)) {
  992. $dir = rtrim($dir, '/');
  993. variable_set('media__icon_base_directory', $dir);
  994. }
  995. try {
  996. _media_install_copy_icons();
  997. }
  998. catch (Exception $e) {
  999. throw new DrupalUpdateException($e->getMessage());
  1000. }
  1001. }
  1002. /**
  1003. * Drop the legacy {cache_media_xml} table.
  1004. */
  1005. function media_update_7218() {
  1006. if (db_table_exists('cache_media_xml')) {
  1007. db_drop_table('cache_media_xml');
  1008. }
  1009. variable_del('media__xml_cache_expire');
  1010. }
  1011. /**
  1012. * Enable the Media WYSIWYG submodule.
  1013. */
  1014. function media_update_7219() {
  1015. if (module_exists('wysiwyg')) {
  1016. module_enable(array('media_wysiwyg'));
  1017. }
  1018. }
  1019. /**
  1020. * Delete the deprecated media__file_list_size variable.
  1021. */
  1022. function media_update_7220() {
  1023. variable_del('media__file_list_size');
  1024. }
  1025. /**
  1026. * Enable the Media Bulk Upload submodule.
  1027. */
  1028. function media_update_7221() {
  1029. if (module_exists('multiform') && module_exists('plupload')) {
  1030. module_enable(array('media_bulk_upload'));
  1031. }
  1032. }
  1033. /**
  1034. * Delete the deprecated media__display_types_migration_mess variable.
  1035. */
  1036. function media_update_7222() {
  1037. variable_del('media__display_types_migration_mess');
  1038. }
  1039. /**
  1040. * Delete legacy variables.
  1041. */
  1042. function media_update_7223() {
  1043. variable_del('media__max_filesize');
  1044. variable_del('media__debug');
  1045. variable_del('media__xml_cache_expire');
  1046. variable_del('media__show_file_type_rebuild_nag');
  1047. variable_del('media__field_select_media_text');
  1048. variable_del('media__field_remove_media_text');
  1049. variable_del('media__browser_library_empty_message');
  1050. variable_del('media__browser_pager_limit');
  1051. variable_del('media__browser_viewtype_default');
  1052. }
  1053. /**
  1054. * Rename variables, removing variable namespace.
  1055. */
  1056. function media_update_7224() {
  1057. // Create an array of variables sans 'media' prefix.
  1058. $variables = array('wysiwyg_title', 'wysiwyg_icon_title', 'wysiwyg_default_view_mode', 'wysiwyg_upload_directory', 'wysiwyg_allowed_types', 'wysiwyg_allowed_attributes', 'wysiwyg_browser_plugins', 'dialog_theme', 'import_batch_size', 'fromurl_supported_schemes', 'icon_base_directory', 'icon_set', 'show_deprecated_view_modes');
  1059. foreach ($variables as $variable) {
  1060. // Find the value of the old variable.
  1061. $value = variable_get('media__' . $variable);
  1062. // Port the value of the variable if it was set.
  1063. if (!is_null($value)) {
  1064. variable_set('media_' . $variable, $value);
  1065. }
  1066. // Remove the old variable.
  1067. variable_del('media__' . $variable);
  1068. }
  1069. }
  1070. /**
  1071. * Migrate variables to appropriate submodules.
  1072. */
  1073. function media_update_7225() {
  1074. $data = array(
  1075. 'media_wysiwyg' => array(
  1076. 'wysiwyg_title',
  1077. 'wysiwyg_icon_title',
  1078. 'wysiwyg_default_view_mode',
  1079. 'wysiwyg_upload_directory',
  1080. 'wysiwyg_allowed_types',
  1081. 'wysiwyg_allowed_attributes',
  1082. 'wysiwyg_browser_plugins',
  1083. ),
  1084. 'media_internet' => array(
  1085. 'fromurl_supported_schemes',
  1086. ),
  1087. 'media_bulk_upload' => array(
  1088. 'import_batch_size',
  1089. ),
  1090. );
  1091. foreach ($data as $module => $variables) {
  1092. foreach ($variables as $variable) {
  1093. // Only port variables to submodules if the submodule exists.
  1094. if (module_exists($module)) {
  1095. // Find the value of the old variable.
  1096. $value = variable_get('media_' . $variable);
  1097. // Port the value of the variable if it was set.
  1098. if (!is_null($value)) {
  1099. variable_set($module . '_' . $variable, $value);
  1100. }
  1101. }
  1102. // Remove the old variable.
  1103. variable_del('media_' . $variable);
  1104. }
  1105. }
  1106. }
  1107. /**
  1108. * Grant existing user access to new media browser permission.
  1109. */
  1110. function media_update_7226() {
  1111. $roles = user_roles(FALSE, 'create files');
  1112. foreach ($roles as $rid => $role) {
  1113. user_role_grant_permissions($rid, array('access media browser'));
  1114. }
  1115. }
  1116. /**
  1117. * Make sure that the image style square_thumbnail is created.
  1118. */
  1119. function media_update_7227() {
  1120. media_update_7211();
  1121. }