drush.api.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. /**
  3. * @file
  4. * Documentation of the Drush API.
  5. *
  6. * All drush commands are invoked in a specific order, using
  7. * drush-made hooks, very similar to the Drupal hook system. See drush_invoke()
  8. * for the actual implementation.
  9. *
  10. * For any commandfile named "hook", the following hooks are called, in
  11. * order, for the command "COMMAND":
  12. *
  13. * 0. drush_COMMAND_init()
  14. * 1. drush_hook_COMMAND_validate()
  15. * 2. drush_hook_pre_COMMAND()
  16. * 3. drush_hook_COMMAND()
  17. * 4. drush_hook_post_COMMAND()
  18. *
  19. * For example, here are the hook opportunities for a mysite.drush.inc file
  20. * that wants to hook into the `pm-download` command.
  21. *
  22. * 1. drush_mysite_pm_download_validate()
  23. * 2. drush_mysite_pre_pm_download()
  24. * 3. drush_mysite_pm_download()
  25. * 4. drush_mysite_post_pm_download()
  26. *
  27. * Note that the drush_COMMAND_init() hook is only for use by the
  28. * commandfile that defines the command.
  29. *
  30. * If any of hook function fails, either by calling drush_set_error
  31. * or by returning FALSE as its function result, then the rollback
  32. * mechanism is called. To fail with an error, call drush_set_error:
  33. *
  34. * return drush_set_error('MY_ERROR_CODE', dt('Error message.'));
  35. *
  36. * To allow the user to confirm or cancel a command, use drush_confirm
  37. * and drush_user_abort:
  38. *
  39. * if (!drush_confirm(dt('Are you sure?'))) {
  40. * return drush_user_abort();
  41. * }
  42. *
  43. * The rollback mechanism will call, in reverse, all _rollback hooks.
  44. * The mysite command file can implement the following rollback hooks:
  45. *
  46. * 1. drush_mysite_post_pm_download_rollback()
  47. * 2. drush_mysite_pm_download_rollback()
  48. * 3. drush_mysite_pre_pm_download_rollback()
  49. * 4. drush_mysite_pm_download_validate_rollback()
  50. *
  51. * Before any command is called, hook_drush_init() is also called.
  52. * hook_drush_exit() is called at the very end of command invocation.
  53. *
  54. * @see includes/command.inc
  55. *
  56. * @see hook_drush_init()
  57. * @see drush_COMMAND_init()
  58. * @see drush_hook_COMMAND_validate()
  59. * @see drush_hook_pre_COMMAND()
  60. * @see drush_hook_COMMAND()
  61. * @see drush_hook_post_COMMAND()
  62. * @see drush_hook_post_COMMAND_rollback()
  63. * @see drush_hook_COMMAND_rollback()
  64. * @see drush_hook_pre_COMMAND_rollback()
  65. * @see drush_hook_COMMAND_validate_rollback()
  66. * @see hook_drush_exit()
  67. */
  68. /**
  69. * @addtogroup hooks
  70. * @{
  71. */
  72. /**
  73. * Take action before any command is run. Logging an error stops command execution.
  74. */
  75. function hook_drush_init() {
  76. }
  77. /**
  78. * Initialize a command prior to validation. If a command
  79. * needs to bootstrap to a higher level, this is best done in
  80. * the command init hook. It is permisible to bootstrap in
  81. * any hook, but note that if bootstrapping adds more commandfiles
  82. * (*.drush.inc) to the commandfile list, the newly-added
  83. * commandfiles will not have any hooks called until the next
  84. * phase. For example, a command that calls drush_bootstrap_max()
  85. * in drush_hook_COMMAND() would only permit commandfiles from
  86. * modules enabled in the site to participate in drush_hook_post_COMMAND()
  87. * hooks.
  88. */
  89. function drush_COMMAND_init() {
  90. drush_bootstrap_max();
  91. }
  92. /**
  93. * Run before a specific command executes.
  94. *
  95. * Logging an error stops command execution, and the rollback function (if any)
  96. * for each hook implementation is invoked.
  97. *
  98. * @see drush_hook_COMMAND_validate_rollback()
  99. */
  100. function drush_hook_COMMAND_validate() {
  101. }
  102. /**
  103. * Run before a specific command executes. Logging an error stops command execution.
  104. *
  105. * Logging an error stops command execution, and the rollback function (if any)
  106. * for each hook implementation is invoked, in addition to the
  107. * validate rollback.
  108. *
  109. * @see drush_hook_pre_COMMAND_rollback()
  110. * @see drush_hook_COMMAND_validate_rollback()
  111. */
  112. function drush_hook_pre_COMMAND() {
  113. }
  114. /**
  115. * Implementation of the actual drush command.
  116. *
  117. * This is where most of the stuff should happen.
  118. *
  119. * Logging an error stops command execution, and the rollback function (if any)
  120. * for each hook implementation is invoked, in addition to pre and
  121. * validate rollbacks.
  122. *
  123. * @see drush_hook_COMMAND_rollback()
  124. * @see drush_hook_pre_COMMAND_rollback()
  125. * @see drush_hook_COMMAND_validate_rollback()
  126. */
  127. function drush_hook_COMMAND() {
  128. }
  129. /**
  130. * Run after a specific command executes. Logging an error stops command execution.
  131. *
  132. * Logging an error stops command execution, and the rollback function (if any)
  133. * for each hook implementation is invoked, in addition to pre, normal
  134. * and validate rollbacks.
  135. *
  136. * @see drush_hook_post_COMMAND_rollback()
  137. * @see drush_hook_COMMAND_rollback()
  138. * @see drush_hook_pre_COMMAND_rollback()
  139. * @see drush_hook_COMMAND_validate_rollback()
  140. */
  141. function drush_hook_post_COMMAND() {
  142. }
  143. /**
  144. * Take action after any command is run.
  145. */
  146. function hook_drush_exit() {
  147. }
  148. /*
  149. * A commandfile may choose to decline to load for the current bootstrap
  150. * level by returning FALSE. This hook must be placed in MODULE.drush.load.inc.
  151. * @see drush_commandfile_list().
  152. */
  153. function hook_drush_load() {
  154. }
  155. /**
  156. * Take action after a project has been downloaded.
  157. */
  158. function hook_drush_pm_post_download($project, $release) {
  159. }
  160. /**
  161. * Take action after a project has been updated.
  162. */
  163. function hook_pm_post_update($release_name, $release_candidate_version, $project_parent_path) {
  164. }
  165. /**
  166. * Adjust the location that a project should be copied to after being downloaded.
  167. *
  168. * See @pm_drush_pm_download_destination_alter().
  169. */
  170. function hook_drush_pm_download_destination_alter(&$project, $release) {
  171. if ($some_condition) {
  172. $project['project_install_location'] = '/path/to/install/to/' . $project['project_dir'];
  173. }
  174. }
  175. /**
  176. * Add information to the upgrade project map; this information
  177. * will be shown to the user when upgrading Drupal to the next
  178. * major version if the module containing this hook is enabled.
  179. *
  180. * @see drush_upgrade_project_map().
  181. */
  182. function hook_drush_upgrade_project_map_alter(&$project_map) {
  183. $project_map['warning']['hook'] = dt("You need to take special action before upgrading this module. See http://mysite.com/mypage for more information.");
  184. }
  185. /**
  186. * Sql-sync sanitization example. This is equivalent to
  187. * the built-in --sanitize option of sql-sync, but simplified
  188. * to only work with default values on Drupal 6 + mysql.
  189. *
  190. * @see sql_drush_sql_sync_sanitize().
  191. */
  192. function hook_drush_sql_sync_sanitize($source) {
  193. drush_sql_register_post_sync_op('my-sanitize-id',
  194. dt('Reset passwords and email addresses in user table'),
  195. "update users set pass = MD5('password'), mail = concat('user+', uid, '@localhost') where uid > 0;");
  196. }
  197. /**
  198. * Take action before modules are disabled in a major upgrade.
  199. * Note that when this hook fires, it will be operating on a
  200. * copy of the database.
  201. */
  202. function drush_hook_pre_site_upgrade_prepare() {
  203. // site upgrade prepare will disable contrib_extensions and
  204. // uninstall the uninstall_extension
  205. $contrib_extensions = func_get_args();
  206. $uninstall_extensions = explode(',', drush_get_option('uninstall', ''));
  207. }
  208. /**
  209. * Add help components to a command
  210. */
  211. function hook_drush_help_alter(&$command) {
  212. if ($command['command'] == 'sql-sync') {
  213. $command['options']['myoption'] = "Description of modification of sql-sync done by hook";
  214. $command['sub-options']['sanitize']['my-sanitize-option'] = "Description of sanitization option added by hook (grouped with --sanitize option)";
  215. }
  216. }
  217. /**
  218. * Add/edit options to cache-clear command
  219. */
  220. function hook_drush_cache_clear(&$types) {
  221. $types['views'] = 'views_invalidate_cache';
  222. }
  223. /*
  224. * Make shell aliases and other .bashrc code available during core-cli command.
  225. *
  226. * @return
  227. * Bash code typically found in a .bashrc file.
  228. *
  229. * @see core_cli_bashrc() for an example implementation.
  230. */
  231. function hook_cli_bashrc() {
  232. $string = "
  233. alias siwef='drush site-install wef --account-name=super --account-mail=me@wef'
  234. alias dump='drush sql-dump --structure-tables-key=wef --ordered-dump'
  235. ";
  236. return $string;
  237. }
  238. /**
  239. * @} End of "addtogroup hooks".
  240. */