magic-export.php
Go to the documentation of this file.00001 <?php
00012 require dirname( dirname( dirname( __DIR__ ) ) ) . '/maintenance/Maintenance.php';
00013
00017 class MagicExport extends Maintenance {
00018 protected $type;
00019 protected $target;
00020
00021 protected $handles = array();
00022 protected $messagesOld = array();
00023
00024 public function __construct() {
00025 parent::__construct();
00026 $this->mDescription = 'Export of aliases and magic words for MediaWiki extensions.';
00027 $this->addOption(
00028 'target',
00029 'Target directory for exported files',
00030 true,
00031 true
00032 );
00033 $this->addOption(
00034 'type',
00035 'magic or special',
00036 true,
00037 true
00038 );
00039 }
00040
00041 public function execute() {
00042 $this->target = $this->getOption( 'target' );
00043 $this->type = $this->getOption( 'type' );
00044
00045 switch ( $this->type ) {
00046 case 'special':
00047 case 'magic':
00048 break;
00049 default:
00050 $this->error( 'Invalid type.', 1 );
00051 }
00052
00053 $this->openHandles();
00054 $this->writeHeaders();
00055 $this->writeFiles();
00056 $this->writeFooters();
00057 $this->closeHandles();
00058 }
00059
00068 protected function openHandles() {
00069 $this->output( "Opening file handles and loading current data...\n" );
00070
00071 $groups = MessageGroups::singleton()->getGroups();
00072 $filename = null;
00073 foreach ( $groups as $group ) {
00074 $filename = null;
00075 if ( $group instanceof MediaWikiExtensionMessageGroup ) {
00076 $conf = $group->getConfiguration();
00077 switch ( $this->type ) {
00078 case 'special':
00079 if ( isset( $conf['FILES']['aliasFile'] ) ) {
00080 $filename = $conf['FILES']['aliasFile'];
00081 }
00082 break;
00083 case 'magic':
00084 if ( isset( $conf['FILES']['magicFile'] ) ) {
00085 $filename = $conf['FILES']['magicFile'];
00086 }
00087 break;
00088 }
00089 }
00090
00091 if ( $filename === null ) {
00092 continue;
00093 }
00094
00095 global $wgTranslateExtensionDirectory;
00096 $inFile = "$wgTranslateExtensionDirectory/$filename";
00097
00098 if ( !file_exists( $inFile ) ) {
00099 continue;
00100 }
00101
00102 include $inFile;
00103 switch ( $this->type ) {
00104 case 'special':
00105 if ( isset( $aliases ) ) {
00106 $this->messagesOld[$group->getId()] = $aliases;
00107 unset( $aliases );
00108 } elseif ( isset( $specialPageAliases ) ) {
00109 $this->messagesOld[$group->getId()] = $specialPageAliases;
00110 unset( $specialPageAliases );
00111 } else {
00112 $this->error( "File '$inFile' does not contain an aliases array.", 1 );
00113 }
00114 break;
00115 case 'magic':
00116 if ( !isset( $magicWords ) ) {
00117 $this->error( "File '$inFile' does not contain a magic words array.", 1 );
00118 }
00119 $this->messagesOld[$group->getId()] = $magicWords;
00120 unset( $magicWords );
00121 break;
00122 }
00123
00124 $outFile = $this->target . '/' . $filename;
00125 wfMkdirParents( dirname( $outFile ), null, __METHOD__ );
00126 $this->handles[$group->getId()] = fopen( $outFile, 'w' );
00127 fwrite( $this->handles[$group->getId()], $this->readHeader( $inFile ) );
00128
00129 $this->output( "\t{$group->getId()}\n" );
00130 }
00131 }
00132
00133 protected function readHeader( $file ) {
00134 $data = file_get_contents( $file );
00135
00136
00137 $end = strpos( $data, '*/' ) + 2;
00138
00139 if ( $end === false ) {
00140 return "<?php\n";
00141 }
00142
00143
00144 return substr( $data, 0, $end );
00145 }
00146
00150 protected function writeHeaders() {
00151 foreach ( $this->handles as $handle ) {
00152 switch ( $this->type ) {
00153 case 'special':
00154 fwrite( $handle, <<<PHP
00155
00156
00157
00158 \$specialPageAliases = array();
00159 PHP
00160 );
00161 break;
00162 case 'magic':
00163 fwrite( $handle, <<<PHP
00164
00165
00166 \$magicWords = array();
00167 PHP
00168 );
00169 break;
00170 }
00171 }
00172 }
00173
00179 protected function writeFiles() {
00180 $langs = TranslateUtils::parseLanguageCodes( '*' );
00181 unset( $langs[array_search( 'en', $langs )] );
00182 $langs = array_merge( array( 'en' ), $langs );
00183 foreach ( $langs as $l ) {
00184
00185 switch ( $this->type ) {
00186 case 'special':
00187 $title = Title::makeTitleSafe( NS_MEDIAWIKI, 'Sp-translate-data-SpecialPageAliases/' . $l );
00188 break;
00189 case 'magic':
00190 $title = Title::makeTitleSafe( NS_MEDIAWIKI, 'Sp-translate-data-MagicWords/' . $l );
00191 break;
00192 default:
00193 exit( 1 );
00194 }
00195
00196
00197 if ( !$title || !$title->exists() ) {
00198 $this->output( "Skiping $l...\n" );
00199
00200 $messagesNew = array();
00201 } else {
00202 $this->output( "Processing $l...\n" );
00203
00204 $article = new Article( $title );
00205 $data = $article->getContent();
00206
00207
00208 $segments = explode( "\n", $data );
00209 array_shift( $segments );
00210 array_shift( $segments );
00211 unset( $segments[count( $segments ) - 1] );
00212 unset( $segments[count( $segments ) - 1] );
00213 $messagesNew = array();
00214 foreach ( $segments as $segment ) {
00215 $parts = explode( ' = ', $segment );
00216 $key = array_shift( $parts );
00217 $translations = explode( ', ', implode( $parts ) );
00218 $messagesNew[$key] = $translations;
00219 }
00220 }
00221
00222
00223 $namesEn = LanguageNames::getNames( 'en' );
00224 $namesNative = Language::getLanguageNames();
00225
00226 foreach ( $this->handles as $group => $handle ) {
00227
00228 $messagesOut = array();
00229 foreach ( $this->messagesOld[$group]['en'] as $key => $message ) {
00230 if ( array_key_exists( $key, $messagesNew ) ) {
00231 $messagesOut[$key] = $messagesNew[$key];
00232 } elseif ( isset( $this->messagesOld[$group][$l][$key] ) ) {
00233 $messagesOut[$key] = $this->messagesOld[$group][$l][$key];
00234 }
00235 }
00236
00237
00238 if ( count( $messagesOut ) > 0 ) {
00239 $out = '';
00240 switch ( $this->type ) {
00241 case 'special':
00242 $out .= "\n\n/** {$namesEn[$l]} ({$namesNative[$l]}) " .
00243 "*/\n\$specialPageAliases['{$l}'] = array(\n";
00244 break;
00245 case 'magic':
00246 $out .= "\n\n/** {$namesEn[$l]} ({$namesNative[$l]}) *" .
00247 "/\n\$magicWords['{$l}'] = array(\n";
00248 break;
00249 }
00250 foreach ( $messagesOut as $key => $translations ) {
00251 foreach ( $translations as $id => $translation ) {
00252 $translations[$id] = addslashes( $translation );
00253 if ( $this->type === 'magic' && $translation == '0' ) {
00254 unset( $translations[$id] );
00255 }
00256 }
00257 $translations = implode( "', '", $translations );
00258 switch ( $this->type ) {
00259 case 'special':
00260 $out .= "\t'$key' => array( '$translations' ),\n";
00261 break;
00262 case 'magic':
00263 if ( $this->messagesOld[$group]['en'][$key][0] === 0 ) {
00264 $out .= "\t'$key' => array( 0, '$translations' ),\n";
00265 } else {
00266 $out .= "\t'$key' => array( '$translations' ),\n";
00267 }
00268 break;
00269 }
00270 }
00271 $out .= ");";
00272 fwrite( $handle, $out );
00273 }
00274 }
00275 }
00276 }
00277
00281 protected function writeFooters() {
00282 $this->output( "Writing file footers...\n" );
00283 }
00284
00288 protected function closeHandles() {
00289 $this->output( "Closing file handles...\n" );
00290 foreach ( $this->handles as $handle ) {
00291 fclose( $handle );
00292 }
00293 }
00294 }
00295
00296 $maintClass = "MagicExport";
00297 require_once DO_MAINTENANCE;