|
|
| (24 intermediate revisions by the same user not shown) |
| Line 1: |
Line 1: |
| − | <?php
| + | {{svn|extensions|MediaWiki/CurrentPages/CurrentPages.php}} |
| − | # Extension:CurrentPages{{Category:Extensions|CurrentPages}}{{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','0.0.0, 2008-05-24');
| |
| − | | |
| − | $egCurrentPagesMagic = 'currentpages';
| |
| − |
| |
| − | $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;
| |
| − | $wgParser->setFunctionHook($egCurrentPagesMagic,'efCurrentPagesMagic');
| |
| − | | |
| − | $title = Title::newFromText($_REQUEST['title']);
| |
| − | if (!is_object($title) || !$title->exists()) return;
| |
| − | $title = $title->getPrefixedText();
| |
| − |
| |
| − | $file = dirname(__FILE__).'/CurrentPages.data';
| |
| − | $data = file_get_contents($file);
| |
| − | $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;
| |
| − | | |
| − | file_put_contents($file,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 = 10) {
| |
| − | global $egCurrentPagesTitles;
| |
| − | $parser->disableCache();
| |
| − |
| |
| − | # Build sorted totals by title
| |
| − | if (!is_array($egCurrentPagesTitles)) {
| |
| − | global $egCurrentPagesData;
| |
| − |
| |
| − | if (!is_array($egCurrentPagesData)) {
| |
| − | $file = dirname(__FILE__).'/CurrentPages.data';
| |
| − | $data = file_get_contents($file);
| |
| − | $egCurrentPagesData = $data ? unserialize($data) : array();
| |
| − | }
| |
| − |
| |
| − | $egCurrentPagesTitles = array();
| |
| − | foreach ($egCurrentPagesData as $titles) {
| |
| − | foreach ($titles as $title => $views) {
| |
| − | $egCurrentPagesTitles[$title] = isset($egCurrentPagesTitles[$title]) ? $egCurrentPagesTitles[$title]+$views : $views;
| |
| − | }
| |
| − | }
| |
| − | arsort($egCurrentPagesTitles,SORT_NUMERIC);
| |
| − | }
| |
| − |
| |
| − | $list = '';
| |
| − | foreach ($egCurrentPagesTitles as $title => $views) {
| |
| − | if (--$n == 0 || $views == 1) break;
| |
| − | $list .= "*[[$title]] <span class=\"currentpages_views\">($views)</span>\n";
| |
| − | }
| |
| − | return $list;
| |
| − | }
| |