Extension:ReplaceRedLinks

From MediaWiki.org
Jump to: navigation, search

Check usage(experimental)

MediaWiki extensions manual - list
Crystal Clear action run.png
ReplaceRedLinks

Release status: Beta

Implementation Tag
Description Extension supports replacing "red links" to Wikipedia for wikification in other projects
Author(s) X-romix
Last version 1.1
MediaWiki 1.15.3
License Creative Commons Attribution/Share-Alike License 3.0 and GFDL
Download No link
Example http://wikiext.org/index.php/Red_links_sample

Tested on MediaWiki 1.15.3


Contents

[edit] Description

Extension supports replacing "red links" to Wikipedia for simple wikification in other projects.

[edit] Switching on

Use tag <ReplaceRedLinks/> to switch extension on. If in page there is not tag <ReplaceRedLinks/>, extension does not anything.

[edit] Language parameter

Use parameter lang e.g. <ReplaceRedLinks lang="en"/> to switch Wikipedia language. There is need a two-letter language identifier, e.g. "ru", "fr" etc. If there is no this parameter or if it incorrect - system uses "en" for default.

[edit] Exclusions parameter

Use parameter exclusions e.g. <ReplaceRedLinks lang="en" exclusions="apple|orange"/> to switch off processing for any article names. Delimitier - is vertical pipe "|".

[edit] Working sample

[edit] Setup

Extension was tested on 1.15.3 version of MediaWiki.

To install this extension, make ReplaceRedLinks.php (source code is below) in folder extensions/ReplaceRedLinks.

Then write

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

in bottom of file LocalSettings.php.

[edit] Source code

Make file ReplaceRedLinks.php in ANSI encoding (do not add any spaces before starting "<?php" and after end "?>").

<?php
//Extension supports replacing "red links" to Wikipedia for wikification in other projects.
if ( ! defined( 'MEDIAWIKI' ) ) die();
 
if ( defined( 'MW_SUPPORTS_PARSERFIRSTCALLINIT' ) ) {
        $wgHooks['ParserFirstCallInit'][] = 'wfReplaceRedLinks';
} else {
        $wgExtensionFunctions[] = 'wfReplaceRedLinks';
}
 
 
// Extension credits that will show up on the page [[Special:Version]]    
$wgExtensionCredits['parserhook'][] = array(
    'path'         => __FILE__,
        'name'         => 'ReplaceRedLinks',
        'version'      => '1.1',
        'author'       => 'X-romix', 
        'url'          => 'http://www.mediawiki.org/wiki/Extension:ReplaceRedLinks',
        'description'  => 'Extension supports replacing "red links" to Wikipedia for wikification in other projects.'
);
 
class ReplaceRedLinks{
        var $SwitchOff = true;
        var $lang = "en";
        var $exclusions=array();
 
        function ReplaceRedLinks() { //Constructor
                $this->setHooks();
        }
 
        function setHooks() {
                global $wgParser, $wgHooks;
 
                //Hook for tag <ReplaceRedLinks> 
                //see http://www.mediawiki.org/wiki/Manual:Tag_extensions for details
                $wgParser->setHook( 'ReplaceRedLinks' , array( &$this, 'fnReplaceRedLinks' ) );
                //function fnReplaceRedLinks) - is below
 
                //Hook to ParserBeforeTidy event - "Used to process the nearly-rendered html code for the page (but before any html tidying occurs)"
                //see also http://www.mediawiki.org/wiki/Manual:Hooks/ParserBeforeTidy
                //http://www.mediawiki.org/wiki/Manual:Hooks
                $wgHooks['ParserBeforeTidy'][] = array( &$this, 'fnParserBeforeTidy' );
                //function fnParserBeforeTidy() - is below
        }
 
        //
        function fnReplaceRedLinks( $str, $argv, $parser ){
                //tag <ReplaceRedLinks/> found
                $this->SwitchOff = false;
 
                //parameter "lang"
                @$s=$argv['lang'];
                if($s){
                        if (preg_match("/[a-z][a-z]/i", $s)) {
                                $this->lang = $s;
                        }else{
                                //unproper language - needed 2 letters - ru, en etc.
                        }
                }
 
                //parameter "exclusions"
                @$s=$argv['exclusions'];
                $arr=array();
                if($s){
                        $arr = explode("|", $s);
                }
                foreach($arr as $el){
                        $this->exclusions[]=trim(strtolower($el));
                }
 
                return $parser->recursiveTagParse($str);
        }
 
        function fnParserBeforeTidy(&$parser, &$text){
                global $IP;
                if($this->SwitchOff == true){
                        return true;
                }
 
                //process links
                $text=preg_replace_callback("/
                        (\"\/index\.php\?title\=)       # start of link
                        ([^\&]+)                                        # any text before &
                        (\&amp\;action\=edit)           # action=edit
                        (\&amp\;redlink\=1\")           # redlink=1
                        (\sclass\=\"new\")                      # class=new
                        (\stitle\=\"[^\"]*\")           # title=
                        /x", 
                        array( __CLASS__, 'ParseRedLinkCallback' ), 
                        $text);
 
                $this->SwitchOff = true; //to prevent drawing it in footer
                return true;
        }
 
 
        function ParseRedLinkCallback($matches){
                $s=$matches[2];
                $tt=trim(urldecode($s));
                $tt=str_replace("_", " ", $tt); 
 
                if (in_array(strtolower($tt), $this->exclusions)){
                        //there is exclusion
                        //do not modify anything
                        return $matches[0];
                }
 
                return '"http://'.$this->lang.'.wikipedia.org/wiki/'.$s.'" title="'.$tt.' (Wikipedia)"';
        }
}
 
function wfReplaceRedLinks() {
        new ReplaceRedLinks;
        return true;
} 
?>
Personal tools
Namespaces

Variants
Actions
Navigation
Support
Download
Development
Communication
Print/export
Toolbox