MediaWiki  REL1_24
mergeMessageFileList.php
Go to the documentation of this file.
00001 <?php
00025 # Start from scratch
00026 define( 'MW_NO_EXTENSION_MESSAGES', 1 );
00027 
00028 require_once __DIR__ . '/Maintenance.php';
00029 $maintClass = 'MergeMessageFileList';
00030 $mmfl = false;
00031 
00038 class MergeMessageFileList extends Maintenance {
00042     protected $hasError;
00043 
00044     function __construct() {
00045         parent::__construct();
00046         $this->addOption(
00047             'list-file',
00048             'A file containing a list of extension setup files, one per line.',
00049             false,
00050             true
00051         );
00052         $this->addOption( 'extensions-dir', 'Path where extensions can be found.', false, true );
00053         $this->addOption( 'output', 'Send output to this file (omit for stdout)', false, true );
00054         $this->mDescription = 'Merge $wgExtensionMessagesFiles and $wgMessagesDirs from ' .
00055             ' various extensions to produce a single file listing all message files and dirs.';
00056     }
00057 
00058     public function execute() {
00059         // @codingStandardsIgnoreStart Ignore error: Global variable "$mmfl" is lacking 'wg' prefix
00060         global $mmfl;
00061         // @codingStandardsIgnoreEnd
00062         global $wgExtensionEntryPointListFiles;
00063 
00064         if ( !count( $wgExtensionEntryPointListFiles )
00065             && !$this->hasOption( 'list-file' )
00066             && !$this->hasOption( 'extensions-dir' )
00067         ) {
00068             $this->error( "Either --list-file or --extensions-dir must be provided if " .
00069                 "\$wgExtensionEntryPointListFiles is not set", 1 );
00070         }
00071 
00072         $mmfl = array( 'setupFiles' => array() );
00073 
00074         # Add setup files contained in file passed to --list-file
00075         if ( $this->hasOption( 'list-file' ) ) {
00076             $extensionPaths = $this->readFile( $this->getOption( 'list-file' ) );
00077             $mmfl['setupFiles'] = array_merge( $mmfl['setupFiles'], $extensionPaths );
00078         }
00079 
00080         # Now find out files in a directory
00081         if ( $this->hasOption( 'extensions-dir' ) ) {
00082             $extdir = $this->getOption( 'extensions-dir' );
00083             $entries = scandir( $extdir );
00084             foreach ( $entries as $extname ) {
00085                 if ( $extname == '.' || $extname == '..' || !is_dir( "$extdir/$extname" ) ) {
00086                     continue;
00087                 }
00088                 $extfile = "{$extdir}/{$extname}/{$extname}.php";
00089                 if ( file_exists( $extfile ) ) {
00090                     $mmfl['setupFiles'][] = $extfile;
00091                 } else {
00092                     $this->hasError = true;
00093                     $this->error( "Extension {$extname} in {$extdir} lacks expected {$extname}.php" );
00094                 }
00095             }
00096         }
00097 
00098         # Add setup files defined via configuration
00099         foreach ( $wgExtensionEntryPointListFiles as $points ) {
00100             $extensionPaths = $this->readFile( $points );
00101             $mmfl['setupFiles'] = array_merge( $mmfl['setupFiles'], $extensionPaths );
00102         }
00103 
00104         if ( $this->hasError ) {
00105             $this->error( "Some files are missing (see above). Giving up.", 1 );
00106         }
00107 
00108         if ( $this->hasOption( 'output' ) ) {
00109             $mmfl['output'] = $this->getOption( 'output' );
00110         }
00111         if ( $this->hasOption( 'quiet' ) ) {
00112             $mmfl['quiet'] = true;
00113         }
00114     }
00115 
00120     private function readFile( $fileName ) {
00121         global $IP;
00122 
00123         $files = array();
00124         $fileLines = file( $fileName );
00125         if ( $fileLines === false ) {
00126             $this->hasError = true;
00127             $this->error( "Unable to open list file $fileName." );
00128 
00129             return $files;
00130         }
00131         # Strip comments, discard empty lines, and trim leading and trailing
00132         # whitespace. Comments start with '#' and extend to the end of the line.
00133         foreach ( $fileLines as $extension ) {
00134             $extension = trim( preg_replace( '/#.*/', '', $extension ) );
00135             if ( $extension !== '' ) {
00136                 # Paths may use the string $IP to be substituted by the actual value
00137                 $extension = str_replace( '$IP', $IP, $extension );
00138                 if ( file_exists( $extension ) ) {
00139                     $files[] = $extension;
00140                 } else {
00141                     $this->hasError = true;
00142                     $this->error( "Extension {$extension} doesn't exist" );
00143                 }
00144             }
00145         }
00146 
00147         return $files;
00148     }
00149 }
00150 
00151 require_once RUN_MAINTENANCE_IF_MAIN;
00152 
00153 foreach ( $mmfl['setupFiles'] as $fileName ) {
00154     if ( strval( $fileName ) === '' ) {
00155         continue;
00156     }
00157     if ( empty( $mmfl['quiet'] ) ) {
00158         fwrite( STDERR, "Loading data from $fileName\n" );
00159     }
00160     // Include the extension to update $wgExtensionMessagesFiles
00161     if ( !( include_once $fileName ) ) {
00162         fwrite( STDERR, "Unable to read $fileName\n" );
00163         exit( 1 );
00164     }
00165 }
00166 fwrite( STDERR, "\n" );
00167 $s =
00168     "<" . "?php\n" .
00169     "## This file is generated by mergeMessageFileList.php. Do not edit it directly.\n\n" .
00170     "if ( defined( 'MW_NO_EXTENSION_MESSAGES' ) ) return;\n\n" .
00171     '$wgExtensionMessagesFiles = ' . var_export( $wgExtensionMessagesFiles, true ) . ";\n\n" .
00172     '$wgMessagesDirs = ' . var_export( $wgMessagesDirs, true ) . ";\n\n";
00173 
00174 $dirs = array(
00175     $IP,
00176     dirname( __DIR__ ),
00177     realpath( $IP )
00178 );
00179 
00180 foreach ( $dirs as $dir ) {
00181     $s = preg_replace( "/'" . preg_quote( $dir, '/' ) . "([^']*)'/", '"$IP\1"', $s );
00182 }
00183 
00184 if ( isset( $mmfl['output'] ) ) {
00185     file_put_contents( $mmfl['output'], $s );
00186 } else {
00187     echo $s;
00188 }