source: branches/unified_backend/devutils/ddtpreproc.php.in @ 84

Last change on this file since 84 was 84, checked in by yermol, 15 years ago

_ddt() trace system now writes to a popup window resulting in much faster execution.
_ddt() messages now identifiy file, line and source of trace message.
_ddt() popup window has "add hr" link
ddtpreprop.php script added to patch in file and line numbers.
full_example-body.html and simple_example.html bugfix to not double-call xinha-init.
simple_example.html now only loads a single Xinha instance.

File size: 5.9 KB
Line 
1#![@@PHP@@]
2<?php
3
4// [NOSTRIP
5// -----------------------------------------------------------------
6// Copyright (C) DTLink, LLC.
7// http://www.dtlink.com and http://www.formvista.com
8// -----------------------------------------------------------------
9// This code is distributed under the the sames terms as Xinha
10// itself. (HTMLArea license based on the BSD license)
11//
12// Please read license.txt in this package for details.
13//
14// All software distributed under the Licenses is provided strictly on
15// an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR
16// IMPLIED, AND DTLINK LLC HEREBY DISCLAIMS ALL SUCH
17// WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF
18// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT,
19// OR NON-INFRINGEMENT.
20// ------------------------------------------------------------------
21// NOSTRIP]
22
23// attempt to prevent this script from being run through the webserver.
24
25if ( @$_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
56function _ddt( $file, $line, $msg )
57{
58
59// uncomment this line to turn on debugging messages.
60
61print( 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
74function 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
81if ( !file_exists( $path ))
82        return  false ;
83
84// if path is not a directory, and it matches the regex, apply the function.
85
86if ( !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        }
103else
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
148function procDDT( $path )
149{
150
151_ddt( __FILE__, __LINE__, "procDDT(): top with path '$path'" );
152
153if ( !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
162if (( $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
173foreach ( $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
205if (( $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
214foreach ( $content_array as $index => $line )
215        fputs( $fh, $line );
216
217fclose( $fh );
218
219_ddt( __FILE__, __LINE__, "procDDT(): bottom" );
220
221return true;
222
223}       // end of procDDT()
224
225// ------------------------------------------------------------------
226
227// check usage.
228
229if ( $argc != 2 )
230        {
231        die( "Usage: ddtpreproc.php path_to_xinha_root\n" );
232        }
233
234$dir_path = $argv[1];
235
236if ( ! 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
244rapply( $dir_path, "/.*js$/", "procDDT" );
245
246// END
247
248?>
Note: See TracBrowser for help on using the repository browser.