Bit more refactoring
[lhc/web/wiklou.git] / maintenance / jsparse.php
1 <?php
2 /**
3 * Maintenance script to do test JavaScript validity parses using jsmin+'s parser
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @ingroup Maintenance
21 */
22
23 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
24
25 class JSParseHelper extends Maintenance {
26 var $errs = 0;
27
28 public function __construct() {
29 parent::__construct();
30 $this->mDescription = "Runs parsing/syntax checks on JavaScript files";
31 $this->addArg( 'file(s)', 'JavaScript file to test', false );
32 }
33
34 public function execute() {
35 $iterations = $this->getOption( 'i', 100 );
36 if ( $this->hasArg() ) {
37 $files = $this->mArgs;
38 } else {
39 $this->maybeHelp( true ); // @fixme this is a lame API :)
40 exit( 1 ); // it should exit from the above first...
41 }
42
43 $parser = new JSParser();
44 foreach ( $files as $filename ) {
45 wfSuppressWarnings();
46 $js = file_get_contents( $filename );
47 wfRestoreWarnings();
48 if ($js === false) {
49 $this->output( "$filename ERROR: could not read file\n" );
50 $this->errs++;
51 continue;
52 }
53
54 try {
55 $parser->parse( $js, $filename, 1 );
56 } catch (Exception $e) {
57 $this->errs++;
58 $this->output( "$filename ERROR: " . $e->getMessage() . "\n" );
59 continue;
60 }
61
62 $this->output( "$filename OK\n" );
63 }
64
65 if ($this->errs > 0) {
66 exit(1);
67 }
68 }
69 }
70
71 $maintClass = "JSParseHelper";
72 require_once( RUN_MAINTENANCE_IF_MAIN );