r101137 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r101136‎ | r101137 | r101138 >
Date:15:12, 28 October 2011
Author:johnduhart
Status:deferred
Tags:
Comment:
Code improvements and cleanup to OnlineStatusBar
Modified paths:
  • /trunk/extensions/OnlineStatusBar/OnlineStatusBar.body.php (modified) (history)
  • /trunk/extensions/OnlineStatusBar/OnlineStatusBar.i18n.php (modified) (history)
  • /trunk/extensions/OnlineStatusBar/OnlineStatusBar.php (modified) (history)
  • /trunk/extensions/OnlineStatusBar/OnlineStatusBarHooks.php (modified) (history)

Diff [purge]

Index: trunk/extensions/OnlineStatusBar/OnlineStatusBar.body.php
@@ -17,48 +17,80 @@
1818
1919 class OnlineStatusBar {
2020
21 - public static function Get_Html( $text, $mode ) {
22 - global $wgOnlineStatusBarColor, $wgOnlineStatusBarY;
23 - $color = $wgOnlineStatusBarColor[$mode];
 21+ public static function getStatusBarHtml( $text ) {
2422 return <<<HTML
2523 <div class="onlinebar metadata topiconstatus" id="status-top">
2624 <div class="statusbaricon">
2725 $text</div></div>
2826 HTML;
2927 }
30 -
 28+
3129 /**
32 - * Returns image
33 - * @param $mode User
 30+ * Returns image element
 31+ *
 32+ * @param $mode String
3433 * @return string
3534 */
3635 public static function GetImageHtml( $mode ) {
3736 global $wgExtensionAssetsPath, $wgOnlineStatusBarIcon, $wgOnlineStatusBarModes;
3837 $icon = "$wgExtensionAssetsPath/OnlineStatusBar/{$wgOnlineStatusBarIcon[$mode]}";
39 - $modeText = $wgOnlineStatusBarModes[$mode];
4038 return Html::element( 'img', array( 'src' => $icon ) );
4139 }
4240
4341 /**
44 - * @param $title Title
45 - * @return user
 42+ * Returns the status and User element
 43+ *
 44+ * @param Title $title
 45+ * @return array|bool Array containing the status and User object
4646 */
47 - public static function GetOwnerFromTitle ( $title )
48 - {
49 - if ( $title === null ) {
50 - return null;
 47+ public static function getUserInfoFromTitle( Title $title ) {
 48+ if ( $title->getNamespace() != NS_USER && $title->getNamespace() != NS_USER_TALK ) {
 49+ return false;
5150 }
52 - $username = $title->getBaseText();
53 - $user_object = User::newFromName ( $username );
54 - return $user_object;
 51+
 52+ $user = User::newFromName( $title->getBaseText() );
 53+ // Invalid user
 54+ if ( $user === false ) {
 55+ return false;
 56+ }
 57+
 58+ if ( !self::isValid( $user ) ) {
 59+ return false;
 60+ }
 61+
 62+ $status = self::getStatus( $user );
 63+
 64+ return array( $status, $user );
5565 }
5666
 67+ public static function getStatus( $user ) {
 68+ global $wgOnlineStatusBarDefaultOffline, $wgOnlineStatusBarDefaultOnline;
 69+
 70+ $dbr = wfGetDB( DB_SLAVE );
 71+ $result = $dbr->selectField( 'online_status', 'username', array( 'username' => $user->getName() ),
 72+ __METHOD__, array( 'LIMIT 1', 'ORDER BY timestamp DESC' ) );
 73+
 74+ if ( $result === false ) {
 75+ $status = $wgOnlineStatusBarDefaultOffline;
 76+ } else {
 77+ $status = $user->getOption( 'OnlineStatusBar_status', $wgOnlineStatusBarDefaultOnline );
 78+ }
 79+
 80+ if ( $status == 'hidden' ) {
 81+ $status = 'offline';
 82+ }
 83+
 84+ return $status;
 85+ }
 86+
5787 /**
5888 * @return bool
5989 */
6090 public static function UpdateDb() {
6191 global $wgUser, $wgOnlineStatusBarDefaultOnline;
62 - if ( OnlineStatusBar::GetStatus( $wgUser->getName() ) != $wgOnlineStatusBarDefaultOnline ) {
 92+ // TODO: This means that if the current status isn't online we insert a
 93+ // new row each request. yuck.
 94+ if ( OnlineStatusBar::GetStatus( $wgUser ) != $wgOnlineStatusBarDefaultOnline ) {
6395 $dbw = wfGetDB( DB_MASTER );
6496 $row = array(
6597 'username' => $wgUser->getName(),
@@ -74,10 +106,11 @@
75107 */
76108 public static function UpdateStatus() {
77109 global $wgUser, $wgOnlineStatusBarDefaultOffline;
78 - if ( OnlineStatusBar::GetStatus( $wgUser->getName() ) == $wgOnlineStatusBarDefaultOffline ) {
 110+ if ( OnlineStatusBar::GetStatus( $wgUser ) == $wgOnlineStatusBarDefaultOffline ) {
79111 OnlineStatusBar::UpdateDb();
80112 return true;
81113 }
 114+
82115 $dbw = wfGetDB( DB_MASTER );
83116 $dbw->update(
84117 'online_status',
@@ -85,6 +118,7 @@
86119 array( 'username' => $wgUser->getID() ),
87120 __METHOD__
88121 );
 122+
89123 return false;
90124 }
91125
@@ -92,9 +126,10 @@
93127 * @return int
94128 */
95129 public static function DeleteOld() {
96 - global $wgOnlineStatusBar_LogoutTime, $wgDBname;
 130+ global $wgOnlineStatusBar_LogoutTime;
97131 $dbw = wfGetDB( DB_MASTER );
98132 $time = wfTimestamp( TS_UNIX ) - $wgOnlineStatusBar_LogoutTime;
 133+ // FIXME: This looks wrong:
99134 $time = $dbw->addQuotes( $dbw->timestamp( $time ) - $wgOnlineStatusBar_LogoutTime );
100135 $dbw->delete( 'online_status', array( "timestamp < $time" ), __METHOD__ );
101136 return 0;
@@ -102,20 +137,19 @@
103138
104139
105140 /**
106 - * @param $userName string
 141+ * Checks to see if the user can be tracked
 142+ *
 143+ * @param User $user
107144 * @return bool
108145 */
109 - public static function IsValid( $userName ) {
110 - global $wgOnlineStatusBarDefaultIpUsers, $wgOnlineStatusBarDefaultEnabled;
 146+ public static function isValid( User $user ) {
 147+ global $wgOnlineStatusBarTrackIpUsers, $wgOnlineStatusBarDefaultEnabled;
 148+
111149 // checks if anon
112 - if ( User::isIP( $userName ) ) {
113 - return $wgOnlineStatusBarDefaultIpUsers;
 150+ if ( $user->isAnon() ) {
 151+ return $wgOnlineStatusBarTrackIpUsers;
114152 }
115 - $user = User::newFromName( $userName );
116 - // check if exist
117 - if ( $user == null ) {
118 - return false;
119 - }
 153+
120154 if ( $user->getId() == 0 )
121155 {
122156 return false;
@@ -126,30 +160,6 @@
127161
128162 /**
129163 * @param $userName string
130 - * @return string
131 - */
132 - static function GetStatus( $userName ) {
133 - global $wgOnlineStatusBarModes, $wgOnlineStatusBarDefaultOffline, $wgOnlineStatusBarDefaultIpUsers, $wgOnlineStatusBarDefaultOnline, $wgDBname;
134 - $dbw = wfGetDB( DB_MASTER );
135 - OnlineStatusBar::DeleteOld();
136 - $user = User::newFromName( $userName );
137 - if ( $user == null && User::isIP ( $userName ) != true ) {
138 - // something is wrong
139 - return $wgOnlineStatusBarDefaultOffline;
140 - }
141 - $result = $dbw->selectField( 'online_status', 'username', array( 'username' => $userName ),
142 - __METHOD__, array( 'limit 1', 'order by timestamp desc' ) );
143 - if ( $result && $user != null ) {
144 - return $user->getOption( "OnlineStatusBar_status", $wgOnlineStatusDefaultOnline );
145 - } else if ( $result && User::isIP ( $userName ) && $wgOnlineStatusBarDefaultIpUsers ) {
146 - // user is anon
147 - return $wgOnlineStatusBarDefaultOnline;
148 - }
149 - return $wgOnlineStatusBarDefaultOffline;
150 - }
151 -
152 - /**
153 - * @param $userName string
154164 * @return bool
155165 */
156166 static function DeleteStatus( $userName ) {
Index: trunk/extensions/OnlineStatusBar/OnlineStatusBar.i18n.php
@@ -11,6 +11,7 @@
1212 /**
1313 * English
1414 * @author Petr Bena
 15+ * @author John Du Hart
1516 */
1617 $messages['en'] = array(
1718 // Description
@@ -25,9 +26,18 @@
2627 'prefs-onlinestatus' => 'Online status',
2728 // Message in config
2829 'onlinestatusbar-hide' => 'Do you want to hide the status bar in order to use just the magic word? (For advanced users)',
 30+
 31+ 'onlinestatusbar-status-online' => 'On-line',
 32+ 'onlinestatusbar-status-busy' => 'Busy',
 33+ 'onlinestatusbar-status-away' => 'Away',
 34+ 'onlinestatusbar-status-offline' => 'Offline',
 35+ 'onlinestatusbar-status-hidden' => 'Hidden',
2936 );
3037
31 -/** Message documentation (Message documentation) */
 38+/** Message documentation (Message documentation)
 39+ * @author Petr Bena
 40+ * @author John Du Hart
 41+ */
3242 $messages['qqq'] = array(
3343 'onlinestatusbar-desc' => '{{desc}}',
3444 'onlinestatusbar-line' => 'Status bar text line (User is now Offline), parameters:
@@ -38,6 +48,12 @@
3949 'onlinestatusbar-status' => 'Message in config asking what status they want to use, option box',
4050 'prefs-onlinestatus' => 'Section for config, located in preferences - misc',
4151 'onlinestatusbar-hide' => 'Ask user if they want to hide status bar this is useful when they are using custom template but need to check if they are online',
 52+
 53+ 'onlinestatusbar-status-online' => 'Status for users who mark themselves as active',
 54+ 'onlinestatusbar-status-busy' => 'Status for users who mark themselves as busy',
 55+ 'onlinestatusbar-status-away' => 'Status for users who mark themselves as away',
 56+ 'onlinestatusbar-status-offline' => 'Status for users who are offline',
 57+ 'onlinestatusbar-status-hidden' => 'Status for users who mark themselves as hidden (used on preferences only)',
4258 );
4359
4460 /** German (Deutsch)
Index: trunk/extensions/OnlineStatusBar/OnlineStatusBar.php
@@ -37,13 +37,6 @@
3838
3939 // Configuration
4040 // Those values can be overriden in LocalSettings, do not change it here
41 -$wgOnlineStatusBarModes = array(
42 - 'online' => "On-line",
43 - 'busy' => "Busy",
44 - 'away' => "Away",
45 - 'hidden' => "Offline",
46 - 'offline' => "Offline",
47 -);
4841 $wgOnlineStatusBarIcon = array(
4942 'online' => "statusgreen.png",
5043 'busy' => "statusorange.png",
@@ -51,16 +44,9 @@
5245 'hidden' => "statusred.png",
5346 'offline' => "statusred.png",
5447 );
55 -$wgOnlineStatusBarColor = array(
56 - 'online' => "green",
57 - 'busy' => "orange",
58 - 'away' => "orange",
59 - 'hidden' => "red",
60 - 'offline' => "red",
61 -);
6248
6349 // default for anonymous and uknown users
64 -$wgOnlineStatusBarDefaultIpUsers = false;
 50+$wgOnlineStatusBarTrackIpUsers = false;
6551 // default for online
6652 $wgOnlineStatusBarDefaultOnline = "online";
6753 // default for offline
Index: trunk/extensions/OnlineStatusBar/OnlineStatusBarHooks.php
@@ -7,11 +7,11 @@
88 */
99 class OnlineStatusBarHooks {
1010 /**
11 - * @param DatabaseUpdate|null $updater
 11+ * @param DatabaseUpdater|null $updater
1212 * @return bool
1313 */
1414 public static function ckSchema( $updater = null ) {
15 - if ( $updater !== null ){
 15+ if ( $updater !== null ) {
1616 $updater->addExtensionUpdate( array( 'addtable', 'online_status', dirname( __FILE__ ) . '/OnlineStatusBar.sql', true ) );
1717 } else {
1818 global $wgExtNewTables;
@@ -47,48 +47,35 @@
4848 * @return bool
4949 */
5050 public static function renderBar( &$article, &$outputDone, &$pcache ) {
51 - global $messages, $wgOnlineStatusBarDefaultIpUsers, $wgOnlineStatusBarModes, $wgOut;
 51+ $context = $article->getContext();
 52+
5253 OnlineStatusBar::UpdateStatus();
53 - // anonymous user
54 - $anon = false;
55 - $ns = $article->getTitle()->getNamespace();
56 - if ( ( $ns == NS_USER_TALK ) || ( $ns == NS_USER ) ) {
57 - $user = OnlineStatusBar::GetOwnerFromTitle ( $article->getTitle() );
58 - $userName = $article->getTitle()->getBaseText();
59 - if ( User::isIP ( $userName ) != true && $user == null ) {
60 - return true;
61 - }
62 - if ( User::isIP ( $userName ) && $user == null && $wgOnlineStatusBarDefaultIpUsers ) {
63 - // it's anon user and we want to track them
64 - $sanitizedusername = $userName;
65 - $anon = true;
66 - } else if ( $user != null ) {
67 - // Fix capitalisation issues
68 - $sanitizedusername = $user->getName();
69 - } else {
70 - return true;
71 - }
72 - if ( $anon == false )
73 - {
74 - // check if it's not anon, let's check the config
75 - if ( $user->getOption ( "OnlineStatusBar_hide" ) == true ) {
76 - return true;
77 - }
78 - }
79 - if ( OnlineStatusBar::IsValid( $userName ) ) {
80 - $mode = OnlineStatusBar::GetStatus( $userName );
81 - $modetext = $wgOnlineStatusBarModes[$mode];
82 - $image = OnlineStatusBar::getImageHtml( $mode );
83 - $text = wfMessage( 'onlinestatusbar-line', $userName )->rawParams( $image )->params( $modetext )->escaped();
84 - $wgOut->addHtml( OnlineStatusBar::Get_Html( $text, $mode ) );
85 - }
 54+ $result = OnlineStatusBar::getUserInfoFromTitle( $article->getTitle() );
 55+
 56+ if ( $result === false ) {
 57+ return true;
8658 }
 59+
 60+ /** @var $user User */
 61+ list( $status, $user ) = $result;
 62+
 63+ // Don't display status of those who have opted out
 64+ if ( $user->getOption( 'OnlineStatusBar_hide' ) == true ) {
 65+ return true;
 66+ }
 67+
 68+ $modetext = wfMessage( 'onlinestatusbar-status-' . $status ) ;
 69+ $image = OnlineStatusBar::getImageHtml( $status );
 70+ $text = wfMessage( 'onlinestatusbar-line', $user->getName() )
 71+ ->rawParams( $image )->params( $modetext )->escaped();
 72+ $context->getOutput()->addHtml( OnlineStatusBar::getStatusBarHtml( $text ) );
 73+
8774 return true;
8875 }
8976
9077 /**
9178 * @param $user User
92 - * @paramNireferences array
 79+ * @param $preferences array
9380 * @return bool
9481 */
9582 public static function preferencesHook( User $user, array &$preferences ) {
@@ -97,10 +84,10 @@
9885 $preferences['OnlineStatusBar_hide'] = array( 'type' => 'toggle', 'label-message' => 'onlinestatusbar-hide', 'section' => 'misc/onlinestatus' );
9986 $preferences['OnlineStatusBar_status'] = array( 'type' => 'radio', 'label-message' => 'onlinestatusbar-status', 'section' => 'misc/onlinestatus',
10087 'options' => array(
101 - $wgOnlineStatusBarModes['online'] => 'online',
102 - $wgOnlineStatusBarModes['busy'] => 'busy',
103 - $wgOnlineStatusBarModes['away'] => 'away',
104 - $wgOnlineStatusBarModes['hidden'] => 'hidden'
 88+ wfMessage( 'onlinestatusbar-status-online' )->escaped() => 'online',
 89+ wfMessage( 'onlinestatusbar-status-busy' )->escaped() => 'busy',
 90+ wfMessage( 'onlinestatusbar-status-away' )->escaped() => 'away',
 91+ wfMessage( 'onlinestatusbar-status-hidden' )->escaped() => 'hidden'
10592 ),
10693 );
10794 return true;
@@ -125,8 +112,8 @@
126113 * @param $ln string?
127114 * @return bool
128115 */
129 - public static function magicWordVar ( array &$magicWords, $ln ) {
130 - $magicWords['isonline'] = array ( 0, 'isonline' );
 116+ public static function magicWordVar( array &$magicWords, $ln ) {
 117+ $magicWords['isonline'] = array( 0, 'isonline' );
131118 return true;
132119 }
133120
@@ -135,7 +122,7 @@
136123 * @param $skin Skin
137124 * @return bool
138125 */
139 - public static function stylePage ( &$out, &$skin ) {
 126+ public static function stylePage( &$out, &$skin ) {
140127 $out->addModules( 'ext.OnlineStatusBar' );
141128 return true;
142129 }
@@ -144,7 +131,7 @@
145132 * @param $vars array
146133 * @return bool
147134 */
148 - public static function magicWordSet ( &$vars ) {
 135+ public static function magicWordSet( &$vars ) {
149136 $vars[] = 'isonline';
150137 return true;
151138 }
@@ -156,25 +143,19 @@
157144 * @param $ret string?
158145 * @return bool
159146 */
160 - public static function parserGetVariable ( &$parser, &$varCache, &$index, &$ret ){
161 - global $wgOnlineStatusBarModes, $wgOnlineStatusBarDefaultOffline;
162 - if( $index == 'isonline' ){
163 - $ns = $parser->getTitle()->getNamespace();
164 - if ( ( $ns != NS_USER_TALK ) && ( $ns != NS_USER ) ) {
165 - $ret = "unknown";
166 - return true;
167 - }
168 - $name = OnlineStatusBar::GetOwnerFromTitle ( $parser->getTitle() )->getName();
 147+ public static function parserGetVariable( &$parser, &$varCache, &$index, &$ret ) {
 148+ if ( $index != 'isonline' ) {
 149+ return true;
 150+ }
169151
170 - if ( OnlineStatusBar::IsValid($name) != true ) {
171 - $ret = "unknown";
172 - return true;
173 - }
174 - $ret = OnlineStatusBar::GetStatus( $name );
175 - if ( $ret == "hidden" ) {
176 - $ret = $wgOnlineStatusBarDefaultOffline;
177 - }
 152+ $result = OnlineStatusBar::getUserInfoFromTitle( $parser->getTitle() );
 153+
 154+ if ( $result === null ) {
 155+ $ret = "unknown";
 156+ return true;
178157 }
 158+
 159+ $ret = $result[0];
179160 return true;
180161 }
181 -}
 162+}
\ No newline at end of file

Follow-up revisions

RevisionCommit summaryAuthorDate
r101139Followup r101137, removed some stuff that snuck in and addedPHP docjohnduhart15:28, 28 October 2011
r101143Followup r101137, Adding ->escape to a wfMessage() call that was missingjohnduhart15:52, 28 October 2011

Status & tagging log