libraries.admin.inc 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. <?php
  2. /**
  3. * @file
  4. * Provides administrative page and form callbacks for Libraries module.
  5. */
  6. /**
  7. * Form generation callback for the libraries overview table.
  8. *
  9. * This is a form instead of a page to allow easier extending in contributed
  10. * modules.
  11. *
  12. * @param array $form
  13. * An associative array containing the structure of the form.
  14. * @param array $form_state
  15. * A keyed array containing the current state of the form.
  16. *
  17. * @return array
  18. * The form array for the overview form.
  19. */
  20. function libraries_admin_overview(array $form, array &$form_state) {
  21. // Only show variants for installed libraries.
  22. $header_installed = array(t('Name'), t('Version'), t('Variants'), t('Dependencies'), t('Provider'), t('Links'));
  23. // Only show status for libraries with an error.
  24. $header_error = array(t('Name'), t('Status'), t('Version'), t('Dependencies'), t('Provider'), t('Links'));
  25. // For unregistered libraries the only information we can show is the path.
  26. $header_unregistered = array(t('Name'), t('Path'));
  27. $rows_installed = array();
  28. $rows_error = array();
  29. $rows_unregistered = array();
  30. // Registered libraries: we prefer to use libraries_detect() since it provides
  31. // library metadata.
  32. $libraries_registered = libraries_detect();
  33. uasort($libraries_registered, 'libraries_admin_sort_title');
  34. // Unregistered libraries: modules can depend on Libraries API without sharing
  35. // metadata by using libraries_get_path(). Libraries can also be placed in the
  36. // filesystem that are incorrectly installed, a wrong version, or a standalone
  37. // not connected to any module. In these cases, libraries_get_libraries()
  38. // provides a full library list. Libraries found by libraries_get_libraries(),
  39. // but not identified by libraries_detect, are displayed in a separate table.
  40. $libraries_unregistered = libraries_get_libraries();
  41. natcasesort($libraries_unregistered);
  42. foreach ($libraries_registered as $machine_name => $library) {
  43. $actions = array();
  44. $row = array();
  45. if ($library['vendor url']) {
  46. $actions[] = l(t('Homepage'), $library['vendor url']);
  47. }
  48. if ($library['download url']) {
  49. $actions[] = l(t('Download'), $library['download url']);
  50. }
  51. $row['data'][] = l($library['name'], 'admin/reports/libraries/' . $machine_name);
  52. // Only show status for libraries with an error. See above.
  53. if (!$library['installed']) {
  54. $row['data'][] = drupal_ucfirst($library['error']);
  55. }
  56. $row['data'][] = isset($library['version']) ? $library['version'] : '';
  57. if ($library['installed']) {
  58. $row['data'][] = implode(', ', array_keys($library['variants']));
  59. }
  60. $row['data'][] = libraries_admin_get_dependencies($library);
  61. $row['data'][] = libraries_admin_get_provider_with_type($library);
  62. $row['data'][] = implode(' | ', $actions);
  63. $row['class'] = $library['installed'] ? array('ok') : array('warning');
  64. if ($library['installed']) {
  65. $rows_installed[] = $row;
  66. }
  67. else {
  68. $rows_error[] = $row;
  69. }
  70. // Filter registered libraries from unregistered libraries.
  71. unset($libraries_unregistered[$library['machine name']]);
  72. }
  73. // Build table of registered libraries with installed status.
  74. $form['libraries']['installed'] = array(
  75. '#theme' => 'libraries_table_with_title',
  76. '#title' => t('Installed'),
  77. '#header' => $header_installed,
  78. '#rows' => $rows_installed,
  79. '#description' => t('These libraries are registered and installed correctly.'),
  80. '#empty' => t('There are currently no libraries that are registered and installed.'),
  81. );
  82. // Build table of registered libraries with error status.
  83. $form['libraries']['error'] = array(
  84. '#theme' => 'libraries_table_with_title',
  85. '#title' => t('Uninstalled'),
  86. '#header' => $header_error,
  87. '#rows' => $rows_error,
  88. '#description' => t('These libraries are registered but not installed. They may not need to be installed in case a module or theme provides optional integration with a library.'),
  89. '#empty' => t('There are currently no libraries that are registered but not installed.'),
  90. );
  91. // Build table of unregistered libraries.
  92. foreach ($libraries_unregistered as $name => $path) {
  93. $rows_unregistered[] = array(
  94. 'data' => array(
  95. $name,
  96. $path,
  97. ),
  98. );
  99. }
  100. $form['libraries']['unregistered'] = array(
  101. '#theme' => 'libraries_table_with_title',
  102. '#title' => t('Unregistered'),
  103. '#header' => $header_unregistered,
  104. '#rows' => $rows_unregistered,
  105. '#description' => t('These libraries were found in the filesystem but there is no metadata about them.'),
  106. // Do not show the table at all, if there are no unregistered libraries.
  107. '#access' => (bool) $libraries_unregistered,
  108. );
  109. // Clear the cached library information so that the library can be loaded if
  110. // it was just downloaded. Because these instructions use libraries_detect()
  111. // directly, they will never use the cached information, but this avoids the
  112. // overview showing a library as installed but it not being loadable.
  113. libraries_cache_clear();
  114. return $form;
  115. }
  116. /**
  117. * Form generation callback for the status overview for a single library.
  118. *
  119. * This is a form instead of a page to allow easier extending in contributed
  120. * modules.
  121. *
  122. * @param array $form
  123. * An associative array containing the structure of the form.
  124. * @param array $form_state
  125. * A keyed array containing the current state of the form.
  126. * @param array $library
  127. * A library information array.
  128. *
  129. * @return array|null
  130. * The form array for the status form or NULL if the library was not found.
  131. *
  132. * @todo Add some var_export($library)-style output
  133. */
  134. function libraries_admin_library_status_form(array $form, array &$form_state, $library) {
  135. drupal_set_title(t('Status report for library %library', array('%library' => $library['name'])), PASS_THROUGH);
  136. if ($library['installed']) {
  137. drupal_set_message(t('The %name library is installed correctly.', array('%name' => $library['name'])));
  138. $form['status'] = libraries_admin_status_table($library);
  139. }
  140. else {
  141. drupal_set_message($library['error message'], 'error');
  142. switch ($library['error']) {
  143. case 'not found':
  144. $form['instructions'] = libraries_admin_instructions_missing($library);
  145. break;
  146. case 'not detected':
  147. $form['instructions'] = libraries_admin_instructions_undetected($library);;
  148. break;
  149. case 'not supported':
  150. $form['instructions'] = libraries_admin_instructions_unsupported($library);
  151. break;
  152. case 'missing dependency':
  153. $form['instructions']['instruction']['#markup'] = t('There is a missing dependency in your configuration that prevents this library from working properly.') . '<br>';
  154. break;
  155. case 'incompatible dependency':
  156. $form['instructions']['instruction']['#markup'] = t('There is an incompatible dependency in your configuration that prevents this library from working properly.') . '<br>';
  157. break;
  158. }
  159. }
  160. return $form;
  161. }
  162. /**
  163. * Displays a table of status information about a library.
  164. *
  165. * @param array $library
  166. * A library information array.
  167. *
  168. * @return array
  169. * A renderable array containing a table with status information.
  170. */
  171. function libraries_admin_status_table(array $library) {
  172. $header = array(array(
  173. // @todo The title implies that other type of information is displayed, as
  174. // well, but this is currently not the case.
  175. // @todo Use CSS instead of a <strong> element.
  176. 'data' => '<strong>' . t('General information') . '</strong>',
  177. 'colspan' => 2,
  178. 'class' => 'table-heading',
  179. 'no_striping' => TRUE,
  180. ));
  181. $rows = array();
  182. // @todo Use CSS instead of <strong> elements.
  183. $rows['name'] = array('<strong>' . t('Name') . '</strong>', check_plain($library['name']));
  184. $rows['machine_name'] = array('<strong>' . t('Machine name') . '</strong>', check_plain($library['machine name']));
  185. if ($library['vendor url']) {
  186. $rows['vendor_url'] = array('<strong>' . t('Vendor URL') . '</strong>', l($library['vendor url'], $library['vendor url']));
  187. }
  188. if ($library['download url']) {
  189. $rows['download_url'] = array('<strong>' . t('Download URL') . '</strong>', l($library['download url'], $library['download url']));
  190. }
  191. $rows['provider'] = array('<strong>' . t('Provider') . '</strong>', libraries_admin_get_provider_with_type($library));
  192. $rows['library_path'] = array('<strong>' . t('Library path') . '</strong>', $library['library path']);
  193. $rows['version'] = array('<strong>' . t('Version') . '</strong>', $library['version']);
  194. if (!empty($library['variants'])) {
  195. $rows['variants'] = array('<strong>' . t('Variants') . '</strong>', implode(', ', array_keys($library['variants'])));
  196. }
  197. return array(
  198. '#theme' => 'table',
  199. '#header' => $header,
  200. '#rows' => $rows,
  201. );
  202. }
  203. /**
  204. * Returns instructions for dealing with a missing library.
  205. *
  206. * @param array $library
  207. * A library information array.
  208. *
  209. * @return array
  210. * A renderable array containing the instructions.
  211. */
  212. function libraries_admin_instructions_missing(array $library) {
  213. $build = array();
  214. $build['instruction']['#markup'] = t('Follow these steps to install the library:');
  215. $items = array();
  216. // 1. Download the library.
  217. // If no supported versions are specified, the latest version is
  218. // recommended.
  219. if (empty($library['versions'])) {
  220. $items[] = t('Download the latest version of the library <a href="@download-url">here</a>.', array(
  221. '@download-url' => $library['download url'],
  222. ));
  223. }
  224. // Otherwise, the latest supported version is recommended.
  225. else {
  226. $versions = array_keys($library['versions']);
  227. usort($versions, 'version_compare');
  228. $versions = array_reverse($versions);
  229. $version = $versions[0];
  230. $items[] = t('Download version %version of the library <a href="@download-url">here</a>.', array(
  231. '%version' => $version,
  232. '@download-url' => $library['download url'],
  233. ));
  234. }
  235. // 2. Unpack it.
  236. $items[] = t('If the library is an archive, i.e. if the file ending is for example <em>.tar.gz</em> or <em>.zip</em>, unpack it.');
  237. // 3. Create the libraries folder.
  238. $items[] = t('In the %library-directory directory of your Drupal installation create a %library directory.', array(
  239. '%library-directory' => 'sites/all/libraries',
  240. '%library' => $library['machine name'],
  241. ));
  242. // 4. Upload it.
  243. // If the library has variant-independent files, give the user the
  244. // location of an example file to check his filesystem against.
  245. if ($directory_layout = libraries_admin_directory_layout($library)) {
  246. $items[] = t('Upload the whole library (which can consist of multiple directories) into the newly created %library-path directory. The following files and directories should be contained in that directory: !directory-layout', array(
  247. '%library-path' => 'sites/all/libraries/' . $library['machine name'],
  248. '!directory-layout' => drupal_render($directory_layout),
  249. ));
  250. }
  251. else {
  252. $items[] = t('Upload the whole library (which can consist of multiple directories) into the newly created %library-path directory.', array(
  253. '%library-path' => 'sites/all/libraries/' . $library['machine name'],
  254. ));
  255. }
  256. // 5. Reload.
  257. $items[] = t('<a href="">Reload</a> the page. If successful, you should see status information about this library.');
  258. $build['steps'] = array(
  259. '#theme' => 'item_list',
  260. '#items' => $items,
  261. '#type' => 'ol'
  262. );
  263. return $build;
  264. }
  265. /**
  266. * Returns instructions for dealing with an undetected library.
  267. *
  268. * @param array $library
  269. * A library information array.
  270. *
  271. * @return array
  272. * A renderable array containing the instructions.
  273. */
  274. function libraries_admin_instructions_undetected($library) {
  275. $build = array();
  276. // Re-check location.
  277. // @todo Avoid usage of <br> elements.
  278. $build['instruction']['#markup'] = t('Check that the whole library is located at %library-path.', array(
  279. '%library-path' => $library['library path'],
  280. )) . '<br>';
  281. // If the library has variant-independent files, give the user the
  282. // exact location of the files to check against.
  283. // @todo It should be possible to display even variant-specific files
  284. // in case the variant is installed, but libraries_detect() does not
  285. // detect variants if the library version cannot be detected.
  286. if ($directory_layout = libraries_admin_directory_layout($library)) {
  287. $build['directory_layout'] = $directory_layout;
  288. $build['directory_layout']['#prefix'] = t('The following files and directories should be contained in that directory:');
  289. }
  290. // If the library is placed correctly the library information is
  291. // incorrect.
  292. // This switch could be avoided by using $library['info type'], but that would
  293. // hinder properly translating these strings.
  294. $build['reload']['#markup'] = t('If you have moved any files, <a href="">reload</a> the page. If successful, you should see status information about this library.') . '<br>';
  295. $build['notice']['#markup'] = t('If the files are placed correctly and the version can still not be detected, the library information is incorrect.') . '<br>';
  296. $provider = libraries_admin_get_provider($library);
  297. switch ($library['info type']) {
  298. case 'module':
  299. $build['contact']['#markup'] = t('Contact the maintainer of the %module module to correct this.', array(
  300. '%module' => $provider,
  301. )) . '<br>';
  302. break;
  303. case 'theme':
  304. $build['contact']['#markup'] = t('Contact the maintainer of the %theme theme to correct this.', array(
  305. '%theme' => $provider,
  306. )) . '<br>';
  307. break;
  308. case 'info file':
  309. $build['contact']['#markup'] = t('Contact the maintainer of the %info-file info file to correct this.', array(
  310. '%info-file' => $provider,
  311. )) . '<br>';
  312. break;
  313. }
  314. return $build;
  315. }
  316. /**
  317. * Returns instructions for dealing with an unsupported library.
  318. *
  319. * @param array $library
  320. * A library information array.
  321. *
  322. * @return array
  323. * A renderable array containing the instructions.
  324. */
  325. function libraries_admin_instructions_unsupported($library) {
  326. $build = array();
  327. $items = array();
  328. // Either download a different version of the library...
  329. $versions = array_keys($library['versions']);
  330. usort($versions, 'version_compare');
  331. $versions = array_reverse($versions);
  332. $version = $versions[0];
  333. $build['instruction']['#markup'] = t('Please install version %version of the library by following the following steps:',
  334. array(
  335. '%version' => $version,
  336. ));
  337. // 1. Delete the old library.
  338. $items[] = t('Delete the entire contents of the %library-path directory.',
  339. array(
  340. '%library-path' => $library['library path'],
  341. ));
  342. // 2. Download the new library.
  343. $items[] = t('Download version %version of the library <a href="@download-url">here</a>.',
  344. array(
  345. '%version' => $version,
  346. '@download-url' => $library['download url'],
  347. ));
  348. // 3. Unpack it.
  349. $items[] = t('If the library is an archive, i.e. if the file ending is for example <em>.tar.gz</em> or <em>.zip</em>, unpack it.');
  350. // 4. Upload the new library.
  351. // If the library has variant-independent files, give the user the
  352. // location of an example file to check his filesystem against.
  353. if ($directory_layout = libraries_admin_directory_layout($library)) {
  354. $items[] = t('Upload the new files into the %library-path directory. The following files and directories should be contained in that directory: !directory-layout',
  355. array(
  356. '%library-path' => $library['library path'],
  357. '!directory-layout' => drupal_render($directory_layout),
  358. ));
  359. }
  360. else {
  361. $items[] = t('Upload the new files into the %library-path directory.',
  362. array(
  363. '%library-path' => $library['library path'],
  364. ));
  365. }
  366. // 5. Reload.
  367. $items[] = t('<a href="">Reload</a> the page. If successful, you should see status information about this library.');
  368. $build['steps'] = array(
  369. '#theme' => 'item_list',
  370. '#items' => $items,
  371. '#type' => 'ol',
  372. );
  373. // ...or contact the maintainer of the library information.
  374. $provider = libraries_admin_get_provider($library);
  375. switch ($library['info type']) {
  376. case 'module':
  377. $build['contact']['#markup'] = t('If you are bound to version @version of the library, ask the maintainer of the %module module to provide support for it.', array(
  378. '@version' => $library['version'],
  379. '%module' => $provider,
  380. )) . '<br>';
  381. break;
  382. case 'theme':
  383. $build['contact']['#markup'] = t('If you are bound to version @version of the library, ask the maintainer of the %theme theme to provide support for it.', array(
  384. '@version' => $library['version'],
  385. '%theme' => $provider,
  386. )) . '<br>';
  387. break;
  388. case 'info file':
  389. $build['contact']['#markup'] = t('If you are bound to version @version of the library, ask the maintainer of the %info-file info file to provide support for it.', array(
  390. '@version' => $library['version'],
  391. '%info-file' => $provider,
  392. )) . '<br>';
  393. break;
  394. }
  395. return $build;
  396. }
  397. /**
  398. * Returns the directory layout of the library, if possible.
  399. *
  400. * The result of this function can help users to verify that they have uploaded
  401. * the library to the correct location.
  402. *
  403. * @param array $library
  404. * A library information array.
  405. *
  406. * @return array|false
  407. * A renderable array containing the directory layout of the library or FALSE
  408. * if a directory layout could not be generated.
  409. */
  410. function libraries_admin_directory_layout(array $library) {
  411. $build = array(
  412. '#theme' => 'item_list',
  413. '#type' => 'ul',
  414. '#items' => array(),
  415. );
  416. $items = &$build['#items'];
  417. if ($library['path']) {
  418. $items = &libraries_admin_path_to_tree($items, $library['path']);
  419. }
  420. foreach (array('js', 'css', 'php') as $type) {
  421. if (!empty($library['files'][$type])) {
  422. $files = array_keys($library['files'][$type]);
  423. foreach ($files as $file) {
  424. // Skip JavaScript settings.
  425. if (is_int($file)) {
  426. continue;
  427. }
  428. $children = &$items;
  429. libraries_admin_path_to_tree($children, $file);
  430. }
  431. }
  432. }
  433. return $build['#items'] ? $build : FALSE;
  434. }
  435. /**
  436. * Converts a file path into a tree structure for use in an item list.
  437. *
  438. * For example, the path 'foo/bar/baz' will be converted into the tree structure
  439. * represented by the following list:
  440. * - foo
  441. * - bar
  442. * - baz
  443. *
  444. * The $items array that is modified by reference or returned (see below) can
  445. * be used as the 'items' variable for theme_item_list().
  446. *
  447. * This function modifies passed-in $items array, so that multiple paths can
  448. * be placed into the same tree structure easily.
  449. *
  450. * @code
  451. * $items = array();
  452. * foreach ($paths as $path) {
  453. * libraries_admin_path_to_tree($items, $path);
  454. * }
  455. * @endcode
  456. *
  457. * It also returns the last item by reference, so that it can also be used to
  458. * traverse into a sub-structure and add further children there.
  459. *
  460. * @code
  461. * $items = array();
  462. * $children = &libraries_admin_path_to_tree($items, $path);
  463. * foreach ($sub_paths as $sub_path) {
  464. * libraries_admin_path_to_tree($children, $sub_path);
  465. * }
  466. * @endcode
  467. *
  468. * @param array $items
  469. * @param string $path
  470. *
  471. * @return array
  472. */
  473. function &libraries_admin_path_to_tree(array &$items, $path) {
  474. $part = strtok($path, '/');
  475. while ($part) {
  476. if (!isset($items[$part])) {
  477. $items[$part] = array(
  478. 'data' => $part,
  479. 'children' => array(),
  480. );
  481. }
  482. $items = &$items[$part]['children'];
  483. $part = strtok('/');
  484. }
  485. return $items;
  486. }
  487. /**
  488. * Sorts libraries by name.
  489. *
  490. * This function can be used as a callback for usort() or uasort().
  491. *
  492. * @param array $a
  493. * The first library information array.
  494. * @param array $b
  495. * The second library information array.
  496. *
  497. * @return int
  498. * Returns -1 if $a is considered smaller than $b, 1 if $a considered greater
  499. * than $b and 0 if $a and $b are considered equal.
  500. *
  501. * @see strnatcasecmp()
  502. * @see usort()
  503. * @see uasort()
  504. */
  505. function libraries_admin_sort_title(array $a, array $b) {
  506. return strnatcasecmp($a['name'], $b['name']);
  507. }
  508. /**
  509. * Returns the library's dependencies, if any.
  510. *
  511. * @param array $library
  512. * A library information array.
  513. *
  514. * @return string
  515. * The dependencies.
  516. */
  517. function libraries_admin_get_dependencies($library) {
  518. $dependencies = array();
  519. foreach ($library['dependencies'] as $dependency_name) {
  520. if ($dependency = libraries_info($dependency_name)) {
  521. $dependencies[] = $dependency['name'];
  522. }
  523. else {
  524. $dependencies[] = $dependency_name;
  525. }
  526. }
  527. return implode(', ', $dependencies);
  528. }
  529. /**
  530. * Returns the library's provider.
  531. *
  532. * The provider can be a module, a theme, or an info file.
  533. *
  534. * @param array $library
  535. * A library information array.
  536. *
  537. * @return string
  538. * The provider.
  539. */
  540. function libraries_admin_get_provider($library) {
  541. $provider = '';
  542. switch ($library['info type']) {
  543. case 'module':
  544. case 'theme':
  545. $info = system_get_info($library['info type'], $library[$library['info type']]);
  546. $provider = $info['name'];
  547. break;
  548. case 'info file':
  549. $provider = basename($library['info file']);
  550. break;
  551. }
  552. return $provider;
  553. }
  554. /**
  555. * Returns the library's provider and provider type.
  556. *
  557. * The provider type is either 'module', 'theme', or 'info file'.
  558. *
  559. * @param array $library
  560. * A library information array.
  561. *
  562. * @return string
  563. * The provider and provider type.
  564. */
  565. function libraries_admin_get_provider_with_type($library) {
  566. $provider = libraries_admin_get_provider($library);
  567. $provider_with_type = '';
  568. // This switch could be avoided by using $library['info type'], but that would
  569. // hinder properly translating these strings.
  570. switch ($library['info type']) {
  571. case 'module':
  572. $provider_with_type = t('%module module', array('%module' => $provider));
  573. break;
  574. case 'theme':
  575. $provider_with_type = t('%theme theme', array('%theme' => $provider));
  576. break;
  577. case 'info file':
  578. $provider_with_type = t('%info-file info file', array('%info-file' => $provider));
  579. break;
  580. }
  581. return $provider_with_type;
  582. }