/[drupal]/contributions/modules/coder/scripts/coder_format/coder_format.php
ViewVC logotype

Contents of /contributions/modules/coder/scripts/coder_format/coder_format.php

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.3 - (show annotations) (download) (as text)
Sun Feb 24 19:40:50 2008 UTC (21 months ago) by sun
Branch: MAIN
CVS Tags: DRUPAL-7--1-0-BETA1, HEAD
Changes since 1.2: +389 -63 lines
File MIME type: text/x-php
Syncing HEAD with latest code in DRUPAL-6--1.
1 <?php
2 // $Id: coder_format.php,v 1.2.4.5 2008/01/26 18:09:56 sun Exp $
3
4
5 /**
6 * @file
7 * Coder format shell invocation script.
8 *
9 * @param $filepath
10 * One or more files or directories containing the source code to process. If
11 * a directory is given, all files in that directory are processed recursively.
12 * Each file will be replaced with the formatted source code. Each original
13 * file is automatically backup to <filename>.coder.orig.
14 * @param --undo
15 * If the optional --undo argument is given, processed files are restored from
16 * their backups. Coder format automatically searches for the latest backup
17 * file (*.coder.orig) and each filename (without extension .coder.orig) is
18 * replaced with its backup file.
19 *
20 * @usage
21 * php coder_format.php <filepath> [filepath] ...
22 * php coder_format.php <path> [path] ...
23 * php coder_format.php --undo <filepath> ...
24 * php coder_format.php --undo <path> ...
25 *
26 * @example
27 * php coder_format.php modules/node/node.module
28 * php coder_format.php index.php modules/node/node.module
29 * php coder_format.php /home/drupal
30 * php coder_format.php /home/drupal/includes /home/drupal/modules
31 * php coder_format.php --undo modules/node/node.module
32 * php coder_format.php --undo /home/drupal
33 *
34 * Important note for Windows users:
35 * - Ensure to encapsulate filename arguments with double quotes.
36 */
37
38 // We are on the command line, so output only real errors.
39 error_reporting(E_ERROR | E_WARNING | E_PARSE);
40
41 require_once realpath(dirname($_SERVER['PHP_SELF'])) .'/coder_format.inc';
42
43 if (!empty($_SERVER['argv'])) {
44 // Remove self-reference.
45 array_shift($_SERVER['argv']);
46
47 // Process command-line arguments.
48 $files = array();
49 $undo = false;
50
51 for ($c = 0, $cc = count($_SERVER['argv']); $c < $cc; ++$c) {
52 switch ($_SERVER['argv'][$c]) {
53 case '--undo':
54 $undo = true;
55 break;
56
57 default:
58 $files[] = $_SERVER['argv'][$c];
59 break;
60 }
61 }
62
63 foreach ($files as $file) {
64 // If argument is a directory, process it recursively.
65 if (is_dir($file)) {
66 coder_format_recursive($file, $undo);
67 }
68 else {
69 coder_format_file($file, $undo);
70 }
71 }
72 }
73
74 /**
75 * @defgroup coder_format_file_functions
76 * @{
77 * These functions are copied from Drupal's file.inc. Almost all function calls
78 * to other Drupal functions have been removed since they would always return
79 * FALSE.
80 */
81
82 define('FILE_CREATE_DIRECTORY', 1);
83 define('FILE_EXISTS_RENAME', 0);
84 define('FILE_EXISTS_REPLACE', 1);
85 define('FILE_EXISTS_ERROR', 2);
86
87 /**
88 * Check that the directory exists and is writable. Directories need to
89 * have execute permissions to be considered a directory by FTP servers, etc.
90 *
91 * @param $directory A string containing the name of a directory path.
92 * @param $mode A Boolean value to indicate if the directory should be created
93 * if it does not exist or made writable if it is read-only.
94 * @param $form_item An optional string containing the name of a form item that
95 * any errors will be attached to. This is useful for settings forms that
96 * require the user to specify a writable directory. If it can't be made to
97 * work, a form error will be set preventing them from saving the settings.
98 * @return FALSE when directory not found, or TRUE when directory exists.
99 */
100 function file_check_directory(&$directory, $mode = 0, $form_item = NULL) {
101 $directory = rtrim($directory, '/\\');
102
103 // Check if directory exists.
104 if (!is_dir($directory)) {
105 // coder_format does not alter the filesystem. 23/01/2008 sun
106 if (!file_exists($directory)) {
107 drupal_set_message(t('The directory %directory does not exist.', array('%directory' => $directory)));
108 return FALSE;
109 }
110 }
111
112 // Check to see if the directory is writable.
113 if (!is_writable($directory)) {
114 // coder_format does not alter the filesystem. 23/01/2008 sun
115 drupal_set_message(t('The directory %directory is not writable', array('%directory' => $directory)));
116 return FALSE;
117 }
118
119 // coder_format is applied outside of Drupal in most cases. 23/01/2008 sun
120
121 return TRUE;
122 }
123
124 /**
125 * Checks path to see if it is a directory, or a dir/file.
126 *
127 * @param $path A string containing a file path. This will be set to the
128 * directory's path.
129 * @return If the directory is not in a Drupal writable directory, FALSE is
130 * returned. Otherwise, the base name of the path is returned.
131 */
132 function file_check_path(&$path) {
133 // Check if path is a directory.
134 // coder_format uses file_check_path() to check for files only. 26/01/2008 sun
135
136 // Check if path is a possible dir/file.
137 $filename = basename($path);
138 $path = dirname($path);
139 if (file_check_directory($path)) {
140 return $filename;
141 }
142
143 return FALSE;
144 }
145
146 /**
147 * Copies a file to a new location. This is a powerful function that in many ways
148 * performs like an advanced version of copy().
149 * - Checks if $source and $dest are valid and readable/writable.
150 * - Performs a file copy if $source is not equal to $dest.
151 * - If file already exists in $dest either the call will error out, replace the
152 * file or rename the file based on the $replace parameter.
153 *
154 * @param $source A string specifying the file location of the original file.
155 * This parameter will contain the resulting destination filename in case of
156 * success.
157 * @param $dest A string containing the directory $source should be copied to.
158 * If this value is omitted, Drupal's 'files' directory will be used.
159 * @param $replace Replace behavior when the destination file already exists.
160 * - FILE_EXISTS_REPLACE - Replace the existing file
161 * - FILE_EXISTS_RENAME - Append _{incrementing number} until the filename is unique
162 * - FILE_EXISTS_ERROR - Do nothing and return FALSE.
163 * @return True for success, FALSE for failure.
164 */
165 function file_copy(&$source, $dest = 0, $replace = FILE_EXISTS_RENAME) {
166 // $dest is almost always outside of Drupal. 23/01/2008 sun
167 // $dest = file_create_path($dest);
168
169 $directory = $dest;
170 $basename = file_check_path($directory);
171
172 // Make sure we at least have a valid directory.
173 if ($basename === FALSE) {
174 $source = is_object($source) ? $source->filepath : $source;
175 drupal_set_message(t('The selected file %file could not be uploaded, because the destination %directory is not properly configured.', array('%file' => $source, '%directory' => $dest)), 'error');
176 return 0;
177 }
178
179 // Removed upload handling. coder_format does not deal with uploads. 23/01/2008 sun
180
181 $source = realpath($source);
182 if (!file_exists($source)) {
183 drupal_set_message(t('The selected file %file could not be copied, because no file by that name exists. Please check that you supplied the correct filename.', array('%file' => $source)), 'error');
184 return 0;
185 }
186
187 // If the destination file is not specified then use the filename of the source file.
188 $basename = $basename ? $basename : basename($source);
189 $dest = $directory .'/'. $basename;
190
191 // Make sure source and destination filenames are not the same, makes no sense
192 // to copy it if they are. In fact copying the file will most likely result in
193 // a 0 byte file. Which is bad. Real bad.
194 if ($source != realpath($dest)) {
195 if (!$dest = file_destination($dest, $replace)) {
196 drupal_set_message(t('The selected file %file could not be copied, because a file by that name already exists in the destination.', array('%file' => $source)), 'error');
197 return FALSE;
198 }
199
200 if (!@copy($source, $dest)) {
201 drupal_set_message(t('The selected file %file could not be copied.', array('%file' => $source)), 'error');
202 return 0;
203 }
204
205 // Give everyone read access so that FTP'd users or
206 // non-webserver users can see/read these files,
207 // and give group write permissions so group members
208 // can alter files uploaded by the webserver.
209 @chmod($dest, 0664);
210 }
211
212 // Removed upload handling. coder_format does not deal with uploads. 23/01/2008 sun
213 $source = $dest;
214
215 return 1; // Everything went ok.
216 }
217
218 /**
219 * Determines the destination path for a file depending on how replacement of
220 * existing files should be handled.
221 *
222 * @param $destination A string specifying the desired path.
223 * @param $replace Replace behavior when the destination file already exists.
224 * - FILE_EXISTS_REPLACE - Replace the existing file
225 * - FILE_EXISTS_RENAME - Append _{incrementing number} until the filename is
226 * unique
227 * - FILE_EXISTS_ERROR - Do nothing and return FALSE.
228 * @return The destination file path or FALSE if the file already exists and
229 * FILE_EXISTS_ERROR was specified.
230 */
231 function file_destination($destination, $replace) {
232 if (file_exists($destination)) {
233 switch ($replace) {
234 case FILE_EXISTS_RENAME:
235 $basename = basename($destination);
236 $directory = dirname($destination);
237 $destination = file_create_filename($basename, $directory);
238 break;
239
240 case FILE_EXISTS_ERROR:
241 drupal_set_message(t('The selected file %file could not be copied, because a file by that name already exists in the destination.', array('%file' => $destination)), 'error');
242 return FALSE;
243 }
244 }
245 return $destination;
246 }
247
248 /**
249 * Moves a file to a new location.
250 * - Checks if $source and $dest are valid and readable/writable.
251 * - Performs a file move if $source is not equal to $dest.
252 * - If file already exists in $dest either the call will error out, replace the
253 * file or rename the file based on the $replace parameter.
254 *
255 * @param $source A string specifying the file location of the original file.
256 * This parameter will contain the resulting destination filename in case of
257 * success.
258 * @param $dest A string containing the directory $source should be copied to.
259 * If this value is omitted, Drupal's 'files' directory will be used.
260 * @param $replace Replace behavior when the destination file already exists.
261 * - FILE_EXISTS_REPLACE - Replace the existing file
262 * - FILE_EXISTS_RENAME - Append _{incrementing number} until the filename is unique
263 * - FILE_EXISTS_ERROR - Do nothing and return FALSE.
264 * @return True for success, FALSE for failure.
265 */
266 function file_move(&$source, $dest = 0, $replace = FILE_EXISTS_RENAME) {
267 $path_original = is_object($source) ? $source->filepath : $source;
268
269 if (file_copy($source, $dest, $replace)) {
270 $path_current = is_object($source) ? $source->filepath : $source;
271
272 if ($path_original == $path_current || file_delete($path_original)) {
273 return 1;
274 }
275 drupal_set_message(t('The removal of the original file %file has failed.', array('%file' => $path_original)), 'error');
276 }
277 return 0;
278 }
279
280 /**
281 * Create a full file path from a directory and filename. If a file with the
282 * specified name already exists, an alternative will be used.
283 *
284 * @param $basename string filename
285 * @param $directory string directory
286 * @return
287 */
288 function file_create_filename($basename, $directory) {
289 $dest = $directory .'/'. $basename;
290
291 if (file_exists($dest)) {
292 // Destination file already exists, generate an alternative.
293 // Always append '.coder.orig' (allows multiple undos). 23/01/2008 sun
294 $name = $basename;
295
296 $counter = 0;
297 do {
298 $dest = $directory .'/'. $name . str_repeat('.coder.orig', $counter++);
299 } while (file_exists($dest));
300 }
301
302 return $dest;
303 }
304
305 /**
306 * Delete a file.
307 *
308 * @param $path A string containing a file path.
309 * @return TRUE for success, FALSE for failure.
310 */
311 function file_delete($path) {
312 if (is_file($path)) {
313 return unlink($path);
314 }
315 }
316
317 /**
318 * Finds all files that match a given mask in a given directory.
319 * Directories and files beginning with a period are excluded; this
320 * prevents hidden files and directories (such as SVN working directories)
321 * from being scanned.
322 *
323 * @param $dir
324 * The base directory for the scan, without trailing slash.
325 * @param $mask
326 * The regular expression of the files to find.
327 * @param $nomask
328 * An array of files/directories to ignore.
329 * @param $callback
330 * The callback function to call for each match.
331 * @param $recurse
332 * When TRUE, the directory scan will recurse the entire tree
333 * starting at the provided directory.
334 * @param $key
335 * The key to be used for the returned array of files. Possible
336 * values are "filename", for the path starting with $dir,
337 * "basename", for the basename of the file, and "name" for the name
338 * of the file without an extension.
339 * @param $min_depth
340 * Minimum depth of directories to return files from.
341 * @param $depth
342 * Current depth of recursion. This parameter is only used internally and should not be passed.
343 *
344 * @return
345 * An associative array (keyed on the provided key) of objects with
346 * "path", "basename", and "name" members corresponding to the
347 * matching files.
348 */
349 function file_scan_directory($dir, $mask, $nomask = array('.', '..', 'CVS'), $callback = 0, $recurse = TRUE, $key = 'filename', $min_depth = 0, $depth = 0) {
350 $key = (in_array($key, array('filename', 'basename', 'name')) ? $key : 'filename');
351 $files = array();
352
353 if (is_dir($dir) && $handle = opendir($dir)) {
354 while ($file = readdir($handle)) {
355 if (!in_array($file, $nomask) && $file[0] != '.') {
356 if (is_dir("$dir/$file") && $recurse) {
357 // Give priority to files in this folder by merging them in after any subdirectory files.
358 $files = array_merge(file_scan_directory("$dir/$file", $mask, $nomask, $callback, $recurse, $key, $min_depth, $depth + 1), $files);
359 }
360 elseif ($depth >= $min_depth && ereg($mask, $file)) {
361 // Always use this match over anything already set in $files with the same $$key.
362 $filename = "$dir/$file";
363 $basename = basename($file);
364 $name = substr($basename, 0, strrpos($basename, '.'));
365 $files[$$key] = new stdClass();
366 $files[$$key]->filename = $filename;
367 $files[$$key]->basename = $basename;
368 $files[$$key]->name = $name;
369 if ($callback) {
370 $callback($filename);
371 }
372 }
373 }
374 }
375
376 closedir($handle);
377 }
378
379 return $files;
380 }
381
382 /**
383 * @} End of "defgroup coder_format_file_functions".
384 */
385
386 /**
387 * @defgroup coder_format_stub_functions
388 * @{
389 */
390 function t($string, $args = 0) {
391 if (!$args) {
392 return $string;
393 }
394 else {
395 return strtr($string, $args);
396 }
397 }
398
399 function drupal_set_message($message = NULL, $type = 'status') {
400 if ($type == 'error') {
401 echo str_repeat('-', 80);
402 echo "\nERROR: $message\n";
403 echo str_repeat('-', 80);
404 echo "\n";
405 }
406 else {
407 echo "$message\n";
408 }
409 }
410
411 /**
412 * @} End of "defgroup coder_format_stub_functions".
413 */
414

  ViewVC Help
Powered by ViewVC 1.1.2