]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - extensions/SyntaxHighlight_GeSHi/maintenance/updateLexerList.php
MediaWiki 1.30.2-scripts
[autoinstalls/mediawiki.git] / extensions / SyntaxHighlight_GeSHi / maintenance / updateLexerList.php
1 <?php
2 /**
3  * Script to update list of supported lexers.
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  * @author Ori Livneh <ori@wikimedia.org>
22  * @ingroup Maintenance
23  */
24
25 use Symfony\Component\Process\ProcessBuilder;
26
27 $IP = getenv( 'MW_INSTALL_PATH' ) ?: __DIR__ . '/../../..';
28
29 require_once "$IP/maintenance/Maintenance.php";
30
31 class UpdateLanguageList extends Maintenance {
32         public function __construct() {
33                 parent::__construct();
34                 $this->addDescription( 'Update list of lexers supported by SyntaxHighlight_GeSHi' );
35         }
36
37         public function execute() {
38                 function lang_filter( $val ) {
39                         return preg_match( '/^[a-zA-Z0-9\-_]+$/', $val );
40                 }
41
42                 $header = '// Generated by ' . basename( __FILE__ ) . "\n\n";
43
44                 $lexers = [];
45
46                 $builder = new ProcessBuilder();
47                 $builder->setPrefix( SyntaxHighlight_GeSHi::getPygmentizePath() );
48
49                 $process = $builder->add( '-L' )->add( 'lexer' )->getProcess();
50                 $process->run();
51
52                 if ( !$process->isSuccessful() ) {
53                         throw new \RuntimeException( $process->getErrorOutput() );
54                 }
55
56                 $output = $process->getOutput();
57                 foreach ( explode( "\n", $output ) as $line ) {
58                         if ( substr( $line, 0, 1 ) === '*' ) {
59                                 $newLexers = explode( ', ', trim( $line, "* :\n" ) );
60                                 $lexers = array_merge( $lexers, $newLexers );
61                         }
62                 }
63                 $lexers = array_unique( $lexers );
64                 sort( $lexers );
65
66                 $code = "<?php\n" .  $header .  'return ' . var_export( $lexers, true ) . ";\n";
67                 $code = preg_replace( '/(\d+ \=\>| (?=\())/i', '', $code );
68                 $code = preg_replace( "/^ +/m", "\t", $code );
69
70                 file_put_contents( __DIR__ . '/../SyntaxHighlight.lexers.php', $code );
71                 $this->output( "Updated language list written to SyntaxHighlight.lexers.php\n" );
72         }
73 }
74
75 $maintClass = 'UpdateLanguageList';
76 require_once RUN_MAINTENANCE_IF_MAIN;