Difference between revisions of "Extension:CurrentPages"
(comment) |
(1.0.1 - forgot to clear out old data on change of hour) |
||
| 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.1, 2008-05-25'); |
$egCurrentPagesMagic = 'currentpages'; | $egCurrentPagesMagic = 'currentpages'; | ||
| Line 45: | Line 45: | ||
$egCurrentPagesData = $data ? unserialize($data) : array(); | $egCurrentPagesData = $data ? unserialize($data) : array(); | ||
| − | # | + | # If the hour has changed, clear any existing data out |
| − | |||
$hour = strftime('%H'); | $hour = strftime('%H'); | ||
| − | if (!isset($egCurrentPagesData[$hour])) $egCurrentPagesData[$hour] = array(); | + | if (!isset($egCurrentPagesData[$hour]) || (isset($egCurrentPagesData['H']) && $egCurrentPagesData['H'] != $hour)) |
| + | $egCurrentPagesData[$hour] = array(); | ||
| + | $egCurrentPagesData['H'] = $hour; | ||
| + | |||
| + | # Increment the entry for current hour and title | ||
$egCurrentPagesData[$hour][$title] = isset($egCurrentPagesData[$hour][$title]) ? $egCurrentPagesData[$hour][$title]+1 : 1; | $egCurrentPagesData[$hour][$title] = isset($egCurrentPagesData[$hour][$title]) ? $egCurrentPagesData[$hour][$title]+1 : 1; | ||
| Line 81: | Line 84: | ||
# Convert the data into a list of title => viewcount | # Convert the data into a list of title => viewcount | ||
$egCurrentPagesTitles = array(); | $egCurrentPagesTitles = array(); | ||
| − | foreach ($egCurrentPagesData as $titles) { | + | foreach ($egCurrentPagesData as $hour => $titles) { |
| − | foreach ($titles as $title => $views) { | + | if (is_numeric($hour)) { |
| − | + | foreach ($titles as $title => $views) { | |
| + | $egCurrentPagesTitles[$title] = isset($egCurrentPagesTitles[$title]) ? $egCurrentPagesTitles[$title]+$views : $views; | ||
| + | } | ||
} | } | ||
} | } | ||
Revision as of 01:11, 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.1, 2008-05-25');
$egCurrentPagesMagic = 'currentpages'; $egCurrentPagesThreshold = 10; # Minimum number of views a title must have to be shown in the list
$egCurrentPagesFile = dirname(__FILE__).'/CurrentPages.data'; # file used to store $egCurrentPagesData array
$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 $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
$title = Title::newFromText($_REQUEST['title']); if (!is_object($title) || !$title->exists()) return; # Don't include non-existent titles
# Only include item if it can resolve to a text name $title = $title->getPrefixedText(); if ($title) {
# Read the $egCurrentPagesData array from file $data = file_get_contents($egCurrentPagesFile); $egCurrentPagesData = $data ? unserialize($data) : array();
# If the hour has changed, clear any existing data out $hour = strftime('%H'); if (!isset($egCurrentPagesData[$hour]) || (isset($egCurrentPagesData['H']) && $egCurrentPagesData['H'] != $hour)) $egCurrentPagesData[$hour] = array(); $egCurrentPagesData['H'] = $hour;
# Increment the entry for current hour and title $egCurrentPagesData[$hour][$title] = isset($egCurrentPagesData[$hour][$title]) ? $egCurrentPagesData[$hour][$title]+1 : 1;
# Write the $egCurrentPagesData array back to file file_put_contents($egCurrentPagesFile, serialize($egCurrentPagesData)); } }
/**
* 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, $egCurrentPagesThreshold; $parser->disableCache();
# Build sorted totals by title if (!is_array($egCurrentPagesTitles)) { global $egCurrentPagesFile, $egCurrentPagesData;
# Read the $egCurrentPagesData array from file if non existent if (!is_array($egCurrentPagesData)) { $data = file_get_contents($egCurrentPagesFile); $egCurrentPagesData = $data ? unserialize($data) : array(); }
# Convert the data into a list of title => viewcount $egCurrentPagesTitles = array(); foreach ($egCurrentPagesData as $hour => $titles) { if (is_numeric($hour)) { foreach ($titles as $title => $views) { $egCurrentPagesTitles[$title] = isset($egCurrentPagesTitles[$title]) ? $egCurrentPagesTitles[$title]+$views : $views; } } }
# Sort the titles by view-count arsort($egCurrentPagesTitles, SORT_NUMERIC); }
# Render the title list $list = ; if ($n < 1) $n = 10; foreach ($egCurrentPagesTitles as $title => $views) { if (--$n == 0 || $views < $egCurrentPagesThreshold) break; $title = preg_replace("/^(Category|Image):/",":$1:",$title); $list .= "{$start}[[{$title}]] ({$views}){$end}"; } return $list; }



