r39651 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r39650‎ | r39651 | r39652 >
Date:18:21, 19 August 2008
Author:ialex
Status:old
Tags:
Comment:
Added the possibility to change status with ajax in OnlineStatus extension, it will add a new tab in the personal tools with a dropdown menu.
Note: this requires a SkinTemplate-based skin to work as the PersonalUrls hook is used.
Modified paths:
  • /trunk/extensions/OnlineStatus/OnlineStatus.css (added) (history)
  • /trunk/extensions/OnlineStatus/OnlineStatus.i18n.php (modified) (history)
  • /trunk/extensions/OnlineStatus/OnlineStatus.js (added) (history)
  • /trunk/extensions/OnlineStatus/OnlineStatus.php (modified) (history)

Diff [purge]

Index: trunk/extensions/OnlineStatus/OnlineStatus.css
@@ -0,0 +1,17 @@
 2+/**
 3+ * CSS stylesheet for OnlineStatus extension
 4+ */
 5+
 6+.online-status-js {
 7+ overflow: auto;
 8+ overflow-x: hidden;
 9+ position: absolute;
 10+ top: 0px;
 11+ left: 0px;
 12+ background-color: white;
 13+ border-style: solid;
 14+ border-color: #AAAAAA;
 15+ border-width: 1px;
 16+ z-index: 99;
 17+ font-size: 120%;
 18+}
Property changes on: trunk/extensions/OnlineStatus/OnlineStatus.css
___________________________________________________________________
Name: svn:eol-style
119 + native
Index: trunk/extensions/OnlineStatus/OnlineStatus.js
@@ -0,0 +1,83 @@
 2+var OnlineStatusCreated = false;
 3+
 4+function OnlineStatus(){
 5+ var status = document.getElementById( 'pt-status' );
 6+ if( typeof status == 'object' ){
 7+ var link = status.firstChild;
 8+ link.onmousedown = status.onkeypress = ShowOnlineToggle;
 9+ }
 10+}
 11+
 12+function ShowOnlineToggle(){
 13+ if( !OnlineStatusCreated ){
 14+ var div = document.createElement( 'div' );
 15+ div.id = 'online-status-js';
 16+ div.className = 'online-status-js';
 17+
 18+ // Taken from skins/mwsuggest.js of core
 19+ // Credit: Robert Stojnić
 20+ var left = 0;
 21+ var top = this.offsetHeight;
 22+ var elem = this;
 23+ while( elem ){
 24+ left += elem.offsetLeft;
 25+ top += elem.offsetTop;
 26+ elem = elem.offsetParent;
 27+ }
 28+ if( navigator.userAgent.indexOf( 'Mac' ) != -1 && typeof document.body.leftMargin != 'undefined' ){
 29+ left += document.body.leftMargin;
 30+ top += document.body.topMargin;
 31+ }
 32+
 33+ div.style.left = left + "px";
 34+ div.style.top = top + "px";
 35+ var table = document.createElement( 'table' );
 36+ sajax_do_call( 'efOnlineStatusAjax', ['get'], function( x ){
 37+ if( x.status == 200 ){
 38+ var resp = x.responseText;
 39+ // A bit unsafe, but...
 40+ var json = eval( resp );
 41+ for( i = 0; i < json.length; i++ ){
 42+ var status = json[i];
 43+ var tr = document.createElement( 'tr' );
 44+ var td = document.createElement( 'td' );
 45+ if( status[2] ){
 46+ td.appendChild( document.createTextNode( status[1] ) );
 47+ } else {
 48+ var a = document.createElement( 'a' );
 49+ a.status = status[0];
 50+ a.onmousedown = a.onkeypress = ChangeOnlineStatus;
 51+ a.appendChild( document.createTextNode( status[1] ) );
 52+ td.appendChild( a );
 53+ }
 54+ tr.appendChild( td );
 55+ table.appendChild( tr );
 56+ }
 57+ }
 58+ } );
 59+ div.appendChild( table );
 60+ div.visible = true;
 61+ document.body.appendChild( div );
 62+ OnlineStatusCreated = true;
 63+ } else {
 64+ var div = document.getElementById( 'online-status-js' );
 65+ div.parentNode.removeChild( div );
 66+ OnlineStatusCreated = false;
 67+ }
 68+}
 69+
 70+function ChangeOnlineStatus(){
 71+ var status = this.status;
 72+ sajax_do_call( 'efOnlineStatusAjax', ['set', status], function( x ){
 73+ if( x.status == 200 ){
 74+ var resp = x.responseText;
 75+ jsMsg( resp, 'watch' );
 76+ // Force update
 77+ var div = document.getElementById( 'online-status-js' );
 78+ div.parentNode.removeChild( div );
 79+ OnlineStatusCreated = false;
 80+ }
 81+ } );
 82+}
 83+
 84+hookEvent( 'load', OnlineStatus );
\ No newline at end of file
Property changes on: trunk/extensions/OnlineStatus/OnlineStatus.js
___________________________________________________________________
Name: svn:eol-style
185 + native
Index: trunk/extensions/OnlineStatus/OnlineStatus.i18n.php
@@ -9,10 +9,14 @@
1010
1111 $messages['en'] = array(
1212 'onlinestatus-desc' => 'Add a preference to show if the user is currently present or not on the wiki',
 13+ 'onlinestatus-js-anon' => 'Error: you must be logged in to use this feature',
 14+ 'onlinestatus-js-changed' => 'Your status has been changed to "$1"',
 15+ 'onlinestatus-js-error' => 'Impossible to change status, value "$1" is invalid',
1316 'onlinestatus-levels' => '* online
1417 * offline', // Do not translate this message
1518 'onlinestatus-subtitle-offline' => 'This user is currently offline',
1619 'onlinestatus-subtitle-online' => 'This user is currently online',
 20+ 'onlinestatus-tab' => 'Status',
1721 'onlinestatus-toggles-desc' => 'Your status:',
1822 'onlinestatus-toggles-explain' => 'This allows you to show to other users if you are actually online or not by viewing your user page.',
1923 'onlinestatus-toggles-show' => 'Show online status on my user page',
@@ -156,8 +160,12 @@
157161 */
158162 $messages['fr'] = array(
159163 'onlinestatus-desc' => "Ajoute une préférence pour montrer si l'utilisateur est présent ou non",
 164+ 'onlinestatus-js-anon' => 'Erreur : vous devez être connecté pour utiliser cette fonctionnalité',
 165+ 'onlinestatus-js-changed' => 'Votre status a été changé à « $1 »',
 166+ 'onlinestatus-js-error' => 'Impossible de changer le statut, la valeur « $1 » est invalide',
160167 'onlinestatus-subtitle-offline' => 'Cet utilisateur est actuellement hors ligne',
161168 'onlinestatus-subtitle-online' => 'Cet utilisateur est actuellement en ligne',
 169+ 'onlinestatus-tab' => 'Statut',
162170 'onlinestatus-toggles-desc' => 'Votre statut&nbsp;:',
163171 'onlinestatus-toggles-explain' => 'Ceci permet aux autres utilisateur de savoir si vous êtes actuellement présent en regardant votre page utilisateur.',
164172 'onlinestatus-toggles-show' => 'Montrer mon statut sur ma page utilisateur',
Index: trunk/extensions/OnlineStatus/OnlineStatus.php
@@ -13,8 +13,8 @@
1414
1515 // Add credit :)
1616 $wgExtensionCredits['other'][] = array(
17 - 'svn-date' => '$LastChangedDate$',
18 - 'svn-revision' => '$LastChangedRevision$',
 17+ 'svn-date' => '$LastChangedDate$',
 18+ 'svn-revision' => '$LastChangedRevision$',
1919 'name' => 'OnlineStatus',
2020 'author' => 'Alexandre Emsenhuber',
2121 'url' => 'http://www.mediawiki.org/wiki/Extension:OnlineStatus',
@@ -29,10 +29,55 @@
3030 */
3131 $wgAllowAnyUserOnlineStatusFunction = true;
3232
 33+function efOnlineStatusAjax( $action, $stat = false ){
 34+ global $wgUser;
 35+ wfLoadExtensionMessages( 'OnlineStatus' );
 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+
3377 class OnlineStatus {
3478
3579 static function init(){
36 - global $wgExtensionMessagesFiles, $wgExtensionFunctions, $wgHooks, $wgAllowAnyUserOnlineStatusFunction;
 80+ global $wgExtensionMessagesFiles, $wgExtensionFunctions, $wgHooks, $wgAjaxExportList;
 81+ global $wgAllowAnyUserOnlineStatusFunction;
3782 // Add messages file
3883 $wgExtensionMessagesFiles['OnlineStatus'] = dirname( __FILE__ ) . '/OnlineStatus.i18n.php';
3984
@@ -60,6 +105,10 @@
61106
62107 // User page
63108 $wgHooks['BeforePageDisplay'][] = 'OnlineStatus::BeforePageDisplay';
 109+ $wgHooks['PersonalUrls'][] = 'OnlineStatus::PersonalUrls';
 110+
 111+ // Ajax stuff
 112+ $wgAjaxExportList[] = 'efOnlineStatusAjax';
64113 }
65114
66115 /**
@@ -241,7 +290,19 @@
242291 * Hook function for BeforePageDisplay
243292 */
244293 static function BeforePageDisplay( &$out ){
245 - global $wgTitle, $wgRequest;
 294+ global $wgTitle, $wgRequest, $wgUser;
 295+ global $wgUseAjax;
 296+
 297+ if( $wgUser->isLoggedIn() && $wgUseAjax ){
 298+ global $wgScriptPath, $wgJsMimeType;
 299+ $out->addScript( "<script type=\"{$wgJsMimeType}\" src=\"{$wgScriptPath}/extensions/OnlineStatus/OnlineStatus.js\"></script>" );
 300+ $out->addLink( array(
 301+ 'rel' => 'stylesheet',
 302+ 'type' => 'text/css',
 303+ 'href' => "{$wgScriptPath}/extensions/OnlineStatus/OnlineStatus.css"
 304+ ) );
 305+ }
 306+
246307 if( !in_array( $wgRequest->getVal( 'action', 'view' ), array( 'view', 'purge' ) ) )
247308 return true;
248309 $status = self::GetUserStatus( $wgTitle, true );
@@ -249,8 +310,32 @@
250311 return true;
251312 wfLoadExtensionMessages( 'OnlineStatus' );
252313 $out->setSubtitle( wfMsgExt( 'onlinestatus-subtitle-' . $status, array( 'parse' ) ) );
 314+
253315 return true;
254316 }
 317+
 318+ /**
 319+ * Hook for PersonalUrls
 320+ */
 321+ static function PersonalUrls( &$urls, &$title ){
 322+ global $wgUser, $wgUseAjax;
 323+ # Require ajax
 324+ if( !$wgUser->isLoggedIn() || !$wgUseAjax )
 325+ return true;
 326+ $arr = array();
 327+ foreach( $urls as $key => $val ){
 328+ if( $key == 'logout' ){
 329+ wfLoadExtensionMessages( 'OnlineStatus' );
 330+ $arr['status'] = array(
 331+ 'text' => wfMsgHtml( 'onlinestatus-tab' ),
 332+ 'href' => 'javascript:;',
 333+ );
 334+ }
 335+ $arr[$key] = $val;
 336+ }
 337+ $urls = $arr;
 338+ return true;
 339+ }
255340 }
256341
257342 OnlineStatus::init();
\ No newline at end of file

Status & tagging log