Extension:WhoIsWatchingTabbed

What can this extension do?
When installed this extension will create a tab in the tab bar that has the text "Who's watching this? (X)" where X is the number of users watching the page. When the tab is viewed it will display a grid of users who are watching the page. Each result will display the username, user real name, and page name. Usernames are linked to the same watching page, but will display all pages that the user is watching. Page names are linked to the same page, but will display all users watching that page. This installation will provide access only to group, sysop. An item will be added to the special page list for accessing this extension. Viewing the special page will display top ten watching users and top ten watched pages.

Download instructions
Please cut and paste the code found below and place it in the correct file in the  directory. Note: $IP stands for the root directory of your MediaWiki installation, the same directory that holds LocalSettings.php.

Installation
To install this extension, add the following to LocalSettings.php:

User rights
If you want to modify the groups that can access/view the tab you should add them to the $wgGroupPermissionsArray where the second index is 'whoiswatchingtabbed'.

Code
To use this extension you will need to create a new directory in the extensions directory called, WhoIsWatchingTabbed. Inside of that directory you will need to create two files: WhoIsWatchingTabbed.php and WhoIsWatchingTabbed.i18n.php. You will find the contents for each file below, just copy and paste. Ensure that you have added the two lines above to LocalSettings.php.

WhoIsWatchingTabbed.php ';   }    //Get the users who are watching the most pages $query = "SELECT ".(in_array(strtolower($wgDBtype),$db_top) ? 'TOP 10' : '')." COUNT(1) AS theCount, u.user_name, u.user_real_name ". "FROM ".$db->tableName("watchlist") ." AS w JOIN ".$db->tableName("user")." AS u ON u.user_id=w.wl_user ". "WHERE w.wl_namespace IN (".NS_MEDIA.",".NS_SPECIAL.",".NS_MAIN.",".NS_USER.",".NS_IMAGE.",".NS_MEDIAWIKI.",".NS_TEMPLATE.",".NS_HELP.",".NS_CATEGORY.",".NS_PROJECT." ) ". "GROUP BY w.wl_user ". "ORDER BY theCount DESC ".(in_array(strtolower($wgDBtype),$db_limit) ? 'LIMIT 10' : ''); if (false == $result = $db->doQuery($query)) { $output .= ' Error accessing watchlist. ';   } else if($db->numRows($result) == 0) { $output .= ' Nobody is watching any page. ';   } else { $output .= ' '; }   $wgOut->addHTML($output); return false; } function onUnknownAction($action, $article) { global $wgOut, $wgSitename, $wgCachePages, $wgUser, $wgTitle, $wgDenyAccessMessage, $wgAllowAnonUsers, $wgRequest,$wgMessageCache, $wgWatchingMessages, $namespace_title, $wgSitename; require_once("WhoIsWatchingTabbed.i18n.php");

$wgCachePages = false; // Gather all the messages from the language file. foreach($wgWatchingMessages as $language_code => $message) { $wgMessageCache->addMessages($message, $language_code); }   //Determine which results are going to be shown and the appropriate columns if(isset($_REQUEST["user_name"])) { $wgOut->setPagetitle(wfMsg("pages_watched_by_user")); $columns = array("page_namespace","page_title"); } else { $columns = array("user_name","user_real_name"); $wgOut->setPagetitle(wfMsg("users_watching_page")); }   //Verify that the action coming in is 'watching' (not to be confused with the system action 'watch/unwatch'    if($action == "watching") {      if (!$wgUser->isAllowed("whoiswatchingtabbed")) {        $wgOut->permissionRequired("whoiswatchingtabbed");        return true;      }      $db   = &wfGetDB(DB_SLAVE);

$order_col = "user_name"; if(isset($_REQUEST["order_col"]) && in_array($_REQUEST["order_col"], $columns)) { $order_col = $_REQUEST["order_col"]; }     //Change the way the results are ordered if(isset($_REQUEST["order_type"]) && $_REQUEST["order_type"] == "DESC") { $ordertype = "DESC"; $ordertypeR = "ASC"; } else { $ordertype = "ASC"; $ordertypeR = "DESC"; }     //Determine the namespace and the title of the current page. $pt = explode(":",$_REQUEST['title']); if(sizeof($pt)==1) { $page_namespace = 0; $page_title = $pt[0]; } else { //Check for namespace that is the NS_PROJECT name if(strtoupper($pt[0])==strtoupper($wgSitename)) { $page_namespace = constant("NS_PROJECT"); } else { $page_namespace = constant("NS_".strtoupper($pt[0])); }       $page_title = $pt[1]; }

//Which query to run? If user_name is set then do the 'User X is watching these pages', else X users are watching this page. if(isset($_REQUEST["user_name"])) { $query = "SELECT w.wl_namespace AS page_namespace, w.wl_title AS page_title " ."FROM ". $db->tableName("user"). " AS u " ."JOIN ". $db->tableName("watchlist"). " AS w " ."ON u.user_id = w.wl_user " ."WHERE LOWER(u.user_name) = ". $db->addQuotes(strtolower($_REQUEST["user_name"])) ." " ."AND w.wl_namespace IN (".NS_MEDIA.",".NS_SPECIAL.",".NS_MAIN.",".NS_USER.",".NS_IMAGE.",".NS_MEDIAWIKI.",".NS_TEMPLATE.",".NS_HELP.",".NS_CATEGORY.",".NS_PROJECT." ) " ."ORDER BY ". $order_col. " " . $ordertype; } else { $query = "SELECT u.user_name, u.user_real_name " ."FROM ". $db->tableName("user"). " AS u " ."JOIN ". $db->tableName("watchlist"). " AS w " ."ON u.user_id = w.wl_user " ."WHERE LOWER(w.wl_title) = ". $db->addQuotes(strtolower($page_title)) ." " ."AND w.wl_namespace = ".$db->addQuotes(strtolower($page_namespace))." " ."ORDER BY ". $order_col. " " . $ordertype; }      $output = ""; if (false == $result = $db->doQuery($query)) { $output .= ' Error accessing watchlist. ';     } else if($db->numRows($result) == 0) { $output .= ' Nobody is watching this page. ';     } else { $output .= ' '; }     $wgOut->addHTML($output); return false; } else { return true; } }  function onSkinTemplateTabs(&$skin, &$content_actions) { global $wgRequest, $wgUser, $wgSitename; $action = $wgRequest->getText('action'); $db = &wfGetDB(DB_SLAVE); $page_title = $_REQUEST['title']; $page_namespace = 0; $pt = explode(":",$_REQUEST['title']); if(sizeof($pt)>1) { if(strtoupper($pt[0])==strtoupper($wgSitename)) { $page_namespace = constant("NS_PROJECT"); } else { $page_namespace = constant("NS_".strtoupper($pt[0])); }     $page_title = $pt[1]; }   //Get count for display in tab $query = "SELECT COUNT(1) AS users_watching " ."FROM ". $db->tableName("watchlist"). " AS w " ."WHERE LOWER(w.wl_title) = ". $db->addQuotes(strtolower($page_title))." AND w.wl_namespace=".$page_namespace." "; $watcherCount = 0;

if ($result = $db->doQuery($query)) { $row = $db->fetchRow($result); $watcherCount = $row["users_watching"]; }   //tab links $content_actions["watching"] = array(     'class' => ($action == 'watching') ? 'selected' : false,      'text' => "who is watching this? (".$watcherCount.")",      'href' => $skin->mTitle->getLocalURL('action=watching')     ); return false; } } //end class /* Global function */ function wfSetupWhoIsWatchingTabbed { global $wgUser; SpecialPage::addPage( new WhoIsWatchingTabbed ); if ($wgUser->isAllowed('whoiswatchingtabbed')) { global $wgWhoIsWatchingTabbed; $wgWhoIsWatchingTabbed = new WhoIsWatchingTabbed; } }
 * 1) Called from $wgExtensionFunctions array when initialising extensions

WhoIsWatchingTabbed.i18n.php

Known Issues

 * In MediaWiki 1.10.0 it has been found that this extension will break the Special:Version page for sysops. This issue is not present in 1.11.1.
 * The language file isn't entirely useful. There is some logic and text initializations occurring outside of the rest of the array/messsage initializations.

Revisions

 * 2008.05.08 - Fixed namespace issues and added the link to the special page list.
 * 2008.05.05 - Initial version released.