MediaWikiExtensions.php

Go to the documentation of this file.
00001 <?php
00014 class PremadeMediawikiExtensionGroups {
00015     protected $useConfigure = true;
00016     protected $idPrefix = 'ext-';
00017     protected $namespace = NS_MEDIAWIKI;
00018 
00023     protected $path;
00024 
00029     protected $definitionFile;
00030 
00039     public function __construct( $def, $path ) {
00040         $this->definitionFile = $def;
00041         $this->path = $path;
00042     }
00043 
00048     public function setUseConfigure( $value ) {
00049         $this->useConfigure = $value;
00050     }
00051 
00056     public function setGroupPrefix( $value ) {
00057         $this->idPrefix = $value;
00058     }
00059 
00064     public function setNamespace( $value ) {
00065         $this->namespace = $value;
00066     }
00067 
00069     static function foldId( $name ) {
00070         return preg_replace( '/\s+/', '', strtolower( $name ) );
00071     }
00072 
00074     public function register( array &$list, array &$deps ) {
00075         $groups = $this->parseFile();
00076         $groups = $this->processGroups( $groups );
00077         foreach ( $groups as $id => $g ) {
00078             $list[$id] = $this->createMessageGroup( $id, $g );
00079         }
00080 
00081         $deps[] = new FileDependency( $this->definitionFile );
00082 
00083         return true;
00084     }
00085 
00092     protected function createMessageGroup( $id, $info ) {
00093         $conf = array();
00094         $conf['BASIC']['class'] = 'MediaWikiExtensionMessageGroup';
00095         $conf['BASIC']['id'] = $id;
00096         $conf['BASIC']['namespace'] = $this->namespace;
00097         $conf['BASIC']['label'] = $info['name'];
00098 
00099         if ( isset( $info['desc'] ) ) {
00100             $conf['BASIC']['description'] = $info['desc'];
00101         } else {
00102             $conf['BASIC']['descriptionmsg'] = $info['descmsg'];
00103             $conf['BASIC']['extensionurl'] = $info['url'];
00104         }
00105 
00106         $conf['FILES']['class'] = 'MediaWikiExtensionFFS';
00107         $conf['FILES']['sourcePattern'] = $this->path . '/' . $info['file'];
00108         // Kind of hacky, export path will be wrong if %GROUPROOT% not used.
00109         $target = str_replace( '%GROUPROOT%/', '', $conf['FILES']['sourcePattern'] );
00110         $conf['FILES']['targetPattern'] = $target;
00111 
00112         // @todo Find a better way
00113         if ( isset( $info['aliasfile'] ) ) {
00114             $conf['FILES']['aliasFile'] = $info['aliasfile'];
00115         }
00116         if ( isset( $info['magicfile'] ) ) {
00117             $conf['FILES']['magicFile'] = $info['magicfile'];
00118         }
00119 
00120         if ( isset( $info['prefix'] ) ) {
00121             $conf['MANGLER']['class'] = 'StringMatcher';
00122             $conf['MANGLER']['prefix'] = $info['prefix'];
00123             $conf['MANGLER']['patterns'] = $info['mangle'];
00124 
00125             $mangler = new StringMatcher( $info['prefix'], $info['mangle'] );
00126             if ( isset( $info['ignored'] ) ) {
00127                 $info['ignored'] = $mangler->mangle( $info['ignored'] );
00128             }
00129             if ( isset( $info['optional'] ) ) {
00130                 $info['optional'] = $mangler->mangle( $info['optional'] );
00131             }
00132         }
00133 
00134         $conf['CHECKER']['class'] = 'MediaWikiMessageChecker';
00135         $conf['CHECKER']['checks'] = array(
00136             'pluralCheck',
00137             'pluralFormsCheck',
00138             'wikiParameterCheck',
00139             'wikiLinksCheck',
00140             'XhtmlCheck',
00141             'braceBalanceCheck',
00142             'pagenameMessagesCheck',
00143             'miscMWChecks',
00144         );
00145 
00146         $conf['INSERTABLES']['class'] = 'MediaWikiInsertablesSuggester';
00147 
00148         if ( isset( $info['optional'] ) ) {
00149             $conf['TAGS']['optional'] = $info['optional'];
00150         }
00151         if ( isset( $info['ignored'] ) ) {
00152             $conf['TAGS']['ignored'] = $info['ignored'];
00153         }
00154 
00155         return MessageGroupBase::factory( $conf );
00156     }
00157 
00158     protected function parseFile() {
00159         $defines = file_get_contents( $this->definitionFile );
00160         $linefeed = '(\r\n|\n)';
00161         $sections = array_map(
00162             'trim',
00163             preg_split( "/$linefeed{2,}/", $defines, -1, PREG_SPLIT_NO_EMPTY )
00164         );
00165         $groups = array();
00166 
00167         foreach ( $sections as $section ) {
00168             $lines = array_map( 'trim', preg_split( "/$linefeed/", $section ) );
00169             $newgroup = array();
00170 
00171             foreach ( $lines as $line ) {
00172                 if ( $line === '' || $line[0] === '#' ) {
00173                     continue;
00174                 }
00175 
00176                 if ( strpos( $line, '=' ) === false ) {
00177                     if ( empty( $newgroup['name'] ) ) {
00178                         $newgroup['name'] = $line;
00179                     } else {
00180                         throw new MWException( "Trying to define name twice: " . $line );
00181                     }
00182                 } else {
00183                     list( $key, $value ) = array_map( 'trim', explode( '=', $line, 2 ) );
00184                     switch ( $key ) {
00185                         case 'file':
00186                         case 'var':
00187                         case 'id':
00188                         case 'descmsg':
00189                         case 'desc':
00190                         case 'magicfile':
00191                         case 'aliasfile':
00192                             $newgroup[$key] = $value;
00193                             break;
00194                         case 'optional':
00195                         case 'ignored':
00196                             $values = array_map( 'trim', explode( ',', $value ) );
00197                             if ( !isset( $newgroup[$key] ) ) {
00198                                 $newgroup[$key] = array();
00199                             }
00200                             $newgroup[$key] = array_merge( $newgroup[$key], $values );
00201                             break;
00202                         case 'prefix':
00203                             list( $prefix, $messages ) = array_map(
00204                                 'trim',
00205                                 explode( '|', $value, 2 )
00206                             );
00207                             if ( isset( $newgroup['prefix'] ) && $newgroup['prefix'] !== $prefix ) {
00208                                 throw new MWException(
00209                                     "Only one prefix supported: {$newgroup['prefix']} !== $prefix"
00210                                 );
00211                             }
00212                             $newgroup['prefix'] = $prefix;
00213 
00214                             if ( !isset( $newgroup['mangle'] ) ) {
00215                                 $newgroup['mangle'] = array();
00216                             }
00217 
00218                             $messages = array_map( 'trim', explode( ',', $messages ) );
00219                             $newgroup['mangle'] = array_merge( $newgroup['mangle'], $messages );
00220                             break;
00221                         default:
00222                             throw new MWException( "Unknown key:" . $key );
00223                     }
00224                 }
00225             }
00226 
00227             if ( count( $newgroup ) ) {
00228                 if ( empty( $newgroup['name'] ) ) {
00229                     throw new MWException( "Name missing\n" . print_r( $newgroup, true ) );
00230                 }
00231                 $groups[] = $newgroup;
00232             }
00233         }
00234 
00235         return $groups;
00236     }
00237 
00238     protected function processGroups( $groups ) {
00239         $configureData = $this->loadConfigureExtensionData();
00240         $fixedGroups = array();
00241         foreach ( $groups as $g ) {
00242             if ( !is_array( $g ) ) {
00243                 $g = array( $g );
00244             }
00245 
00246             $name = $g['name'];
00247 
00248             if ( isset( $g['id'] ) ) {
00249                 $id = $g['id'];
00250             } else {
00251                 $id = $this->idPrefix . preg_replace( '/\s+/', '', strtolower( $name ) );
00252             }
00253 
00254             if ( isset( $g['file'] ) ) {
00255                 $file = $g['file'];
00256             } else {
00257                 $file = preg_replace( '/\s+/', '', "$name/$name.i18n.php" );
00258             }
00259 
00260             if ( isset( $g['descmsg'] ) ) {
00261                 $descmsg = $g['descmsg'];
00262             } else {
00263                 $descmsg = str_replace( $this->idPrefix, '', $id ) . '-desc';
00264             }
00265 
00266             $configureId = self::foldId( $name );
00267             if ( isset( $configureData[$configureId]['url'] ) ) {
00268                 $url = $configureData[$configureId]['url'];
00269             } else {
00270                 $url = false;
00271             }
00272 
00273             $newgroup = array(
00274                 'name' => $name,
00275                 'file' => $file,
00276                 'descmsg' => $descmsg,
00277                 'url' => $url,
00278             );
00279 
00280             $copyvars = array(
00281                 'ignored',
00282                 'optional',
00283                 'var',
00284                 'desc',
00285                 'prefix',
00286                 'mangle',
00287                 'magicfile',
00288                 'aliasfile'
00289             );
00290 
00291             foreach ( $copyvars as $var ) {
00292                 if ( isset( $g[$var] ) ) {
00293                     $newgroup[$var] = $g[$var];
00294                 }
00295             }
00296 
00297             $fixedGroups[$id] = $newgroup;
00298         }
00299 
00300         return $fixedGroups;
00301     }
00302 
00303     protected function loadConfigureExtensionData() {
00304         if ( !$this->useConfigure ) {
00305             return array();
00306         }
00307 
00308         global $wgAutoloadClasses, $IP, $wgTranslateExtensionDirectory;
00309 
00310         $postfix = 'Configure/load_txt_def/TxtDef.php';
00311         if ( file_exists( "$IP/extensions/$postfix" ) ) {
00312             $prefix = "$IP/extensions";
00313         } elseif ( file_exists( "$wgTranslateExtensionDirectory/$postfix" ) ) {
00314             $prefix = $wgTranslateExtensionDirectory;
00315         } else {
00316             $prefix = false;
00317         }
00318 
00319         if ( $prefix ) {
00320             $wgAutoloadClasses['TxtDef'] = "$prefix/$postfix";
00321             $tmp = TxtDef::loadFromFile( "$prefix/Configure/settings/Settings-ext.txt" );
00322 
00323             return array_combine(
00324                 array_map( array( __CLASS__, 'foldId' ), array_keys( $tmp ) ),
00325                 array_values( $tmp )
00326             );
00327         }
00328 
00329         return array();
00330     }
00331 }
Generated on Tue Oct 29 00:00:23 2013 for MediaWiki Translate Extension by  doxygen 1.6.3