jsparse.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. /**
  3. * 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. * @file
  21. * @ingroup Maintenance
  22. */
  23. require_once __DIR__ . '/Maintenance.php';
  24. /**
  25. * Maintenance script to do test JavaScript validity parses using jsmin+'s parser
  26. *
  27. * @ingroup Maintenance
  28. */
  29. class JSParseHelper extends Maintenance {
  30. public $errs = 0;
  31. public function __construct() {
  32. parent::__construct();
  33. $this->addDescription( 'Runs parsing/syntax checks on JavaScript files' );
  34. $this->addArg( 'file(s)', 'JavaScript file to test', false );
  35. }
  36. public function execute() {
  37. if ( $this->hasArg() ) {
  38. $files = $this->mArgs;
  39. } else {
  40. $this->maybeHelp( true ); // @todo fixme this is a lame API :)
  41. exit( 1 ); // it should exit from the above first...
  42. }
  43. $parser = new JSParser();
  44. foreach ( $files as $filename ) {
  45. MediaWiki\suppressWarnings();
  46. $js = file_get_contents( $filename );
  47. MediaWiki\restoreWarnings();
  48. if ( $js === false ) {
  49. $this->output( "$filename ERROR: could not read file\n" );
  50. $this->errs++;
  51. continue;
  52. }
  53. try {
  54. $parser->parse( $js, $filename, 1 );
  55. } catch ( Exception $e ) {
  56. $this->errs++;
  57. $this->output( "$filename ERROR: " . $e->getMessage() . "\n" );
  58. continue;
  59. }
  60. $this->output( "$filename OK\n" );
  61. }
  62. if ( $this->errs > 0 ) {
  63. exit( 1 );
  64. }
  65. }
  66. }
  67. $maintClass = "JSParseHelper";
  68. require_once RUN_MAINTENANCE_IF_MAIN;