Difference between revisions of "Extension:CurrentPages"

From Organic Design wiki
(1.0.1 - forgot to clear out old data on change of hour)
m (Nad moved page Extension:CurrentPages.php to Extension:CurrentPages without leaving a redirect)
 
(21 intermediate revisions by the same user not shown)
Line 1: Line 1:
<?php
+
{{svn|extensions|MediaWiki/CurrentPages/CurrentPages.php}}
# Extension:CurrentPages{{Category:Extensions|CurrentUsers}}{{php}}
+
[[Category:Extensions|CurrentPages]]
# - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html)
 
# - Author: [http://www.organicdesign.co.nz/nad 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'      => '[http://www.organicdesign.co.nz/nad 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}]] <span class=\"currentpages_views\">({$views})</span>{$end}";
 
}
 
return $list;
 
}
 

Latest revision as of 10:24, 30 April 2015

Info.svg This code is in our Git repository here.

Note: If there is no information in this page about this code and it's a MediaWiki extension, there may be something at mediawiki.org.