Extension:CurrentPages

From Organic Design wiki
Revision as of 23:56, 24 May 2008 by Nad (talk | contribs) (1.0.0)

<?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.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; }