r80704 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r80703‎ | r80704 | r80705 >
Date:19:39, 21 January 2011
Author:roberthl
Status:ok
Tags:
Comment:
Babel:
- (bug 20119) Support category names with complex language and level names.
Category names are now configured via a configuration variable (an array element for each level), with three possible variables (language code, name in wiki language, and name in native language). This removes the category-prefix and category-suffix functionality that was configured via the message system - this method is probably more desireable as changing these will potentially result in the automatic creation of thousands of categories.
- Autocreated categories now appear in recent changes (but as a bot edit).
- Fix function argument order in calls to category auto creation for individual level categories (and fix matching mistake in how the arguments are interpreted).
- Add a few bits of missing method documentation.
- Various slight improvements to code style.
Modified paths:
  • /trunk/extensions/Babel/Babel.class.php (modified) (history)
  • /trunk/extensions/Babel/Babel.i18n.php (modified) (history)
  • /trunk/extensions/Babel/Babel.php (modified) (history)
  • /trunk/extensions/Babel/BabelAutoCreate.class.php (modified) (history)
  • /trunk/extensions/Babel/BabelLanguageCodes.class.php (modified) (history)
  • /trunk/extensions/Babel/codes.txt (modified) (history)

Diff [purge]

Index: trunk/extensions/Babel/Babel.class.php
@@ -126,16 +126,22 @@
127127 }
128128 // Try splitting the paramter in to language and level, split on last hyphen.
129129 $lastSplit = strrpos( $parameter, '-' );
130 - if ( $lastSplit === false ) return false;
 130+ if ( $lastSplit === false ) {
 131+ return false;
 132+ }
131133 $code = substr( $parameter, 0, $lastSplit );
132134 $level = substr( $parameter, $lastSplit + 1 );
133135
134136 // Validate code.
135137 $return['code'] = BabelLanguageCodes::getCode( $code );
136 - if ( !$return['code'] ) return false;
 138+ if ( !$return['code'] ) {
 139+ return false;
 140+ }
137141 // Validate level.
138142 $intLevel = (int) $level;
139 - if ( ( $intLevel < 0 || $intLevel > 5 ) && $level !== 'N' ) return false;
 143+ if ( ( $intLevel < 0 || $intLevel > 5 ) && $level !== 'N' ) {
 144+ return false;
 145+ }
140146 $return['level'] = $level;
141147
142148 return $return;
@@ -146,6 +152,7 @@
147153 *
148154 * @param $code String: Language code to use.
149155 * @param $level String or Integer: Level of ability to use.
 156+ * @return String: A single babel box, in wikitext format.
150157 */
151158 protected function mGenerateBox( $code, $level ) {
152159 $header = "[[{$this->mAddFixes( $code,'portal' )}|" . wfBCP47( $code ) . "]]<span class=\"mw-babel-box-level-$level\">-$level</span>";
@@ -177,10 +184,10 @@
178185 * @return String: Text for display, in wikitext format.
179186 */
180187 protected function mGetText( $name, $language, $level ) {
181 - global $wgTitle, $wgBabelUseLevelZeroCategory;
 188+ global $wgTitle, $wgBabelUseLevelZeroCategory, $wgBabelCategoryNames;
182189
183 - $categoryLevel = ":Category:{$this->mAddFixes( "$language-$level",'category' )}";
184 - $categorySuper = ":Category:{$this->mAddFixes( $language,'category' )}";
 190+ $categoryLevel = ':Category:' . $this->mReplaceCategoryVariables( $wgBabelCategoryNames[$level], $language );
 191+ $categorySuper = ':Category:' . $this->mReplaceCategoryVariables( $wgBabelCategoryNames['main'], $language );
185192
186193 if ( !$wgBabelUseLevelZeroCategory && $level === '0' ) {
187194 $categoryLevel = $wgTitle->getFullText();
@@ -211,23 +218,47 @@
212219 *
213220 * @param $code String: Language code to use.
214221 * @param $level String or Integer: Level of ability to use.
 222+ * @return String: Wikitext to add categories.
215223 */
216224 protected function mGenerateCategories( $code, $level ) {
217225 global $wgBabelUseMainCategories, $wgBabelUseLevelZeroCategory,
218 - $wgBabelUseSimpleCategories;
 226+ $wgBabelUseSimpleCategories, $wgBabelCategoryNames, $wgLanguageCode;
219227
220228 $r = '';
221229
222230 if ( $wgBabelUseMainCategories && ( $level === 'N' || ( $wgBabelUseLevelZeroCategory && $level === '0' ) || $level > 0 ) ) {
223 - $r .= "[[Category:{$this->mAddFixes( $code,'category' )}|$level]]";
224 - BabelAutoCreate::create( $this->mAddFixes( "$code", 'category' ), BabelLanguageCodes::getName( $code ) );
 231+ $category = $this->mReplaceCategoryVariables( $wgBabelCategoryNames['main'], $code );
 232+ $r .= "[[Category:$category|$level]]";
 233+ BabelAutoCreate::create( $category, BabelLanguageCodes::getName( $code, $wgLanguageCode ) );
225234 }
226235
227236 if ( !$wgBabelUseSimpleCategories && ( $level === 'N' || ( $wgBabelUseLevelZeroCategory && $level === '0' ) || $level > 0 ) ) {
228 - $r .= "[[Category:{$this->mAddFixes( "$code-$level",'category' )}]]";
229 - BabelAutoCreate::create( $this->mAddFixes( "$code-$level", 'category' ), $level, BabelLanguageCodes::getName( $code ) );
 237+ $category = $this->mReplaceCategoryVariables( $wgBabelCategoryNames[$level], $code );
 238+ $r .= "[[Category:$category]]";
 239+ BabelAutoCreate::create( $category, BabelLanguageCodes::getName( $code, $wgLanguageCode ), $level );
230240 }
231241
232242 return $r;
233243 }
 244+
 245+ /**
 246+ * Replace the placeholder variables from the category names configurtion
 247+ * array with actual values.
 248+ *
 249+ * @param $category String: Category name (containing variables).
 250+ * @param $code String: Language code of category.
 251+ * @return String: Category name with variables replaced.
 252+ */
 253+ protected function mReplaceCategoryVariables( $category, $code ) {
 254+ global $wgLanguageCode;
 255+ $vars = array(
 256+ '%code%' => $code,
 257+ '%wikiname%' => BabelLanguageCodes::getName( $code, $wgLanguageCode ),
 258+ '%nativename%' => BabelLanguageCodes::getName( $code )
 259+ );
 260+ foreach ( $vars as $find => $replace ) {
 261+ $category = str_replace( $find, $replace, $category );
 262+ }
 263+ return $category;
 264+ }
234265 }
Index: trunk/extensions/Babel/BabelLanguageCodes.class.php
@@ -38,9 +38,11 @@
3939 * - Names constant database.
4040 *
4141 * @param $code String: Code to get name for.
 42+ * @param $language String: Code of language to attempt to get name in,
 43+ * defaults to language of code.
4244 * @return String (name of language) or false (invalid language code).
4345 */
44 - public static function getName( $code ) {
 46+ public static function getName( $code, $language = null ) {
4547 $cacheType = 'name';
4648 // Get correct code, even though it should already be correct.
4749 $code = self::getCode( $code );
@@ -48,9 +50,13 @@
4951 return false;
5052 }
5153
 54+ if ( $language === null ) {
 55+ $language = $code;
 56+ }
 57+
5258 // Try CLDR extension, then MediaWiki native.
5359 if ( class_exists( 'LanguageNames' ) ) {
54 - $names = LanguageNames::getNames( $code, LanguageNames::FALLBACK_NORMAL, LanguageNames::LIST_MW_AND_CLDR );
 60+ $names = LanguageNames::getNames( $language, LanguageNames::FALLBACK_NORMAL, LanguageNames::LIST_MW_AND_CLDR );
5561 } else {
5662 $names = Language::getLanguageNames();
5763 }
Index: trunk/extensions/Babel/Babel.i18n.php
@@ -43,8 +43,6 @@
4444
4545 'babel-box-cellspacing' => '0', # Do not translate or duplicate this message to other languages.
4646
47 - 'babel-category-prefix' => '', # Do not translate or duplicate this message to other languages.
48 - 'babel-category-suffix' => '', # Do not translate or duplicate this message to other languages.
4947 'babel-portal-prefix' => '', # Do not translate or duplicate this message to other languages.
5048 'babel-portal-suffix' => '', # Do not translate or duplicate this message to other languages.
5149 'babel-template-prefix' => '', # Do not translate or duplicate this message to other languages.
@@ -55,7 +53,6 @@
5654
5755 /** Message documentation (Message documentation)
5856 * @author Lloffiwr
59 - * @author MinuteElectron
6057 * @author Purodha
6158 * @author Raimond Spekking
6259 * @author Raymond
Index: trunk/extensions/Babel/Babel.php
@@ -49,6 +49,17 @@
5050 // Language names and codes constant database files, the defaults should suffice.
5151 $wgBabelLanguageCodesCdb = $dir . '/codes.cdb';
5252 $wgBabelLanguageNamesCdb = $dir . '/names.cdb';
 53+// Format of category names - variables: %code% %wikiname% %nativename%
 54+$wgBabelCategoryNames = array(
 55+ 0 => '%code%-0',
 56+ 1 => '%code%-1',
 57+ 2 => '%code%-2',
 58+ 3 => '%code%-3',
 59+ 4 => '%code%-4',
 60+ 5 => '%code%-5',
 61+ 'N' => '%code%-N',
 62+ 'main' => '%code%'
 63+);
5364
5465 /* Other settings, to be made in-wiki:
5566 MediaWiki:Babel-template-prefix and MediaWiki:Babel-template-suffix
Index: trunk/extensions/Babel/codes.txt
@@ -679,6 +679,7 @@
680680 - bgx "Balkan Gagauz Turkish"
681681 - bgy "Benggoi"
682682 - bgz "Banggai"
 683+bh - "Bihari"
683684 - bha "Bharia"
684685 - bhb "Bhili"
685686 - bhc "Biga"
@@ -4105,6 +4106,7 @@
41064107 - moi "Mboi"
41074108 - moj "Monzombo"
41084109 - mok "Morori"
 4110+mo mol "Moldavian"
41094111 - mom "Mangue"
41104112 mn mon "Mongolian"
41114113 - moo "Monom"
Index: trunk/extensions/Babel/BabelAutoCreate.class.php
@@ -17,6 +17,10 @@
1818
1919 /**
2020 * Create category.
 21+ *
 22+ * @param $category String: Name of category to create.
 23+ * @param $language String: Name of language that the category is for.
 24+ * @param $level String: Level that the category is for.
2125 */
2226 public static function create( $category, $language, $level = null ) {
2327 $category = strip_tags( $category );
@@ -27,14 +31,16 @@
2832 if ( $level === null ) {
2933 $text = wfMsgForContent( 'babel-autocreate-text-main', $language );
3034 } else {
31 - $text = wfMsgForContent( 'babel-autocreate-text-levels', $language, $level );
 35+ $text = wfMsgForContent( 'babel-autocreate-text-levels', $level, $language );
3236 }
3337 $article = new Article( $title );
34 - $article->doEdit( $text, wfMsgForContent( 'babel-autocreate-reason', wfMsgForContent( 'babel-url' ) ), EDIT_SUPPRESS_RC, false, self::user() );
 38+ $article->doEdit( $text, wfMsgForContent( 'babel-autocreate-reason', wfMsgForContent( 'babel-url' ) ), EDIT_FORCE_BOT, false, self::user() );
3539 }
3640
3741 /**
3842 * Get user object.
 43+ *
 44+ * @return User object: User object for autocreate user.
3945 */
4046 public static function user() {
4147 if ( !self::$user ) {

Follow-up revisions

RevisionCommit summaryAuthorDate
r80707Babel:...roberthl19:42, 21 January 2011
r80708Follow-up r80704 for translatewikiraymond20:02, 21 January 2011

Status & tagging log