openlayers.drush.inc 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. /**
  3. * @file
  4. * drush integration for Openlayers.
  5. */
  6. /**
  7. * Implements hook_drush_command().
  8. */
  9. function openlayers_drush_command() {
  10. $items = array();
  11. // The key in the $items array is the name of the command.
  12. $items['dl-openlayers'] = array(
  13. 'callback' => 'drush_openlayers_plugin',
  14. 'description' => dt('Download and install the Openlayers library.'),
  15. 'bootstrap' => DRUSH_BOOTSTRAP_DRUSH, // No bootstrap.
  16. 'arguments' => array(
  17. 'path' => dt('Optional. A path where to install the Openlayers library. If omitted Drush will use the default location.'),
  18. ),
  19. );
  20. return $items;
  21. }
  22. /**
  23. * Implements hook_drush_help().
  24. */
  25. function openlayers_drush_help($section) {
  26. switch ($section) {
  27. case 'drush:dl-openlayers':
  28. return dt('Download and install the Openlayers library from http://openlayers.org, default location is sites/all/libraries.');
  29. }
  30. }
  31. /**
  32. * Command to download the Openlayers library.
  33. */
  34. function drush_openlayers_plugin() {
  35. $args = func_get_args();
  36. if (!empty($args[0])) {
  37. $path = $args[0];
  38. }
  39. else {
  40. $path = 'sites/all/libraries';
  41. }
  42. // Create the path if it does not exist.
  43. if (!is_dir($path)) {
  44. drush_op('mkdir', $path);
  45. drush_log(dt('Directory @path was created', array('@path' => $path)), 'notice');
  46. }
  47. // Set the directory to the download location.
  48. $olddir = getcwd();
  49. chdir($path);
  50. $library = libraries_detect('openlayers3');
  51. // Download the archive.
  52. if ($filepath = drush_download_file($library['download url'])) {
  53. $filename = basename($filepath);
  54. $dirname = basename($filepath, '.zip');
  55. // Remove any existing Openlayers library directory.
  56. if (is_dir($dirname) || is_dir('openlayers3')) {
  57. drush_delete_dir($dirname, TRUE);
  58. drush_delete_dir('openlayers3', TRUE);
  59. drush_log(dt('A existing Openlayers library was deleted from @path', array('@path' => $path)), 'notice');
  60. }
  61. // Decompress the archive.
  62. drush_tarball_extract($filename);
  63. drush_move_dir($dirname, 'openlayers3', TRUE);
  64. $dirname = 'openlayers3';
  65. }
  66. if (is_dir($dirname)) {
  67. drush_log(dt('Openlayers library has been installed in @path', array('@path' => $path)), 'success');
  68. }
  69. else {
  70. drush_log(dt('Drush was unable to install the Openlayers library to @path', array('@path' => $path)), 'error');
  71. }
  72. // Set working directory back to the previous working directory.
  73. chdir($olddir);
  74. }