Difference between revisions of "Extension:CurrentPages"
(1.0.2alpha - change storage from file to DB - not tested or debugged yet) |
(1.0.2 - debugged and tested DB code) |
||
| 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. | + | define('CURRENTPAGES_VERSION','1.0.2, 2008-05-25'); |
$egCurrentPagesMagic = 'currentpages'; | $egCurrentPagesMagic = 'currentpages'; | ||
| Line 33: | Line 33: | ||
# Get article ID corresponding to title request | # Get article ID corresponding to title request | ||
$title = is_object($wgTitle) ? $wgTitle : Title::newFromText($_REQUEST['title']); | $title = is_object($wgTitle) ? $wgTitle : Title::newFromText($_REQUEST['title']); | ||
| − | $ | + | $page = is_object($title) ? $title->getArticleID() : 0; |
| − | if ($ | + | if ($page < 1) return; |
# Create the DB table if it doesn't exist | # Create the DB table if it doesn't exist | ||
| Line 42: | Line 42: | ||
$query = "CREATE TABLE $table (hour INTEGER, page INTEGER, views INTEGER);"; | $query = "CREATE TABLE $table (hour INTEGER, page INTEGER, views INTEGER);"; | ||
$result = $db->query($query); | $result = $db->query($query); | ||
| − | |||
} | } | ||
# 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'); | + | $hour = strftime('%H') +2; |
| − | $cond = | + | $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); | |
| − | if ($row | + | $db->delete($table, array('hour' => $hour)); |
| − | |||
| − | $db->update($table, array('hour' => -1, 'views' => $hour), $cond); | ||
| − | $db->delete($table, | ||
} | } | ||
| − | } else | + | } else $db->insert($table, array('hour' => -1, 'views' => $hour)); |
| − | |||
| − | |||
| − | |||
| − | |||
# Increment a title | # Increment a title | ||
| − | $cond = | + | $cond = array('hour' => $hour, 'page' => $page); |
| − | $db->selectRow($table, 'views', $cond | + | 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)); | |
| − | $db->update($table, array('hour' => $hour, 'page' => $page, 'views' => $row | ||
| − | |||
| − | else | ||
| − | |||
| − | |||
| − | |||
| − | |||
} | } | ||
| Line 93: | Line 78: | ||
# 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 = $ | + | $table = $dbr->tableName('currentpages_hourlyviews'); |
| − | $res = $ | + | $res = $dbr->select( |
$table, | $table, | ||
'page, SUM(views) AS totals', | 'page, SUM(views) AS totals', | ||
| Line 104: | Line 89: | ||
# Render the title list | # Render the title list | ||
$list = ''; | $list = ''; | ||
| − | while ($row = $ | + | while ($row = $dbr->fetchRow($res)) { |
$title = Title::newFromID($row[0]); | $title = Title::newFromID($row[0]); | ||
| − | $page = $title->getPrefixedText(); | + | if (is_object($title)) { |
| − | + | $page = $title->getPrefixedText(); | |
| − | + | $link = $title->getText(); | |
| − | } | + | $list .= "{$start}[[:{$page}|{$link}]] <span class=\"currentpages_views\">({$row[1]})</span>{$end}"; |
| − | $ | + | } |
| + | } | ||
| + | $dbr->freeResult($res); | ||
return $list; | return $list; | ||
} | } | ||
Revision as of 07:47, 25 May 2008
<?php
- Extension:CurrentPages
- - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html)
- - Author: User:Nad
- - Started: 2008-05-24
if (!defined('MEDIAWIKI')) die('Not an entry point.');
define('CURRENTPAGES_VERSION','1.0.2, 2008-05-25');
$egCurrentPagesMagic = 'currentpages';
$wgExtensionFunctions[] = 'efSetupCurrentPages'; $wgHooks['LanguageGetMagic'][] = 'efCurrentPagesLanguageGetMagic';
$wgExtensionCredits['parserhook'][] = array( 'name' => 'CurrentPages', 'author' => 'User:Nad', 'description' => 'Adds a magic word for return a bullet list of most viewed pages within the last 24 hours.', 'url' => 'http://www.mediawiki.org/wiki/Extension:CurrentPages', 'version' => CURRENTPAGES_VERSION );
/**
* Called from $wgExtensionFunctions array when initialising extensions */
function efSetupCurrentPages() { global $wgTitle, $wgUser, $wgParser, $egCurrentPagesMagic, $egCurrentPagesData, $egCurrentPagesFile; $wgParser->setFunctionHook($egCurrentPagesMagic, 'efCurrentPagesMagic');
if (isset($_REQUEST['action'])) return; # Don't count pages which are not a simple page view request
# Get article ID corresponding to title request $title = is_object($wgTitle) ? $wgTitle : Title::newFromText($_REQUEST['title']); $page = is_object($title) ? $title->getArticleID() : 0; if ($page < 1) return;
# 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);"; $result = $db->query($query); }
# If the hour has changed, clear any existing data out # - current hour is stored in a special row where hour is -1 $hour = strftime('%H') +2; $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 a title $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));
}
/**
* 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; }
function efCurrentPagesMagic(&$parser, $n = 0, $start = "*", $end = "\n") { global $egCurrentPagesTitles; $parser->disableCache();
# 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; }



