('bovender') * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, * MA 02110-1301, USA. */ /// @file /// Helper function for development and debugging. /// @param $var Any variable. Raw content will be dumped to stderr. /// @return undefined function dump($var) { error_log(print_r($var, TRUE) . "\n", 3, 'php://stderr'); }; /// Central class of the extension. Sets up parser hooks. /// This class contains only static functions; do not instantiate. /// @todo Use the current PageContentSave hook rather than the deprecated ArticleSave hook. class LinkTitles { /// Setup function, hooks the extension's functions to MediaWiki events. public static function setup() { global $wgLinkTitlesParseOnEdit; global $wgLinkTitlesParseOnRender; global $wgHooks; if ( $wgLinkTitlesParseOnEdit ) { $wgHooks['ArticleSave'][] = 'LinkTitles::onArticleSave'; }; if ( $wgLinkTitlesParseOnRender ) { $wgHooks['ArticleAfterFetchContent'][] = 'LinkTitles::onArticleAfterFetchContent'; }; $wgHooks['ParserBeforeTidy'][] = 'LinkTitles::removeMagicWord'; } /// Event handler that is hooked to the ArticleSave event. public static function onArticleSave( &$article, &$user, &$text, &$summary, $minor, $watchthis, $sectionanchor, &$flags, &$status ) { // To prevent time-consuming parsing of the page whenever // it is edited and saved, we only parse it if the flag // 'minor edits' is not set. return $minor or self::parseContent( $article, $text ); } /// Event handler that is hooked to the ArticleAfterFetchContent event. /// @param $article Article object /// @param $content String that holds the article content public static function onArticleAfterFetchContent( &$article, &$content ) { // The ArticleAfterFetchContent event is triggered whenever page content // is retrieved from the database, i.e. also for editing etc. // Therefore we access the global $action variabl to only parse the // content when the page is viewed. global $action; if ( in_array( $action, array('view', 'render', 'purge') ) ) { self::parseContent( $article, $content ); }; return true; } /// Core function of the extension, performs the actual parsing of the content. /// @param $article Article object /// @param $text String that holds the article content /// @returns true static function parseContent( &$article, &$text ) { // If the page contains the magic word '__NOAUTOLINKS__', do not parse // the content. $noAutoLinks = MagicWord::get('MAG_LINKTITLES_NOAUTOLINKS'); if ( $noAutoLinks -> match( $text ) ) { return true; } $noAutoLinkTarget = MagicWord::get('MAG_LINKTITLES_NOTARGET'); // Configuration variables need to be defined here as globals. global $wgLinkTitlesPreferShortTitles; global $wgLinkTitlesMinimumTitleLength; global $wgLinkTitlesParseHeadings; global $wgLinkTitlesBlackList; global $wgLinkTitlesSkipTemplates; global $wgLinkTitlesFirstOnly; global $wgLinkTitlesWordStartOnly; global $wgLinkTitlesWordEndOnly; // global $wgLinkTitlesIgnoreCase; global $wgLinkTitlesSmartMode; global $wgCapitalLinks; ( $wgLinkTitlesWordStartOnly ) ? $wordStartDelim = '\b' : $wordStartDelim = ''; ( $wgLinkTitlesWordEndOnly ) ? $wordEndDelim = '\b' : $wordEndDelim = ''; // ( $wgLinkTitlesIgnoreCase ) ? $regexModifier = 'i' : $regexModifier = ''; // To prevent adding self-references, we now // extract the current page's title. $myTitle = $article->getTitle(); ( $wgLinkTitlesPreferShortTitles ) ? $sort_order = 'ASC' : $sort_order = 'DESC'; ( $wgLinkTitlesFirstOnly ) ? $limit = 1 : $limit = -1; if ( $wgLinkTitlesSkipTemplates ) { $templatesDelimiter = '{{.+?}}|'; } else { $templatesDelimiter = '{{[^|]+?}}|{{.+\||'; }; // Build a regular expression that will capture existing wiki links ("[[...]]"), // wiki headings ("= ... =", "== ... ==" etc.), // urls ("http://example.com", "[http://example.com]", "[http://example.com Description]", // and email addresses ("mail@example.com"). // Since there is a user option to skip headings, we make this part of the expression // optional. Note that in order to use preg_split(), it is important to have only one // capturing subpattern (which precludes the use of conditional subpatterns). ( $wgLinkTitlesParseHeadings ) ? $delimiter = '' : $delimiter = '=+.+?=+|'; $urlPattern = '[a-z]+?\:\/\/(?:\S+\.)+\S+(?:\/.*)?'; $delimiter = '/(' . // exclude from linking: '\[\[.*?\]\]|' . // links $delimiter . // titles (if requested) $templatesDelimiter . // templates (if requested) '^ .+?\n|\n .+?\n|\n .+?$|^ .+?$|' . // preformatted text '.*?<.nowiki>|.*?<\/code>|' . // nowiki/code '
.*?<\/pre>|.*?<\/html>|' .      // pre/html
				'