Revision 1424479 of "Extension:TopTenPages/Code" on mediawikiwiki

{{warning|This code doesn't work on MediaWiki 1.25+}}

__TOC__
==TopTenPages.php==
<syntaxhighlight lang="php">
<?php
 
# To install the extension, add to your LocalSettings.php:
# require_once("$IP/extensions/TopTenPages.php");

/*
	Syntax:
	<TopTenPages/>
	<TopTenPages>5</TopTenPages>
	<TopTenPages offset="1"/>
	{{Special:TopTenPages}}
	{{Special:TopTenPages/-/5}}
	{{Special:TopTenPages/1/5}}
*/
 
$wgExtensionCredits['specialpage'][] = array(
	'name' => 'TopTenPages',
	'version' => '0.3.2',
	'author' => array(
		'Timo Tijhof',
		'Sascha',
	),
	'url' => 'https://www.mediawiki.org/wiki/Extension:TopTenPages',
	'description' => 'Shows most viewed pages.',
);
 
$wgAutoloadClasses['SpecialTopTenPages'] = __DIR__ . '/SpecialTopTenPages.php';
$wgSpecialPages['TopTenPages'] = 'SpecialTopTenPages';
$wgSpecialPageGroups['TopTenPages'] = 'other';
 
$wgExtensionFunctions[] = 'efTopTenPages';

# When including, always start the list numbering at one, even if offset was set.
# Defaults to false so that if (for example) offset is 1, the list will be numbered 2, 3, 4 ...
$wgttpAlwaysStartAtOne = false;
 
function efTopTenPages() {
	global $wgParser;
	$wgParser->setHook( 'TopTenPages', 'efTopTenPagesRender' );
}
 
/**
 * The callback function for converting the input text to HTML output.
 */
function efTopTenPagesRender( $text, array $args, Parser $parser, PPFrame $frame ) {
	if (array_key_exists('offset', $args)) {
		$offset = (int) $args['offset'];
	} else {
		$offset = 0;
	}
	if ($text > 0){
		$limit = (int) $text;
	} else {
		$limit = 10;
	}
 	return $parser->recursiveTagParse( "{{Special:TopTenPages/$offset/$limit}}", $frame );
}

</syntaxhighlight>
==SpecialTopTenPages.php==
<syntaxhighlight lang="php">

<?php
class SpecialTopTenPages extends PopularPagesPage {
	public function __construct( $name = 'TopTenPages' ) {
		parent::__construct( $name );
		$inc = $this->including();
	}
 
	public function isIncludable() {
		return true;
	}
 
	function isListed() {
		return false;
	}
 
	function execute( $par ) {
		$inc = $this->including();
 
		if ( $inc ) {
			$parts = explode( '/', $par, 3 );
			$this->offset = (int)$parts[0];
			$this->limit = (int)$parts[1];
		}
		$this->setListoutput( false );
		$this->shownavigation = !$inc;
		parent::execute( $par );
	}

	function openList( $offset ) {
		global $wgttpAlwaysStartAtOne;
		if ( $wgttpAlwaysStartAtOne ) {
			return parent::openList( 0 );
		} else {
			return parent::openList( $offset );
		}

	}
}

</syntaxhighlight>