Extension:AutoLink

Extension re-worked....

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:-

version which matches all pages
",                       "", "",                        "", "",                        "[", "]",                        "",                        " ", " "                ),                $text,                'autoLinkWords'        );        return true; }

function mapIgnoringBlocks($delimiters, $text, $map) { if (count($delimiters) == 0) return $map($text);

$block_start = array_shift($delimiters); $block_end = array_shift($delimiters);

$result = ""; $post_headers = explode($block_start, $text);

$first_inter_block = array_shift($post_headers); $result = $result. mapIgnoringBlocks($delimiters, $first_inter_block, $map);

foreach($post_headers as $post_header) { $block_and_inter_block = explode($block_end, $post_header, 2);

$block = array_shift($block_and_inter_block); $result = $result. $block_start. $block. $block_end;

$inter_block = array_shift($block_and_inter_block); $result = $result. mapIgnoringBlocks($delimiters, $inter_block, $map); }

return $result; }

function autoLinkWords($text) { global $autolink__current_page;

$patterns = array; $replacements = array; foreach(getPages as $page) { if ($page != $autolink__current_page) { $pattern = preg_replace('/_/', '[_ ]', preg_quote($page, '/')); $pattern = '/\b((?<!\[)(?<!\/\/)' . $pattern . '(?!\]))\b/i';

array_push($patterns, $pattern); array_push($replacements, '\1'); }       }        return preg_replace($patterns, $replacements, $text); }

function getPages { $dbr = &wfGetDB(DB_SLAVE); $result_set = $dbr->select('page', array('page_title'), array('page_namespace' => 0)); $titles = array; while (               ($row = $dbr->fetchObject($result_set))        ) { array_push($titles, $row->page_title); }       $dbr->freeResult($result_set); return $titles; }
 * 1) Function to return page names from AutoLinkPages as an array

version which matches all pages giving preference to longer matches
",                       "", "",                        "", "",                        "[", "]",                        "",                        " ", " "                ),                $text        );        return true; }

function mapIgnoringBlocks($delimiters, $text, $pattern, $replacement) { if (count($delimiters) == 0) { return preg_replace($pattern, $replacement, $text); }

$block_start = array_shift($delimiters); $block_end = array_shift($delimiters);

$result = ""; $post_headers = explode($block_start, $text);

$first_inter_block = array_shift($post_headers); $result = $result. mapIgnoringBlocks($delimiters, $first_inter_block, $pattern, $replacement);

foreach($post_headers as $post_header) { $block_and_inter_block = explode($block_end, $post_header, 2);

$block = array_shift($block_and_inter_block); $result = $result. $block_start. $block. $block_end;

$inter_block = array_shift($block_and_inter_block); $result = $result. mapIgnoringBlocks($delimiters, $inter_block, $pattern, $replacement); }

return $result; }

function autoLinkWords($delimiters, $text) { global $autolink__current_page;

$patternMap = array; foreach(getPages as $page) { if ($page != $autolink__current_page) { $pattern = preg_replace('/_/', '[_ ]', preg_quote($page, '/')); $pattern = '/\b((?<!\[)(?<!\/\/)' . $pattern . '(?!\]))\b/i';

$patternMap[$pattern] = '\1'; }       }        foreach($patternMap as $pattern=>$replacement) { $text = mapIgnoringBlocks($delimiters, $text, $pattern, $replacement); }       return $text; }

function getPages { $dbr = &wfGetDB(DB_SLAVE); $result_set = $dbr->select('page', array('page_title'), array('page_namespace' => 0), __METHOD__, array('ORDER BY' =>'page_title desc')); $titles = array; while (               ($row = $dbr->fetchObject($result_set))        ) { array_push($titles, $row->page_title); }       $dbr->freeResult($result_set); return $titles; }
 * 1) Function to return page names from AutoLinkPages as an array

version which matches all pages preferring longer page names and provides shorter, easier to read links
",                       "", "",                        "", "",                        "[", "]",                        "",                        " ", " "                ),                $text        );        return true; } function mapIgnoringBlocks($delimiters, $text, $pattern, $replacement, &$phrasecount) {        if (count($delimiters) == 0) {           if ($phrasecount[$replacement] < 2) { #allow 2 occurrances because the first occurance may be in a header line                $phrasecount[$replacement]++;                return preg_replace($pattern, $replacement, $text, 1);           }           else           {                return $text;           }        }        $block_start = array_shift($delimiters);        $block_end = array_shift($delimiters);        $result = "";        $post_headers = explode($block_start, $text);        $first_inter_block = array_shift($post_headers); $result = $result. mapIgnoringBlocks($delimiters, $first_inter_block, $pattern, $replacement, $phrasecount); foreach($post_headers as $post_header) { $block_and_inter_block = explode($block_end, $post_header, 2); $block = array_shift($block_and_inter_block); $result = $result. $block_start. $block. $block_end; $inter_block = array_shift($block_and_inter_block); $result = $result. mapIgnoringBlocks($delimiters, $inter_block, $pattern, $replacement, $phrasecount); }       return $result; } function autoLinkWords($delimiters, $text) { global $autolink__current_page; $phrasecount = array;

foreach(getPages as $page) { if ($page != $autolink__current_page) { $pattern = preg_replace('/_/', '[_ ]', preg_quote($page, '/')); $pattern = '/\b((?<!\[)(?<!\/\/)' . $pattern . '(?!\]))\b/i'; $match = '\1'; if ($match = $page) {                             $match = preg_replace('/_/',' ', $match); $patternMap[$pattern] =  . $match . ; }               }                $phrasecount[$patternMap[$pattern]] = 0; }       foreach($patternMap as $pattern=>$replacement) { $text = mapIgnoringBlocks($delimiters, $text, $pattern, $replacement, $phrasecount); }       return $text; } function getPages { $dbr = &wfGetDB(DB_SLAVE); $result_set = $dbr->select('page', array('page_title'), array('page_namespace' => 0), __METHOD__, array('ORDER BY' =>'page_title desc')); $titles = array; while (               ($row = $dbr->fetchObject($result_set))        ) { array_push($titles, $row->page_title); }       $dbr->freeResult($result_set); uasort($titles,"Ascii_sort"); //Longest page titles first return $titles; }
 * 1) Function to return page names from AutoLinkPages as an array

function Ascii_sort($val_1, $val_2) {       // initialize the return value to zero $retVal = 0; // compare lengths $firstVal = strlen($val_1); $secondVal = strlen($val_2); if($firstVal > $secondVal) {               $retVal = -1; }       else if($firstVal < $secondVal) {               $retVal = 1; }       return $retVal; }

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