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

?>

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