r100501 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r100500‎ | r100501 | r100502 >
Date:16:47, 22 October 2011
Author:petrb
Status:deferred (Comments)
Tags:
Comment:
Work in progress, extension which adds a status if user is online, there is still a lot to do,
also it's my first extension so it's little bit ugly :)
Modified paths:
  • /trunk/extensions/OnlineStatusBar (added) (history)
  • /trunk/extensions/OnlineStatusBar/OnlineStatusBar.body.php (added) (history)
  • /trunk/extensions/OnlineStatusBar/OnlineStatusBar.i18n.php (added) (history)
  • /trunk/extensions/OnlineStatusBar/OnlineStatusBar.php (added) (history)
  • /trunk/extensions/OnlineStatusBar/OnlineStatusBar.sql (added) (history)

Diff [purge]

Index: trunk/extensions/OnlineStatusBar/OnlineStatusBar.body.php
@@ -0,0 +1,88 @@
 2+<?
 3+if ( !defined( 'MEDIAWIKI' ) ) {
 4+ echo "This is a part of mediawiki and can't be started separately";
 5+ die();
 6+}
 7+
 8+/**
 9+ * Main file of Online status bar extension.
 10+ *
 11+ * @file
 12+ * @ingroup Extensions
 13+ * @author Petr Bena <benapetr@gmail.com>
 14+ * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
 15+ * @link http://www.mediawiki.org/wiki/Extension:OnlineStatusBar Documentation
 16+ */
 17+
 18+class OnlineStatusBar {
 19+ public static $Timeout = 8000;
 20+
 21+ private static function GetNow()
 22+ {
 23+ return gmdate('Ymdhis', time());
 24+ }
 25+
 26+ public static function Get_Html( $text, $mode)
 27+ {
 28+ global $wgOnlineStatusBarModes, $wgOnlineStatusBarColor;
 29+ $color= $wgOnlineStatusBarColor[$mode];
 30+ return '<div style="border: 0px solid black; background: transparent; float: right; position: relative; top:-3px; padding: 5px"><p><b>' . $text . ': <span style="color: ' . $color . '; font:bold;"><img alt="Ledorange.svg" src="//upload.wikimedia.org/wikipedia/commons/thumb/5/5c/Ledorange.svg/20px-Ledorange.svg.png" width="20" height="20" />' . $wgOnlineStatusBarModes[$mode] . '</span></b></p></div>';
 31+ }
 32+
 33+
 34+ static function UpdateDb($user, $db)
 35+ {
 36+ global $wgUser, $wgDBname, $wgOnlineStatusBarTable;
 37+ if ( OnlineStatusBar::GetStatus($wgUser->getID()) != $OnlineStatusBar->DefaultOnline )
 38+ {
 39+ $db = wfGetDB( DB_MASTER );
 40+ $db->SelectDB( $wgDBname );
 41+ $now = OnlineStatusBar::GetNow();
 42+ $row = array(
 43+ 'userid' => $wgUser->getID(),
 44+ 'username' => $wgUser->getName(),
 45+ 'timestamp' => $now,
 46+ );
 47+ $db->insert( $wgOnlineStatusBarTable, $row, __METHOD__, 'DELAYED' );
 48+ }
 49+
 50+ return false;
 51+ }
 52+
 53+ public static function DeleteOld()
 54+ {
 55+ global $wgOnlineStatusBarTable, $wgDBname;
 56+ $db = wfGetDB ( DB_MASTER );
 57+ $time = OnlineStatusBar::GetNow() - $Timeout;
 58+ $db->SelectDB( $wgDBname );
 59+ $db->delete( $wgOnlineStatusBarTable, array( 'timestamp < "' . $time . '"' ) ,__METHOD__ );
 60+ return 0;
 61+ }
 62+
 63+ static function GetStatus( $userID ) {
 64+ global $wgOnlineStatusBarTable, $wgOnlineStatusBarModes, $wgOnlineStatusBarDefaultOffline, $wgOnlineStatusBarDefaultOnline, $wgDBname;
 65+ $db = wfGetDB ( DB_MASTER );
 66+ OnlineStatusBar::DeleteOld();
 67+ $db->SelectDB( $wgDBname );
 68+ $result = $db->select( $wgOnlineStatusBarTable, array ('userid', 'username', 'timestamp'), array('username' => $userID), __METHOD__, array('limit 1', 'order by timestamp desc'));
 69+ $values = new ResultWrapper ($db, $result);
 70+
 71+ if ($values->numRows() > 0)
 72+ {
 73+ return $wgOnlineStatusBarDefaultOnline;
 74+ }
 75+
 76+ return $wgOnlineStatusBarDefaultOffline;
 77+ }
 78+
 79+ static function DeleteStatus( $user )
 80+ {
 81+ global $wgOnlineStatusBarTable, $wgDBname;
 82+ $db = wfGetDB ( DB_MASTER );
 83+ $db->SelectDB( $wgDBname );
 84+ $db->delete( $wgOnlineStatusBarTable, array ('username' => $user), __METHOD__ ); // delete user
 85+ return true;
 86+ }
 87+}
 88+
 89+?>
Property changes on: trunk/extensions/OnlineStatusBar/OnlineStatusBar.body.php
___________________________________________________________________
Added: svn:eol-style
190 + native
Index: trunk/extensions/OnlineStatusBar/OnlineStatusBar.i18n.php
@@ -0,0 +1,18 @@
 2+<?php
 3+/**
 4+ * Internationalisation file for Online status bar extension.
 5+ *
 6+ * @file
 7+ * @ingroup Extensions
 8+ */
 9+
 10+$messages = array();
 11+
 12+/** English
 13+ * @author Petr Bena
 14+ */
 15+$messages['en'] = array(
 16+'onlinestatusbar-desc' => "Status bar which shows whether a user is online, based on preferences, on their user page",
 17+'onlinestatusbar-line' => " is now ",
 18+);
 19+?>
Property changes on: trunk/extensions/OnlineStatusBar/OnlineStatusBar.i18n.php
___________________________________________________________________
Added: svn:eol-style
120 + native
Index: trunk/extensions/OnlineStatusBar/OnlineStatusBar.php
@@ -0,0 +1,111 @@
 2+<?php
 3+if ( !defined( 'MEDIAWIKI' ) ) {
 4+ echo "This is a part of mediawiki and can't be started separately";
 5+ die();
 6+}
 7+
 8+/**
 9+ * Insert a special box on user page showing their status.
 10+ *
 11+ * @file
 12+ * @ingroup Extensions
 13+ * @author Petr Bena <benapetr@gmail.com>
 14+ * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
 15+ * @link http://www.mediawiki.org/wiki/Extension:OnlineStatusBar Documentation
 16+ */
 17+
 18+$wgExtensionCredits[version_compare($wgVersion, '1.17', '>=') ? 'userpage tools' : 'other'][] = array(
 19+ 'path' => __FILE__,
 20+ 'name' => 'Online status bar',
 21+ 'version' => '1.0.0',
 22+ 'author' => array( 'Petr Bena' ),
 23+ 'descriptionmsg' => 'onlinestatusbar-desc',
 24+ 'url' => 'http://www.mediawiki.org/wiki/Extension:OnlineStatusBar',
 25+);
 26+
 27+$dir = dirname( __FILE__ );
 28+$wgExtensionMessagesFiles['OnlineStatusBar'] = "$dir/OnlineStatusBar.i18n.php";
 29+
 30+$wgAutoloadClasses['OnlineStatusBar'] = "$dir/OnlineStatusBar.body.php";
 31+
 32+// Configuration
 33+$wgOnlineStatusBarModes = array (
 34+ 'online' => "On-line",
 35+ 'busy' => "Busy",
 36+ 'away' => "Away",
 37+ 'hidden' => "Offline",
 38+ 'offline' => "Offline",
 39+);
 40+
 41+$wgOnlineStatusBarColor = array (
 42+ 'online' => "green",
 43+ 'busy' => "orange",
 44+ 'away' => "orange",
 45+ 'hidden' => "red",
 46+ 'offline' => "red",
 47+);
 48+
 49+$wgOnlineStatusBarDefaultOnline = "online";
 50+$wgOnlineStatusBarDefaultOffline = "offline";
 51+$wgOnlineStatusBarTable = "online_status";
 52+$wgOnlineStatusBarDefaultEnabled = false;
 53+$wgOnlineStatusBar_LogoutTime = 3600;
 54+
 55+$wgHooks['LoadExtensionSchemaUpdates'][] = 'wfOnlineStatusBar_CkSchema';
 56+
 57+ function wfOnlineStatusBar_CkSchema($updater = null)
 58+ {
 59+ global $wgOnlineStatusBarTable;
 60+ if ($updater != null)
 61+ {
 62+ $updater->addExtensionUpdate( array ( 'addtable', $wgOnlineStatusBarTable, dirname( __FILE__) . '/OnlineStatusBar.sql', true));
 63+ }
 64+ else
 65+ {
 66+ global $wgExtNewTables;
 67+ $wgExtNewTables[] = array(
 68+ $wgOnlineStatusBarTable, dirname( __FILE__ ) . '/OnlineStatusBar.sql' );
 69+ }
 70+ return true;
 71+ }
 72+
 73+$wgHooks['UserLogoutComplete'][] = 'wfOnlineStatusBar_Logout';
 74+
 75+ function wfOnlineStatusBar_Logout(&$user, &$inject_html, $old_name)
 76+ {
 77+ global $wgUser;
 78+ OnlineStatusBar::DeleteStatus($old_name);
 79+ return true;
 80+ }
 81+
 82+$wgHooks['ArticleViewHeader'][] = 'wfOnlineStatusBar_RenderBar';
 83+ function wfOnlineStatusBar_RenderBar(&$article, &$outputDone, &$pcache)
 84+ {
 85+ global $wgOnlineStatusBar_Template, $messages, $wgOnlineStatusBarModes, $wgOut;
 86+ $ns=$article->getTitle()->getNamespace();
 87+ if(($ns == "3") || ($ns == "2"))
 88+ {
 89+ // better way to get a username would be great :)
 90+ $user = preg_replace('/\/.*/', '', preg_replace('/^.*\:/', "", $article->getTitle()));
 91+ $OnlineStatus_Text = $user . language::getMessageFromDB("onlinestatusbar-line");
 92+ $OnlineStatus_Mode = OnlineStatusBar::GetStatus($user);
 93+ $wgOut->addHtml(OnlineStatusBar::Get_Html($OnlineStatus_Text, $OnlineStatus_Mode));
 94+ }
 95+ return true;
 96+ }
 97+
 98+$wgHooks['UserLoginComplete'][] = 'wfOnlineStatusBar_UpdateStatus';
 99+
 100+ function wfOnlineStatusBar_UpdateStatus()
 101+ {
 102+ OnlineStatusBar::UpdateDb();
 103+ return 0;
 104+ }
 105+
 106+
 107+$commonModuleInfo = array(
 108+ 'localBasePath' => dirname( __FILE__ ) . '/modules',
 109+ 'remoteExtPath' => 'OnlineStatusBar/modules',
 110+);
 111+
 112+?>
Property changes on: trunk/extensions/OnlineStatusBar/OnlineStatusBar.php
___________________________________________________________________
Added: svn:eol-style
1113 + native
Index: trunk/extensions/OnlineStatusBar/OnlineStatusBar.sql
@@ -0,0 +1,7 @@
 2+CREATE TABLE online_status (
 3+ `userid` int(5) NOT NULL default '0',
 4+ `username` varchar(255) NOT NULL default '',
 5+ `timestamp` char(14) NOT NULL default '',
 6+ PRIMARY KEY USING HASH (`userid`, `username`)
 7+) ENGINE=MEMORY;
 8+
Property changes on: trunk/extensions/OnlineStatusBar/OnlineStatusBar.sql
___________________________________________________________________
Added: svn:eol-style
19 + native

Follow-up revisions

RevisionCommit summaryAuthorDate
r100507fixed timeout valuepetrb17:50, 22 October 2011
r100532Improved some stuff according to recommendations from reviewpetrb08:43, 23 October 2011

Comments

#Comment by Nikerabbit (talk | contribs)   08:07, 23 October 2011

Here're some tips for moving from a little bit ugly to awesomeness :D

  1. Weird indentation, also mixes tabs and spaces.
  2. We don't use ?> at the end of the file.
  3. Messages cannot have trailing whitespace
  4. If possible, make the message look at least like $1 is now $2
  5. Hooks should return true or false, or string, but not zero.
  6. language::getMessageFromDB isn't a static function, use wfMessage or some other variant of wfMsg* instead.
  7. instead of $ns == "3" use constants like NS_USER_TALK
  8. Why are you calling $db->SelectDB() all the time?
  9. The usual naming convention is $dbr for slaves and $dbw for masters.
#Comment by Petrb (talk | contribs)   09:28, 23 October 2011

thanks, btw if you had any ideas feel free to change it in svn too, I tried to fix some of mentioned things, but for instance I have no clue how to implement in php comment 4 and no sure about 1st and 3th comment, I know that indentation may look weird, it's little bit c-ish :) I will try to make it look like other files

#Comment by Nikerabbit (talk | contribs)   09:54, 23 October 2011

Sure, I can try to fix them for you to show what I mean.

Status & tagging log