Difference between revisions of "Extension:CurrentPages"

From Organic Design wiki
(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.0, 2008-05-25');
+
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();
  
# Add the current title data to the array
+
# If the hour has changed, clear any existing data out
# $egCurrentPagesData[ current hour (00-23) ][ requested title ] = view count
 
 
$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)) {
$egCurrentPagesTitles[$title] = isset($egCurrentPagesTitles[$title]) ? $egCurrentPagesTitles[$title]+$views : $views;
+
foreach ($titles as $title => $views) {
 +
$egCurrentPagesTitles[$title] = isset($egCurrentPagesTitles[$title]) ? $egCurrentPagesTitles[$title]+$views : $views;
 +
}
 
}
 
}
 
}
 
}

Revision as of 01:11, 25 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.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; }