Index: trunk/phase3/includes/LinkBatch.php |
— | — | @@ -94,6 +94,7 @@ |
95 | 95 | wfProfileIn( __METHOD__ ); |
96 | 96 | $res = $this->doQuery(); |
97 | 97 | $ids = $this->addResultToCache( $cache, $res ); |
| 98 | + $this->doGenderQuery(); |
98 | 99 | wfProfileOut( __METHOD__ ); |
99 | 100 | return $ids; |
100 | 101 | } |
— | — | @@ -157,6 +158,20 @@ |
158 | 159 | return $res; |
159 | 160 | } |
160 | 161 | |
| 162 | + public function doGenderQuery() { |
| 163 | + if ( $this->isEmpty() ) { |
| 164 | + return false; |
| 165 | + } |
| 166 | + |
| 167 | + global $wgContLang; |
| 168 | + if ( !$wgContLang->needsGenderDistinction() ) { |
| 169 | + return false; |
| 170 | + } |
| 171 | + |
| 172 | + $genderCache = GenderCache::singleton(); |
| 173 | + $genderCache->dolinkBatch( $this->data, $this->caller ); |
| 174 | + } |
| 175 | + |
161 | 176 | /** |
162 | 177 | * Construct a WHERE clause which will match all the given titles. |
163 | 178 | * |
Index: trunk/phase3/includes/LocalisationCache.php |
— | — | @@ -87,7 +87,7 @@ |
88 | 88 | 'defaultUserOptionOverrides', 'linkTrail', 'namespaceAliases', |
89 | 89 | 'dateFormats', 'datePreferences', 'datePreferenceMigrationMap', |
90 | 90 | 'defaultDateFormat', 'extraUserToggles', 'specialPageAliases', |
91 | | - 'imageFiles', 'preloadedMessages', |
| 91 | + 'imageFiles', 'preloadedMessages', 'namespaceGenderAliases', |
92 | 92 | ); |
93 | 93 | |
94 | 94 | /** |
Index: trunk/phase3/includes/AutoLoader.php |
— | — | @@ -98,6 +98,7 @@ |
99 | 99 | 'ForkController' => 'includes/ForkController.php', |
100 | 100 | 'FormatExif' => 'includes/Exif.php', |
101 | 101 | 'FormOptions' => 'includes/FormOptions.php', |
| 102 | + 'GenderCache' => 'includes/GenderCache.php', |
102 | 103 | 'GlobalDependency' => 'includes/CacheDependency.php', |
103 | 104 | 'HashBagOStuff' => 'includes/BagOStuff.php', |
104 | 105 | 'HashtableReplacer' => 'includes/StringUtils.php', |
Index: trunk/phase3/includes/Title.php |
— | — | @@ -617,6 +617,13 @@ |
618 | 618 | return MWNamespace::getCanonicalName( $this->mNamespace ); |
619 | 619 | } |
620 | 620 | } |
| 621 | + |
| 622 | + if ( $wgContLang->needsGenderDistinction() && |
| 623 | + MWNamespace::hasGenderDistinction( $this->mNamespace ) ) { |
| 624 | + $gender = GenderCache::singleton()->getGenderOf( $this->getText(), __METHOD__ ); |
| 625 | + return $wgContLang->getGenderNsText( $this->mNamespace, $gender ); |
| 626 | + } |
| 627 | + |
621 | 628 | return $wgContLang->getNsText( $this->mNamespace ); |
622 | 629 | } |
623 | 630 | |
Index: trunk/phase3/includes/Namespace.php |
— | — | @@ -276,4 +276,17 @@ |
277 | 277 | // Default to the global setting |
278 | 278 | return $wgCapitalLinks; |
279 | 279 | } |
| 280 | + |
| 281 | + /** |
| 282 | + * Does the namespace (potentially) have different aliases for different |
| 283 | + * genders. Not all languages make a distinction here. |
| 284 | + * |
| 285 | + * @since 1.18 |
| 286 | + * @param $index int Index to check |
| 287 | + * @return bool |
| 288 | + */ |
| 289 | + public static function hasGenderDistinction( $index ) { |
| 290 | + return $index == NS_USER || $index == NS_USER_TALK; |
| 291 | + } |
| 292 | + |
280 | 293 | } |
Index: trunk/phase3/includes/GenderCache.php |
— | — | @@ -0,0 +1,119 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +/** |
| 5 | + * Caches user genders when needed to use correct namespace alises. |
| 6 | + * @author Niklas Laxström |
| 7 | + * @since 1.18 |
| 8 | + */ |
| 9 | +class GenderCache { |
| 10 | + protected $cache = array(); |
| 11 | + protected $default; |
| 12 | + protected $misses = 0; |
| 13 | + protected $missLimit = 1000; |
| 14 | + |
| 15 | + public static function singleton() { |
| 16 | + static $that = null; |
| 17 | + if ( $that === null ) { |
| 18 | + $that = new self(); |
| 19 | + } |
| 20 | + return $that; |
| 21 | + } |
| 22 | + |
| 23 | + protected function __construct() {} |
| 24 | + |
| 25 | + /** |
| 26 | + * Returns the default gender option in this wiki. |
| 27 | + * @return String |
| 28 | + */ |
| 29 | + protected function getDefault() { |
| 30 | + if ( $this->default === null ) { |
| 31 | + $this->default = User::getDefaultOption( 'gender' ); |
| 32 | + } |
| 33 | + return $this->default; |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Returns the gender for given username. |
| 38 | + * @param $users String: username |
| 39 | + * @param $caller String: the calling method |
| 40 | + * @return String |
| 41 | + */ |
| 42 | + public function getGenderOf( $username, $caller = '' ) { |
| 43 | + global $wgUser; |
| 44 | + |
| 45 | + $username = strtr( $username, '_', ' ' ); |
| 46 | + if ( !isset( $this->cache[$username] ) ) { |
| 47 | + |
| 48 | + if ( $this->misses >= $this->missLimit && $wgUser->getName() !== $username ) { |
| 49 | + if( $this->misses === $this->missLimit ) { |
| 50 | + $this->misses++; |
| 51 | + wfDebug( __METHOD__ . ": too many misses, returning default onwards\n" ); |
| 52 | + } |
| 53 | + return $this->getDefault(); |
| 54 | + |
| 55 | + } else { |
| 56 | + $this->misses++; |
| 57 | + if ( !User::isValidUserName( $username ) ) { |
| 58 | + $this->cache[$username] = $this->getDefault(); |
| 59 | + } else { |
| 60 | + $this->doQuery( $username, $caller ); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + } |
| 65 | + |
| 66 | + /* Undefined if there is a valid username which for some reason doesn't |
| 67 | + * exist in the database. |
| 68 | + */ |
| 69 | + return isset( $this->cache[$username] ) ? $this->cache[$username] : $this->getDefault(); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Wrapper for doQuery that processes raw LinkBatch data. |
| 74 | + */ |
| 75 | + public function doLinkBatch( $data, $caller = '' ) { |
| 76 | + $users = array(); |
| 77 | + foreach ( $data as $ns => $pagenames ) { |
| 78 | + if ( !MWNamespace::hasGenderDistinction( $ns ) ) continue; |
| 79 | + foreach ( array_keys( $pagenames ) as $username ) { |
| 80 | + if ( isset( $this->cache[$username] ) ) continue; |
| 81 | + $users[$username] = true; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + $this->doQuery( array_keys( $users ), $caller ); |
| 86 | + |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Preloads genders for given list of users. |
| 91 | + * @param $users List|String: usernames |
| 92 | + * @param $caller String: the calling method |
| 93 | + */ |
| 94 | + public function doQuery( $users, $caller = '' ) { |
| 95 | + if ( count( $users ) === 0 ) return false; |
| 96 | + |
| 97 | + foreach ( (array) $users as $index => $value ) { |
| 98 | + $users[$index] = strtr( $value, '_', ' ' ); |
| 99 | + } |
| 100 | + |
| 101 | + $dbr = wfGetDB( DB_SLAVE ); |
| 102 | + $table = array( 'user', 'user_properties' ); |
| 103 | + $fields = array( 'user_name', 'up_value' ); |
| 104 | + $conds = array( 'user_name' => $users ); |
| 105 | + $joins = array( 'user_properties' => |
| 106 | + array( 'LEFT JOIN', array( 'user_id = up_user', 'up_property' => 'gender' ) ) ); |
| 107 | + |
| 108 | + $comment = __METHOD__; |
| 109 | + if ( strval( $caller ) !== '' ) { |
| 110 | + $comment .= "/$caller"; |
| 111 | + } |
| 112 | + $res = $dbr->select( $table, $fields, $conds, $comment, $joins, $joins ); |
| 113 | + |
| 114 | + $default = $this->getDefault(); |
| 115 | + foreach ( $res as $row ) { |
| 116 | + $this->cache[$row->user_name] = $row->up_value ? $row->up_value : $default; |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | +} |
Property changes on: trunk/phase3/includes/GenderCache.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 121 | + native |
Index: trunk/phase3/languages/messages/MessagesEn.php |
— | — | @@ -112,6 +112,16 @@ |
113 | 113 | $namespaceAliases = array(); |
114 | 114 | |
115 | 115 | /** |
| 116 | + * Array of gender specific. namespace aliases. |
| 117 | + * Mapping NS_xxx to array of GENDERKEY to alias. |
| 118 | + * Example: |
| 119 | +$namespaceGenderAliases = array( |
| 120 | + NS_USER => array( 'male' => 'Male_user', 'female' => 'Female_user' ), |
| 121 | +); |
| 122 | + */ |
| 123 | +$namespaceGenderAliases = array(); |
| 124 | + |
| 125 | +/** |
116 | 126 | * Deprecated, use the message array |
117 | 127 | */ |
118 | 128 | $mathNames = array( |
Index: trunk/phase3/languages/Language.php |
— | — | @@ -339,6 +339,29 @@ |
340 | 340 | } |
341 | 341 | |
342 | 342 | /** |
| 343 | + * Returns gender-dependent namespace alias if available. |
| 344 | + * @param $index Int: namespace index |
| 345 | + * @param $gender String: gender key (male, female... ) |
| 346 | + * @return String |
| 347 | + * @since 1.18 |
| 348 | + */ |
| 349 | + function getGenderNsText( $index, $gender ) { |
| 350 | + $ns = self::$dataCache->getItem( $this->mCode, 'namespaceGenderAliases' ); |
| 351 | + return isset( $ns[$index][$gender] ) ? $ns[$index][$gender] : $this->getNsText( $index ); |
| 352 | + } |
| 353 | + |
| 354 | + /** |
| 355 | + * Whether this language makes distinguishes genders for example in |
| 356 | + * namespaces. |
| 357 | + * @return bool |
| 358 | + * @since 1.18 |
| 359 | + */ |
| 360 | + function needsGenderDistinction() { |
| 361 | + $aliases = self::$dataCache->getItem( $this->mCode, 'namespaceGenderAliases' ); |
| 362 | + return count( $aliases ) > 0; |
| 363 | + } |
| 364 | + |
| 365 | + /** |
343 | 366 | * Get a namespace key by value, case insensitive. |
344 | 367 | * Only matches namespace names for the current language, not the |
345 | 368 | * canonical ones defined in Namespace.php. |
— | — | @@ -366,6 +389,14 @@ |
367 | 390 | } |
368 | 391 | } |
369 | 392 | } |
| 393 | + |
| 394 | + $genders = self::$dataCache->getItem( $this->mCode, 'namespaceGenderAliases' ); |
| 395 | + foreach ( $genders as $index => $forms ) { |
| 396 | + foreach ( $forms as $alias ) { |
| 397 | + $aliases[$alias] = $index; |
| 398 | + } |
| 399 | + } |
| 400 | + |
370 | 401 | $this->namespaceAliases = $aliases; |
371 | 402 | } |
372 | 403 | return $this->namespaceAliases; |
Index: trunk/phase3/RELEASE-NOTES |
— | — | @@ -176,6 +176,8 @@ |
177 | 177 | regularly. Below only new and removed languages are listed, as well as |
178 | 178 | changes to languages because of Bugzilla reports. |
179 | 179 | |
| 180 | +* (bug 17160) Gender specific display text for User namespace |
| 181 | + |
180 | 182 | == Compatibility == |
181 | 183 | |
182 | 184 | MediaWiki 1.18 requires PHP 5.1 (5.2 recommended). PHP 4 is no longer |