[ Index ]

PHP Cross Reference of MediaWiki-1.24.0

title

Body

[close]

/maintenance/ -> mergeMessageFileList.php (source)

   1  <?php
   2  /**
   3   * Merge $wgExtensionMessagesFiles from various extensions to produce a
   4   * single array containing all message files.
   5   *
   6   * This program is free software; you can redistribute it and/or modify
   7   * it under the terms of the GNU General Public License as published by
   8   * the Free Software Foundation; either version 2 of the License, or
   9   * (at your option) any later version.
  10   *
  11   * This program is distributed in the hope that it will be useful,
  12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14   * GNU General Public License for more details.
  15   *
  16   * You should have received a copy of the GNU General Public License along
  17   * with this program; if not, write to the Free Software Foundation, Inc.,
  18   * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19   * http://www.gnu.org/copyleft/gpl.html
  20   *
  21   * @file
  22   * @ingroup Maintenance
  23   */
  24  
  25  # Start from scratch
  26  define( 'MW_NO_EXTENSION_MESSAGES', 1 );
  27  
  28  require_once  __DIR__ . '/Maintenance.php';
  29  $maintClass = 'MergeMessageFileList';
  30  $mmfl = false;
  31  
  32  /**
  33   * Maintenance script that merges $wgExtensionMessagesFiles from various
  34   * extensions to produce a single array containing all message files.
  35   *
  36   * @ingroup Maintenance
  37   */
  38  class MergeMessageFileList extends Maintenance {
  39      /**
  40       * @var bool
  41       */
  42      protected $hasError;
  43  
  44  	function __construct() {
  45          parent::__construct();
  46          $this->addOption(
  47              'list-file',
  48              'A file containing a list of extension setup files, one per line.',
  49              false,
  50              true
  51          );
  52          $this->addOption( 'extensions-dir', 'Path where extensions can be found.', false, true );
  53          $this->addOption( 'output', 'Send output to this file (omit for stdout)', false, true );
  54          $this->mDescription = 'Merge $wgExtensionMessagesFiles and $wgMessagesDirs from ' .
  55              ' various extensions to produce a single file listing all message files and dirs.';
  56      }
  57  
  58  	public function execute() {
  59          // @codingStandardsIgnoreStart Ignore error: Global variable "$mmfl" is lacking 'wg' prefix
  60          global $mmfl;
  61          // @codingStandardsIgnoreEnd
  62          global $wgExtensionEntryPointListFiles;
  63  
  64          if ( !count( $wgExtensionEntryPointListFiles )
  65              && !$this->hasOption( 'list-file' )
  66              && !$this->hasOption( 'extensions-dir' )
  67          ) {
  68              $this->error( "Either --list-file or --extensions-dir must be provided if " .
  69                  "\$wgExtensionEntryPointListFiles is not set", 1 );
  70          }
  71  
  72          $mmfl = array( 'setupFiles' => array() );
  73  
  74          # Add setup files contained in file passed to --list-file
  75          if ( $this->hasOption( 'list-file' ) ) {
  76              $extensionPaths = $this->readFile( $this->getOption( 'list-file' ) );
  77              $mmfl['setupFiles'] = array_merge( $mmfl['setupFiles'], $extensionPaths );
  78          }
  79  
  80          # Now find out files in a directory
  81          if ( $this->hasOption( 'extensions-dir' ) ) {
  82              $extdir = $this->getOption( 'extensions-dir' );
  83              $entries = scandir( $extdir );
  84              foreach ( $entries as $extname ) {
  85                  if ( $extname == '.' || $extname == '..' || !is_dir( "$extdir/$extname" ) ) {
  86                      continue;
  87                  }
  88                  $extfile = "{$extdir}/{$extname}/{$extname}.php";
  89                  if ( file_exists( $extfile ) ) {
  90                      $mmfl['setupFiles'][] = $extfile;
  91                  } else {
  92                      $this->hasError = true;
  93                      $this->error( "Extension {$extname} in {$extdir} lacks expected {$extname}.php" );
  94                  }
  95              }
  96          }
  97  
  98          # Add setup files defined via configuration
  99          foreach ( $wgExtensionEntryPointListFiles as $points ) {
 100              $extensionPaths = $this->readFile( $points );
 101              $mmfl['setupFiles'] = array_merge( $mmfl['setupFiles'], $extensionPaths );
 102          }
 103  
 104          if ( $this->hasError ) {
 105              $this->error( "Some files are missing (see above). Giving up.", 1 );
 106          }
 107  
 108          if ( $this->hasOption( 'output' ) ) {
 109              $mmfl['output'] = $this->getOption( 'output' );
 110          }
 111          if ( $this->hasOption( 'quiet' ) ) {
 112              $mmfl['quiet'] = true;
 113          }
 114      }
 115  
 116      /**
 117       * @param string $fileName
 118       * @return array List of absolute extension paths
 119       */
 120  	private function readFile( $fileName ) {
 121          global $IP;
 122  
 123          $files = array();
 124          $fileLines = file( $fileName );
 125          if ( $fileLines === false ) {
 126              $this->hasError = true;
 127              $this->error( "Unable to open list file $fileName." );
 128  
 129              return $files;
 130          }
 131          # Strip comments, discard empty lines, and trim leading and trailing
 132          # whitespace. Comments start with '#' and extend to the end of the line.
 133          foreach ( $fileLines as $extension ) {
 134              $extension = trim( preg_replace( '/#.*/', '', $extension ) );
 135              if ( $extension !== '' ) {
 136                  # Paths may use the string $IP to be substituted by the actual value
 137                  $extension = str_replace( '$IP', $IP, $extension );
 138                  if ( file_exists( $extension ) ) {
 139                      $files[] = $extension;
 140                  } else {
 141                      $this->hasError = true;
 142                      $this->error( "Extension {$extension} doesn't exist" );
 143                  }
 144              }
 145          }
 146  
 147          return $files;
 148      }
 149  }
 150  
 151  require_once RUN_MAINTENANCE_IF_MAIN;
 152  
 153  foreach ( $mmfl['setupFiles'] as $fileName ) {
 154      if ( strval( $fileName ) === '' ) {
 155          continue;
 156      }
 157      if ( empty( $mmfl['quiet'] ) ) {
 158          fwrite( STDERR, "Loading data from $fileName\n" );
 159      }
 160      // Include the extension to update $wgExtensionMessagesFiles
 161      if ( !( include_once $fileName ) ) {
 162          fwrite( STDERR, "Unable to read $fileName\n" );
 163          exit( 1 );
 164      }
 165  }
 166  fwrite( STDERR, "\n" );
 167  $s =
 168      "<" . "?php\n" .
 169      "## This file is generated by mergeMessageFileList.php. Do not edit it directly.\n\n" .
 170      "if ( defined( 'MW_NO_EXTENSION_MESSAGES' ) ) return;\n\n" .
 171      '$wgExtensionMessagesFiles = ' . var_export( $wgExtensionMessagesFiles, true ) . ";\n\n" .
 172      '$wgMessagesDirs = ' . var_export( $wgMessagesDirs, true ) . ";\n\n";
 173  
 174  $dirs = array(
 175      $IP,
 176      dirname( __DIR__ ),
 177      realpath( $IP )
 178  );
 179  
 180  foreach ( $dirs as $dir ) {
 181      $s = preg_replace( "/'" . preg_quote( $dir, '/' ) . "([^']*)'/", '"$IP\1"', $s );
 182  }
 183  
 184  if ( isset( $mmfl['output'] ) ) {
 185      file_put_contents( $mmfl['output'], $s );
 186  } else {
 187      echo $s;
 188  }


Generated: Fri Nov 28 14:03:12 2014 Cross-referenced by PHPXref 0.7.1