3 * Script to update list of supported lexers.
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.
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.
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
21 * @author Ori Livneh <ori@wikimedia.org>
22 * @ingroup Maintenance
25 use Symfony\Component\Process\ProcessBuilder;
27 $IP = getenv( 'MW_INSTALL_PATH' ) ?: __DIR__ . '/../../..';
29 require_once "$IP/maintenance/Maintenance.php";
31 class UpdateLanguageList extends Maintenance {
32 public function __construct() {
33 parent::__construct();
34 $this->addDescription( 'Update list of lexers supported by SyntaxHighlight_GeSHi' );
37 public function execute() {
38 function lang_filter( $val ) {
39 return preg_match( '/^[a-zA-Z0-9\-_]+$/', $val );
42 $header = '// Generated by ' . basename( __FILE__ ) . "\n\n";
46 $builder = new ProcessBuilder();
47 $builder->setPrefix( SyntaxHighlight_GeSHi::getPygmentizePath() );
49 $process = $builder->add( '-L' )->add( 'lexer' )->getProcess();
52 if ( !$process->isSuccessful() ) {
53 throw new \RuntimeException( $process->getErrorOutput() );
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 );
63 $lexers = array_unique( $lexers );
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 );
70 file_put_contents( __DIR__ . '/../SyntaxHighlight.lexers.php', $code );
71 $this->output( "Updated language list written to SyntaxHighlight.lexers.php\n" );
75 $maintClass = 'UpdateLanguageList';
76 require_once RUN_MAINTENANCE_IF_MAIN;