Extension:IncludeArticle

From MediaWiki.org
Jump to: navigation, search
MediaWiki extensions manual - list
Crystal Clear action run.png
IncludeArticle

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)
readme.txt

Translate the IncludeArticle extension if possible

Check usage and version matrix; code metrics

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.

Example[edit | edit source]

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

<IncArticle>{{{content}}}</IncArticle>

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}}} ==
{{{content}}}
</IncArticle>

Installation[edit | edit source]

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

Options[edit | edit source]

  • 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

Parameters[edit | edit source]

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

Changes to LocalSettings.php[edit | edit source]

require_once("$IP/extensions/IncludeArticle/IncludeArticle.php");

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.

Code[edit | edit source]

<?php
# 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') ) {
  die();
}
 
$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())
    {
       $rev=Revision::newFromTitle($preloadTitle);
       if ( is_object( $rev ) )
       {
       	  $text = $rev->getText();
          $text = preg_replace( '~</?includeonly>~', '', $text );
          $aTitlecomp = $rev->getTitle()->getPrefixedText();
          $aTitleblank = $rev->getTitle()->getText();
       }
       else
       {
       	   $text = '';
       }
    }
    if ($argv["lines"]==false)
    {
    	$text = substr($text, $articlestart, $articleend);
    }
    else
    {
    	$linestart=$articlestart;
    	$lineend=$articleend;
    	$artoutput="";
    	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;
 
    }
    $newvariables["title"]=$aTitlecomp;
    $newvariables["titleblank"]=$aTitleblank;
    $newvariables["content"]=$text;
    $variables = $parser->replaceVariables( $input, $newvariables );
    $output = $parser->parse( $variables, $parser->mTitle, $parser->mOptions, true, false );
    $inhalt = $output->getText();
    $html=$inhalt;
    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;
}

Alternatives[edit | edit source]

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:

{{:IncludePage}}

See also[edit | edit source]