r100835 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r100834‎ | r100835 | r100836 >
Date:18:30, 26 October 2011
Author:petrb
Status:deferred (Comments)
Tags:
Comment:
user configuration
fixed db
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/OnlineStatusBar.sql (modified) (history)

Diff [purge]

Index: trunk/extensions/OnlineStatusBar/OnlineStatusBar.body.php
@@ -45,8 +45,7 @@
4646 {
4747 $dbw = wfGetDB( DB_MASTER );
4848 $row = array(
49 - 'userid' => $wgUser->getID(),
50 - 'username' => $wgUser->getName(),
 49+ 'username' => strtolower($wgUser->getName()),
5150 'timestamp' => $dbw->timestamp( wfTimestamp() ),
5251 );
5352 $dbw->insert( 'online_status', $row, __METHOD__, 'DELAYED' );
@@ -67,7 +66,7 @@
6867 $dbw->update(
6968 'online_status',
7069 array( 'timestamp' => $dbw->timestamp( wfTimestamp() ) ),
71 - array( 'username' => $wgUser->getID() ),
 70+ array( 'username' => strtolower($wgUser->getID()) ),
7271 __METHOD__
7372 );
7473
@@ -85,11 +84,33 @@
8685 return 0;
8786 }
8887
 88+ public static function IsValid($id)
 89+ {
 90+ global $wgOnlineStatusBarDefaultIpUsers;
 91+ // checks if anon
 92+ if (User::isIP($id))
 93+ {
 94+ return $wgOnlineStatusBarDefaultIpUsers;
 95+ }
 96+ $user = User::newFromName($id);
 97+ // check if exist
 98+ if ($user == null)
 99+ {
 100+ return false;
 101+ }
 102+ // do we track them
 103+ if ($user->getOption("OnlineStatusBar_active") == true)
 104+ {
 105+ return true;
 106+ }
 107+ return false;
 108+ }
 109+
89110 static function GetStatus( $userID ) {
90111 global $wgOnlineStatusBarModes, $wgOnlineStatusBarDefaultOffline, $wgOnlineStatusBarDefaultOnline, $wgDBname;
91112 $dbw = wfGetDB( DB_MASTER );
92113 OnlineStatusBar::DeleteOld();
93 - $result = $dbw->selectField( 'online_status', 'username', array( 'username' => $userID ), __METHOD__, array( 'limit 1', 'order by timestamp desc' ) );
 114+ $result = $dbw->selectField( 'online_status', 'username', array( 'username' => strtolower($userID) ), __METHOD__, array( 'limit 1', 'order by timestamp desc' ) );
94115 if ( $result )
95116 {
96117 return $wgOnlineStatusBarDefaultOnline;
Index: trunk/extensions/OnlineStatusBar/OnlineStatusBar.i18n.php
@@ -15,4 +15,8 @@
1616 $messages['en'] = array(
1717 'onlinestatusbar-desc' => "Status bar which shows whether a user is online, based on preferences, on their user page",
1818 'onlinestatusbar-line' => "$1 is now $2 $3",
 19+ 'onlinestatusbar-used' => 'Do you want to let others see if you are online?',
 20+ 'onlinestatusbar-status' => 'What is the default status you wish to use:',
 21+ 'prefs-gadgets' => 'Gadgets',
 22+ 'prefs-onlinestatus' => 'Online Status',
1923 );
Index: trunk/extensions/OnlineStatusBar/OnlineStatusBar.php
@@ -52,6 +52,8 @@
5353 'offline' => "red",
5454 );
5555
 56+// default for anonymous and uknown users
 57+$wgOnlineStatusBarDefaultIpUsers = false;
5658 // default for online
5759 $wgOnlineStatusBarDefaultOnline = "online";
5860 // default for offline
@@ -96,12 +98,14 @@
9799 {
98100 // better way to get a username would be great :)
99101 $user = preg_replace( '/\/.*/', '', preg_replace( '/^.*\:/', "", $article->getTitle() ) );
100 -
101 - $mode = OnlineStatusBar::GetStatus( $user );
102 - $modetext = $wgOnlineStatusBarModes[$mode];
103 - $image = OnlineStatusBar::getImageHtml( $mode );
104 - $text = wfMessage( 'onlinestatusbar-line', $user )->rawParams( $image )->params( $modetext )->escaped();
105 - $wgOut->addHtml( OnlineStatusBar::Get_Html( $text, $mode ) );
 102+ if (OnlineStatusBar::IsValid($user))
 103+ {
 104+ $mode = OnlineStatusBar::GetStatus( $user );
 105+ $modetext = $wgOnlineStatusBarModes[$mode];
 106+ $image = OnlineStatusBar::getImageHtml( $mode );
 107+ $text = wfMessage( 'onlinestatusbar-line', $user )->rawParams( $image )->params( $modetext )->escaped();
 108+ $wgOut->addHtml( OnlineStatusBar::Get_Html( $text, $mode ) );
 109+ }
106110 }
107111 return true;
108112 }
@@ -113,8 +117,19 @@
114118 return true;
115119 }
116120
117 -// Unused?
118 -$commonModuleInfo = array(
119 - 'localBasePath' => dirname( __FILE__ ) . '/modules',
120 - 'remoteExtPath' => 'OnlineStatusBar/modules',
121 -);
 121+$wgHooks['GetPreferences'][] = 'wfOnlineStatusBar_PreferencesHook';
 122+function wfOnlineStatusBar_PreferencesHook($user, &$preferences)
 123+{
 124+ global $wgOnlineStatusBarModes;
 125+ $preferences['OnlineStatusBar_active'] = array ('type' => 'toggle', 'label-message' => 'onlinestatusbar-used', 'section' => 'gadgets/onlinestatus' );
 126+ $preferences['OnlineStatusBar_status'] = array ('type' => 'radio', 'label-message' => 'onlinestatusbar-status', 'section' => 'gadgets/onlinestatus',
 127+ 'options' => array(
 128+ $wgOnlineStatusBarModes['online'] => 'online',
 129+ $wgOnlineStatusBarModes['busy'] => 'busy',
 130+ $wgOnlineStatusBarModes['away'] => 'away',
 131+ $wgOnlineStatusBarModes['hidden'] => 'hidden'
 132+ ),
 133+ 'default' => 'online',
 134+ );
 135+ return true;
 136+}
Index: trunk/extensions/OnlineStatusBar/OnlineStatusBar.sql
@@ -1,7 +1,6 @@
22 CREATE TABLE /*$wgDBprefix*/online_status (
3 - `userid` int(5) NOT NULL default '0',
43 `username` varchar(255) NOT NULL default '',
54 `timestamp` char(14) NOT NULL default '',
6 - PRIMARY KEY USING HASH (`userid`, `username`)
 5+ PRIMARY KEY USING HASH (`username`)
76 ) ENGINE=MEMORY;
87

Follow-up revisions

RevisionCommit summaryAuthorDate
r100848several fixes including collision fix with gadgetspetrb19:11, 26 October 2011
r100973removed tolower, per comments on previous revpetrb13:26, 27 October 2011

Comments

#Comment by Raymond (talk | contribs)   19:08, 26 October 2011

This is a dupe message key. It exists in the Gadget extension too. Therefore this extension cannot be added to Translatewiki.net

+	'prefs-gadgets' => 'Gadgets',

Please add message documentation for the newly added messages. Thanks.

#Comment by Petrb (talk | contribs)   19:12, 26 October 2011

renamed

#Comment by TheDJ (talk | contribs)   19:16, 26 October 2011
#Comment by Petrb (talk | contribs)   19:17, 26 October 2011

could you tell me how those messages should look like? I didn't find example there

#Comment by TheDJ (talk | contribs)   19:20, 26 October 2011

See http://svn.wikimedia.org/viewvc/mediawiki/trunk/phase3/languages/messages/MessagesQqq.php?revision=100558&view=markup

P.S. when you make fixes, please add something like "Follow up to r100835". This will make the original revision findable from your fix, and also will automatically add a follow up indication to the issue that you have fixed itself.

#Comment by Nikerabbit (talk | contribs)   06:13, 27 October 2011
+			array( 'username' => strtolower($wgUser->getID()) ),

Why are you lower casing user names? This should probably be $wgUser->getName() too.

#Comment by Petrb (talk | contribs)   13:16, 27 October 2011

why is this fixme?

I lowercase it because when someone login as bob while his name is Bob it's possible that somewhere it would look up bob in the db, this is absolutely safe method to look up username, since it's impossible to create two users bob and Bob. I am of course not sure if it's possible in mw, but from my experience if you don't need to have stuff case sensitive it's better not to have it. So I see absolutely no problem in this, and I would like to keep it

#Comment by Nikerabbit (talk | contribs)   13:18, 27 October 2011

User object is guaranteed to return the user name in correct capitalization. Now you cannot for example do a database join to user table.

#Comment by Petrb (talk | contribs)   13:20, 27 October 2011

I don't see a reason to do join, anyway, user object may return correct name, but there are other ways used than that, for instance oldUsername etc. are you sure that all of those variables return correct capitalisation? everywhere? it's same risk as if you create a function which doesn't handle potentialy unset variable just because "you are sure" this variable is always set...

#Comment by Nikerabbit (talk | contribs)   13:25, 27 October 2011

If the user name comes from some other source, I would pass it through the user object to get the correct capitalisation.

#Comment by Petrb (talk | contribs)   13:27, 27 October 2011

I removed tolower now I hope it never fail because of that...

#Comment by TheDJ (talk | contribs)   20:06, 27 October 2011
#Comment by Petrb (talk | contribs)   13:18, 27 October 2011

also lowercase should be only in mediawiki hooks which work in db, otherwise it isn't necessary on User:bob it shouldn't show status because page wouldn't exist and mediawiki would display error on such page too, so that would look silly if there was Bob is Offline etc

Status & tagging log