| 22 | | |
| 23 | | // TO BE DEVELOPED |
| | 23 | // attempt to prevent this script from being run through the webserver. |
| | 24 | |
| | 25 | if ( @$_SERVER["HTTP_HOST"] != NULL ) |
| | 26 | die( "No" ); |
| | 27 | |
| | 28 | /** |
| | 29 | * ddtpreproc |
| | 30 | * |
| | 31 | * Since Javascript does not seem to have variables similar to __FILE__ and |
| | 32 | * __LINE__ from php, this script pre-processes .js files to patch in file and |
| | 33 | * line numbers into all _ddt() calls. |
| | 34 | * |
| | 35 | * After making an editing pass through the .js files, this script should be |
| | 36 | * called to update the line numbers. |
| | 37 | * |
| | 38 | * @package xinha |
| | 39 | * @subpackage devutils |
| | 40 | * @author Yermo Lamers |
| | 41 | * @copyright DTLink, LLC 2005 |
| | 42 | * |
| | 43 | * @todo handle multi-line comments. |
| | 44 | * @todo handle nested ");" |
| | 45 | * @todo distinguish between _ddt( $file, $line, $msg ) and _ddt( "file", "100", "message" ); |
| | 46 | */ |
| | 47 | |
| | 48 | // -------------------------------------------------- |
| | 49 | |
| | 50 | /** |
| | 51 | * primitive ddt() placeholder. |
| | 52 | * |
| | 53 | * edit this function to turn on debugging messages. |
| | 54 | */ |
| | 55 | |
| | 56 | function _ddt( $file, $line, $msg ) |
| | 57 | { |
| | 58 | |
| | 59 | // uncomment this line to turn on debugging messages. |
| | 60 | |
| | 61 | print( basename( $file ) . ":$line - $msg\n" ); |
| | 62 | |
| | 63 | } |
| | 64 | |
| | 65 | /* ----------------------------------------------------- */ |
| | 66 | |
| | 67 | /** |
| | 68 | * recursive apply |
| | 69 | * |
| | 70 | * recurse through a directory looking for files that match the given expression |
| | 71 | * and apply the given function. |
| | 72 | */ |
| | 73 | |
| | 74 | function rapply( $path, $regex, $function ) |
| | 75 | { |
| | 76 | |
| | 77 | _ddt( __FILE__, __LINE__, "rapply(): top with path '$path', regex '$regex', function '$function'" ); |
| | 78 | |
| | 79 | // does the directory or file exist? |
| | 80 | |
| | 81 | if ( !file_exists( $path )) |
| | 82 | return false ; |
| | 83 | |
| | 84 | // if path is not a directory, and it matches the regex, apply the function. |
| | 85 | |
| | 86 | if ( !is_dir( $path ) ) |
| | 87 | { |
| | 88 | |
| | 89 | if ( preg_match( $regex, $path ) ) |
| | 90 | { |
| | 91 | |
| | 92 | if ( ! $function( $path ) ) |
| | 93 | { |
| | 94 | |
| | 95 | _ddt( __FILE__, __LINE__, "rapply(): function '$function' returned false" ); |
| | 96 | |
| | 97 | return false ; |
| | 98 | } |
| | 99 | } |
| | 100 | |
| | 101 | return true ; |
| | 102 | } |
| | 103 | else |
| | 104 | { |
| | 105 | |
| | 106 | // process the entries in the directory |
| | 107 | |
| | 108 | if (( $dh = opendir($path)) === false ) |
| | 109 | { |
| | 110 | _ddt( __FILE__, __LINE__, "rapply(): unable to open directory '$path'" ); |
| | 111 | |
| | 112 | return false ; |
| | 113 | } |
| | 114 | |
| | 115 | while (false !== ($filename = readdir( $dh ))) |
| | 116 | { |
| | 117 | if (( $filename != ".") && ( $filename != ".." )) |
| | 118 | { |
| | 119 | if ( ! rapply( "$path/$filename", $regex, $function )) |
| | 120 | { |
| | 121 | return false ; |
| | 122 | } |
| | 123 | } |
| | 124 | } |
| | 125 | |
| | 126 | closedir($dh); |
| | 127 | |
| | 128 | return true ; |
| | 129 | |
| | 130 | } // end of if it's a directory |
| | 131 | |
| | 132 | } // end of rapply() |
| | 133 | |
| | 134 | // -------------------------------------------------------------- |
| | 135 | |
| | 136 | /** |
| | 137 | * process _ddt() calls in a javascript file. |
| | 138 | * |
| | 139 | * Adds in file and linenumber arguments to all _ddt() calls |
| | 140 | * in a javascript file. Handles the case where no file and line |
| | 141 | * info has been added (a fresh debug statement) and where ones |
| | 142 | * are already present. |
| | 143 | * |
| | 144 | * @todo handle rare case of multiple ddt() calls on a single line. |
| | 145 | * @todo improve ddt identification regex so that it handles embedded ); |
| | 146 | */ |
| | 147 | |
| | 148 | function procDDT( $path ) |
| | 149 | { |
| | 150 | |
| | 151 | _ddt( __FILE__, __LINE__, "procDDT(): top with path '$path'" ); |
| | 152 | |
| | 153 | if ( !file_exists( $path ) ) |
| | 154 | { |
| | 155 | _ddt( __FILE__, __LINE__, "procDDT(): file '$path' does not exist" ); |
| | 156 | return false; |
| | 157 | } |
| | 158 | |
| | 159 | // load into an array. loop over each line in the array doing the replace. |
| | 160 | // then write the whole thing out. |
| | 161 | |
| | 162 | if (( $content_array = file( $path )) === false ) |
| | 163 | { |
| | 164 | |
| | 165 | _ddt( __FILE__, __LINE__, "procDDT(): file() returned false on '$path'" ); |
| | 166 | return false; |
| | 167 | } |
| | 168 | |
| | 169 | _ddt( __FILE__, __LINE__, "procDDT(): read in file with '" . count( $content_array ) . "' lines" ); |
| | 170 | |
| | 171 | // now loop over the array doing a search and replace |
| | 172 | |
| | 173 | foreach ( $content_array as $index => $line ) |
| | 174 | { |
| | 175 | |
| | 176 | // do we have a _ddt() call with existing file and line numbers? |
| | 177 | // |
| | 178 | // we make a nasty assmption here that ); will not be present inside the debug message. |
| | 179 | // Doing the double regex here is wasteful ... |
| | 180 | |
| | 181 | $line_number = $index + 1; |
| | 182 | |
| | 183 | if ( preg_match( "/_ddt\([\s]*\"[^,]+\"[\s]*,[\s]*\"[0-9]+\"[\s]*,.*?\);/", $line ) ) |
| | 184 | { |
| | 185 | |
| | 186 | _ddt( __FILE__, __LINE__, "procDDT(): found an existing file/line ddt on line $index" ); |
| | 187 | |
| | 188 | $content_array[ $index ] = preg_replace( "/_ddt\([\s]*\"[^,]+\"[\s]*,[\s]*\"[0-9]+\"[\s]*,[\s]*(.*?)[\s]*\);/", "_ddt( \"" . basename( $path ) . "\",\"$line_number\", $1 );", $line ); |
| | 189 | |
| | 190 | } |
| | 191 | else if ( preg_match( "/_ddt\((.*?)\);/", $line ) ) |
| | 192 | { |
| | 193 | |
| | 194 | _ddt( __FILE__, __LINE__, "procDDT(): found a new ddt on line $index" ); |
| | 195 | |
| | 196 | $content_array[ $index ] = preg_replace( "/_ddt\((.*?)\);/", "_ddt( \"" . basename( $path ) . "\",\"$line_number\", $1 );", $line ); |
| | 197 | |
| | 198 | _ddt( __FILE__, __LINE__, "procDDT(): existing line changed to '" . $content_array[$index] . "'" ); |
| | 199 | } |
| | 200 | |
| | 201 | } // end of foreach. |
| | 202 | |
| | 203 | // write the thing out. |
| | 204 | |
| | 205 | if (( $fh = fopen( $path, "w" )) == NULL ) |
| | 206 | { |
| | 207 | |
| | 208 | _ddt( __FILE__, __LINE__, "procDDT(): unable to open '$path' for writing" ); |
| | 209 | |
| | 210 | return false; |
| | 211 | |
| | 212 | } |
| | 213 | |
| | 214 | foreach ( $content_array as $index => $line ) |
| | 215 | fputs( $fh, $line ); |
| | 216 | |
| | 217 | fclose( $fh ); |
| | 218 | |
| | 219 | _ddt( __FILE__, __LINE__, "procDDT(): bottom" ); |
| | 220 | |
| | 221 | return true; |
| | 222 | |
| | 223 | } // end of procDDT() |
| | 224 | |
| | 225 | // ------------------------------------------------------------------ |
| | 226 | |
| | 227 | // check usage. |
| | 228 | |
| | 229 | if ( $argc != 2 ) |
| | 230 | { |
| | 231 | die( "Usage: ddtpreproc.php path_to_xinha_root\n" ); |
| | 232 | } |
| | 233 | |
| | 234 | $dir_path = $argv[1]; |
| | 235 | |
| | 236 | if ( ! file_exists( $dir_path ) ) |
| | 237 | { |
| | 238 | die( "Path '$dir_path' does not exist\n" ); |
| | 239 | } |
| | 240 | |
| | 241 | // recurse through the directory applying the procDDT function on all |
| | 242 | // files ending in ".js". |
| | 243 | |
| | 244 | rapply( $dir_path, "/.*js$/", "procDDT" ); |
| | 245 | |
| | 246 | // END |
| | 247 | |
| | 248 | ?> |