Pathologic was missing because of a .git folder inside.
[yaffs-website] / web / modules / contrib / hacked / hacked.drush.inc
1 <?php
2
3
4 /**
5  * @file
6  *   Hacked drush command.
7  *
8  *   Enables drush support for the Hacked! module.
9  */
10
11 /**
12  * Implementation of hook_drush_help().
13  */
14 function hacked_drush_help($section) {
15   switch ($section) {
16     case 'drush:hacked-list-projects':
17       return dt('List projects and their hacked/unhacked status.');
18     case 'drush:hacked-details':
19       return dt('Show details of the files in one project, and the hacked/unhacked status of those files.');
20     case 'drush:hacked-diff':
21       return dt('Output a unified diff of the specified project.');
22
23   }
24 }
25
26 /**
27  * Implementation of hook_drush_command().
28  *
29  * @See drush_parse_command() for a list of recognized keys.
30  *
31  * @return
32  *   An associative array describing your command(s).
33  */
34 function hacked_drush_command() {
35   $items = array();
36
37   $items['hacked-list-projects'] = array(
38     'description' => "List all projects that can be analysed by Hacked! ",
39     'drupal dependencies' => array('hacked'),
40     'options' => array(
41       'force-rebuild' => 'Rebuild the Hacked! report instead of getting a cached version.'
42     ),
43     'aliases' => array('hlp'),
44   );
45
46   $items['hacked-lock-modified'] = array(
47     'description' => "Lock all projects that Hacked! detects are modified, so that drush pm-updatecode will not touch them. (drush-4.x+ only)",
48     'drupal dependencies' => array('hacked'),
49   );
50
51   $items['hacked-details'] = array(
52     'description' => "Show the Hacked! report about a specific project.",
53     'drupal dependencies' => array('hacked'),
54     'arguments' => array(
55       'project' => 'The machine name of the project to report on.',
56     ),
57     'options' => array(
58       'include-unchanged' => 'Show the files that are unchanged too.',
59     ),
60     'aliases' => array('hd'),
61   );
62
63   $items['hacked-diff'] = array(
64     'description' => "Output a unified diff of the project specified.",
65     'drupal dependencies' => array('hacked'),
66     'arguments' => array(
67       'project' => 'The machine name of the project to report on.',
68     ),
69     'options' => array(
70       'diff-options' => 'Command line options to pass through to the diff command.'
71     ),
72   );
73
74   return $items;
75 }
76
77 /**
78  * Compute the report data for hacked.
79  *
80  * WARNING: This function can invoke a batch process and end your current page.
81  * So you'll want to be very careful if you call this!
82  */
83 function hacked_calculate_project_data_drush($projects, $force = FALSE, $redirect = NULL) {
84   include_once DRUPAL_ROOT . '/includes/batch.inc';
85
86   // Try to get the report form cache if we can.
87   $cache = cache_get('hacked:drush:full-report', HACKED_CACHE_TABLE);
88   if (!empty($cache->data) && !$force) {
89     return $cache->data;
90   }
91
92   // Enter a batch to build the report.
93   $operations = array();
94   foreach ($projects as $project) {
95     $operations[] = array(
96       'hacked_build_report_batch',
97       array($project['name']),
98     );
99   }
100
101   $batch = array(
102     'operations' => $operations,
103     'finished' => 'hacked_build_report_batch_finished_drush',
104     'file' => drupal_get_path('module', 'hacked') . '/hacked.report.inc',
105     'title' => t('Building report'),
106   );
107
108   drush_print('Rebuilding Hacked! report');
109   batch_set($batch);
110   $batch =& batch_get();
111   $batch['progressive'] = FALSE;
112   drush_backend_batch_process();
113   drush_print('Done.');
114
115   // Now we can get the data from the cache.
116   $cache = cache_get('hacked:drush:full-report', HACKED_CACHE_TABLE);
117   if (!empty($cache->data)) {
118     return $cache->data;
119   }
120 }
121
122 /**
123  * Completion callback for the report batch.
124  */
125 function hacked_build_report_batch_finished_drush($success, $results, $operations) {
126   if ($success) {
127     // Sort the results.
128     usort($results['report'], '_hacked_project_report_sort_by_status');
129     // Store them.
130     cache_set('hacked:drush:full-report', $results['report'], HACKED_CACHE_TABLE, strtotime('+1 day'));
131   }
132 }
133
134 /**
135  * Drush command callback that shows the listing of changed/unchanged projects.
136  */
137 function drush_hacked_list_projects() {
138
139   // Go get the data:
140   module_load_include('inc', 'update', 'update.report');
141   if ($available = update_get_available(TRUE)) {
142     module_load_include('inc', 'update', 'update.compare');
143     $data = update_calculate_project_data($available);
144     $force_rebuild = drush_get_option('force-rebuild', FALSE);
145     $projects = hacked_calculate_project_data_drush($data, $force_rebuild);
146     // Now print the data using drush:
147     $rows[] = array(
148       dt('Title'),
149       dt('Name'),
150       dt('Version'),
151       dt('Status'),
152       dt('Changed'),
153       dt('Deleted'),
154     );
155     foreach ($projects as $project) {
156       $row = array(
157         $project['title'],
158         $project['name'],
159         $project['existing_version']
160       );
161
162       // Now add the status:
163       switch ($project['status']) {
164         case HACKED_STATUS_UNHACKED:
165           $row[] = dt('Unchanged');
166           break;
167         case HACKED_STATUS_HACKED:
168           $row[] = t('Changed');
169           break;
170         case HACKED_STATUS_UNCHECKED:
171         default:
172           $row[] = t('Unchecked');
173           break;
174       }
175
176       $row[] = $project['counts']['different'];
177       $row[] = $project['counts']['missing'];
178
179
180       $rows[] = $row;
181     }
182     drush_print_table($rows, TRUE);
183
184     return $projects;
185   }
186 }
187
188 /**
189  * Lock all of the modified files so that pm-updatecode will not
190  * touch them.
191  */
192 function drush_hacked_lock_modified() {
193   $drupal_root = drush_get_context('DRUSH_DRUPAL_ROOT');
194   module_load_include('inc', 'update', 'update.report');
195   if (isset($drupal_root) && ($available = update_get_available(TRUE))) {
196     module_load_include('inc', 'update', 'update.compare');
197     $data = update_calculate_project_data($available);
198     $projects = hacked_calculate_project_data_drush($data, TRUE);
199
200     foreach ($projects as $project) {
201       $row = array(
202         empty($project['title']) ? $project['name'] : $project['title'],
203         $project['name'],
204         $project['existing_version']
205       );
206
207       // Lock the file if it is not already locked.
208       switch ($project['status']) {
209         case HACKED_STATUS_HACKED:
210           $project_ob = NULL;
211           $project_ob = hacked_project_load($project['project_name']);
212           $lockfile = $project_ob->file_get_location('local', '.drush-lock-update');
213           if (!file_exists($lockfile)) {
214             drush_op('file_put_contents', $lockfile, dt("Locked: modified."));
215             drush_print(dt("Locked: @project", array('@project' => $project['name'])));
216           }
217           else {
218             drush_print(dt("@project is modified and already locked", array('@project' => $project['name'])));
219           }
220           break;
221         case HACKED_STATUS_UNHACKED:
222         case HACKED_STATUS_UNCHECKED:
223         default:
224           break;
225       }
226     }
227   }
228 }
229
230 /**
231  * Add a --lock-modified flag to pm-updatecode
232  */
233 function drush_hacked_pre_pm_updatecode() {
234   if (drush_get_option('lock-modified')) {
235     drush_print(dt('Hacked! is checking for modified projects...'));
236     drush_hacked_lock_modified();
237     drush_print(dt('Hacked! modification check complete.'));
238   }
239 }
240
241 /**
242  * Add --lock-modified to the pm-updatecode and pm-update help
243  */
244 function hacked_drush_help_alter(&$command) {
245   if (($command['command'] == 'pm-updatecode') || ($command['command'] == 'pm-update')) {
246     $command['sub-options']['--lock']['--lock-modified'] = "Lock any project that Hacked! determines is modified.";
247   }
248 }
249
250 /**
251  * Validate hook for the hacked_details drush command.
252  */
253 function drush_hacked_details_validate($short_name = '') {
254   return drush_hacked_drush_command_validate($short_name);
255 }
256
257 /**
258  * Validate hook for the hacked drush commands that need a project.
259  */
260 function drush_hacked_drush_command_validate($short_name = '') {
261   if (empty($short_name)) {
262     return drush_set_error('HACKED_PROJECT_NOT_FOUND', dt('A valid project must be specified', array('@project' => $short_name)));
263   }
264
265   $project = hacked_project_load($short_name);
266   $project->identify_project();
267   if (!$project->project_identified) {
268     return drush_set_error('HACKED_PROJECT_NOT_FOUND', dt('Could not find project: @project', array('@project' => $short_name)));
269   }
270   $project = NULL;
271 }
272
273 /**
274  * Drush command callback that shows the list of changes/unchanged files in a project.
275  *
276  * You may specify the --include-unchanged option to show unchanged files too,
277  * otherwise just the changed and deleted files are shown.
278  */
279 function drush_hacked_details($short_name) {
280   $project = hacked_project_load($short_name);
281   $report = $project->compute_details();
282
283   drush_print(dt('Details for project: @name', array('@name' => $project->title())));
284   drush_print(dt('Total files: @total_files, files changed: @changed_files, deleted files: @deleted_files', array(
285     '@total_files' => count($report['files']),
286     '@changed_files' => $report['counts']['different'],
287     '@deleted_files' => $report['counts']['missing'],
288   )));
289   drush_print('');
290
291   drush_print(dt('Detailed results:'));
292   // Sort the results:
293   arsort($report['files']);
294
295   $rows[] = array(
296     dt('Status'),
297     dt('File'),
298   );
299   $show_unchanged = drush_get_option('include-unchanged', FALSE);
300   foreach ($report['files'] as $file => $status) {
301     if (!$show_unchanged && $status == HACKED_STATUS_UNHACKED) {
302       continue;
303     }
304     $row = array(
305     );
306
307     // Now add the status:
308     switch ($status) {
309       case HACKED_STATUS_UNHACKED:
310         $row[] = dt('Unchanged');
311         break;
312       case HACKED_STATUS_HACKED:
313         $row[] = t('Changed');
314         break;
315       case HACKED_STATUS_DELETED:
316         $row[] = t('Deleted');
317         break;
318       case HACKED_STATUS_UNCHECKED:
319       default:
320         $row[] = t('Unchecked');
321         break;
322     }
323
324     $row[] = $file;
325
326
327     $rows[] = $row;
328   }
329   drush_print_table($rows, TRUE);
330
331
332 }
333
334 /**
335  * Validate hook for the hacked_diff drush command.
336  */
337 function drush_hacked_diff_validate($short_name = '') {
338   return drush_hacked_drush_command_validate($short_name);
339 }
340
341 /**
342  * Drush command callback that shows the list of changes/unchanged files in a project.
343  *
344  * You may specify the --include-unchanged option to show unchanged files too,
345  * otherwise just the changed and deleted files are shown.
346  */
347 function drush_hacked_diff($short_name) {
348   $project = hacked_project_load($short_name);
349
350   $local_location = $project->file_get_location('local', '');
351   $clean_location = $project->file_get_location('remote', '');
352
353   // If the hasher is our ignore line endings one, then ignore line endings.
354   if (variable_get('hacked_selected_file_hasher', HACKED_DEFAULT_FILE_HASHER) == 'hacked_ignore_line_endings') {
355     $default_options = '-uprb';
356   }
357   else {
358     $default_options = '-upr';
359   }
360
361   $diff_options = drush_get_option('diff-options', $default_options);
362   drush_shell_exec("diff $diff_options $clean_location $local_location");
363
364   $lines = drush_shell_exec_output();
365   $local_location_trim = dirname($local_location . '/dummy.file') . '/';
366   $clean_location_trim = dirname($clean_location . '/dummy.file') . '/';
367   foreach ($lines as $line) {
368     if (strpos($line, '+++') === 0) {
369       $line = str_replace($local_location_trim, '', $line);
370     }
371     if (strpos($line, '---') === 0) {
372       $line = str_replace($clean_location_trim, '', $line);
373     }
374     if (strpos($line, 'diff -upr') === 0) {
375       $line = str_replace($clean_location_trim, 'a/', $line);
376       $line = str_replace($local_location_trim, 'b/', $line);
377     }
378
379     drush_print($line);
380
381   }
382
383 }