mwcore-export.php
Go to the documentation of this file.00001 <?php
00012
00013 if ( getenv( 'MW_INSTALL_PATH' ) !== false ) {
00014 $IP = getenv( 'MW_INSTALL_PATH' );
00015 } else {
00016 $dir = __DIR__;
00017 $IP = "$dir/../../..";
00018 }
00019 require_once "$IP/maintenance/Maintenance.php";
00020
00021 class MwCoreExport extends Maintenance {
00022 public function __construct() {
00023 parent::__construct();
00024 $this->mDescription = 'Core special features exporter.';
00025 $this->addOption(
00026 'target',
00027 'Target directory for exported files',
00028 true,
00029 true
00030 );
00031 $this->addOption(
00032 'lang',
00033 '(optional) Comma separated list of language codes. Default: *',
00034 false,
00035 true
00036 );
00037 $this->addOption(
00038 'type',
00039 'Export type: "namespace", "special" or "magic"',
00040 true,
00041 true
00042 );
00043 }
00044
00045 public function execute() {
00046 if ( !is_writable( $this->getOption( 'target' ) ) ) {
00047 $this->error( 'Target directory is not writable.', 1 );
00048 }
00049
00050 $langs = TranslateUtils::parseLanguageCodes( $this->getOption( 'lang' ) );
00051 $group = MessageGroups::getGroup( 'core' );
00052 $type = $this->getOption( 'type' );
00053
00054 foreach ( $langs as $l ) {
00055 $o = null;
00056
00057 switch ( $type ) {
00058 case 'special':
00059 $o = new SpecialPageAliasesCM( $l );
00060 break;
00061 case 'magic':
00062 $o = new MagicWordsCM( $l );
00063 break;
00064 case 'namespace':
00065 $o = new NamespaceCM( $l );
00066 break;
00067 default:
00068 $this->error( 'Invalid type: Must be one of special, magic, namespace.', 1 );
00069 }
00070
00071 $export = $o->export( 'core' );
00072 if ( $export === '' ) {
00073 continue;
00074 }
00075
00076 $matches = array();
00077 preg_match( '~^(\$[a-zA-Z]+)\s*=~m', $export, $matches );
00078
00079 if ( !isset( $matches[1] ) ) {
00080 continue;
00081 }
00082
00083 # remove useles comment
00084 $export = preg_replace( "~^# .*$\n~m", '', $export );
00085
00086 if ( strpos( $export, '#!!' ) !== false ) {
00087 $this->error( "There are warnings with $l." );
00088 }
00089
00090 $variable = preg_quote( $matches[1], '~' );
00091
00095 $file = $group->getMessageFileWithPath( $l );
00096
00097 if ( !file_exists( $file ) ) {
00098 $this->error( "File $file does not exist!" );
00099 continue;
00100 }
00101
00102 $data = file_get_contents( $file );
00103
00104 $export = trim( $export ) . "\n";
00105 $escExport = addcslashes( $export, '\\$' ); # Darn backreferences
00106
00107 $outFile = $this->getOption( 'target' ) . '/' . $group->getMessageFile( $l );
00108
00109 $count = 0;
00110 $data = preg_replace( "~$variable\s*=.*?\n\);\n~s", $escExport, $data, 1, $count );
00111 if ( $count ) {
00112 file_put_contents( $outFile, $data );
00113 } else {
00114 $this->error( "Adding new entry to $outFile, please double check location." );
00115 $pos = strpos( $data, "*/" );
00116 if ( $pos === false ) {
00117 $this->error( ". FAILED! Totally new file? No header?" );
00118 } else {
00119 $pos += 3;
00120 }
00121
00122 $data = substr( $data, 0, $pos ) . "\n" . $export . substr( $data, $pos );
00123
00124 file_put_contents( $outFile, $data );
00125 }
00126 }
00127 }
00128 }
00129
00130 $maintClass = 'MwCoreExport';
00131 require_once RUN_MAINTENANCE_IF_MAIN;