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 watching users and top watched pages defaulted to 10 each but with ability to raise that limit.

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 $tables = array("watchlist", "user"); $columns = array("user_name", "user_real_name", "COUNT(1) AS pages_watched_by_user"); $conditions = array("wl_user = user_id", "wl_namespace IN (".implode(", ", array_keys($namespace_titles)).")"); $fname = "Database::select"; $options = array(           "GROUP BY" => "wl_user",            "ORDER BY" => "COUNT(1) DESC",        ); if ($user_limit > 0) { $options["LIMIT"] = $user_limit; }       if (false == $result = $db->select($tables, $columns, $conditions, $fname, $options)) { $output .= ' Error accessing watchlist. ';       } else if($db->numRows($result) == 0) { $output .= ' Nobody is watching this page. ';       } else { $output .= ' ';       }        $wgOut->addHTML($output); return false; }   function onUnknownAction($action, $article) { global $wgOut, $wgSitename, $wgCachePages, $wgLang, $wgUser, $wgTitle, $wgDenyAccessMessage, $wgAllowAnonUsers, $wgRequest,$wgMessageCache, $wgWatchingMessages, $namespace_titles, $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); }       //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;            }            //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(str_replace(" ","_",$wgSitename))) {                    $page_namespace = constant("NS_PROJECT");                } else {                    $page_namespace = constant("NS_".strtoupper($pt[0]));                }                $page_title = $pt[1]; }           $db = &wfGetDB(DB_SLAVE); $tables = array("user", "watchlist"); $conditions = array("wl_user = user_id", "wl_namespace IN (".implode(", ", array_keys($namespace_titles)).")"); $fname = "Database::select"; //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("wl_namespace","wl_title"); $conditions[] = "LOWER(user_name) = ". $db->addQuotes(strtolower($_REQUEST["user_name"])); } else { $wgOut->setPagetitle(wfMsg("users_watching_page")); $columns = array("user_name", "user_real_name"); $conditions[] = "LOWER(wl_title) = ". $db->addQuotes(strtolower($page_title)); }           $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"; }           $options = array("ORDER BY" => "$order_col $ordertype"); $output = ""; if (false == $result = $db->select($tables, $columns, $conditions, $fname, $options)) { $output .= ' Error accessing watchlist. ';           } else if($db->numRows($result) == 0) { $output .= ' Nobody is watching this page. ';           } else { $style = ' style="border-bottom:2px solid #000; text-align:left;"'; $output .= ' '; }           $wgOut->addHTML($output); return false; } else { return true; }   }    function onSkinTemplateTabs(&$skin, &$content_actions) { global $wgRequest, $wgUser, $wgSitename; if (!$wgUser->isAllowed("whoiswatchingtabbed")) { return false; }       $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(str_replace(" ","_",$wgSitename))) { $page_namespace = constant("NS_PROJECT"); } else { $page_namespace = constant("NS_".strtoupper($pt[0])); }           $page_title = $pt[1]; }       $tables = array("watchlist","user"); $columns = array("COUNT(1) AS users_watching_page"); $conditions = array("wl_user = user_id", "LOWER(wl_title) = ". $db->addQuotes(strtolower($page_title)),"wl_namespace=".$page_namespace); $fname = "Database::select"; $watcherCount = 0; if (false == $result = $db->select($tables, $columns, $conditions, $fname)) { $output .= ' Error accessing watchlist. ';       } else { $row = $db->fetchRow($result); $watcherCount = $row["users_watching_page"]; $content_actions["watching"] = array(               "class" => ($action == "watching") ? "selected" : false,                "text" => "who is watching this? (".$watcherCount.")",                "href" => $skin->mTitle->getLocalURL("action=watching")            ); }       //tab links 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

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/message initializations.

Revisions

 * 2008.06.30 - Cleaned up last revision.
 * 2008.06.27 - Added limit selection to top ten lists on the special page.
 * 2008.05.08 - Fixed namespace issues and added the link to the special page list.
 * 2008.05.05 - Initial version released.