Index: trunk/phase3/includes/GenderCache.php |
— | — | @@ -1,135 +0,0 @@ |
2 | | -<?php |
3 | | - |
4 | | -/** |
5 | | - * Caches user genders when needed to use correct namespace aliases. |
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 | | - /** |
16 | | - * @return GenderCache |
17 | | - */ |
18 | | - public static function singleton() { |
19 | | - static $that = null; |
20 | | - if ( $that === null ) { |
21 | | - $that = new self(); |
22 | | - } |
23 | | - return $that; |
24 | | - } |
25 | | - |
26 | | - protected function __construct() {} |
27 | | - |
28 | | - /** |
29 | | - * Returns the default gender option in this wiki. |
30 | | - * @return String |
31 | | - */ |
32 | | - protected function getDefault() { |
33 | | - if ( $this->default === null ) { |
34 | | - $this->default = User::getDefaultOption( 'gender' ); |
35 | | - } |
36 | | - return $this->default; |
37 | | - } |
38 | | - |
39 | | - /** |
40 | | - * Returns the gender for given username. |
41 | | - * @param $username String: username |
42 | | - * @param $caller String: the calling method |
43 | | - * @return String |
44 | | - */ |
45 | | - public function getGenderOf( $username, $caller = '' ) { |
46 | | - global $wgUser; |
47 | | - |
48 | | - $username = strtr( $username, '_', ' ' ); |
49 | | - if ( !isset( $this->cache[$username] ) ) { |
50 | | - |
51 | | - if ( $this->misses >= $this->missLimit && $wgUser->getName() !== $username ) { |
52 | | - if( $this->misses === $this->missLimit ) { |
53 | | - $this->misses++; |
54 | | - wfDebug( __METHOD__ . ": too many misses, returning default onwards\n" ); |
55 | | - } |
56 | | - return $this->getDefault(); |
57 | | - |
58 | | - } else { |
59 | | - $this->misses++; |
60 | | - if ( !User::isValidUserName( $username ) ) { |
61 | | - $this->cache[$username] = $this->getDefault(); |
62 | | - } else { |
63 | | - $this->doQuery( $username, $caller ); |
64 | | - } |
65 | | - } |
66 | | - |
67 | | - } |
68 | | - |
69 | | - /* Undefined if there is a valid username which for some reason doesn't |
70 | | - * exist in the database. |
71 | | - */ |
72 | | - return isset( $this->cache[$username] ) ? $this->cache[$username] : $this->getDefault(); |
73 | | - } |
74 | | - |
75 | | - /** |
76 | | - * Wrapper for doQuery that processes raw LinkBatch data. |
77 | | - * |
78 | | - * @param $data |
79 | | - * @param $caller |
80 | | - */ |
81 | | - public function doLinkBatch( $data, $caller = '' ) { |
82 | | - $users = array(); |
83 | | - foreach ( $data as $ns => $pagenames ) { |
84 | | - if ( !MWNamespace::hasGenderDistinction( $ns ) ) continue; |
85 | | - foreach ( array_keys( $pagenames ) as $username ) { |
86 | | - if ( isset( $this->cache[$username] ) ) continue; |
87 | | - $users[$username] = true; |
88 | | - } |
89 | | - } |
90 | | - |
91 | | - $this->doQuery( array_keys( $users ), $caller ); |
92 | | - } |
93 | | - |
94 | | - /** |
95 | | - * Preloads genders for given list of users. |
96 | | - * @param $users List|String: usernames |
97 | | - * @param $caller String: the calling method |
98 | | - */ |
99 | | - public function doQuery( $users, $caller = '' ) { |
100 | | - $default = $this->getDefault(); |
101 | | - |
102 | | - foreach ( (array) $users as $index => $value ) { |
103 | | - $name = strtr( $value, '_', ' ' ); |
104 | | - if ( isset( $this->cache[$name] ) ) { |
105 | | - // Skip users whose gender setting we already know |
106 | | - unset( $users[$index] ); |
107 | | - } else { |
108 | | - $users[$index] = $name; |
109 | | - // For existing users, this value will be overwritten by the correct value |
110 | | - $this->cache[$name] = $default; |
111 | | - } |
112 | | - } |
113 | | - |
114 | | - if ( count( $users ) === 0 ) { |
115 | | - return; |
116 | | - } |
117 | | - |
118 | | - $dbr = wfGetDB( DB_SLAVE ); |
119 | | - $table = array( 'user', 'user_properties' ); |
120 | | - $fields = array( 'user_name', 'up_value' ); |
121 | | - $conds = array( 'user_name' => $users ); |
122 | | - $joins = array( 'user_properties' => |
123 | | - array( 'LEFT JOIN', array( 'user_id = up_user', 'up_property' => 'gender' ) ) ); |
124 | | - |
125 | | - $comment = __METHOD__; |
126 | | - if ( strval( $caller ) !== '' ) { |
127 | | - $comment .= "/$caller"; |
128 | | - } |
129 | | - $res = $dbr->select( $table, $fields, $conds, $comment, $joins, $joins ); |
130 | | - |
131 | | - foreach ( $res as $row ) { |
132 | | - $this->cache[$row->user_name] = $row->up_value ? $row->up_value : $default; |
133 | | - } |
134 | | - } |
135 | | - |
136 | | -} |
Index: trunk/phase3/includes/AutoLoader.php |
— | — | @@ -89,7 +89,6 @@ |
90 | 90 | 'FormAction' => 'includes/Action.php', |
91 | 91 | 'FormOptions' => 'includes/FormOptions.php', |
92 | 92 | 'FormSpecialPage' => 'includes/SpecialPage.php', |
93 | | - 'GenderCache' => 'includes/GenderCache.php', |
94 | 93 | 'HashtableReplacer' => 'includes/StringUtils.php', |
95 | 94 | 'HistoryBlob' => 'includes/HistoryBlob.php', |
96 | 95 | 'HistoryBlobCurStub' => 'includes/HistoryBlob.php', |
— | — | @@ -366,6 +365,7 @@ |
367 | 366 | 'DependencyWrapper' => 'includes/cache/CacheDependency.php', |
368 | 367 | 'FileCacheBase' => 'includes/cache/FileCacheBase.php', |
369 | 368 | 'FileDependency' => 'includes/cache/CacheDependency.php', |
| 369 | + 'GenderCache' => 'includes/cache/GenderCache.php', |
370 | 370 | 'GlobalDependency' => 'includes/cache/CacheDependency.php', |
371 | 371 | 'HTMLCacheUpdate' => 'includes/cache/HTMLCacheUpdate.php', |
372 | 372 | 'HTMLCacheUpdateJob' => 'includes/cache/HTMLCacheUpdate.php', |
Index: trunk/phase3/includes/cache/GenderCache.php |
— | — | @@ -0,0 +1,135 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +/** |
| 5 | + * Caches user genders when needed to use correct namespace aliases. |
| 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 | + /** |
| 16 | + * @return GenderCache |
| 17 | + */ |
| 18 | + public static function singleton() { |
| 19 | + static $that = null; |
| 20 | + if ( $that === null ) { |
| 21 | + $that = new self(); |
| 22 | + } |
| 23 | + return $that; |
| 24 | + } |
| 25 | + |
| 26 | + protected function __construct() {} |
| 27 | + |
| 28 | + /** |
| 29 | + * Returns the default gender option in this wiki. |
| 30 | + * @return String |
| 31 | + */ |
| 32 | + protected function getDefault() { |
| 33 | + if ( $this->default === null ) { |
| 34 | + $this->default = User::getDefaultOption( 'gender' ); |
| 35 | + } |
| 36 | + return $this->default; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Returns the gender for given username. |
| 41 | + * @param $username String: username |
| 42 | + * @param $caller String: the calling method |
| 43 | + * @return String |
| 44 | + */ |
| 45 | + public function getGenderOf( $username, $caller = '' ) { |
| 46 | + global $wgUser; |
| 47 | + |
| 48 | + $username = strtr( $username, '_', ' ' ); |
| 49 | + if ( !isset( $this->cache[$username] ) ) { |
| 50 | + |
| 51 | + if ( $this->misses >= $this->missLimit && $wgUser->getName() !== $username ) { |
| 52 | + if( $this->misses === $this->missLimit ) { |
| 53 | + $this->misses++; |
| 54 | + wfDebug( __METHOD__ . ": too many misses, returning default onwards\n" ); |
| 55 | + } |
| 56 | + return $this->getDefault(); |
| 57 | + |
| 58 | + } else { |
| 59 | + $this->misses++; |
| 60 | + if ( !User::isValidUserName( $username ) ) { |
| 61 | + $this->cache[$username] = $this->getDefault(); |
| 62 | + } else { |
| 63 | + $this->doQuery( $username, $caller ); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + } |
| 68 | + |
| 69 | + /* Undefined if there is a valid username which for some reason doesn't |
| 70 | + * exist in the database. |
| 71 | + */ |
| 72 | + return isset( $this->cache[$username] ) ? $this->cache[$username] : $this->getDefault(); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Wrapper for doQuery that processes raw LinkBatch data. |
| 77 | + * |
| 78 | + * @param $data |
| 79 | + * @param $caller |
| 80 | + */ |
| 81 | + public function doLinkBatch( $data, $caller = '' ) { |
| 82 | + $users = array(); |
| 83 | + foreach ( $data as $ns => $pagenames ) { |
| 84 | + if ( !MWNamespace::hasGenderDistinction( $ns ) ) continue; |
| 85 | + foreach ( array_keys( $pagenames ) as $username ) { |
| 86 | + if ( isset( $this->cache[$username] ) ) continue; |
| 87 | + $users[$username] = true; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + $this->doQuery( array_keys( $users ), $caller ); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Preloads genders for given list of users. |
| 96 | + * @param $users List|String: usernames |
| 97 | + * @param $caller String: the calling method |
| 98 | + */ |
| 99 | + public function doQuery( $users, $caller = '' ) { |
| 100 | + $default = $this->getDefault(); |
| 101 | + |
| 102 | + foreach ( (array) $users as $index => $value ) { |
| 103 | + $name = strtr( $value, '_', ' ' ); |
| 104 | + if ( isset( $this->cache[$name] ) ) { |
| 105 | + // Skip users whose gender setting we already know |
| 106 | + unset( $users[$index] ); |
| 107 | + } else { |
| 108 | + $users[$index] = $name; |
| 109 | + // For existing users, this value will be overwritten by the correct value |
| 110 | + $this->cache[$name] = $default; |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + if ( count( $users ) === 0 ) { |
| 115 | + return; |
| 116 | + } |
| 117 | + |
| 118 | + $dbr = wfGetDB( DB_SLAVE ); |
| 119 | + $table = array( 'user', 'user_properties' ); |
| 120 | + $fields = array( 'user_name', 'up_value' ); |
| 121 | + $conds = array( 'user_name' => $users ); |
| 122 | + $joins = array( 'user_properties' => |
| 123 | + array( 'LEFT JOIN', array( 'user_id = up_user', 'up_property' => 'gender' ) ) ); |
| 124 | + |
| 125 | + $comment = __METHOD__; |
| 126 | + if ( strval( $caller ) !== '' ) { |
| 127 | + $comment .= "/$caller"; |
| 128 | + } |
| 129 | + $res = $dbr->select( $table, $fields, $conds, $comment, $joins, $joins ); |
| 130 | + |
| 131 | + foreach ( $res as $row ) { |
| 132 | + $this->cache[$row->user_name] = $row->up_value ? $row->up_value : $default; |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | +} |
Property changes on: trunk/phase3/includes/cache/GenderCache.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 137 | + native |