bb53ff92c4827fc650b060c1b36e680cc82d7968
[yaffs-website] / vendor / drush / drush / examples / xkcd.drush.inc
1 <?php
2
3 /**
4  * @file
5  * Example XKCD Drush command.
6  *
7  * To run this *fun* command, execute `drush --include=./examples xkcd` from
8  * within your drush directory.
9  *
10  * See `drush topic docs-commands` for more information about command authoring.
11  *
12  * You can copy this file to any of the following
13  *   1. A .drush folder in your HOME folder.
14  *   2. Anywhere in a folder tree below an active module on your site.
15  *   3. /usr/share/drush/commands (configurable)
16  *   4. In an arbitrary folder specified with the --include option.
17  *   5. Drupal's /drush or /sites/all/drush folders, or in the /drush
18  *       folder in the directory above the Drupal root.
19  */
20
21 /**
22  * Implements hook_drush_command().
23  *
24  * In this hook, you specify which commands your drush module makes available,
25  * what it does and description.
26  *
27  * Notice how this structure closely resembles how you define menu hooks.
28  *
29  * See `drush topic docs-commands` for a list of recognized keys.
30  */
31 function xkcd_drush_command() {
32   $items = array();
33
34   // The 'xkcd' command.
35   $items['xkcd-fetch'] = array(
36     'description' => "Retrieve and display xkcd cartoons.",
37     'arguments' => array(
38       'search' => 'Optional argument to retrive the cartoons matching an index number, keyword search or "random". If omitted the latest cartoon will be retrieved.',
39     ),
40     'options' => array(
41       'image-viewer' => 'Command to use to view images (e.g. xv, firefox). Defaults to "display" (from ImageMagick).',
42       'google-custom-search-api-key' => 'Google Custom Search API Key, available from https://code.google.com/apis/console/. Default key limited to 100 queries/day globally.',
43     ),
44     'examples' => array(
45       'drush xkcd' => 'Retrieve and display the latest cartoon.',
46       'drush xkcd sandwich' => 'Retrieve and display cartoons about sandwiches.',
47       'drush xkcd 123 --image-viewer=eog' => 'Retrieve and display cartoon #123 in eog.',
48       'drush xkcd random --image-viewer=firefox' => 'Retrieve and display a random cartoon in Firefox.',
49     ),
50     'aliases' => array('xkcd'),
51     // No bootstrap at all.
52     'bootstrap' => DRUSH_BOOTSTRAP_NONE,
53   );
54
55   return $items;
56 }
57
58 /**
59  * Implements hook_drush_help().
60  *
61  * This function is called whenever a drush user calls
62  * 'drush help <name-of-your-command>'. This hook is optional. If a command
63  * does not implement this hook, the command's description is used instead.
64  *
65  * This hook is also used to look up help metadata, such as help
66  * category title and summary.  See the comments below for a description.
67  */
68 function xkcd_drush_help($section) {
69   switch ($section) {
70     case 'drush:xkcd-fetch':
71       return dt("A command line tool (1) for a web site tool (2), that emulates
72 (badly) a web based tool (3) that emulates (badly) a command line tool (4) to
73 access a web site (5) with awesome geek humor.\n
74 (1) Drush
75 (2) Drupal
76 (3) http://uni.xkcd.com/
77 (4) BASH
78 (5) http://xkcd.com/");
79   }
80 }
81
82 /**
83  * Implements drush_hook_COMMAND().
84  *
85  * The command callback is where the action takes place.
86  *
87  * The function name should be same as command name but with dashes turned to
88  * underscores and 'drush_commandfile_' prepended, where 'commandfile' is
89  * taken from the file 'commandfile.drush.inc', which in this case is
90  * 'sandwich'. Note also that a simplification step is also done in instances
91  * where the commandfile name is the same as the beginning of the command name,
92  * "drush_example_example_foo" is simplified to just "drush_example_foo".
93  * To also implement a hook that is called before your command, implement
94  * "drush_hook_pre_example_foo".  For a list of all available hooks for a
95  * given command, run drush in --debug mode.
96  *
97  * If for some reason you do not want your hook function to be named
98  * after your command, you may define a 'callback' item in your command
99  * object that specifies the exact name of the function that should be
100  * called.
101  *
102  * In this function, all of Drupal's API is (usually) available, including
103  * any functions you have added in your own modules/themes.
104  *
105  * @see drush_invoke()
106  * @see drush.api.php
107  *
108  * @param string $search
109  *   An optional string with search keyworks, cartoon ID or "random".
110  */
111 function drush_xkcd_fetch($search = '') {
112   if (empty($search)) {
113     drush_xkcd_display('http://xkcd.com');
114   }
115   elseif (is_numeric($search)) {
116     drush_xkcd_display('http://xkcd.com/' . $search);
117   }
118   elseif ($search == 'random') {
119     $xkcd_response = @json_decode(file_get_contents('http://xkcd.com/info.0.json'));
120     if (!empty($xkcd_response->num)) {
121       drush_xkcd_display('http://xkcd.com/' . rand(1, $xkcd_response->num));
122     }
123   }
124   else {
125     // This uses an API key with a limited number of searches per.
126     $search_response = @json_decode(file_get_contents('https://www.googleapis.com/customsearch/v1?key=' . drush_get_option('google-custom-search-api-key', 'AIzaSyDpE01VDNNT73s6CEeJRdSg5jukoG244ek') . '&cx=012652707207066138651:zudjtuwe28q&q=' . $search));
127     if (!empty($search_response->items)) {
128       foreach ($search_response->items as $item) {
129         drush_xkcd_display($item->link);
130       }
131     }
132     else {
133       drush_set_error('DRUSH_XKCD_SEARCH_FAIL', dt('The search failed or produced no results.'));
134     }
135   }
136 }
137
138 /**
139  * Display a given XKCD cartoon.
140  *
141  * Retrieve and display a table of metadata for an XKCD cartoon, then retrieve
142  * and display the cartoon using a specified image viewer.
143  *
144  * @param string $url
145  *   A string with the URL of the cartoon to display.
146  */
147 function drush_xkcd_display($url) {
148   $xkcd_response = @json_decode(file_get_contents($url . '/info.0.json'));
149   if (!empty($xkcd_response->num)) {
150     $data = (array) $xkcd_response;
151     $data['date'] = $data['year'] . '/' . $data['month'] . '/' . $data['day'];
152     unset($data['safe_title'], $data['news'], $data['link'], $data['year'], $data['month'], $data['day']);
153     drush_print_table(drush_key_value_to_array_table($data));
154     $img = drush_download_file($data['img']);
155     drush_register_file_for_deletion($img);
156     drush_shell_exec(drush_get_option('image-viewer', 'display') . ' ' . $img);
157   }
158   else {
159     drush_set_error('DRUSH_XKCD_METADATA_FAIL', dt('Unable to retrieve cartoon metadata.'));
160   }
161 }