Jump to content

Extension talk:IncludeArticle

Add topic
From mediawiki.org
Latest comment: 7 years ago by Sauron~plwiki in topic My changes, to work with 1.29

My changes, to work with 1.29

[edit]

Since download does not work here is patched, original source. Sauron~plwiki (talk) 11:45, 26 August 2017 (UTC)Reply

My changes, to work with 1.16b3

[edit]
--- IncludeArticle.php-old      2010-06-09 19:59:43.636557466 +0200
+++ IncludeArticle.php  2010-06-09 20:06:46.808558295 +0200
@@ -7,22 +7,27 @@
 if( !defined('MEDIAWIKI') ) {
   die();
 }
- 
-$wgExtensionFunctions[] = "wfIncArticleExtension";
+
+$wgHooks['ParserFirstCallInit'][] = '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 wfIncArticleExtension(&$parser) {
+    $parser->setHook( 'IncArticle', 'renderIncArticle' );
+    return true;
 }
  
-function renderIncArticle( $input, $argv, &$parser ) {
+
+function renderIncArticle( $input, $argv, $parser, $frame ){
     global $wgUser;
+
+# To disable cache we need this:
+    $parser->disableCache();
     $newvariables[] = array();
     if (!isset($argv["start"])){$argv["start"]=0;}     
     if (!isset($argv["count"])){$argv["count"]=200;}

After upgrade from 1.15 to 1.16 "IncludeArticle" became unusable. This is quick fix and disableCache() (probably new cache system changed to much to work in old way).

Since download does not work here is patched, original source. Sauron 18:13, 9 June 2010 (UTC)Reply

how to include all articles from category?

[edit]

should i use a namespace?

[edit]

It appears to be trying to execute the php file rather than serve it up statically. --Jimbojw 17:44, 5 April 2007 (UTC)Reply

[edit]

Please use right click - save as ... to download the php-file.


definitely a problem

[edit]

i am having issues saving as myself. perhaps copy the code onto the wiki?

dynamic content update

[edit]

It would be nice to have a dynamic content update. Is this possible?

Category pages broken??

[edit]

Trying to use specified include pages that are categories, but this extrnsion does not seem to work :-(

I am using this code:

<IncArticle article="Category:SIPNEW"‎>{{{content}}}</IncArticle>
<IncArticle article="Category:SIPWIP"‎>{{{content}}}</IncArticle>
<IncArticle article="Category:SIPCOMPLETE"‎>{{{content}}}</IncArticle>

IncludeArticle - substr

[edit]

I would like to retrieve a segment of an article delimited with stringSTART and stringEND.

For example

<IncArticle article="Project 4643" stringSTART="XYZ" stringEND="ZYX">{{{content}}}</IncArticle>

I've included the following snipet..

if( isset( $argv["stringSTART"] ) AND isset( $argv["stringEND"] ) ) {
 $articlestart = strpos( $text, $argv["stringSTART"] );
 $articleend = strpos( $text, $argv["stringEND"] ) - $articlestart;
 $argv["lines"] = false;
} # end string delimited sub string specification

but can't get the stringSTART stringEND argv variables passed. What's the trick?

jtg

Include other wiki pages

[edit]

I want to include parts of Wikipedia articles on my wiki. Can this extension be expanded to do this? If not, is there another extension that can do it? —Eep² 07:59, 20 August 2007 (UTC)Reply

Did you try: Extension:Include --76.27.82.88 06:59, 23 September 2007 (UTC)Reply
[edit]

My experience is that if you use this to include another article and that article has sections in it, the edit links will display to users but they don't work correctly when someone clicks on them. FYI. --76.27.82.88 07:01, 23 September 2007 (UTC)Reply

Edit by Dannyle: Thats my problem, too. Would be a great idea to fix that.

Default to include the entire article

[edit]

I wanted IncludeArticle to default to include the entire article instead of the first 200 characters. In fact, there is no way to get the entire article unless you specify a sufficiently large count for each article individually.

I made the following change, which sets the default count to include the entire article, and also allows you to specify start.

function renderIncArticle( $input, $argv, &$parser ) {
    global $wgUser;
    $newvariables[] = array();
    if (!isset($argv["start"])){$argv["start"]=0;}     
    // --------------------------------------------------------------------------------
    // By default, set count to false
    if (!isset($argv["count"])){$argv["count"]=false;}
    // --------------------------------------------------------------------------------
    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)
    {
    // --------------------------------------------------------------------------------
                // if count is false, return the article starting at start until the end of the article
		if ($argv["count"]==false)
		{
			$text = substr($text, $articlestart);
		}
		else
		{
        	        $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;
}

Brion.finlay 21:02, 13 October 2007 (UTC)Reply

Filter to demote headers

[edit]

I would like a filter that demotes all headers when including an article, and the ability to construct a master table of content for an article built up of included articlesBrion.finlay 21:02, 13 October 2007 (UTC)Reply

Definitely agreed. --129.21.254.209 13:58, 9 March 2009 (UTC)Reply

Include content from Wikipedia into my wiki?

[edit]

Hi there, your extension is perfect except it works only in my wiki's main namespace. Can you explain how your extension might work to include content from Wikipedia (or any other external source housed in a MediaWiki-based site? Fingers crossed... Thanks for writing the extension!

Not yet compatible with 1.12 (new parser)

[edit]

I was only able to get IncludeArticle.php to work with 1.12 by reverting back to the older parser, with this code in LocalSettings.php:

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

I emailed the Extension author requesting coding changes to be compatible with the new parser. --12.187.196.130 11:56, 25 May 2008 (UTC)Reply

Modifications to accomodate subpages, break on white space character, and linked ellipsis

[edit]

Here is code for IncludeArticle.php with the following modifications:

    • Accomodates subpages, which show up in the page name after a slash character "/". Works for any number of levels of subpages
    • When the include limit is by number of characters, the limit is increased until the first white space character is found. This avoids breaking words in the middle, but does not prevent wiki text from being partially rendered.
    • Adds a hyperlinked ellipsis, which defaults to "<...more>" in the global variable $wgEllipsis. This link is appended to the end of the included text to make it easy for the reader to go to the included page to read the full text.

I use this functionality to include short exerpts from subpages on a main page, i.e. when the main page is a book review and the subpages are highlights and commentaries on specific sections in the book.

By using subpages, I can add semantic annotations to each one, while still aggregating them into sections on the main page (the book review).

This code is still not compatible with the new parser in 1.12. I hope the orgininal developer or someone else can make this change.

<?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
  
# RCT modified to add $ellipsis when transcluded text is cut short.  
# $ellipsis creates a hyperlink to the transcluded page, with the text in $wgEllipsis = "<...more>"  
  
if( !defined('MEDIAWIKI') ) {
  die(); 
}
 
$wgEllipsis = "<...more>";  ## RCT added
$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;
	global $wgEllipsis; ## RCT added
    $newvariables[] = array();
    if (!isset($argv["start"])){$argv["start"]=0;}     
    // --------------------------------------------------------------------------------
    // By default, set count to false
    if (!isset($argv["count"])){$argv["count"]=false;}
    // --------------------------------------------------------------------------------
    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();

			// RCT added to handle sub pages  -- this code works for any number of subpage levels
			$findslash   = '/';
			$pos = strpos($aTitleblank, $findslash);
			// Note use of !==.  Simply != would not work as expected
			// because the position of 'a' was the 0th (first) character.
			while ($pos !== false) {
    			// strip out everything that comes before the slash
				$aTitleblank = substr($aTitleblank, $pos+1);
				$pos = strpos($aTitleblank, $findslash);
			}
       }
       else
       {
           $text = '';
		   $ellipsis=""; ## RCT Added 
       }
    }
    if ($argv["lines"]==false)
    {
    // --------------------------------------------------------------------------------
                // if count is false, return the article starting at start until the end of the article
                if ($argv["count"]==false)
                {
                        $text = substr($text, $articlestart);
						$ellipsis=""; ## RCT Added 
                }
                else
                {
                        //RCT added -- increment $articleend until a whitespace character appears
						$iswhitespace = false;
						$whitespacechar = array(" ", "\t", "\n", "\r", "\0", "\x0B");
						$articleend = $argv["count"] + $articlestart;
						$charcount = $argv["count"];
						$iswhitespace = in_array(substr($text, $articleend,1),$whitespacechar);
						while (! $iswhitespace)  {
							$articleend++;
							$charcount++;
							$iswhitespace = in_array(substr($text, $articleend,1),$whitespacechar);
						}
						//RCT end ---------------------------------------------------------------------
						$text = substr($text, $articlestart, $charcount);
						$ellipsis=" [[".$aTitlecomp."|".$wgEllipsis."]]"; ## RCT Added 
                }
    // --------------------------------------------------------------------------------
    }
    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;
 		$ellipsis=" [[".$aTitlecomp."|".$wgEllipsis."]]"; ## RCT Added  
    }
    $newvariables["title"]=$aTitlecomp;
    $newvariables["titleblank"]=$aTitleblank;
    $newvariables["content"]=$text.$ellipsis; ## RCT added $ellipsis
	
    $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;
}

--Russell.thomas 18:39, 5 June 2008 (UTC)Reply

"Section" option code patch

[edit]

The following patch enable the use of the "section" option, that only include a specific subsection of an article. (Sections are defined by header levels)

Use example :

<incarticle article="Debian installation" section="partitioning" lines="100">
=== {{{title}}} ===
{{{content}}}
</incarticle>
--- IncludeArticle.php	2008-09-26 14:31:08.000000000 +0200
+++ IncludeArticle.php.orig	2008-09-26 14:22:04.000000000 +0200
@@ -48,21 +48,6 @@
            $text = '';
        }
     }
-    if (isset($argv["section"]))
-    {
-    	$section = $argv["section"];
-	$TitleSection = "/".$section;
-    	// from section start to next section (same level) start	
-	$match1='#(=+)\s*'.$section.'\s*\1\n*(.*)[^=]\1[^=]#Usi';
-	// from section start to text end
-	//$match2='#(=+)\s*'.$section.'\s*\1\n*(.*)\z#si';
-	$match2='#(=+)\s*'.$section.'\s*\1\n*(.*)\z#si';
-	preg_match($match1,$text,$matches);
-	if (isset($matches[1])) {
-		if ($matches[1] == "=") preg_match($match2,$text,$matches);
-	}
-	if (isset($matches[2])) $text=$matches[2];
-    }
     if ($argv["lines"]==false)
     {
         $text = substr($text, $articlestart, $articleend);
@@ -91,14 +76,11 @@
     }
     $newvariables["title"]=$aTitlecomp;
     $newvariables["titleblank"]=$aTitleblank;
-    $newvariables["content"]="__NOTOC__\n".$text;
+    $newvariables["content"]=$text;
     $variables = $parser->replaceVariables( $input, $newvariables );
     $output = $parser->parse( $variables, $parser->mTitle, $parser->mOptions, true, false );
     $inhalt = $output->getText();
     $html=$inhalt;
-    $html="<div id=\"includeart\">".$html."<span class=\"titreincludeart\">Included article \"<a class=\"titreincludeart\" href=\"";
-    $html.=$aTitlecomp."\">".$aTitlecomp.$TitleSection."</a>\"</span></div>";
-
     return $html;
 }

How to use the patch above

[edit]

I am very interested in the "section" option enabled by the patch above, but I don't know how to use it or how can I modify the IncludeArticle code. Pls help. Moreover, how can I disable the "table of contents" in the included article. Thx!

To disable TOC enter __NOTOC__ anywhere on the page.

To use the extension open IncludeArticle and find the text "if ($argv["lines"]==false)" It should be around Line 55. Then enter everything that is preceded with a '-'. Delete the '-' and save the extension.

Strange problem after upgrading to the last version of Mediawiki

[edit]

If I enable this extension, I get this error message:

Fatal error: Call to a member function getPrefixedDBkey() on a non-object in
includes/parser/Preprocessor_DOM.php on line 996


Die Klasse 'Parser_OldPP' wurde mit Mediawiki Version 1.14 entfernt.

I've also tried inserting:

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

How can I solve it?

template processing

[edit]

A tip that would've saved me a lot of head-pounding today: Templates in the included article are not processed until after they are added to the current article. So if you try to hack this extension to do a preg_replace on a regex that's generated by a transcluded template... it won't work.

Download Broken?

[edit]

I tried doing the right-click thing and then save as for the PHP file, but I get served up the executed error of the PHP script. Can you post the code on the wiki page or package it into a ZIP?

IncludeArticle parser function

[edit]

The tag extension did not add the included article headings to the including article's Table of Contents. I rewrote IncludeArticle as a parser function, which fixed the TOC and also removed the non-working "Edit" links. I only needed to include the full content of other articles, so my parser function does not have any additional functionality such as random article or start/count/lines options. Also, the including articles do not show up in wiki search when a search term only contains text from the included article (although I think this is also true of the tag extension).

UPDATE: Turns out MediaWiki already has the functionality I was looking for: Transclusion

Random Article from Category

[edit]

Is there a way to get the 'Random' option to restrict results to articles within a particular category? So, like the 'Namespaces' option, but for 'Category'?

If I could get this to work, this would be perfect for what I'm trying to achieve: a way of creating a 'Featured Articles' category that pushes the first few lines of a selected article to a front page.

Thanks --Dshinks (talk) 22:08, 30 July 2015 (UTC)Reply