Extension:AutoLink
From MediaWiki.org
|
AutoLink Release status: beta |
|
|---|---|
| Implementation | Extended syntax |
| Description | Used to make auto links in a page, when a given page is saved. |
| Author(s) | Sanjeev / Samuel Gelineau / Dirk Kreisel |
| Version | 1.2 / 2.2 |
| Download | AutoLink |
Extension re-worked....
Contents |
[edit] 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.
[edit] 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.
[edit] 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.
[edit] Changes to LocalSettings.php
Include this extension in LocalSettings.php as follows:-
require_once("$IP/extensions/AutoLink.php");
[edit] Code
[edit] new version with restricted wordlist giving preference to longer matches
<?php /** * AutoLink extension * Adds Auto Links in A page. * Reads page 'AutoLinkPages', which works as Dictonary. * * @author Sanjeev, Samuel Gelineau, Dirk Kreisel * @version 1.3 * @url http://www.mediawiki.org/wiki/Extension:AutoLink */ define('AUTOLINK_REPLACE_FIRST_ONLY', false); define('AUTOLINK_EXCLUDED_PAGES', 'AutoLinkPages|Welcome to ASK'); define('AUTOLINK_LINK_DELIMITER', '|'); if (!defined('MEDIAWIKI' )) { die(); } $wgExtensionCredits['other'][] = array( 'name' => 'AutoLink', 'version' => '1.3', 'author' => array('Sanjeev', 'Samuel Gelineau', '[mailto://dirk.kreisel@gmx.net Dirk Kreisel]'), 'url' => 'http://www.mediawiki.org/wiki/Extension:AutoLink' ); $wgHooks['ArticleSave'][] = 'wfAddAutoLinks'; #Event handler for ArticleSave event. function wfAddAutoLinks(&$article, &$user, &$text) { #file_put_contents('c:\test.txt', $text); $content = $text; $pages = array(); #This should not work for the Page AutoLinkPages. $excludedPages = explode('|', AUTOLINK_EXCLUDED_PAGES); foreach ($excludedPages as $excludedPage) { if ($article->mTitle->mTextform == $excludedPage) { return true; } } $pages = getPages(); foreach ($pages as $page) { $pos = 0; $offset = 0; while ($pos !== false) { if ($pos != 0) { $pos = $pos + strlen($page) + $offset; } $pos = strpos($content, $page, $pos); $offset = 0; if ($pos === false) { continue; } if (preg_match('/[a-zA-Z0-9\[\<</span>]/', $content[$pos-1]) == 0 && preg_match('/[a-zA-Z0-9\]\>]/', $content[$pos+strlen($page)]) == 0) { $content = substr($content, 0, $pos) . '[[' . substr($content, $pos, strlen($page)) . ']]' . substr($content, $pos + strlen($page)); $offset = 4; if (AUTOLINK_REPLACE_FIRST_ONLY === true) { $pos = false; } } if (AUTOLINK_REPLACE_FIRST_ONLY === true && preg_match('/[\[]/', $content[$pos-1]) != 0 && preg_match('/[\]]/', $content[$pos+strlen($page)]) != 0) { $pos = false; } } } $text = $content; return true; } #Function to return page names from AutoLinkPages as an array() function getPages() { $title = Title::newFromURL('AutoLinkPages'); $article = new Article($title); $pagenames = $article->getContent(); $pages = explode(AUTOLINK_LINK_DELIMITER, $pagenames); return $pages; }
[edit] version with restricted wordlist
<?php /** * AutoLink extension * Adds Auto Links in A page. * Reads page 'AutoLinkPages', which works as Dictonary. * * @author Sanjeev * @version 1.2 * @url http://www.mediawiki.org/wiki/Extension:AutoLink */ if ( ! defined( 'MEDIAWIKI' ) ) die(); $wgExtensionCredits['other'][] = array( 'name' => 'AutoLink', 'version' => '1.2', 'author' => '[http://www.mediawiki.org/wiki/User:Sanjualone Sanjeev]', 'url' => 'http://www.mediawiki.org/wiki/Extension:AutoLink', ); $wgHooks['ArticleSave'][] = 'wfAddAutoLinks'; #Event handler for ArticleSave event. function wfAddAutoLinks(&$article, &$user, &$text) { $content = $text; $pages = array(); #This should not work for the Page AutoLinkPages. if($article->mTitle->mTextform != 'AutoLinkPages') { $pages = getPages(); $textlines = array(); $textlines = explode('\n',$content); $newlines = array(); foreach($textlines as $line) { $textword = array(); $textword = explode(' ',$line); $newwords = array(); foreach($textword as $word) { if(strpos($word,'[') == false && strpos($word,']') == false && strpos($word,'<') == false && strpos($word,'>') == false) { if(strstr($word,'.') == false || strstr($word,'.')== '.') { foreach($pages as $page) { if(strstr($word,$page)) $word=str_replace($page,"[[".$page."]]",$word); } } } $newwords[] = $word; } $newlines[] = implode(' ',$newwords); } $content = implode('\n',$newlines); $text = $content; } return true; } #Function to return page names from AutoLinkPages as an array() function getPages() { $title = Title::newFromURL('AutoLinkPages'); $article = new Article($title); $pagenames = $article->getContent(); $pages = explode(':',$pagenames); return $pages; }
[edit] version which matches all pages
<?php /** * AutoLink extension * Turn ordinary words into page links, if the name matches. to match plurals and variations, * write lots and lots of redirection pages. * The original version, by [http://www.mediawiki.org/wiki/User:Sanjualone Sanjeev], required * users to manually enter all the page names they wanted to match. * [http://www.mediawiki.org/wiki/User:Gelisam] completely rewrote the script (minus the * boilerplate) to match any page. * * @author Samuel Gelineau * @version 2.2 * @url http://www.mediawiki.org/wiki/Extension:AutoLink */ if ( ! defined( 'MEDIAWIKI' ) ) die(); $wgExtensionCredits['other'][] = array( 'name' => 'AutoLink', 'version' => '2.2', 'author' => 'Samuel Gelineau', 'url' => 'http://www.mediawiki.org/wiki/Extension:AutoLink' ); $wgHooks['ArticleSave'][] = 'wfAddAutoLinks'; #Event Handler for ArticleSave Event. function wfAddAutoLinks(&$article, &$user, &$text) { global $autolink__current_page; $autolink__current_page = preg_replace('/ /', '_', $article->mTitle->mTextform); $text = mapIgnoringBlocks( array( "<createbox>", "</createbox>", "<categorytree>", "</categorytree>", "<DPL>", "</DPL>", "[[", "]]", "[", "]", "{{", "}}", "<nowiki>", "</nowiki>" ), $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, '[[' . $page . '|\1]]'); } } return preg_replace($patterns, $replacements, $text); } #Function to return page names from AutoLinkPages as an array() 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; }
[edit] version which matches all pages giving preference to longer matches
<?php /** * AutoLink extension * Turn ordinary words into page links, if the name matches. to match plurals and variations, * write lots and lots of redirection pages. * The original version, by [http://www.mediawiki.org/wiki/User:Sanjualone Sanjeev], required * users to manually enter all the page names they wanted to match. * [http://www.mediawiki.org/wiki/User:Gelisam] completely rewrote the script (minus the * boilerplate) to match any page. * * On 08/24/2007, a new version was created by [http://www.mediawiki.org/wiki/User:Vjg] which * gives preference to longer page names. It switches the order of processing, handling the * article for each page, but only building the page array once. The previous version handled * the article once, but built the page array on each pass. I haven't fully explored all the * performance implications. This was for use on a relatively small site. * * @author Virgil Green * @version 2.3 * @url http://www.mediawiki.org/wiki/Extension:AutoLink */ if ( ! defined( 'MEDIAWIKI' ) ) die(); $wgExtensionCredits['other'][] = array( 'name' => 'AutoLink', 'version' => '2.3', 'author' => 'Virgil Green', 'url' => 'http://www.mediawiki.org/wiki/Extension:AutoLink', ); $wgHooks['ArticleSave'][] = 'wfAddAutoLinks'; #Event Handler for ArticleSave Event. function wfAddAutoLinks(&$article, &$user, &$text) { global $autolink__current_page; $autolink__current_page = preg_replace('/ /', '_', $article->mTitle->mTextform); $text = autoLinkWords( array( "<createbox>", "</createbox>", "<categorytree>", "</categorytree>", "<DPL>", "</DPL>", "[[", "]]", "[", "]", "{{", "}}", "<nowiki>", "</nowiki>" ), $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] = '[[' . $page . '|\1]]'; } } foreach($patternMap as $pattern=>$replacement) { $text = mapIgnoringBlocks($delimiters, $text, $pattern, $replacement); } return $text; } #Function to return page names from AutoLinkPages as an array() 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; }
[edit] Contact
In case of any issues please get in touch with Sanjeev. Thanks!

