Yaffs site version 1.1
[yaffs-website] / vendor / drush / drush / commands / core / notify.drush.inc
1 <?php
2 /**
3  * @file
4  * Add system notifications as a new drush option.
5  */
6
7 /**
8  * @todo there are no hooks fired after a command errors out.
9  */
10 register_shutdown_function('drush_notify_shutdown');
11
12 /**
13  * Implements hook_drush_help_alter().
14  */
15 function notify_drush_help_alter(&$command) {
16   if ($command['command'] == 'global-options') {
17     // Do not include these in options in standard help.
18     if ($command['#brief'] === FALSE) {
19       $command['options']['notify'] = array(
20         'description' => 'Use system notifications to signal command completion. If set to a number, commands that finish in fewer seconds will not trigger a notification.',
21         'example-value' => 60,
22         'never-propagate' => TRUE,
23       );
24       $command['options']['notify-audio'] = array(
25         'description' => 'Trigger an audio alert to signal command completion. If set to a number, commands that finish in fewer seconds will not trigger a notification.',
26         'example-value' => 60,
27         'never-propagate' => TRUE,
28       );
29       $command['sub-options']['notify']['notify-cmd'] = array(
30         'description' => 'Specify the shell command to trigger the notification.',
31         'never-propagate' => TRUE,
32       );
33       $command['sub-options']['notify']['notify-cmd-audio'] = array(
34         'description' => 'Specify the shell command to trigger the audio notification.',
35         'never-propagate' => TRUE,
36       );
37     }
38   }
39 }
40
41 /**
42  * Implements hook_drush_help().
43  */
44 function notify_drush_help($section) {
45   switch ($section) {
46     case 'notify:cache-clear':
47       return dt('Caches have been cleared.');
48     case 'notify:site-install:error':
49       return dt('Failed on site installation');
50   }
51 }
52
53 /**
54  * Shutdown function to signal on errors.
55  */
56 function drush_notify_shutdown() {
57   $cmd = drush_get_command();
58
59   if (empty($cmd['command'])) {
60     return;
61   }
62
63   // pm-download handles its own notification.
64   if ($cmd['command'] != 'pm-download' && drush_notify_allowed($cmd['command'])) {
65     $msg = dt("Command '!command' completed.", array('!command' => $cmd['command']));
66     drush_notify_send(drush_notify_command_message($cmd['command'], $msg));
67   }
68
69   if (drush_get_option('notify', FALSE) && drush_get_error()) {
70     // If the only error is that notify failed, do not try to notify again.
71     $log = drush_get_error_log();
72     if (count($log) == 1 && array_key_exists('NOTIFY_COMMAND_NOT_FOUND', $log)) {
73       return;
74     }
75
76     // Send an alert that the command failed.
77     if (drush_notify_allowed($cmd['command'])) {
78       $msg = dt("Command '!command' failed.", array('!command' => $cmd['command']));
79       drush_notify_send(drush_notify_command_message($cmd['command'] . ':error', $msg));
80     }
81   }
82 }
83
84 /**
85  * Determine the message to send on command completion.
86  *
87  * @param string $command
88  *   Name of the Drush command for which we check message overrides.
89  * @param string $default
90  *   (Default: NULL) Default message to use if there are not notification message overrides.
91  *
92  * @return string
93  *   Message to use for notification.
94  */
95 function drush_notify_command_message($command, $default = NULL) {
96   if ($msg = drush_command_invoke_all('drush_help', 'notify:' . $command)) {
97     $msg = implode("\n", $msg);
98   }
99   else {
100     $msg = $default ? $default : $msg = $command . ': No news is good news.';
101   }
102
103   return $msg;
104 }
105
106 /**
107  * Prepares and dispatches notifications to delivery mechanisms.
108  *
109  * You may avoid routing a message to secondary messaging mechanisms (e.g. audio),
110  * by direct use of the delivery functions.
111  *
112  * @param string $msg
113  *   Message to send via notification.
114  */
115 function drush_notify_send($msg) {
116   drush_notify_send_text($msg);
117   if (drush_get_option('notify-audio', FALSE)) {
118     drush_notify_send_audio($msg);
119   }
120 }
121
122 /**
123  * Send text-based system notification.
124  *
125  * This is the automatic, default behavior. It is intended for use with tools
126  * such as libnotify in Linux and Notification Center on OSX.
127  *
128  * @param string $msg
129  *   Message text for delivery.
130  *
131  * @return bool
132  *   TRUE on success, FALSE on failure
133  */
134 function drush_notify_send_text($msg) {
135   $override = drush_get_option('notify-cmd', FALSE);
136
137   if (!empty($override)) {
138     $cmd = $override;
139   }
140   else {
141     switch (PHP_OS) {
142       case 'Darwin':
143         $cmd = 'terminal-notifier -message %s -title Drush';
144         $error_message = dt('terminal-notifier command failed. Please install it from https://github.com/alloy/terminal-notifier.');
145         break;
146       case 'Linux':
147       default:
148         $icon = drush_normalize_path(DRUSH_BASE_PATH . '/drush_logo-black.png');
149         $cmd = "notify-send %s -i $icon";
150         $error_message = dt('notify-send command failed. Please install it as per http://coderstalk.blogspot.com/2010/02/how-to-install-notify-send-in-ubuntu.html.');
151         break;
152     }
153   }
154
155   if (!drush_shell_exec($cmd, $msg)) {
156     return drush_set_error('NOTIFY_COMMAND_NOT_FOUND', $error_message . ' ' . dt('Or you may specify an alternate command to run by specifying --notify-cmd=<my_command>'));
157   }
158
159   return TRUE;
160 }
161
162 /**
163  * Send an audio-based system notification.
164  *
165  * This function is only automatically invoked with the additional use of the
166  * --notify-audio flag or configuration state.
167  *
168  * @param $msg
169  *   Message for audio recital.
170  *
171  * @return bool
172  *   TRUE on success, FALSE on failure
173  */
174 function drush_notify_send_audio($msg) {
175   $override = drush_get_option('notify-cmd-audio', FALSE);
176
177   if (!empty($override)) {
178     $cmd = $override;
179   }
180   else {
181     switch (PHP_OS) {
182       case 'Darwin':
183         $cmd = 'say %s';
184         break;
185       case 'Linux':
186       default:
187         $cmd = drush_get_option('notify-cmd-audio', 'spd-say') . ' %s';
188     }
189   }
190
191   if (!drush_shell_exec($cmd, $msg)) {
192     return drush_set_error('NOTIFY_COMMAND_NOT_FOUND', dt('The third party notification utility failed.'));
193   }
194 }
195
196 /**
197  * Identify if the given Drush request should trigger a notification.
198  *
199  * @param $command
200  *   Name of the command.
201  *
202  * @return
203  *   Boolean
204  */
205 function drush_notify_allowed($command) {
206   $notify = drush_get_option(array('notify', 'notify-audio'), FALSE);
207   $execution = time() - $_SERVER['REQUEST_TIME'];
208
209   return ($notify === TRUE ||
210     (is_numeric($notify) && $notify > 0 && $execution > $notify));
211 }
212