Difference between revisions of "Extension:CurrentPages"
(New page: <?php # Extension:CurrentPages{{Catego...) |
(1.0.0) |
||
| Line 1: | Line 1: | ||
<?php | <?php | ||
| − | # Extension:CurrentPages{{Category:Extensions| | + | # Extension:CurrentPages{{Category:Extensions|CurrentUsers}}{{php}} |
# - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html) | # - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html) | ||
# - Author: [http://www.organicdesign.co.nz/nad User:Nad] | # - Author: [http://www.organicdesign.co.nz/nad User:Nad] | ||
| Line 7: | Line 7: | ||
if (!defined('MEDIAWIKI')) die('Not an entry point.'); | if (!defined('MEDIAWIKI')) die('Not an entry point.'); | ||
| − | define('CURRENTPAGES_VERSION',' | + | define('CURRENTPAGES_VERSION','1.0.0, 2008-05-25'); |
$egCurrentPagesMagic = 'currentpages'; | $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'; | $wgExtensionFunctions[] = 'efSetupCurrentPages'; | ||
| Line 22: | Line 25: | ||
); | ); | ||
| − | + | /** | |
| + | * Called from $wgExtensionFunctions array when initialising extensions | ||
| + | */ | ||
function efSetupCurrentPages() { | function efSetupCurrentPages() { | ||
| − | global $wgUser,$wgParser,$egCurrentPagesMagic,$egCurrentPagesData; | + | global $wgUser, $wgParser, $egCurrentPagesMagic, $egCurrentPagesData, $egCurrentPagesFile; |
| − | $wgParser->setFunctionHook($egCurrentPagesMagic,'efCurrentPagesMagic'); | + | $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']); | $title = Title::newFromText($_REQUEST['title']); | ||
| − | if (!is_object($title) || !$title->exists()) return; | + | 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(); | $title = $title->getPrefixedText(); | ||
| − | + | if ($title) { | |
| − | + | ||
| − | + | # Read the $egCurrentPagesData array from file | |
| − | + | $data = file_get_contents($egCurrentPagesFile); | |
| + | $egCurrentPagesData = $data ? unserialize($data) : array(); | ||
| − | + | $hour = strftime('%H'); | |
| − | + | if (!isset($egCurrentPagesData[$hour])) $egCurrentPagesData[$hour] = array(); | |
| − | + | $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) { | function efCurrentPagesLanguageGetMagic(&$magicWords,$langCode = 0) { | ||
global $egCurrentPagesMagic; | global $egCurrentPagesMagic; | ||
| Line 49: | Line 63: | ||
} | } | ||
| − | function efCurrentPagesMagic(&$parser,$n = | + | function efCurrentPagesMagic(&$parser, $n = 0, $start = "*", $end = "\n") { |
| − | global $egCurrentPagesTitles; | + | global $egCurrentPagesTitles, $egCurrentPagesThreshold; |
$parser->disableCache(); | $parser->disableCache(); | ||
# Build sorted totals by title | # Build sorted totals by title | ||
if (!is_array($egCurrentPagesTitles)) { | if (!is_array($egCurrentPagesTitles)) { | ||
| − | global $egCurrentPagesData; | + | global $egCurrentPagesFile, $egCurrentPagesData; |
| + | # Read the $egCurrentPagesData array from file if non existent | ||
if (!is_array($egCurrentPagesData)) { | if (!is_array($egCurrentPagesData)) { | ||
| − | + | $data = file_get_contents($egCurrentPagesFile); | |
| − | $data = file_get_contents($ | ||
$egCurrentPagesData = $data ? unserialize($data) : array(); | $egCurrentPagesData = $data ? unserialize($data) : array(); | ||
} | } | ||
| + | # Convert the data into a list of title => viewcount | ||
$egCurrentPagesTitles = array(); | $egCurrentPagesTitles = array(); | ||
foreach ($egCurrentPagesData as $titles) { | foreach ($egCurrentPagesData as $titles) { | ||
| Line 69: | Line 84: | ||
} | } | ||
} | } | ||
| − | arsort($egCurrentPagesTitles,SORT_NUMERIC); | + | |
| + | # Sort the titles by view-count | ||
| + | arsort($egCurrentPagesTitles, SORT_NUMERIC); | ||
} | } | ||
| + | # Render the title list | ||
$list = ''; | $list = ''; | ||
| + | if ($n < 1) $n = 10; | ||
foreach ($egCurrentPagesTitles as $title => $views) { | foreach ($egCurrentPagesTitles as $title => $views) { | ||
| − | if (--$n == 0 || $views = | + | if (--$n == 0 || $views < $egCurrentPagesThreshold) break; |
| − | $list .= " | + | $title = preg_replace("/^(Category|Image):/",":$1:",$title); |
| + | $list .= "{$start}[[{$title}]] <span class=\"currentpages_views\">({$views})</span>{$end}"; | ||
} | } | ||
return $list; | return $list; | ||
} | } | ||
Revision as of 23:56, 24 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.0, 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();
$hour = strftime('%H'); if (!isset($egCurrentPagesData[$hour])) $egCurrentPagesData[$hour] = array(); $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 $titles) { 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; }



