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