Version 1
[yaffs-website] / vendor / drush / drush / examples / sync_enable.drush.inc
1 <?php
2
3 /**
4  * @file
5  * Example "Sync enable" sql-sync command alter.
6  *
7  * Sync_enable adds options to sql-sync to enable and disable
8  * modules after an sql-sync operation.  One use case for this
9  * is to use Drush site aliases to automatically enable your
10  * development modules whenever you sync from your live site to
11  * your dev site.  You may also add or remove permissions at
12  * the same time.
13  *
14  * For example:
15  *
16  * @code
17  * $aliases['dev'] = array (
18  *   'root' => '/srv/www/drupal',
19  *   'uri' => 'site.com',
20  *   'target-command-specific' => array(
21  *     'sql-sync'  => array(
22  *       'enable'  => array('devel', 'hacked'),
23  *       'disable' => array('securepages'),
24  *       'permission' => array(
25  *         'authenticated user' => array(
26  *           'add' => array('access devel information', 'access environment indicator'),
27  *           'remove' => 'change own password',
28  *         ),
29  *         'anonymous user' => array(
30  *           'add' => 'access environment indicator',
31  *         ),
32  *       ),
33  *     ),
34  *   ),
35  * );
36  * @endcode
37  *
38  * To use this feature, copy the 'target-command-specific'
39  * item from the example alias above, place it in your development
40  * site aliases, and customize the development module list
41  * to suit.  You must also copy the sync_enable.drush.inc
42  * file to a location where Drush will find it, such as
43  * $HOME/.drush.  See `drush topic docs-commands` for more
44  * information.
45  *
46  * To set variables on a development site:
47  *
48  * Instead of calling variable_set and variable_delete in a post-sync
49  * hook, consider adding $conf variables to settings.php.
50  *
51  * For example:
52  *
53  * $conf['error_level'] = 2;
54  * error_reporting(E_ALL);
55  * ini_set('display_errors', TRUE);
56  * ini_set('display_startup_errors', TRUE);
57  * $conf['preprocess_css'] = 0;
58  * $conf['cache'] = 0;
59  * $conf['googleanalytics_account'] = '';
60  */
61
62 /**
63  * Implements hook_drush_help_alter().
64  *
65  * When a hook extends a command with additional options, it must
66  * implement help alter and declare the option(s).  Doing so will add
67  * the option to the help text for the modified command, and will also
68  * allow the new option to be specified on the command line.  Without
69  * this, Drush will fail with an error when a user attempts to use
70  * the option.
71  */
72 function sync_enable_drush_help_alter(&$command) {
73   if ($command['command'] == 'sql-sync') {
74     $command['options']['updb']  = "Apply database updates on the target database after the sync operation has completed.";
75     $command['options']['enable']  = "Enable the specified modules in the target database after the sync operation has completed.";
76     $command['options']['disable'] = "Disable the specified modules in the target database after the sync operation has completed.";
77     $command['options']['permission'] = "Add or remove permissions from a role in the target database after the sync operation has completed. The value of this option must be an array, so it may only be specified in a site alias record or drush configuration file.  See `drush topic docs-example-sync-extension`.";
78   }
79 }
80
81 /**
82  * Implements drush_hook_post_COMMAND().
83  *
84  * The post hook is only called if the sql-sync operation completes
85  * without an error.  When called, we check to see if the user specified
86  * any modules to enable/disable.  If so, we will call pm-enable/pm-disable on
87  * each module.
88  */
89 function drush_sync_enable_post_sql_sync($source = NULL, $destination = NULL) {
90   $updb = drush_get_option('updb', FALSE);
91   if ($updb) {
92     drush_log('Run database updates', 'ok');
93     drush_invoke_process($destination, 'updatedb', array(), array('yes' => TRUE));
94   }
95   $modules_to_enable = drush_get_option_list('enable');
96   if (!empty($modules_to_enable)) {
97     drush_log(dt("Enable !modules post-sql-sync", array('!modules' => implode(',', $modules_to_enable))), 'ok');
98     drush_invoke_process($destination, 'pm-enable', $modules_to_enable, array('yes' => TRUE));
99   }
100   $modules_to_disable = drush_get_option_list('disable');
101   if (!empty($modules_to_disable)) {
102     drush_log(dt("Disable !modules post-sql-sync", array('!modules' => implode(',', $modules_to_disable))), 'ok');
103     drush_invoke_process($destination, 'pm-disable', $modules_to_disable, array('yes' => TRUE));
104   }
105   $permissions_table = drush_get_option('permission');
106   if (!empty($permissions_table)) {
107     foreach ($permissions_table as $role_name => $actions) {
108       if (array_key_exists('add', $actions)) {
109         $permissions_to_add = is_array($actions['add']) ? $actions['add'] : explode(', ', $actions['add']);
110         foreach ($permissions_to_add as $permission) {
111           $values = drush_invoke_process($destination, 'role-add-perm', array($role_name, $permission), array(), array('integrate' => TRUE));
112         }
113       }
114       if (array_key_exists('remove', $actions)) {
115         $permissions_to_remove = is_array($actions['remove']) ? $actions['remove'] : explode(', ', $actions['remove']);
116         foreach ($permissions_to_remove as $permission) {
117           $values = drush_invoke_process($destination, 'role-remove-perm', array($role_name, $permission), array(), array('integrate' => TRUE));
118         }
119       }
120     }
121   }
122 }