From MediaWiki.org
Jump to navigation Jump to search
MediaWiki extensions manual
Crystal Clear action run.svg
Release status: beta
Implementation Data extraction
Description Include articles in an article
Author(s) Markus Rückerl (Bombertalk)
Latest version 0.1
License GNU General Public License 2.0 or later
Download IncludeArticle.php (right-click & save-as)
Translate the IncludeArticle extension if it is available at translatewiki.net
Check usage and version matrix.

The Include Article extension allows any page to be shown on any other wiki page. An editor can choose how many letters or lines of the article are shown.

The page is shown with a custom tag, <IncArticle>. The editor has several filtering options. This extension allows full custom formatting of external pages, using regular wiki template coding.


To get the first 200 letters from a random article in the MAIN NAMESPACE


To get the first 300 letters from a random article:

<IncArticle count="300">{{{content}}}</IncArticle>

To define your own format for the generated article, you can use your own style.

<IncArticle count="300">
== {{{titleblank}}} ==


Copy the IncludeArticle directory into the extensions folder of your MediaWiki installation.


  • article: Which article to show
  • random: Overides article and shows a random article of the given namespaces
  • namespaces: Limit results to random articles. Multiple namespaces can be given separated by "|"; you can use "0", "-", or "main" to refer to the default namespace.
  • start: Which letter to start with
  • count: How many letters will be shown, negative values will display the whole article removing the specific number of letters from the end
  • lines: Count lines (not letters) that will be shown


  • title: Full title including namespace
  • titleblank: Title without namespace
  • content: Article content

Changes to LocalSettings.php[edit]


To make this extension work with mediawiki 1.12 or 1.13, add this line.

$wgParserConf = array ('class'=>'Parser_OldPP');

In mediawiki >= 1.14 the class 'Parser_OldPP' was dropped, so this extension would have to be fixed to use the new parser.


# Include Article Extension
# author Markus Rückerl, bomber-online.de
# copyright © 2007 Markus Rückerl
# licence GNU General Public Licence 2.0 or later

if( !defined('MEDIAWIKI') ) {

$wgExtensionFunctions[] = "wfIncArticleExtension";
$wgExtensionCredits['parserhook'][] = array(
  'name' => 'IncludeArticle',
  'author' => 'Markus Rueckerl',
  'url' => 'http://www.mediawiki.org/wiki/Extension:IncludeArticle',
  'description' => 'Allows the inclusion of any article to be shown on any wiki page.'

function wfIncArticleExtension() {
    global $wgParser;
    $wgParser->setHook( "IncArticle", "renderIncArticle" );

function renderIncArticle( $input, $argv, &$parser ) {
    global $wgUser;
    $newvariables[] = array();
    if (!isset($argv["start"])){$argv["start"]=0;}     
    if (!isset($argv["count"])){$argv["count"]=200;}
    if (!isset($argv["lines"])){$argv["lines"]=false;}
    if (!isset($argv['namespaces'])){$argv['namespaces']=0;}
    if (!isset($argv["article"])){$argv["article"]=getrandompage($argv['namespaces']);}
    if (isset($argv["random"])){$argv["article"]=getrandompage($argv['namespaces']);}
    $articlestart = $argv["start"];
    $articleend = $argv["count"] + $articlestart;
    $preloadTitle = Title::newFromText( $argv["article"] );
    if ( isset($preloadTitle ) && $preloadTitle->userCanRead())
       if ( is_object( $rev ) )
       	  $text = $rev->getText();
          $text = preg_replace( '~</?includeonly>~', '', $text );
          $aTitlecomp = $rev->getTitle()->getPrefixedText();
          $aTitleblank = $rev->getTitle()->getText();
       	   $text = '';
    if ($argv["lines"]==false)
    	$text = substr($text, $articlestart, $articleend);
    	for ($i=0;$i<=$lineend;$i++)
    		$endpos = stripos($text,"\n");
    		$artlen = strlen($text);
    		$artoutput2 = substr($text,0,$endpos+1);
                $artoutput = $artoutput.$artoutput2;
    		$text = substr($text, $endpos+1, $artlen);
    	for ($i=1;$i<$linestart;$i++)
    		$endpos = stripos($artoutput,"\n");
    		$artlen = strlen($artoutput);
    		$artoutput = substr($artoutput, $endpos+1, $artlen);
    	$text = $artoutput;

    $variables = $parser->replaceVariables( $input, $newvariables );
    $output = $parser->parse( $variables, $parser->mTitle, $parser->mOptions, true, false );
    $inhalt = $output->getText();
    return $html;

function getrandompage( $varns) {
	global $wgOut, $wgExtraRandompageSQL, $wgContLang;
	$namespaces = $varns;
	$namespaces = preg_split('!\s*(\|\s*)+!', trim( $namespaces ) );
        $fname = 'wfSpecialRandompage';
        $i = array_rand($namespaces);
	# Determine namespace
	$t = Title::newFromText ( $namespaces[$i] . ":Dummy" ) ;
	$namespace = $t->getNamespace () ;

	# NOTE! We use a literal constant in the SQL instead of the RAND()
	# function because RAND() will return a different value for every row
	# in the table. That's both very slow and returns results heavily
	# biased towards low values, as rows later in the table will likely
	# never be reached for comparison.
	# Using a literal constant means the whole thing gets optimized on
	# the index, and the comparison is both fast and fair.

	# interpolation and sprintf() can muck up with locale-specific decimal separator
	$randstr = wfRandom();

	$db =& wfGetDB( DB_SLAVE );
	$use_index = $db->useIndexClause( 'page_random' );
	$page = $db->tableName( 'page' );

	$extra = $wgExtraRandompageSQL ? "AND ($wgExtraRandompageSQL)" : '';
	$sql = "SELECT page_id,page_title
		FROM $page $use_index
		WHERE page_namespace=$namespace AND page_is_redirect=0 $extra
		AND page_random>$randstr
		ORDER BY page_random";
	$sql = $db->limitResult($sql, 1, 0);
	$res = $db->query( $sql, $fname );

	$title = null;
	if( $s = $db->fetchObject( $res ) ) {
		$title =& Title::makeTitle( $namespace, $s->page_title );
	if( is_null( $title ) ) {
		# That's not supposed to happen :)
		$title = Title::newMainPage();
	$title = $title->getPrefixedText();
	return $title;


Mediawiki >1.18: IncludeArticle isn't working well, there´s a problem to phrase. There´s now a built-in solution: Just have a look to Transclusion.

A neat trick to include another page, e.g. IncludePage, into the current one:


See also[edit]