r95996 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r95995‎ | r95996 | r95997 >
Date:15:19, 1 September 2011
Author:siebrand
Status:ok
Tags:
Comment:
Comments updated/added.
Whitespace and formatting updates.
Modified paths:
  • /trunk/extensions/OnlineStatus/OnlineStatus.body.php (modified) (history)

Diff [purge]

Index: trunk/extensions/OnlineStatus/OnlineStatus.body.php
@@ -4,37 +4,48 @@
55 /**
66 * Get the user online status
77 *
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
99 * User: of User_talk: namespace.
1010 * @return either bool or null
1111 */
1212 static function GetUserStatus( $title, $checkShowPref = false ){
1313 if( is_object( $title ) ){
14 - if( !$title instanceof Title )
 14+ if( !$title instanceof Title ) {
1515 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 ) ) ) {
1719 return null;
 20+ }
 21+
1822 $username = explode( '/', $title->getDBkey() );
1923 $username = $username[0];
2024 } else {
2125 $username = $title;
2226 }
 27+
2328 $user = User::newFromName( $username );
24 - if( !$user instanceof User || $user->getId() == 0 )
 29+
 30+ if( !$user instanceof User || $user->getId() == 0 ) {
2531 return null;
26 - if( $checkShowPref && !$user->getOption( 'showonline' ) )
 32+ }
 33+
 34+ if( $checkShowPref && !$user->getOption( 'showonline' ) ) {
2735 return null;
 36+ }
 37+
2838 return $user->getOption( 'online' );
2939 }
3040
3141 /**
32 - * Used for ajax requests
 42+ * Used for AJAX requests
3343 */
3444 static function Ajax( $action, $stat = false ){
3545 global $wgUser;
3646
37 - if( $wgUser->isAnon() )
 47+ if( $wgUser->isAnon() ) {
3848 return wfMsgHtml( 'onlinestatus-js-anon' );
 49+ }
3950
4051 switch( $action ){
4152 case 'get':
@@ -42,9 +53,12 @@
4354 $msg = wfMsgForContentNoTrans( 'onlinestatus-levels' );
4455 $lines = explode( "\n", $msg );
4556 $radios = array();
 57+
4658 foreach( $lines as $line ){
47 - if( substr( $line, 0, 1 ) != '*' )
 59+ if( substr( $line, 0, 1 ) != '*' ) {
4860 continue;
 61+ }
 62+
4963 $lev = trim( $line, '* ' );
5064 $radios[] = array(
5165 $lev,
@@ -52,6 +66,7 @@
5367 $lev == $def
5468 );
5569 }
 70+
5671 return json_encode( $radios );
5772 case 'set':
5873 if( $stat ){
@@ -59,6 +74,7 @@
6075 $dbw->begin();
6176 $actual = $wgUser->getOption( 'online' );
6277 $wgUser->setOption( 'online', $stat );
 78+
6379 if( $actual != $stat ){
6480 $wgUser->getUserPage()->invalidateCache();
6581 $wgUser->getTalkPage()->invalidateCache();
@@ -66,6 +82,7 @@
6783 $wgUser->saveSettings();
6884 $wgUser->invalidateCache();
6985 $dbw->commit();
 86+
7087 return wfMsgHtml( 'onlinestatus-js-changed', wfMsgHtml( 'onlinestatus-toggle-'.$stat ) );
7188 } else {
7289 return wfMsgHtml( 'onlinestatus-js-error', $stat );
@@ -78,6 +95,7 @@
7996 */
8097 static function ParserFirstCallInit( $parser ){
8198 global $wgAllowAnyUserOnlineStatusFunction;
 99+
82100 if( $wgAllowAnyUserOnlineStatusFunction )
83101 $parser->setFunctionHook( 'anyuseronlinestatus', array( __CLASS__, 'ParserHookCallback' ) );
84102 return true;
@@ -88,8 +106,10 @@
89107 */
90108 static function ParserHookCallback( &$parser, $user, $raw = false ){
91109 $status = self::GetUserStatus( $user );
 110+
92111 if( $status === null )
93112 return array( 'found' => false );
 113+
94114 if( empty( $raw ) ){
95115 return wfMsgNoTrans( 'onlinestatus-toggle-' . $status );
96116 } else {
@@ -103,6 +123,7 @@
104124 static function MagicWordVariable( &$magicWords ) {
105125 $magicWords[] = 'onlinestatus_word';
106126 $magicWords[] = 'onlinestatus_word_raw';
 127+
107128 return true;
108129 }
109130
@@ -112,17 +133,24 @@
113134 static function ParserGetVariable( &$parser, &$varCache, &$index, &$ret ){
114135 if( $index == 'onlinestatus_word' ){
115136 $status = self::GetUserStatus( $parser->getTitle() );
116 - if( $status === null )
 137+
 138+ if( $status === null ) {
117139 return true;
 140+ }
 141+
118142 $ret = wfMsgNoTrans( 'onlinestatus-toggle-' . $status );
119143 $varCache['onlinestatus'] = $ret;
120144 } elseif( $index == 'onlinestatus_word_raw' ){
121145 $status = self::GetUserStatus( $parser->getTitle() );
122 - if( $status === null )
 146+
 147+ if( $status === null ) {
123148 return true;
 149+ }
 150+
124151 $ret = $status;
125152 $varCache['onlinestatus'] = $ret;
126153 }
 154+
127155 return true;
128156 }
129157
@@ -133,6 +161,7 @@
134162 $msg = wfMsgForContentNoTrans( 'onlinestatus-levels' );
135163 $lines = explode( "\n", $msg );
136164 $radios = array();
 165+
137166 foreach( $lines as $line ){
138167 if( substr( $line, 0, 1 ) != '*' )
139168 continue;
@@ -183,6 +212,7 @@
184213 $user->setOption( 'online', 'online' );
185214 $user->saveSettings();
186215 }
 216+
187217 return true;
188218 }
189219
@@ -190,15 +220,21 @@
191221 * Hook for UserLoginComplete
192222 */
193223 static function UserLogoutComplete( &$newUser, &$injected_html, $oldName = null ){
194 - if( $oldName === null )
 224+ if( $oldName === null ) {
195225 return true;
 226+ }
 227+
196228 $oldUser = User::newFromName( $oldName );
197 - if( !$oldUser instanceof User )
 229+
 230+ if( !$oldUser instanceof User ) {
198231 return true;
 232+ }
 233+
199234 if( $oldUser->getOption( 'offlineonlogout' ) ){
200235 $oldUser->setOption( 'online', 'offline' );
201236 $oldUser->saveSettings();
202237 }
 238+
203239 return true;
204240 }
205241
@@ -206,18 +242,24 @@
207243 * Hook function for BeforePageDisplay
208244 */
209245 static function BeforePageDisplay( &$out ){
210 - global $wgRequest, $wgUser;
211 - global $wgUseAjax;
 246+ global $wgRequest, $wgUser, $wgUseAjax;
212247
213248 if( $wgUser->isLoggedIn() && $wgUseAjax ){
214249 $out->addModules( 'ext.onlineStatus' );
215250 }
216251
217 - if( !in_array( $wgRequest->getVal( 'action', 'view' ), array( 'view', 'purge' ) ) )
 252+ if( !in_array( $wgRequest->getVal( 'action', 'view' ), array( 'view', 'purge' ) ) ) {
218253 return true;
 254+ }
 255+
219256 $status = self::GetUserStatus( $out->getTitle(), true );
220 - if( $status === null )
 257+
 258+ if( $status === null ) {
221259 return true;
 260+ }
 261+
 262+ // For grep. Message keys used here:
 263+ // onlinestatus-subtitle-offline, onlinestatus-subtitle-onfline
222264 $out->setSubtitle( wfMsgExt( 'onlinestatus-subtitle-' . $status, array( 'parse' ) ) );
223265
224266 return true;
@@ -228,10 +270,13 @@
229271 */
230272 static function PersonalUrls( &$urls, &$title ){
231273 global $wgUser, $wgUseAjax;
 274+
232275 # Require ajax
233276 if( !$wgUser->isLoggedIn() || !$wgUseAjax || $title->isSpecial( 'Preferences' ) )
234277 return true;
 278+
235279 $arr = array();
 280+
236281 foreach( $urls as $key => $val ){
237282 if( $key == 'logout' ){
238283 $arr['status'] = array(
@@ -240,8 +285,10 @@
241286 'active' => false,
242287 );
243288 }
 289+
244290 $arr[$key] = $val;
245291 }
 292+
246293 $urls = $arr;
247294 return true;
248295 }

Status & tagging log