MediaWiki  1.34.0
updateLexerList.php
Go to the documentation of this file.
1 <?php
27 
28 $IP = getenv( 'MW_INSTALL_PATH' ) ?: __DIR__ . '/../../..';
29 
30 require_once "$IP/maintenance/Maintenance.php";
31 
33  public function __construct() {
34  parent::__construct();
35 
36  $this->requireExtension( 'SyntaxHighlight' );
37  $this->addDescription( 'Update list of lexers supported by SyntaxHighlight_GeSHi' );
38  }
39 
40  public function execute() {
41  $header = 'Generated by ' . basename( __FILE__ );
42 
43  $lexers = [];
44 
45  $result = Shell::command(
46  SyntaxHighlight::getPygmentizePath(),
47  '-L', 'lexer'
48  )
49  ->restrict( Shell::RESTRICT_DEFAULT | Shell::NO_NETWORK )
50  ->execute();
51 
52  if ( $result->getExitCode() != 0 ) {
53  throw new \RuntimeException( $result->getStderr() );
54  }
55 
56  $output = $result->getStdout();
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  $data = [];
66  foreach ( $lexers as $lexer ) {
67  $data[$lexer] = true;
68  }
69 
70  $writer = new StaticArrayWriter();
71  $code = $writer->create( $data, $header );
72 
73  file_put_contents( __DIR__ . '/../SyntaxHighlight.lexers.php', $code );
74  $this->output( "Updated language list written to SyntaxHighlight.lexers.php\n" );
75  }
76 }
77 
78 $maintClass = UpdateLexerList::class;
79 require_once RUN_MAINTENANCE_IF_MAIN;
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:39
MediaWiki\Shell\Shell
Executes shell commands.
Definition: Shell.php:44
$IP
$IP
Definition: updateLexerList.php:28
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:348
$maintClass
$maintClass
Definition: updateLexerList.php:78
UpdateLexerList\execute
execute()
Do the actual work.
Definition: updateLexerList.php:40
UpdateLexerList\__construct
__construct()
Default constructor.
Definition: updateLexerList.php:33
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:82
UpdateLexerList
Definition: updateLexerList.php:32
Maintenance\requireExtension
requireExtension( $name)
Indicate that the specified extension must be loaded before the script can run.
Definition: Maintenance.php:638
$output
$output
Definition: SyntaxHighlight.php:335
$line
$line
Definition: cdb.php:59
$header
$header
Definition: updateCredits.php:41
Wikimedia\StaticArrayWriter
Format a static PHP array to be written to a file.
Definition: StaticArrayWriter.php:26
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:453