Index: trunk/extensions/OnlineStatus/OnlineStatus.body.php |
— | — | @@ -4,37 +4,48 @@ |
5 | 5 | /** |
6 | 6 | * Get the user online status |
7 | 7 | * |
8 | | - * @param mixed $title string of Title object, if it's a title, if have to be in |
| 8 | + * @param mixed $title string of Title object, if it's a title, if has to be in |
9 | 9 | * User: of User_talk: namespace. |
10 | 10 | * @return either bool or null |
11 | 11 | */ |
12 | 12 | static function GetUserStatus( $title, $checkShowPref = false ){ |
13 | 13 | if( is_object( $title ) ){ |
14 | | - if( !$title instanceof Title ) |
| 14 | + if( !$title instanceof Title ) { |
15 | 15 | return null; |
16 | | - if( !in_array( $title->getNamespace(), array( NS_USER, NS_USER_TALK ) ) ) |
| 16 | + } |
| 17 | + |
| 18 | + if( !in_array( $title->getNamespace(), array( NS_USER, NS_USER_TALK ) ) ) { |
17 | 19 | return null; |
| 20 | + } |
| 21 | + |
18 | 22 | $username = explode( '/', $title->getDBkey() ); |
19 | 23 | $username = $username[0]; |
20 | 24 | } else { |
21 | 25 | $username = $title; |
22 | 26 | } |
| 27 | + |
23 | 28 | $user = User::newFromName( $username ); |
24 | | - if( !$user instanceof User || $user->getId() == 0 ) |
| 29 | + |
| 30 | + if( !$user instanceof User || $user->getId() == 0 ) { |
25 | 31 | return null; |
26 | | - if( $checkShowPref && !$user->getOption( 'showonline' ) ) |
| 32 | + } |
| 33 | + |
| 34 | + if( $checkShowPref && !$user->getOption( 'showonline' ) ) { |
27 | 35 | return null; |
| 36 | + } |
| 37 | + |
28 | 38 | return $user->getOption( 'online' ); |
29 | 39 | } |
30 | 40 | |
31 | 41 | /** |
32 | | - * Used for ajax requests |
| 42 | + * Used for AJAX requests |
33 | 43 | */ |
34 | 44 | static function Ajax( $action, $stat = false ){ |
35 | 45 | global $wgUser; |
36 | 46 | |
37 | | - if( $wgUser->isAnon() ) |
| 47 | + if( $wgUser->isAnon() ) { |
38 | 48 | return wfMsgHtml( 'onlinestatus-js-anon' ); |
| 49 | + } |
39 | 50 | |
40 | 51 | switch( $action ){ |
41 | 52 | case 'get': |
— | — | @@ -42,9 +53,12 @@ |
43 | 54 | $msg = wfMsgForContentNoTrans( 'onlinestatus-levels' ); |
44 | 55 | $lines = explode( "\n", $msg ); |
45 | 56 | $radios = array(); |
| 57 | + |
46 | 58 | foreach( $lines as $line ){ |
47 | | - if( substr( $line, 0, 1 ) != '*' ) |
| 59 | + if( substr( $line, 0, 1 ) != '*' ) { |
48 | 60 | continue; |
| 61 | + } |
| 62 | + |
49 | 63 | $lev = trim( $line, '* ' ); |
50 | 64 | $radios[] = array( |
51 | 65 | $lev, |
— | — | @@ -52,6 +66,7 @@ |
53 | 67 | $lev == $def |
54 | 68 | ); |
55 | 69 | } |
| 70 | + |
56 | 71 | return json_encode( $radios ); |
57 | 72 | case 'set': |
58 | 73 | if( $stat ){ |
— | — | @@ -59,6 +74,7 @@ |
60 | 75 | $dbw->begin(); |
61 | 76 | $actual = $wgUser->getOption( 'online' ); |
62 | 77 | $wgUser->setOption( 'online', $stat ); |
| 78 | + |
63 | 79 | if( $actual != $stat ){ |
64 | 80 | $wgUser->getUserPage()->invalidateCache(); |
65 | 81 | $wgUser->getTalkPage()->invalidateCache(); |
— | — | @@ -66,6 +82,7 @@ |
67 | 83 | $wgUser->saveSettings(); |
68 | 84 | $wgUser->invalidateCache(); |
69 | 85 | $dbw->commit(); |
| 86 | + |
70 | 87 | return wfMsgHtml( 'onlinestatus-js-changed', wfMsgHtml( 'onlinestatus-toggle-'.$stat ) ); |
71 | 88 | } else { |
72 | 89 | return wfMsgHtml( 'onlinestatus-js-error', $stat ); |
— | — | @@ -78,6 +95,7 @@ |
79 | 96 | */ |
80 | 97 | static function ParserFirstCallInit( $parser ){ |
81 | 98 | global $wgAllowAnyUserOnlineStatusFunction; |
| 99 | + |
82 | 100 | if( $wgAllowAnyUserOnlineStatusFunction ) |
83 | 101 | $parser->setFunctionHook( 'anyuseronlinestatus', array( __CLASS__, 'ParserHookCallback' ) ); |
84 | 102 | return true; |
— | — | @@ -88,8 +106,10 @@ |
89 | 107 | */ |
90 | 108 | static function ParserHookCallback( &$parser, $user, $raw = false ){ |
91 | 109 | $status = self::GetUserStatus( $user ); |
| 110 | + |
92 | 111 | if( $status === null ) |
93 | 112 | return array( 'found' => false ); |
| 113 | + |
94 | 114 | if( empty( $raw ) ){ |
95 | 115 | return wfMsgNoTrans( 'onlinestatus-toggle-' . $status ); |
96 | 116 | } else { |
— | — | @@ -103,6 +123,7 @@ |
104 | 124 | static function MagicWordVariable( &$magicWords ) { |
105 | 125 | $magicWords[] = 'onlinestatus_word'; |
106 | 126 | $magicWords[] = 'onlinestatus_word_raw'; |
| 127 | + |
107 | 128 | return true; |
108 | 129 | } |
109 | 130 | |
— | — | @@ -112,17 +133,24 @@ |
113 | 134 | static function ParserGetVariable( &$parser, &$varCache, &$index, &$ret ){ |
114 | 135 | if( $index == 'onlinestatus_word' ){ |
115 | 136 | $status = self::GetUserStatus( $parser->getTitle() ); |
116 | | - if( $status === null ) |
| 137 | + |
| 138 | + if( $status === null ) { |
117 | 139 | return true; |
| 140 | + } |
| 141 | + |
118 | 142 | $ret = wfMsgNoTrans( 'onlinestatus-toggle-' . $status ); |
119 | 143 | $varCache['onlinestatus'] = $ret; |
120 | 144 | } elseif( $index == 'onlinestatus_word_raw' ){ |
121 | 145 | $status = self::GetUserStatus( $parser->getTitle() ); |
122 | | - if( $status === null ) |
| 146 | + |
| 147 | + if( $status === null ) { |
123 | 148 | return true; |
| 149 | + } |
| 150 | + |
124 | 151 | $ret = $status; |
125 | 152 | $varCache['onlinestatus'] = $ret; |
126 | 153 | } |
| 154 | + |
127 | 155 | return true; |
128 | 156 | } |
129 | 157 | |
— | — | @@ -133,6 +161,7 @@ |
134 | 162 | $msg = wfMsgForContentNoTrans( 'onlinestatus-levels' ); |
135 | 163 | $lines = explode( "\n", $msg ); |
136 | 164 | $radios = array(); |
| 165 | + |
137 | 166 | foreach( $lines as $line ){ |
138 | 167 | if( substr( $line, 0, 1 ) != '*' ) |
139 | 168 | continue; |
— | — | @@ -183,6 +212,7 @@ |
184 | 213 | $user->setOption( 'online', 'online' ); |
185 | 214 | $user->saveSettings(); |
186 | 215 | } |
| 216 | + |
187 | 217 | return true; |
188 | 218 | } |
189 | 219 | |
— | — | @@ -190,15 +220,21 @@ |
191 | 221 | * Hook for UserLoginComplete |
192 | 222 | */ |
193 | 223 | static function UserLogoutComplete( &$newUser, &$injected_html, $oldName = null ){ |
194 | | - if( $oldName === null ) |
| 224 | + if( $oldName === null ) { |
195 | 225 | return true; |
| 226 | + } |
| 227 | + |
196 | 228 | $oldUser = User::newFromName( $oldName ); |
197 | | - if( !$oldUser instanceof User ) |
| 229 | + |
| 230 | + if( !$oldUser instanceof User ) { |
198 | 231 | return true; |
| 232 | + } |
| 233 | + |
199 | 234 | if( $oldUser->getOption( 'offlineonlogout' ) ){ |
200 | 235 | $oldUser->setOption( 'online', 'offline' ); |
201 | 236 | $oldUser->saveSettings(); |
202 | 237 | } |
| 238 | + |
203 | 239 | return true; |
204 | 240 | } |
205 | 241 | |
— | — | @@ -206,18 +242,24 @@ |
207 | 243 | * Hook function for BeforePageDisplay |
208 | 244 | */ |
209 | 245 | static function BeforePageDisplay( &$out ){ |
210 | | - global $wgRequest, $wgUser; |
211 | | - global $wgUseAjax; |
| 246 | + global $wgRequest, $wgUser, $wgUseAjax; |
212 | 247 | |
213 | 248 | if( $wgUser->isLoggedIn() && $wgUseAjax ){ |
214 | 249 | $out->addModules( 'ext.onlineStatus' ); |
215 | 250 | } |
216 | 251 | |
217 | | - if( !in_array( $wgRequest->getVal( 'action', 'view' ), array( 'view', 'purge' ) ) ) |
| 252 | + if( !in_array( $wgRequest->getVal( 'action', 'view' ), array( 'view', 'purge' ) ) ) { |
218 | 253 | return true; |
| 254 | + } |
| 255 | + |
219 | 256 | $status = self::GetUserStatus( $out->getTitle(), true ); |
220 | | - if( $status === null ) |
| 257 | + |
| 258 | + if( $status === null ) { |
221 | 259 | return true; |
| 260 | + } |
| 261 | + |
| 262 | + // For grep. Message keys used here: |
| 263 | + // onlinestatus-subtitle-offline, onlinestatus-subtitle-onfline |
222 | 264 | $out->setSubtitle( wfMsgExt( 'onlinestatus-subtitle-' . $status, array( 'parse' ) ) ); |
223 | 265 | |
224 | 266 | return true; |
— | — | @@ -228,10 +270,13 @@ |
229 | 271 | */ |
230 | 272 | static function PersonalUrls( &$urls, &$title ){ |
231 | 273 | global $wgUser, $wgUseAjax; |
| 274 | + |
232 | 275 | # Require ajax |
233 | 276 | if( !$wgUser->isLoggedIn() || !$wgUseAjax || $title->isSpecial( 'Preferences' ) ) |
234 | 277 | return true; |
| 278 | + |
235 | 279 | $arr = array(); |
| 280 | + |
236 | 281 | foreach( $urls as $key => $val ){ |
237 | 282 | if( $key == 'logout' ){ |
238 | 283 | $arr['status'] = array( |
— | — | @@ -240,8 +285,10 @@ |
241 | 286 | 'active' => false, |
242 | 287 | ); |
243 | 288 | } |
| 289 | + |
244 | 290 | $arr[$key] = $val; |
245 | 291 | } |
| 292 | + |
246 | 293 | $urls = $arr; |
247 | 294 | return true; |
248 | 295 | } |