Difference between revisions of "Extension:CurrentPages"

From Organic Design wiki
m
(1.0.5 - moved count code into ParserBeforeStrip)
Line 7: Line 7:
 
if (!defined('MEDIAWIKI')) die('Not an entry point.');
 
if (!defined('MEDIAWIKI')) die('Not an entry point.');
 
   
 
   
define('CURRENTPAGES_VERSION','1.0.4, 2008-05-25');
+
define('CURRENTPAGES_VERSION','1.0.5, 2008-05-26');
  
$egCurrentPagesMagic           = 'currentpages';
+
$egCurrentPagesMagic           = 'currentpages';
$wgExtensionFunctions[]       = 'efSetupCurrentPages';
+
$wgExtensionFunctions[]         = 'efCurrentPagesSetup';
$wgHooks['LanguageGetMagic'][] = 'efCurrentPagesLanguageGetMagic';
+
$wgHooks['ParserBeforeStrip'][] = 'efCurrentPagesCount';
 +
$wgHooks['LanguageGetMagic'][] = 'efCurrentPagesLanguageGetMagic';
 
   
 
   
 
$wgExtensionCredits['parserhook'][] = array(
 
$wgExtensionCredits['parserhook'][] = array(
Line 22: Line 23:
 
   
 
   
 
/**
 
/**
  * Called from $wgExtensionFunctions array when initialising extensions
+
  * Add the parser-function hook and create the counter table if non-existent
 
  */
 
  */
function efSetupCurrentPages() {
+
function efCurrentPagesSetup() {
global $wgTitle, $wgParser, $egCurrentPagesMagic;
+
global $wgParser, $egCurrentPagesMagic;
 +
 
 +
# Add parser-function hook
 
$wgParser->setFunctionHook($egCurrentPagesMagic, 'efCurrentPagesMagic');
 
$wgParser->setFunctionHook($egCurrentPagesMagic, 'efCurrentPagesMagic');
  
Line 36: Line 39:
 
$db->freeResult($res);
 
$db->freeResult($res);
 
}
 
}
 +
}
  
if (isset($_REQUEST['action'])) return; # Don't count pages which are not a simple page view request
+
/**
 +
* Called for a normal page view request to update the counter table
 +
*/
 +
function efCurrentPagesCount(&$parser) {
  
 
# Get article ID corresponding to title request or return if unattainable
 
# Get article ID corresponding to title request or return if unattainable
$title = is_object($wgTitle) ? $wgTitle : Title::newFromText($_REQUEST['title']);
+
$title = $parser->mTitle;
$page = is_object($title) ? $title->getArticleID() : 0;
+
$page = is_object($title) ? $title->getArticleID() : 0;
if ($page < 1) return;
+
if ($page < 1) return true;
  
 
# If the hour has changed, clear any existing data out
 
# If the hour has changed, clear any existing data out
 
# - current hour is stored in a special row where hour is -1
 
# - current hour is stored in a special row where hour is -1
$hour = strftime('%H');
+
$db    = &wfGetDB(DB_MASTER);
$cond = array('hour' => -1);
+
$table = $db->tableName('currentpages_hourlyviews');
 +
$hour = strftime('%H');
 +
$cond = array('hour' => -1);
 
if ($row = $db->selectRow($table, 'views', $cond)) {
 
if ($row = $db->selectRow($table, 'views', $cond)) {
 
if ($row->views != $hour) {
 
if ($row->views != $hour) {
Line 60: Line 69:
 
$db->update($table, array('hour' => $hour, 'page' => $page, 'views' => 1+$row->views), $cond);
 
$db->update($table, array('hour' => $hour, 'page' => $page, 'views' => 1+$row->views), $cond);
 
else $db->insert($table, array('hour' => $hour, 'page' => $page, 'views' => 1));
 
else $db->insert($table, array('hour' => $hour, 'page' => $page, 'views' => 1));
 +
 +
return true;
 
}
 
}
  
Line 70: Line 81:
 
 
 
# Query DB to get total views for each title over all hours
 
# Query DB to get total views for each title over all hours
$dbr = &wfGetDB(DB_SLAVE);
+
$dbr   = &wfGetDB(DB_SLAVE);
 
$table = $dbr->tableName('currentpages_hourlyviews');
 
$table = $dbr->tableName('currentpages_hourlyviews');
$res = $dbr->select(
+
$res   = $dbr->select(
 
$table, 'page, SUM(views) AS totals', 'hour >= 0', '',
 
$table, 'page, SUM(views) AS totals', 'hour >= 0', '',
 
array('GROUP BY' => 'page', 'ORDER BY' => 'totals DESC', 'LIMIT' => $n)
 
array('GROUP BY' => 'page', 'ORDER BY' => 'totals DESC', 'LIMIT' => $n)

Revision as of 00:09, 26 May 2008

<?php

  1. Extension:CurrentPages
Info.svg These are the MediaWiki extensions we're using and/or developing. Please refer to the information on the mediawiki.org wiki for installation and usage details. Extensions here which have no corresponding mediawiki article are either not ready for use or have been superseded. You can also browse our extension code in our local Subversion repository or our GitHub mirror.

Template:Php

  1. - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html)
  2. - Author: User:Nad
  3. - Started: 2008-05-24

if (!defined('MEDIAWIKI')) die('Not an entry point.');

define('CURRENTPAGES_VERSION','1.0.5, 2008-05-26');

$egCurrentPagesMagic = 'currentpages'; $wgExtensionFunctions[] = 'efCurrentPagesSetup'; $wgHooks['ParserBeforeStrip'][] = 'efCurrentPagesCount'; $wgHooks['LanguageGetMagic'][] = 'efCurrentPagesLanguageGetMagic';

$wgExtensionCredits['parserhook'][] = array( 'name' => 'CurrentPages', 'author' => 'User:Nad', 'description' => 'Adds a parser function to return a bullet list of most viewed pages within the last 24 hours.', 'url' => 'http://www.mediawiki.org/wiki/Extension:CurrentPages', 'version' => CURRENTPAGES_VERSION );

/**

* Add the parser-function hook and create the counter table if non-existent
*/

function efCurrentPagesSetup() { global $wgParser, $egCurrentPagesMagic;

# Add parser-function hook $wgParser->setFunctionHook($egCurrentPagesMagic, 'efCurrentPagesMagic');

# Create the DB table if it doesn't exist $db = &wfGetDB(DB_MASTER); $table = $db->tableName('currentpages_hourlyviews'); if (!$db->tableExists($table)) { $query = "CREATE TABLE $table (hour INTEGER, page INTEGER, views INTEGER);"; $res = $db->query($query); $db->freeResult($res); } }

/**

* Called for a normal page view request to update the counter table
*/

function efCurrentPagesCount(&$parser) {

# Get article ID corresponding to title request or return if unattainable $title = $parser->mTitle; $page = is_object($title) ? $title->getArticleID() : 0; if ($page < 1) return true;

# If the hour has changed, clear any existing data out # - current hour is stored in a special row where hour is -1 $db = &wfGetDB(DB_MASTER); $table = $db->tableName('currentpages_hourlyviews'); $hour = strftime('%H'); $cond = array('hour' => -1); if ($row = $db->selectRow($table, 'views', $cond)) { if ($row->views != $hour) { $db->update($table, array('hour' => -1, 'page' => -1, 'views' => $hour), $cond); $db->delete($table, array('hour' => $hour)); } } else $db->insert($table, array('hour' => -1, 'views' => $hour));

# Increment the view count for the current title and hour $cond = array('hour' => $hour, 'page' => $page); if ($row = $db->selectRow($table, 'views', $cond)) $db->update($table, array('hour' => $hour, 'page' => $page, 'views' => 1+$row->views), $cond); else $db->insert($table, array('hour' => $hour, 'page' => $page, 'views' => 1));

return true; }

/**

* Expand parser function
*/

function efCurrentPagesMagic(&$parser, $n = 0, $start = "*", $end = "\n") { $parser->disableCache(); if ($n < 1) $n = 10;

# Query DB to get total views for each title over all hours $dbr = &wfGetDB(DB_SLAVE); $table = $dbr->tableName('currentpages_hourlyviews'); $res = $dbr->select( $table, 'page, SUM(views) AS totals', 'hour >= 0', , array('GROUP BY' => 'page', 'ORDER BY' => 'totals DESC', 'LIMIT' => $n) );

# Render the title list $list = ; while ($row = $dbr->fetchRow($res)) { $title = Title::newFromID($row[0]); if (is_object($title)) { $page = $title->getPrefixedText(); $link = $title->getText(); $list .= "{$start}[[:{$page}|{$link}]] ({$row[1]}){$end}"; } } $dbr->freeResult($res);

return $list; }

/**

* Needed in MediaWiki >1.8.0 for magic word hooks to work properly
*/

function efCurrentPagesLanguageGetMagic(&$magicWords,$langCode = 0) { global $egCurrentPagesMagic; $magicWords[$egCurrentPagesMagic] = array($langCode,$egCurrentPagesMagic); return true; }