r85123 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r85122‎ | r85123 | r85124 >
Date:14:35, 1 April 2011
Author:nikerabbit
Status:ok
Tags:
Comment:
Split init() into smaller methods
Modified paths:
  • /trunk/extensions/Translate/groups/MediaWikiExtensions.php (modified) (history)
  • /trunk/extensions/Translate/groups/Toolserver/ToolserverTextdomains.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Translate/groups/Toolserver/ToolserverTextdomains.php
@@ -1,6 +1,6 @@
22 <?php
33 /**
4 - * Class for Toolserver Intuition for TranslateWiki.net
 4+ * Class for Toolserver Intuition for Translatewiki.net
55 *
66 * @file
77 * @author Niklas Laxström
Index: trunk/extensions/Translate/groups/MediaWikiExtensions.php
@@ -28,34 +28,63 @@
2929 /// Initialisation function
3030 public function init() {
3131 if ( $this->groups !== null ) return;
 32+ $groups = $this->parseFile();
 33+ $this->groups = $this->processGroups( $groups );
 34+ }
3235
33 - global $wgAutoloadClasses, $IP, $wgTranslateExtensionDirectory;
 36+ /// Makes an group id from extension name
 37+ static function foldId( $name ) {
 38+ return preg_replace( '/\s+/', '', strtolower( $name ) );
 39+ }
3440
35 - $postfix = 'Configure/load_txt_def/TxtDef.php';
36 - if ( file_exists( "$IP/extensions/$postfix" ) ) {
37 - $prefix = "$IP/extensions";
38 - } elseif( file_exists( "$wgTranslateExtensionDirectory/$postfix" ) ) {
39 - $prefix = $wgTranslateExtensionDirectory;
40 - } else {
41 - $prefix = false;
 41+ /// Registers all extensions
 42+ public function addAll() {
 43+ global $wgTranslateAC, $wgTranslateEC;
 44+ $this->init();
 45+
 46+ if ( !count( $this->groups ) ) return;
 47+
 48+ foreach ( $this->groups as $id => $g ) {
 49+ $wgTranslateAC[$id] = array( $this, 'factory' );
 50+ $wgTranslateEC[] = $id;
4251 }
 52+ }
4353
44 - if ( $this->useConfigure && $prefix ) {
45 - $wgAutoloadClasses['TxtDef'] = "$prefix/$postfix";
46 - $tmp = TxtDef::loadFromFile( "$prefix/Configure/settings/Settings-ext.txt" );
47 - $configureData = array_combine( array_map( array( __CLASS__, 'foldId' ), array_keys( $tmp ) ), array_values( $tmp ) );
 54+ public function factory( $id ) {
 55+ $info = $this->groups[$id];
 56+ $group = ExtensionMessageGroup::factory( $info['name'], $id );
 57+ $group->setMessageFile( $info['file'] );
 58+ $group->setPath( $this->path );
 59+ $group->namespaces = $this->namespaces;
 60+
 61+ if ( isset( $info['prefix'] ) ) {
 62+ $mangler = new StringMatcher( $info['prefix'], $info['mangle'] );
 63+ $group->setMangler( $mangler );
 64+ $info['ignored'] = $mangler->mangle( $info['ignored'] );
 65+ $info['optional'] = $mangler->mangle( $info['optional'] );
 66+ }
 67+
 68+ if ( !empty( $info['var'] ) ) $group->setVariableName( $info['var'] );
 69+ if ( !empty( $info['optional'] ) ) $group->setOptional( $info['optional'] );
 70+ if ( !empty( $info['ignored'] ) ) $group->setIgnored( $info['ignored'] );
 71+ if ( isset( $info['desc'] ) ) {
 72+ $group->setDescription( $info['desc'] );
4873 } else {
49 - $configureData = array();
 74+ $group->setDescriptionMsg( $info['descmsg'], $info['url'] );
5075 }
5176
 77+ if ( isset( $info['aliasfile'] ) ) $group->setAliasFile( $info['aliasfile'] );
 78+ if ( isset( $info['magicfile'] ) ) $group->setMagicFile( $info['magicfile'] );
 79+
 80+ return $group;
 81+ }
 82+
 83+ protected function parseFile() {
5284 $defines = file_get_contents( $this->definitionFile );
53 -
5485 $linefeed = '(\r\n|\n)';
55 -
5686 $sections = array_map( 'trim', preg_split( "/$linefeed{2,}/", $defines, - 1, PREG_SPLIT_NO_EMPTY ) );
 87+ $groups = array();
5788
58 - $groups = $fixedGroups = array();
59 -
6089 foreach ( $sections as $section ) {
6190 $lines = array_map( 'trim', preg_split( "/$linefeed/", $section ) );
6291 $newgroup = array();
@@ -115,7 +144,12 @@
116145 }
117146 }
118147
 148+ return $groups;
 149+ }
119150
 151+ protected function processGroups( $groups ) {
 152+ $configureData = $this->loadConfigureExtensionData();
 153+ $fixedGroups = array();
120154 foreach ( $groups as $g ) {
121155 if ( !is_array( $g ) ) {
122156 $g = array( $g );
@@ -164,54 +198,33 @@
165199
166200 $fixedGroups[$id] = $newgroup;
167201 }
168 -
169 - $this->groups = $fixedGroups;
 202+ return $fixedGroups;
170203 }
171204
172 - /// Makes an group id from extension name
173 - static function foldId( $name ) {
174 - return preg_replace( '/\s+/', '', strtolower( $name ) );
175 - }
176 -
177 - /// Registers all extensions
178 - public function addAll() {
179 - global $wgTranslateAC, $wgTranslateEC;
180 - $this->init();
181 -
182 - if ( !count( $this->groups ) ) return;
183 -
184 - foreach ( $this->groups as $id => $g ) {
185 - $wgTranslateAC[$id] = array( $this, 'factory' );
186 - $wgTranslateEC[] = $id;
 205+ protected function loadConfigureExtensionData() {
 206+ if ( !$this->useConfigure ) {
 207+ return array();
187208 }
188 - }
189209
190 - public function factory( $id ) {
191 - $info = $this->groups[$id];
192 - $group = ExtensionMessageGroup::factory( $info['name'], $id );
193 - $group->setMessageFile( $info['file'] );
194 - $group->setPath( $this->path );
195 - $group->namespaces = $this->namespaces;
 210+ global $wgAutoloadClasses, $IP, $wgTranslateExtensionDirectory;
196211
197 - if ( isset( $info['prefix'] ) ) {
198 - $mangler = new StringMatcher( $info['prefix'], $info['mangle'] );
199 - $group->setMangler( $mangler );
200 - $info['ignored'] = $mangler->mangle( $info['ignored'] );
201 - $info['optional'] = $mangler->mangle( $info['optional'] );
 212+ $postfix = 'Configure/load_txt_def/TxtDef.php';
 213+ if ( file_exists( "$IP/extensions/$postfix" ) ) {
 214+ $prefix = "$IP/extensions";
 215+ } elseif( file_exists( "$wgTranslateExtensionDirectory/$postfix" ) ) {
 216+ $prefix = $wgTranslateExtensionDirectory;
 217+ } else {
 218+ $prefix = false;
202219 }
203220
204 - if ( !empty( $info['var'] ) ) $group->setVariableName( $info['var'] );
205 - if ( !empty( $info['optional'] ) ) $group->setOptional( $info['optional'] );
206 - if ( !empty( $info['ignored'] ) ) $group->setIgnored( $info['ignored'] );
207 - if ( isset( $info['desc'] ) ) {
208 - $group->setDescription( $info['desc'] );
209 - } else {
210 - $group->setDescriptionMsg( $info['descmsg'], $info['url'] );
 221+ if ( $prefix ) {
 222+ $wgAutoloadClasses['TxtDef'] = "$prefix/$postfix";
 223+ $tmp = TxtDef::loadFromFile( "$prefix/Configure/settings/Settings-ext.txt" );
 224+ return array_combine( array_map( array( __CLASS__, 'foldId' ), array_keys( $tmp ) ), array_values( $tmp ) );
211225 }
212226
213 - if ( isset( $info['aliasfile'] ) ) $group->setAliasFile( $info['aliasfile'] );
214 - if ( isset( $info['magicfile'] ) ) $group->setMagicFile( $info['magicfile'] );
 227+ return array();
 228+ }
215229
216 - return $group;
217 - }
 230+
218231 }

Follow-up revisions

RevisionCommit summaryAuthorDate
r85125Using new methods introduced in r85123 + fixing typo (sanatized > sanitized)krinkle14:53, 1 April 2011

Status & tagging log