Index: trunk/extensions/OnlineStatus/OnlineStatus.body.php |
— | — | @@ -0,0 +1,248 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +class OnlineStatus { |
| 5 | + /** |
| 6 | + * Get the user online status |
| 7 | + * |
| 8 | + * @param mixed $title string of Title object, if it's a title, if have to be in |
| 9 | + * User: of User_talk: namespace. |
| 10 | + * @return either bool or null |
| 11 | + */ |
| 12 | + static function GetUserStatus( $title, $checkShowPref = false ){ |
| 13 | + if( is_object( $title ) ){ |
| 14 | + if( !$title instanceof Title ) |
| 15 | + return null; |
| 16 | + if( !in_array( $title->getNamespace(), array( NS_USER, NS_USER_TALK ) ) ) |
| 17 | + return null; |
| 18 | + $username = explode( '/', $title->getDBkey() ); |
| 19 | + $username = $username[0]; |
| 20 | + } else { |
| 21 | + $username = $title; |
| 22 | + } |
| 23 | + $user = User::newFromName( $username ); |
| 24 | + if( !$user instanceof User || $user->getId() == 0 ) |
| 25 | + return null; |
| 26 | + if( $checkShowPref && !$user->getOption( 'showonline' ) ) |
| 27 | + return null; |
| 28 | + return $user->getOption( 'online' ); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Used for ajax requests |
| 33 | + */ |
| 34 | + static function Ajax( $action, $stat = false ){ |
| 35 | + global $wgUser; |
| 36 | + |
| 37 | + if( $wgUser->isAnon() ) |
| 38 | + return wfMsgHtml( 'onlinestatus-js-anon' ); |
| 39 | + |
| 40 | + switch( $action ){ |
| 41 | + case 'get': |
| 42 | + $def = $wgUser->getOption( 'online' ); |
| 43 | + $msg = wfMsgForContentNoTrans( 'onlinestatus-levels' ); |
| 44 | + $lines = explode( "\n", $msg ); |
| 45 | + $radios = array(); |
| 46 | + foreach( $lines as $line ){ |
| 47 | + if( substr( $line, 0, 1 ) != '*' ) |
| 48 | + continue; |
| 49 | + $lev = trim( $line, '* ' ); |
| 50 | + $radios[] = array( |
| 51 | + $lev, |
| 52 | + wfMsg( 'onlinestatus-toggle-' . $lev ), |
| 53 | + $lev == $def |
| 54 | + ); |
| 55 | + } |
| 56 | + return json_encode( $radios ); |
| 57 | + case 'set': |
| 58 | + if( $stat ){ |
| 59 | + $dbw = wfGetDB( DB_MASTER ); |
| 60 | + $dbw->begin(); |
| 61 | + $actual = $wgUser->getOption( 'online' ); |
| 62 | + $wgUser->setOption( 'online', $stat ); |
| 63 | + if( $actual != $stat ){ |
| 64 | + $wgUser->getUserPage()->invalidateCache(); |
| 65 | + $wgUser->getTalkPage()->invalidateCache(); |
| 66 | + } |
| 67 | + $wgUser->saveSettings(); |
| 68 | + $wgUser->invalidateCache(); |
| 69 | + $dbw->commit(); |
| 70 | + return wfMsgHtml( 'onlinestatus-js-changed', wfMsgHtml( 'onlinestatus-toggle-'.$stat ) ); |
| 71 | + } else { |
| 72 | + return wfMsgHtml( 'onlinestatus-js-error', $stat ); |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Hook for ParserFirstCallInit |
| 79 | + */ |
| 80 | + static function ParserFirstCallInit( $parser ){ |
| 81 | + global $wgAllowAnyUserOnlineStatusFunction; |
| 82 | + if( $wgAllowAnyUserOnlineStatusFunction ) |
| 83 | + $parser->setFunctionHook( 'anyuseronlinestatus', array( __CLASS__, 'ParserHookCallback' ) ); |
| 84 | + return true; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Callback for {{#anyuserstatus:}} |
| 89 | + */ |
| 90 | + static function ParserHookCallback( &$parser, $user, $raw = false ){ |
| 91 | + $status = self::GetUserStatus( $user ); |
| 92 | + if( $status === null ) |
| 93 | + return array( 'found' => false ); |
| 94 | + if( empty( $raw ) ){ |
| 95 | + return wfMsgNoTrans( 'onlinestatus-toggle-' . $status ); |
| 96 | + } else { |
| 97 | + return $status; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Hook function for MagicWordwgVariableIDs |
| 103 | + */ |
| 104 | + static function MagicWordVariable( &$magicWords ) { |
| 105 | + $magicWords[] = 'onlinestatus_word'; |
| 106 | + $magicWords[] = 'onlinestatus_word_raw'; |
| 107 | + return true; |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Hook function for ParserGetVariableValueSwitch |
| 112 | + */ |
| 113 | + static function ParserGetVariable( &$parser, &$varCache, &$index, &$ret ){ |
| 114 | + if( $index == 'onlinestatus_word' ){ |
| 115 | + $status = self::GetUserStatus( $parser->getTitle() ); |
| 116 | + if( $status === null ) |
| 117 | + return true; |
| 118 | + $ret = wfMsgNoTrans( 'onlinestatus-toggle-' . $status ); |
| 119 | + $varCache['onlinestatus'] = $ret; |
| 120 | + } else if( $index == 'onlinestatus_word_raw' ){ |
| 121 | + $status = self::GetUserStatus( $parser->getTitle() ); |
| 122 | + if( $status === null ) |
| 123 | + return true; |
| 124 | + $ret = $status; |
| 125 | + $varCache['onlinestatus'] = $ret; |
| 126 | + } |
| 127 | + return true; |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Hook for user preferences |
| 132 | + */ |
| 133 | + public static function GetPreferences( $user, &$preferences ) { |
| 134 | + $msg = wfMsgForContentNoTrans( 'onlinestatus-levels' ); |
| 135 | + $lines = explode( "\n", $msg ); |
| 136 | + $radios = array(); |
| 137 | + foreach( $lines as $line ){ |
| 138 | + if( substr( $line, 0, 1 ) != '*' ) |
| 139 | + continue; |
| 140 | + $lev = trim( $line, '* ' ); |
| 141 | + $radios[wfMsg( 'onlinestatus-toggle-' . $lev )] = $lev; |
| 142 | + } |
| 143 | + |
| 144 | + $preferences['onlineonlogin'] = |
| 145 | + array( |
| 146 | + 'type' => 'toggle', |
| 147 | + 'section' => 'misc', |
| 148 | + 'label-message' => 'onlinestatus-pref-onlineonlogin', |
| 149 | + ); |
| 150 | + |
| 151 | + $preferences['offlineonlogout'] = |
| 152 | + array( |
| 153 | + 'type' => 'toggle', |
| 154 | + 'section' => 'misc', |
| 155 | + 'label-message' => 'onlinestatus-pref-offlineonlogout', |
| 156 | + ); |
| 157 | + |
| 158 | + $prefs = array( |
| 159 | + 'online' => array( |
| 160 | + 'type' => 'radio', |
| 161 | + 'section' => 'personal/info', |
| 162 | + 'options' => $radios, |
| 163 | + 'label-message' => 'onlinestatus-toggles-desc', |
| 164 | + ), |
| 165 | + 'showonline' => array( |
| 166 | + 'type' => 'check', |
| 167 | + 'section' => 'personal/info', |
| 168 | + 'label-message' => 'onlinestatus-toggles-show', |
| 169 | + 'help-message' => 'onlinestatus-toggles-explain', |
| 170 | + ) |
| 171 | + ); |
| 172 | + |
| 173 | + $after = array_key_exists( 'registrationdate', $preferences ) ? 'registrationdate' : 'editcount'; |
| 174 | + $preferences = wfArrayInsertAfter( $preferences, $prefs, $after ); |
| 175 | + |
| 176 | + return true; |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * Hook for UserLoginComplete |
| 181 | + */ |
| 182 | + static function UserLoginComplete( $user ){ |
| 183 | + if( $user->getOption( 'onlineonlogin' ) ){ |
| 184 | + $user->setOption( 'online', 'online' ); |
| 185 | + $user->saveSettings(); |
| 186 | + } |
| 187 | + return true; |
| 188 | + } |
| 189 | + |
| 190 | + /** |
| 191 | + * Hook for UserLoginComplete |
| 192 | + */ |
| 193 | + static function UserLogoutComplete( &$newUser, &$injected_html, $oldName = null ){ |
| 194 | + if( $oldName === null ) |
| 195 | + return true; |
| 196 | + $oldUser = User::newFromName( $oldName ); |
| 197 | + if( !$oldUser instanceof User ) |
| 198 | + return true; |
| 199 | + if( $oldUser->getOption( 'offlineonlogout' ) ){ |
| 200 | + $oldUser->setOption( 'online', 'offline' ); |
| 201 | + $oldUser->saveSettings(); |
| 202 | + } |
| 203 | + return true; |
| 204 | + } |
| 205 | + |
| 206 | + /** |
| 207 | + * Hook function for BeforePageDisplay |
| 208 | + */ |
| 209 | + static function BeforePageDisplay( &$out ){ |
| 210 | + global $wgRequest, $wgUser; |
| 211 | + global $wgUseAjax; |
| 212 | + |
| 213 | + if( $wgUser->isLoggedIn() && $wgUseAjax ){ |
| 214 | + $out->addModules( 'ext.onlineStatus' ); |
| 215 | + } |
| 216 | + |
| 217 | + if( !in_array( $wgRequest->getVal( 'action', 'view' ), array( 'view', 'purge' ) ) ) |
| 218 | + return true; |
| 219 | + $status = self::GetUserStatus( $out->getTitle(), true ); |
| 220 | + if( $status === null ) |
| 221 | + return true; |
| 222 | + $out->setSubtitle( wfMsgExt( 'onlinestatus-subtitle-' . $status, array( 'parse' ) ) ); |
| 223 | + |
| 224 | + return true; |
| 225 | + } |
| 226 | + |
| 227 | + /** |
| 228 | + * Hook for PersonalUrls |
| 229 | + */ |
| 230 | + static function PersonalUrls( &$urls, &$title ){ |
| 231 | + global $wgUser, $wgUseAjax; |
| 232 | + # Require ajax |
| 233 | + if( !$wgUser->isLoggedIn() || !$wgUseAjax || $title->isSpecial( 'Preferences' ) ) |
| 234 | + return true; |
| 235 | + $arr = array(); |
| 236 | + foreach( $urls as $key => $val ){ |
| 237 | + if( $key == 'logout' ){ |
| 238 | + $arr['status'] = array( |
| 239 | + 'text' => wfMsgHtml( 'onlinestatus-tab' ), |
| 240 | + 'href' => 'javascript:;', |
| 241 | + 'active' => false, |
| 242 | + ); |
| 243 | + } |
| 244 | + $arr[$key] = $val; |
| 245 | + } |
| 246 | + $urls = $arr; |
| 247 | + return true; |
| 248 | + } |
| 249 | +} |
Property changes on: trunk/extensions/OnlineStatus/OnlineStatus.body.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 250 | + native |
Added: svn:keywords |
2 | 251 | + LastChangedDate LastChangedRevision |
Index: trunk/extensions/OnlineStatus/OnlineStatus.php |
— | — | @@ -37,6 +37,9 @@ |
38 | 38 | $wgDefaultUserOptions['onlineonlogin'] = 1; |
39 | 39 | $wgDefaultUserOptions['offlineonlogout'] = 1; |
40 | 40 | |
| 41 | +// Classes |
| 42 | +$wgAutoloadClasses['OnlineStatus'] = dirname( __FILE__ ) . '/OnlineStatus.body.php'; |
| 43 | + |
41 | 44 | // Add messages files |
42 | 45 | $wgExtensionMessagesFiles['OnlineStatus'] = dirname( __FILE__ ) . '/OnlineStatus.i18n.php'; |
43 | 46 | $wgExtensionMessagesFiles['OnlineStatusMagic'] = dirname( __FILE__ ) . '/OnlineStatus.i18n.magic.php'; |
— | — | @@ -66,251 +69,3 @@ |
67 | 70 | 'scripts' => 'extensions/OnlineStatus/OnlineStatus.js', |
68 | 71 | 'styles' => 'extensions/OnlineStatus/OnlineStatus.css', |
69 | 72 | ); |
70 | | - |
71 | | -// FIXME: Should be a separate class file |
72 | | -class OnlineStatus { |
73 | | - /** |
74 | | - * Get the user online status |
75 | | - * |
76 | | - * @param mixed $title string of Title object, if it's a title, if have to be in |
77 | | - * User: of User_talk: namespace. |
78 | | - * @return either bool or null |
79 | | - */ |
80 | | - static function GetUserStatus( $title, $checkShowPref = false ){ |
81 | | - if( is_object( $title ) ){ |
82 | | - if( !$title instanceof Title ) |
83 | | - return null; |
84 | | - if( !in_array( $title->getNamespace(), array( NS_USER, NS_USER_TALK ) ) ) |
85 | | - return null; |
86 | | - $username = explode( '/', $title->getDBkey() ); |
87 | | - $username = $username[0]; |
88 | | - } else { |
89 | | - $username = $title; |
90 | | - } |
91 | | - $user = User::newFromName( $username ); |
92 | | - if( !$user instanceof User || $user->getId() == 0 ) |
93 | | - return null; |
94 | | - if( $checkShowPref && !$user->getOption( 'showonline' ) ) |
95 | | - return null; |
96 | | - return $user->getOption( 'online' ); |
97 | | - } |
98 | | - |
99 | | - /** |
100 | | - * Used for ajax requests |
101 | | - */ |
102 | | - static function Ajax( $action, $stat = false ){ |
103 | | - global $wgUser; |
104 | | - |
105 | | - if( $wgUser->isAnon() ) |
106 | | - return wfMsgHtml( 'onlinestatus-js-anon' ); |
107 | | - |
108 | | - switch( $action ){ |
109 | | - case 'get': |
110 | | - $def = $wgUser->getOption( 'online' ); |
111 | | - $msg = wfMsgForContentNoTrans( 'onlinestatus-levels' ); |
112 | | - $lines = explode( "\n", $msg ); |
113 | | - $radios = array(); |
114 | | - foreach( $lines as $line ){ |
115 | | - if( substr( $line, 0, 1 ) != '*' ) |
116 | | - continue; |
117 | | - $lev = trim( $line, '* ' ); |
118 | | - $radios[] = array( |
119 | | - $lev, |
120 | | - wfMsg( 'onlinestatus-toggle-' . $lev ), |
121 | | - $lev == $def |
122 | | - ); |
123 | | - } |
124 | | - return json_encode( $radios ); |
125 | | - case 'set': |
126 | | - if( $stat ){ |
127 | | - $dbw = wfGetDB( DB_MASTER ); |
128 | | - $dbw->begin(); |
129 | | - $actual = $wgUser->getOption( 'online' ); |
130 | | - $wgUser->setOption( 'online', $stat ); |
131 | | - if( $actual != $stat ){ |
132 | | - $wgUser->getUserPage()->invalidateCache(); |
133 | | - $wgUser->getTalkPage()->invalidateCache(); |
134 | | - } |
135 | | - $wgUser->saveSettings(); |
136 | | - $wgUser->invalidateCache(); |
137 | | - $dbw->commit(); |
138 | | - return wfMsgHtml( 'onlinestatus-js-changed', wfMsgHtml( 'onlinestatus-toggle-'.$stat ) ); |
139 | | - } else { |
140 | | - return wfMsgHtml( 'onlinestatus-js-error', $stat ); |
141 | | - } |
142 | | - } |
143 | | - } |
144 | | - |
145 | | - /** |
146 | | - * Hook for ParserFirstCallInit |
147 | | - */ |
148 | | - static function ParserFirstCallInit( $parser ){ |
149 | | - global $wgAllowAnyUserOnlineStatusFunction; |
150 | | - if( $wgAllowAnyUserOnlineStatusFunction ) |
151 | | - $parser->setFunctionHook( 'anyuseronlinestatus', array( __CLASS__, 'ParserHookCallback' ) ); |
152 | | - return true; |
153 | | - } |
154 | | - |
155 | | - /** |
156 | | - * Callback for {{#anyuserstatus:}} |
157 | | - */ |
158 | | - static function ParserHookCallback( &$parser, $user, $raw = false ){ |
159 | | - $status = self::GetUserStatus( $user ); |
160 | | - if( $status === null ) |
161 | | - return array( 'found' => false ); |
162 | | - if( empty( $raw ) ){ |
163 | | - return wfMsgNoTrans( 'onlinestatus-toggle-' . $status ); |
164 | | - } else { |
165 | | - return $status; |
166 | | - } |
167 | | - } |
168 | | - |
169 | | - /** |
170 | | - * Hook function for MagicWordwgVariableIDs |
171 | | - */ |
172 | | - static function MagicWordVariable( &$magicWords ) { |
173 | | - $magicWords[] = 'onlinestatus_word'; |
174 | | - $magicWords[] = 'onlinestatus_word_raw'; |
175 | | - return true; |
176 | | - } |
177 | | - |
178 | | - /** |
179 | | - * Hook function for ParserGetVariableValueSwitch |
180 | | - */ |
181 | | - static function ParserGetVariable( &$parser, &$varCache, &$index, &$ret ){ |
182 | | - if( $index == 'onlinestatus_word' ){ |
183 | | - $status = self::GetUserStatus( $parser->getTitle() ); |
184 | | - if( $status === null ) |
185 | | - return true; |
186 | | - $ret = wfMsgNoTrans( 'onlinestatus-toggle-' . $status ); |
187 | | - $varCache['onlinestatus'] = $ret; |
188 | | - } else if( $index == 'onlinestatus_word_raw' ){ |
189 | | - $status = self::GetUserStatus( $parser->getTitle() ); |
190 | | - if( $status === null ) |
191 | | - return true; |
192 | | - $ret = $status; |
193 | | - $varCache['onlinestatus'] = $ret; |
194 | | - } |
195 | | - return true; |
196 | | - } |
197 | | - |
198 | | - /** |
199 | | - * Hook for user preferences |
200 | | - */ |
201 | | - public static function GetPreferences( $user, &$preferences ) { |
202 | | - $msg = wfMsgForContentNoTrans( 'onlinestatus-levels' ); |
203 | | - $lines = explode( "\n", $msg ); |
204 | | - $radios = array(); |
205 | | - foreach( $lines as $line ){ |
206 | | - if( substr( $line, 0, 1 ) != '*' ) |
207 | | - continue; |
208 | | - $lev = trim( $line, '* ' ); |
209 | | - $radios[wfMsg( 'onlinestatus-toggle-' . $lev )] = $lev; |
210 | | - } |
211 | | - |
212 | | - $preferences['onlineonlogin'] = |
213 | | - array( |
214 | | - 'type' => 'toggle', |
215 | | - 'section' => 'misc', |
216 | | - 'label-message' => 'onlinestatus-pref-onlineonlogin', |
217 | | - ); |
218 | | - |
219 | | - $preferences['offlineonlogout'] = |
220 | | - array( |
221 | | - 'type' => 'toggle', |
222 | | - 'section' => 'misc', |
223 | | - 'label-message' => 'onlinestatus-pref-offlineonlogout', |
224 | | - ); |
225 | | - |
226 | | - $prefs = array( |
227 | | - 'online' => array( |
228 | | - 'type' => 'radio', |
229 | | - 'section' => 'personal/info', |
230 | | - 'options' => $radios, |
231 | | - 'label-message' => 'onlinestatus-toggles-desc', |
232 | | - ), |
233 | | - 'showonline' => array( |
234 | | - 'type' => 'check', |
235 | | - 'section' => 'personal/info', |
236 | | - 'label-message' => 'onlinestatus-toggles-show', |
237 | | - 'help-message' => 'onlinestatus-toggles-explain', |
238 | | - ) |
239 | | - ); |
240 | | - |
241 | | - $after = array_key_exists( 'registrationdate', $preferences ) ? 'registrationdate' : 'editcount'; |
242 | | - $preferences = wfArrayInsertAfter( $preferences, $prefs, $after ); |
243 | | - |
244 | | - return true; |
245 | | - } |
246 | | - |
247 | | - /** |
248 | | - * Hook for UserLoginComplete |
249 | | - */ |
250 | | - static function UserLoginComplete( $user ){ |
251 | | - if( $user->getOption( 'onlineonlogin' ) ){ |
252 | | - $user->setOption( 'online', 'online' ); |
253 | | - $user->saveSettings(); |
254 | | - } |
255 | | - return true; |
256 | | - } |
257 | | - |
258 | | - /** |
259 | | - * Hook for UserLoginComplete |
260 | | - */ |
261 | | - static function UserLogoutComplete( &$newUser, &$injected_html, $oldName = null ){ |
262 | | - if( $oldName === null ) |
263 | | - return true; |
264 | | - $oldUser = User::newFromName( $oldName ); |
265 | | - if( !$oldUser instanceof User ) |
266 | | - return true; |
267 | | - if( $oldUser->getOption( 'offlineonlogout' ) ){ |
268 | | - $oldUser->setOption( 'online', 'offline' ); |
269 | | - $oldUser->saveSettings(); |
270 | | - } |
271 | | - return true; |
272 | | - } |
273 | | - |
274 | | - /** |
275 | | - * Hook function for BeforePageDisplay |
276 | | - */ |
277 | | - static function BeforePageDisplay( &$out ){ |
278 | | - global $wgRequest, $wgUser; |
279 | | - global $wgUseAjax; |
280 | | - |
281 | | - if( $wgUser->isLoggedIn() && $wgUseAjax ){ |
282 | | - $out->addModules( 'ext.onlineStatus' ); |
283 | | - } |
284 | | - |
285 | | - if( !in_array( $wgRequest->getVal( 'action', 'view' ), array( 'view', 'purge' ) ) ) |
286 | | - return true; |
287 | | - $status = self::GetUserStatus( $out->getTitle(), true ); |
288 | | - if( $status === null ) |
289 | | - return true; |
290 | | - $out->setSubtitle( wfMsgExt( 'onlinestatus-subtitle-' . $status, array( 'parse' ) ) ); |
291 | | - |
292 | | - return true; |
293 | | - } |
294 | | - |
295 | | - /** |
296 | | - * Hook for PersonalUrls |
297 | | - */ |
298 | | - static function PersonalUrls( &$urls, &$title ){ |
299 | | - global $wgUser, $wgUseAjax; |
300 | | - # Require ajax |
301 | | - if( !$wgUser->isLoggedIn() || !$wgUseAjax || $title->isSpecial( 'Preferences' ) ) |
302 | | - return true; |
303 | | - $arr = array(); |
304 | | - foreach( $urls as $key => $val ){ |
305 | | - if( $key == 'logout' ){ |
306 | | - $arr['status'] = array( |
307 | | - 'text' => wfMsgHtml( 'onlinestatus-tab' ), |
308 | | - 'href' => 'javascript:;', |
309 | | - 'active' => false, |
310 | | - ); |
311 | | - } |
312 | | - $arr[$key] = $val; |
313 | | - } |
314 | | - $urls = $arr; |
315 | | - return true; |
316 | | - } |
317 | | -} |