MediaWiki REL1_41
jsparse.php
Go to the documentation of this file.
1<?php
22require_once __DIR__ . '/Maintenance.php';
23
33 public $errs = 0;
34
35 public function __construct() {
36 parent::__construct();
37 $this->addDescription( 'Validate syntax of JavaScript files' );
38 $this->addArg( 'file(s)', 'JavaScript files or "-" to read stdin', true );
39 }
40
41 public function execute() {
42 $files = $this->getArgs();
43
44 $parser = new JSParser();
45 foreach ( $files as $filename ) {
46 $js = $filename === '-'
47 ? stream_get_contents( STDIN )
48 // phpcs:ignore Generic.PHP.NoSilencedErrors
49 : @file_get_contents( $filename );
50 if ( $js === false ) {
51 $this->output( "$filename ERROR: could not read file\n" );
52 $this->errs++;
53 continue;
54 }
55
56 try {
57 // phpcs:ignore Generic.PHP.NoSilencedErrors
58 @$parser->parse( $js, $filename, 1 );
59 } catch ( Exception $e ) {
60 $this->errs++;
61 $this->output( "$filename ERROR: " . $e->getMessage() . "\n" );
62 continue;
63 }
64
65 $this->output( "$filename OK\n" );
66 }
67
68 if ( $this->errs > 0 ) {
69 $this->fatalError( 'Failed.' );
70 }
71 }
72}
73
74$maintClass = JSParseHelper::class;
75require_once RUN_MAINTENANCE_IF_MAIN;
Ad-hoc run ResourceLoader validation for user-supplied JavaScript.
Definition jsparse.php:32
execute()
Do the actual work.
Definition jsparse.php:41
__construct()
Default constructor.
Definition jsparse.php:35
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
addArg( $arg, $description, $required=true, $multi=false)
Add some args that are needed.
getArgs( $offset=0)
Get arguments.
output( $out, $channel=null)
Throw some output to the user.
addDescription( $text)
Set the description text.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
$maintClass
Definition jsparse.php:74