Index: trunk/extensions/OnlineStatusBar/OnlineStatusBar.body.php |
— | — | @@ -17,48 +17,80 @@ |
18 | 18 | |
19 | 19 | class OnlineStatusBar { |
20 | 20 | |
21 | | - public static function Get_Html( $text, $mode ) { |
22 | | - global $wgOnlineStatusBarColor, $wgOnlineStatusBarY; |
23 | | - $color = $wgOnlineStatusBarColor[$mode]; |
| 21 | + public static function getStatusBarHtml( $text ) { |
24 | 22 | return <<<HTML |
25 | 23 | <div class="onlinebar metadata topiconstatus" id="status-top"> |
26 | 24 | <div class="statusbaricon"> |
27 | 25 | $text</div></div> |
28 | 26 | HTML; |
29 | 27 | } |
30 | | - |
| 28 | + |
31 | 29 | /** |
32 | | - * Returns image |
33 | | - * @param $mode User |
| 30 | + * Returns image element |
| 31 | + * |
| 32 | + * @param $mode String |
34 | 33 | * @return string |
35 | 34 | */ |
36 | 35 | public static function GetImageHtml( $mode ) { |
37 | 36 | global $wgExtensionAssetsPath, $wgOnlineStatusBarIcon, $wgOnlineStatusBarModes; |
38 | 37 | $icon = "$wgExtensionAssetsPath/OnlineStatusBar/{$wgOnlineStatusBarIcon[$mode]}"; |
39 | | - $modeText = $wgOnlineStatusBarModes[$mode]; |
40 | 38 | return Html::element( 'img', array( 'src' => $icon ) ); |
41 | 39 | } |
42 | 40 | |
43 | 41 | /** |
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 |
46 | 46 | */ |
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; |
51 | 50 | } |
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 ); |
55 | 65 | } |
56 | 66 | |
| 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 | + |
57 | 87 | /** |
58 | 88 | * @return bool |
59 | 89 | */ |
60 | 90 | public static function UpdateDb() { |
61 | 91 | 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 ) { |
63 | 95 | $dbw = wfGetDB( DB_MASTER ); |
64 | 96 | $row = array( |
65 | 97 | 'username' => $wgUser->getName(), |
— | — | @@ -74,10 +106,11 @@ |
75 | 107 | */ |
76 | 108 | public static function UpdateStatus() { |
77 | 109 | global $wgUser, $wgOnlineStatusBarDefaultOffline; |
78 | | - if ( OnlineStatusBar::GetStatus( $wgUser->getName() ) == $wgOnlineStatusBarDefaultOffline ) { |
| 110 | + if ( OnlineStatusBar::GetStatus( $wgUser ) == $wgOnlineStatusBarDefaultOffline ) { |
79 | 111 | OnlineStatusBar::UpdateDb(); |
80 | 112 | return true; |
81 | 113 | } |
| 114 | + |
82 | 115 | $dbw = wfGetDB( DB_MASTER ); |
83 | 116 | $dbw->update( |
84 | 117 | 'online_status', |
— | — | @@ -85,6 +118,7 @@ |
86 | 119 | array( 'username' => $wgUser->getID() ), |
87 | 120 | __METHOD__ |
88 | 121 | ); |
| 122 | + |
89 | 123 | return false; |
90 | 124 | } |
91 | 125 | |
— | — | @@ -92,9 +126,10 @@ |
93 | 127 | * @return int |
94 | 128 | */ |
95 | 129 | public static function DeleteOld() { |
96 | | - global $wgOnlineStatusBar_LogoutTime, $wgDBname; |
| 130 | + global $wgOnlineStatusBar_LogoutTime; |
97 | 131 | $dbw = wfGetDB( DB_MASTER ); |
98 | 132 | $time = wfTimestamp( TS_UNIX ) - $wgOnlineStatusBar_LogoutTime; |
| 133 | + // FIXME: This looks wrong: |
99 | 134 | $time = $dbw->addQuotes( $dbw->timestamp( $time ) - $wgOnlineStatusBar_LogoutTime ); |
100 | 135 | $dbw->delete( 'online_status', array( "timestamp < $time" ), __METHOD__ ); |
101 | 136 | return 0; |
— | — | @@ -102,20 +137,19 @@ |
103 | 138 | |
104 | 139 | |
105 | 140 | /** |
106 | | - * @param $userName string |
| 141 | + * Checks to see if the user can be tracked |
| 142 | + * |
| 143 | + * @param User $user |
107 | 144 | * @return bool |
108 | 145 | */ |
109 | | - public static function IsValid( $userName ) { |
110 | | - global $wgOnlineStatusBarDefaultIpUsers, $wgOnlineStatusBarDefaultEnabled; |
| 146 | + public static function isValid( User $user ) { |
| 147 | + global $wgOnlineStatusBarTrackIpUsers, $wgOnlineStatusBarDefaultEnabled; |
| 148 | + |
111 | 149 | // checks if anon |
112 | | - if ( User::isIP( $userName ) ) { |
113 | | - return $wgOnlineStatusBarDefaultIpUsers; |
| 150 | + if ( $user->isAnon() ) { |
| 151 | + return $wgOnlineStatusBarTrackIpUsers; |
114 | 152 | } |
115 | | - $user = User::newFromName( $userName ); |
116 | | - // check if exist |
117 | | - if ( $user == null ) { |
118 | | - return false; |
119 | | - } |
| 153 | + |
120 | 154 | if ( $user->getId() == 0 ) |
121 | 155 | { |
122 | 156 | return false; |
— | — | @@ -126,30 +160,6 @@ |
127 | 161 | |
128 | 162 | /** |
129 | 163 | * @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 |
154 | 164 | * @return bool |
155 | 165 | */ |
156 | 166 | static function DeleteStatus( $userName ) { |
Index: trunk/extensions/OnlineStatusBar/OnlineStatusBar.i18n.php |
— | — | @@ -11,6 +11,7 @@ |
12 | 12 | /** |
13 | 13 | * English |
14 | 14 | * @author Petr Bena |
| 15 | + * @author John Du Hart |
15 | 16 | */ |
16 | 17 | $messages['en'] = array( |
17 | 18 | // Description |
— | — | @@ -25,9 +26,18 @@ |
26 | 27 | 'prefs-onlinestatus' => 'Online status', |
27 | 28 | // Message in config |
28 | 29 | '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', |
29 | 36 | ); |
30 | 37 | |
31 | | -/** Message documentation (Message documentation) */ |
| 38 | +/** Message documentation (Message documentation) |
| 39 | + * @author Petr Bena |
| 40 | + * @author John Du Hart |
| 41 | + */ |
32 | 42 | $messages['qqq'] = array( |
33 | 43 | 'onlinestatusbar-desc' => '{{desc}}', |
34 | 44 | 'onlinestatusbar-line' => 'Status bar text line (User is now Offline), parameters: |
— | — | @@ -38,6 +48,12 @@ |
39 | 49 | 'onlinestatusbar-status' => 'Message in config asking what status they want to use, option box', |
40 | 50 | 'prefs-onlinestatus' => 'Section for config, located in preferences - misc', |
41 | 51 | '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)', |
42 | 58 | ); |
43 | 59 | |
44 | 60 | /** German (Deutsch) |
Index: trunk/extensions/OnlineStatusBar/OnlineStatusBar.php |
— | — | @@ -37,13 +37,6 @@ |
38 | 38 | |
39 | 39 | // Configuration |
40 | 40 | // 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 | | -); |
48 | 41 | $wgOnlineStatusBarIcon = array( |
49 | 42 | 'online' => "statusgreen.png", |
50 | 43 | 'busy' => "statusorange.png", |
— | — | @@ -51,16 +44,9 @@ |
52 | 45 | 'hidden' => "statusred.png", |
53 | 46 | 'offline' => "statusred.png", |
54 | 47 | ); |
55 | | -$wgOnlineStatusBarColor = array( |
56 | | - 'online' => "green", |
57 | | - 'busy' => "orange", |
58 | | - 'away' => "orange", |
59 | | - 'hidden' => "red", |
60 | | - 'offline' => "red", |
61 | | -); |
62 | 48 | |
63 | 49 | // default for anonymous and uknown users |
64 | | -$wgOnlineStatusBarDefaultIpUsers = false; |
| 50 | +$wgOnlineStatusBarTrackIpUsers = false; |
65 | 51 | // default for online |
66 | 52 | $wgOnlineStatusBarDefaultOnline = "online"; |
67 | 53 | // default for offline |
Index: trunk/extensions/OnlineStatusBar/OnlineStatusBarHooks.php |
— | — | @@ -7,11 +7,11 @@ |
8 | 8 | */ |
9 | 9 | class OnlineStatusBarHooks { |
10 | 10 | /** |
11 | | - * @param DatabaseUpdate|null $updater |
| 11 | + * @param DatabaseUpdater|null $updater |
12 | 12 | * @return bool |
13 | 13 | */ |
14 | 14 | public static function ckSchema( $updater = null ) { |
15 | | - if ( $updater !== null ){ |
| 15 | + if ( $updater !== null ) { |
16 | 16 | $updater->addExtensionUpdate( array( 'addtable', 'online_status', dirname( __FILE__ ) . '/OnlineStatusBar.sql', true ) ); |
17 | 17 | } else { |
18 | 18 | global $wgExtNewTables; |
— | — | @@ -47,48 +47,35 @@ |
48 | 48 | * @return bool |
49 | 49 | */ |
50 | 50 | public static function renderBar( &$article, &$outputDone, &$pcache ) { |
51 | | - global $messages, $wgOnlineStatusBarDefaultIpUsers, $wgOnlineStatusBarModes, $wgOut; |
| 51 | + $context = $article->getContext(); |
| 52 | + |
52 | 53 | 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; |
86 | 58 | } |
| 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 | + |
87 | 74 | return true; |
88 | 75 | } |
89 | 76 | |
90 | 77 | /** |
91 | 78 | * @param $user User |
92 | | - * @paramNireferences array |
| 79 | + * @param $preferences array |
93 | 80 | * @return bool |
94 | 81 | */ |
95 | 82 | public static function preferencesHook( User $user, array &$preferences ) { |
— | — | @@ -97,10 +84,10 @@ |
98 | 85 | $preferences['OnlineStatusBar_hide'] = array( 'type' => 'toggle', 'label-message' => 'onlinestatusbar-hide', 'section' => 'misc/onlinestatus' ); |
99 | 86 | $preferences['OnlineStatusBar_status'] = array( 'type' => 'radio', 'label-message' => 'onlinestatusbar-status', 'section' => 'misc/onlinestatus', |
100 | 87 | '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' |
105 | 92 | ), |
106 | 93 | ); |
107 | 94 | return true; |
— | — | @@ -125,8 +112,8 @@ |
126 | 113 | * @param $ln string? |
127 | 114 | * @return bool |
128 | 115 | */ |
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' ); |
131 | 118 | return true; |
132 | 119 | } |
133 | 120 | |
— | — | @@ -135,7 +122,7 @@ |
136 | 123 | * @param $skin Skin |
137 | 124 | * @return bool |
138 | 125 | */ |
139 | | - public static function stylePage ( &$out, &$skin ) { |
| 126 | + public static function stylePage( &$out, &$skin ) { |
140 | 127 | $out->addModules( 'ext.OnlineStatusBar' ); |
141 | 128 | return true; |
142 | 129 | } |
— | — | @@ -144,7 +131,7 @@ |
145 | 132 | * @param $vars array |
146 | 133 | * @return bool |
147 | 134 | */ |
148 | | - public static function magicWordSet ( &$vars ) { |
| 135 | + public static function magicWordSet( &$vars ) { |
149 | 136 | $vars[] = 'isonline'; |
150 | 137 | return true; |
151 | 138 | } |
— | — | @@ -156,25 +143,19 @@ |
157 | 144 | * @param $ret string? |
158 | 145 | * @return bool |
159 | 146 | */ |
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 | + } |
169 | 151 | |
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; |
178 | 157 | } |
| 158 | + |
| 159 | + $ret = $result[0]; |
179 | 160 | return true; |
180 | 161 | } |
181 | | -} |
| 162 | +} |
\ No newline at end of file |