Drupal investigation

drush.api.php 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. <?php
  2. /**
  3. * @file
  4. * Documentation of the Drush API.
  5. */
  6. /**
  7. * Declare a new command.
  8. */
  9. function hook_drush_command() {
  10. // To learn more, run `drush topic docs-commands` and
  11. // `drush topic docs-examplecommand`.
  12. }
  13. /**
  14. * All Drush commands are invoked in a specific order, using
  15. * drush-made hooks, very similar to the Drupal hook system. See drush_invoke()
  16. * for the actual implementation.
  17. *
  18. * For any commandfile named "hook", the following hooks are called, in
  19. * order, for the command "COMMAND":
  20. *
  21. * 0. drush_COMMAND_init()
  22. * 1. drush_hook_COMMAND_pre_validate()
  23. * 2. drush_hook_COMMAND_validate()
  24. * 3. drush_hook_pre_COMMAND()
  25. * 4. drush_hook_COMMAND()
  26. * 5. drush_hook_post_COMMAND()
  27. *
  28. * For example, here are the hook opportunities for a mysite.drush.inc file
  29. * that wants to hook into the `pm-download` command.
  30. *
  31. * 1. drush_mysite_pm_download_pre_validate()
  32. * 2. drush_mysite_pm_download_validate()
  33. * 3. drush_mysite_pre_pm_download()
  34. * 4. drush_mysite_pm_download()
  35. * 5. drush_mysite_post_pm_download()
  36. *
  37. * Note that the drush_COMMAND_init() hook is only for use by the
  38. * commandfile that defines the command.
  39. *
  40. * If any of hook function fails, either by calling drush_set_error
  41. * or by returning FALSE as its function result, then the rollback
  42. * mechanism is called. To fail with an error, call drush_set_error:
  43. *
  44. * return drush_set_error('MY_ERROR_CODE', dt('Error message.'));
  45. *
  46. * To allow the user to confirm or cancel a command, use drush_confirm
  47. * and drush_user_abort:
  48. *
  49. * if (!drush_confirm(dt('Are you sure?'))) {
  50. * return drush_user_abort();
  51. * }
  52. *
  53. * The rollback mechanism will call, in reverse, all _rollback hooks.
  54. * The mysite command file can implement the following rollback hooks:
  55. *
  56. * 1. drush_mysite_post_pm_download_rollback()
  57. * 2. drush_mysite_pm_download_rollback()
  58. * 3. drush_mysite_pre_pm_download_rollback()
  59. * 4. drush_mysite_pm_download_validate_rollback()
  60. * 5. drush_mysite_pm_download_pre_validate_rollback()
  61. *
  62. * Before any command is called, hook_drush_init() is also called.
  63. * hook_drush_exit() is called at the very end of command invocation.
  64. *
  65. * @see includes/command.inc
  66. *
  67. * @see hook_drush_init()
  68. * @see drush_COMMAND_init()
  69. * @see drush_hook_COMMAND_pre_validate()
  70. * @see drush_hook_COMMAND_validate()
  71. * @see drush_hook_pre_COMMAND()
  72. * @see drush_hook_COMMAND()
  73. * @see drush_hook_post_COMMAND()
  74. * @see drush_hook_post_COMMAND_rollback()
  75. * @see drush_hook_COMMAND_rollback()
  76. * @see drush_hook_pre_COMMAND_rollback()
  77. * @see drush_hook_COMMAND_validate_rollback()
  78. * @see drush_hook_COMMAND_pre_validate_rollback()
  79. * @see hook_drush_exit()
  80. */
  81. /**
  82. * @addtogroup hooks
  83. * @{
  84. */
  85. /**
  86. * Take action before any command is run.
  87. *
  88. * Logging an error stops command execution.
  89. */
  90. function hook_drush_init() {
  91. }
  92. /**
  93. * Initialize a command prior to validation.
  94. *
  95. * If a command needs to bootstrap to a higher level, this is best done in the
  96. * command init hook. It is permisible to bootstrap in any hook, but note that
  97. * if bootstrapping adds more commandfiles (*.drush.inc) to the commandfile
  98. * list, the newly-added commandfiles will not have any hooks called until the
  99. * next phase. For example, a command that calls drush_bootstrap_max() in
  100. * drush_hook_COMMAND() would only permit commandfiles from modules enabled in
  101. * the site to participate in drush_hook_post_COMMAND() hooks.
  102. */
  103. function drush_COMMAND_init() {
  104. drush_bootstrap_max();
  105. }
  106. /**
  107. * Run before a specific command validates.
  108. *
  109. * Logging an error stops command execution, and the rollback function (if any)
  110. * for each hook implementation is invoked.
  111. *
  112. * @see drush_hook_COMMAND_pre_validate_rollback()
  113. */
  114. function drush_hook_COMMAND_pre_validate() {
  115. }
  116. /**
  117. * Run before a specific command executes.
  118. *
  119. * Logging an error stops command execution, and the rollback function (if any)
  120. * for each hook implementation is invoked.
  121. *
  122. * @see drush_hook_COMMAND_validate_rollback()
  123. */
  124. function drush_hook_COMMAND_validate() {
  125. }
  126. /**
  127. * Run before a specific command executes.
  128. *
  129. * Logging an error stops command execution, and the rollback function (if any)
  130. * for each hook implementation is invoked, in addition to the validate
  131. * rollback.
  132. *
  133. * @see drush_hook_pre_COMMAND_rollback()
  134. * @see drush_hook_COMMAND_validate_rollback()
  135. */
  136. function drush_hook_pre_COMMAND() {
  137. }
  138. /**
  139. * Implementation of the actual drush command.
  140. *
  141. * This is where most of the stuff should happen.
  142. *
  143. * Logging an error stops command execution, and the rollback function (if any)
  144. * for each hook implementation is invoked, in addition to pre and
  145. * validate rollbacks.
  146. *
  147. * @return mixed|false
  148. * The return value will be passed along to the caller if --backend option is
  149. * present. A boolean FALSE indicates failure and rollback will be inititated.
  150. *
  151. * @see drush_hook_COMMAND_rollback()
  152. * @see drush_hook_pre_COMMAND_rollback()
  153. * @see drush_hook_COMMAND_validate_rollback()
  154. */
  155. function drush_hook_COMMAND() {
  156. }
  157. /**
  158. * Run after a specific command executes.
  159. *
  160. * Logging an error stops command execution, and the rollback function (if any)
  161. * for each hook implementation is invoked, in addition to pre, normal
  162. * and validate rollbacks.
  163. *
  164. * @see drush_hook_post_COMMAND_rollback()
  165. * @see drush_hook_COMMAND_rollback()
  166. * @see drush_hook_pre_COMMAND_rollback()
  167. * @see drush_hook_COMMAND_validate_rollback()
  168. */
  169. function drush_hook_post_COMMAND() {
  170. }
  171. /**
  172. * Take action after any command is run.
  173. */
  174. function hook_drush_exit() {
  175. }
  176. /**
  177. * Adjust the contents of any command structure prior to dispatch.
  178. *
  179. * @see core_drush_command_alter()
  180. */
  181. function hook_drush_command_alter(&$command) {
  182. }
  183. /**
  184. * Adjust the contents of a site alias.
  185. */
  186. function hook_drush_sitealias_alter(&$alias_record) {
  187. // If the alias is "remote", but the remote site is
  188. // the system this command is running on, convert the
  189. // alias record to a local alias.
  190. if (isset($alias_record['remote-host'])) {
  191. $uname = php_uname('n');
  192. if ($alias_record['remote-host'] == $uname) {
  193. unset($alias_record['remote-host']);
  194. unset($alias_record['remote-user']);
  195. }
  196. }
  197. }
  198. /**
  199. * Take action after a project has been downloaded.
  200. */
  201. function hook_drush_pm_post_download($project, $release) {
  202. }
  203. /**
  204. * Take action after a project has been updated.
  205. */
  206. function hook_pm_post_update($project_name, $installed_release, $project) {
  207. }
  208. /**
  209. * Adjust the location a project should be copied to after being downloaded.
  210. *
  211. * See @pm_drush_pm_download_destination_alter().
  212. */
  213. function hook_drush_pm_download_destination_alter(&$project, $release) {
  214. if ($some_condition) {
  215. $project['project_install_location'] = '/path/to/install/to/' . $project['project_dir'];
  216. }
  217. }
  218. /**
  219. * Automatically download project dependencies at pm-enable time.
  220. *
  221. * Use a pre-pm_enable hook to download before your module is enabled,
  222. * or a post-pm_enable hook (drush_hook_post_pm_enable) to run after
  223. * your module is enabled.
  224. *
  225. * Your hook will be called every time pm-enable is executed; you should
  226. * only download dependencies when your module is being enabled. Respect
  227. * the --skip flag, and take no action if it is present.
  228. */
  229. function drush_hook_pre_pm_enable() {
  230. // Get the list of modules being enabled; only download dependencies if our
  231. // module name appears in the list.
  232. $modules = drush_get_context('PM_ENABLE_MODULES');
  233. if (in_array('hook', $modules) && !drush_get_option('skip')) {
  234. $url = 'http://server.com/path/MyLibraryName.tgz';
  235. $path = drush_get_context('DRUSH_DRUPAL_ROOT');
  236. drush_include_engine('drupal', 'environment');
  237. if (drush_module_exists('libraries')) {
  238. $path .= '/' . libraries_get_path('MyLibraryName') . '/MyLibraryName.tgz';
  239. }
  240. else {
  241. $path .= '/' . drupal_get_path('module', 'hook') . '/MyLibraryName.tgz';
  242. }
  243. drush_download_file($url, $path) && drush_tarball_extract($path);
  244. }
  245. }
  246. /**
  247. * Sql-sync sanitization example.
  248. *
  249. * This is equivalent to the built-in --sanitize option of sql-sync, but
  250. * simplified to only work with default values on Drupal 6 + mysql.
  251. *
  252. * @see sql_drush_sql_sync_sanitize()
  253. */
  254. function hook_drush_sql_sync_sanitize($source) {
  255. $table = drush_get_option('db-prefix') ? '{users}' : 'users';
  256. drush_sql_register_post_sync_op('my-sanitize-id',
  257. dt('Reset passwords and email addresses in user table.'),
  258. "UPDATE $table SET pass = MD5('password'), mail = concat('user+', uid, '@localhost') WHERE uid > 0;");
  259. }
  260. /**
  261. * Add help components to a command.
  262. */
  263. function hook_drush_help_alter(&$command) {
  264. if ($command['command'] == 'sql-sync') {
  265. $command['options']['myoption'] = "Description of modification of sql-sync done by hook";
  266. $command['sub-options']['sanitize']['my-sanitize-option'] = "Description of sanitization option added by hook (grouped with --sanitize option)";
  267. }
  268. if ($command['command'] == 'global-options') {
  269. // Recommended: don't show global hook options in brief global options help.
  270. if ($command['#brief'] === FALSE) {
  271. $command['options']['myglobaloption'] = 'Description of option used globally in all commands (e.g. in a commandfile init hook)';
  272. }
  273. }
  274. }
  275. /**
  276. * Add/edit options to cache-clear command.
  277. *
  278. * @param array $types
  279. * Adjust types as needed. Is passed by reference.
  280. *
  281. * @param bool $include_bootstrapped_types
  282. * If FALSE, omit types which require a FULL bootstrap.
  283. */
  284. function hook_drush_cache_clear(&$types, $include_bootstrapped_types) {
  285. $types['views'] = 'views_invalidate_cache';
  286. }
  287. /**
  288. * Inform drush about one or more engine types.
  289. *
  290. * This hook allow to declare available engine types, the cli option to select
  291. * between engine implementatins, which one to use by default, global options
  292. * and other parameters. Commands may override this info when declaring the
  293. * engines they use.
  294. *
  295. * @return array
  296. * An array whose keys are engine type names and whose values describe
  297. * the characteristics of the engine type in relation to command definitions:
  298. *
  299. * - description: The engine type description.
  300. * - topic: If specified, the name of the topic command that will
  301. * display the automatically generated topic for this engine.
  302. * - topic-file: If specified, the path to the file that will be
  303. * displayed at the head of the automatically generated topic for
  304. * this engine. This path is relative to the Drush root directory;
  305. * non-core commandfiles should therefore use:
  306. * 'topic-file' => dirname(__FILE__) . '/mytopic.html';
  307. * - topics: If set, contains a list of topics that should be added to
  308. * the "Topics" section of any command that uses this engine. Note
  309. * that if 'topic' is set, it will automatically be added to the topics
  310. * list, and therefore does not need to also be listed here.
  311. * - option: The command line option to choose an implementation for
  312. * this engine type.
  313. * FALSE means there's no option. That is, the engine type is for internal
  314. * usage of the command and thus an implementation is not selectable.
  315. * - default: The default implementation to use by the engine type.
  316. * - options: Engine options common to all implementations.
  317. * - add-options-to-command: If there's a single implementation for this
  318. * engine type, add its options as command level options.
  319. * - combine-help: If there are multiple implementations for this engine
  320. * type, then instead of adding multiple help items in the form of
  321. * --engine-option=engine-type [description], instead combine all help
  322. * options into a single --engine-option that lists the different possible
  323. * values that can be used.
  324. *
  325. * @see drush_get_engine_types_info()
  326. * @see pm_drush_engine_type_info()
  327. */
  328. function hook_drush_engine_type_info() {
  329. return array(
  330. 'dessert' => array(
  331. 'description' => 'Choose a dessert while the sandwich is baked.',
  332. 'option' => 'dessert',
  333. 'default' => 'ice-cream',
  334. 'options' => 'sweetness',
  335. 'add-options-to-command' => FALSE,
  336. ),
  337. );
  338. }
  339. /**
  340. * Inform drush about one or more engines implementing a given engine type.
  341. *
  342. * - description: The engine implementation's description.
  343. * - implemented-by: The engine that actually implements this engine.
  344. * This is useful to allow the implementation of similar engines
  345. * in the reference one.
  346. * Defaults to the engine type key (e.g. 'ice-cream').
  347. * - verbose-only: The engine implementation will only appear in help
  348. * output in --verbose mode.
  349. *
  350. * This hook allow to declare implementations for an engine type.
  351. *
  352. * @see pm_drush_engine_package_handler()
  353. * @see pm_drush_engine_version_control()
  354. */
  355. function hook_drush_engine_ENGINE_TYPE() {
  356. return array(
  357. 'ice-cream' => array(
  358. 'description' => 'Feature rich ice-cream with all kind of additives.',
  359. 'options' => array(
  360. 'flavour' => 'Choose your favorite flavour',
  361. ),
  362. ),
  363. 'frozen-yogurt' => array(
  364. 'description' => 'Frozen dairy dessert made with yogurt instead of milk and cream.',
  365. 'implemented-by' => 'ice-cream',
  366. ),
  367. );
  368. }
  369. /**
  370. * Alter the order that hooks are invoked.
  371. *
  372. * When implementing a given hook we may need to ensure it is invoked before
  373. * or after another implementation of the same hook. For example, let's say
  374. * you want to implement a hook that would be called after drush_make. You'd
  375. * write a drush_MY_MODULE_post_make() function. But if you need your hook to
  376. * be called before drush_make_post_make(), you can ensure this by implemen-
  377. * ting MY_MODULE_drush_invoke_alter().
  378. *
  379. * @see drush_command_invoke_all_ref()
  380. */
  381. function hook_drush_invoke_alter($modules, $hook) {
  382. if ($hook == 'some_hook') {
  383. // Take the module who's hooks would normally be called last.
  384. $module = array_pop($modules);
  385. // Ensure it'll be called first for 'some_hook'.
  386. array_unshift($modules, $module);
  387. }
  388. }
  389. /**
  390. * @} End of "addtogroup hooks".
  391. */