Extension:AutoLink

What can this extension do?
When a new page is created, or an existing page is saved, Some of the specific words in the page content will automatically become links to other pages. Such Extension was very much required for my application so i thought of placing it here so that others if interested can use it.

Usage
Once this extension is installed, Just edit a page,add some content to it and save the page. The words in the page content which are present in the page AutoLinkPages will become links to respective page. So AutoLinkPages acts as a dictionary for the Auto Link words.

Installation

 * Create a page with name AutoLinkPages.This page will contain : separated pagenames which have to be auto linked.For Example AutoLinkPages.
 * Just copy the extension directy from AutoLink.
 * Save it as AutoLink.php in extensions directory.

Changes to LocalSettings.php
Include this extension in LocalSettings.php as follows:- require_once("$IP/extensions/AutoLink.php");

Code
 "AutoLink (version-1.0)",    'author' => 'Sanjeev Sanjeev' );

$wgHooks['ArticleSave'][] = 'wfAddAutoLinks';

function wfAddAutoLinks(&$article,&$user,&$text) {       $content = $text; $pages = array; #This should not work for the Page AutoLinkPages.
 * 1) Event Handler for ArticleSave Event.

# Why not: #       #        #        # if($article->mTitle->mTextform == 'AutoLinkPages') # {       #        #   # "Do you want to have all your articles put into this page?" #  # if "yes" do it automatically #       # }        # else # {       #        #              // the code you had before #       # }

if($article->mTitle->mTextform != 'AutoLinkPages') {               $pages = getPages; foreach($pages as $p) {                       #Ensure that the first char can be upper or lower case. $pregex = "[".strtoupper($p[0])."|".strtolower($p[0])."]".substr($p, 1);

#Replace the already present auto links, to prevent linking again. $content = preg_replace("/\\[\[(".$pregex.")\]\]/", "$1", $content);

if(preg_match("/".$pregex."/", $content)) {

# replace "animal" but don't replace "animal.jpg" or "animal.png" # animal is ok - animal.jpg isn't                               # --- # How to do this job?? I am no programmer.

$content = preg_replace("/".$pregex."/", "$0", $content); }               }                $text = $content; }       return true; }

function getPages {       $title = Title::newFromURL('AutoLinkPages'); $article = new Article($title); $pagenames = $article->getContent; $pages = explode(':',$pagenames); return $pages; } ?>
 * 1) Function to return page names from AutoLinkPages as an array

Contact
In case of any issues please get in touch with me. Sanjeev Thanks!!!