('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. class LinkTitles { /// A Title object for the page that is being parsed. private static $currentTitle; /// A Title object for the target page currently being examined. private static $targetTitle; /// The content object for the currently processed target page. /// This variable is necessary to be able to prevent loading the target /// content twice. private static $targetContent; /// Holds the page title of the currently processed target page /// as a string. private static $targetTitleText; /// Setup function, hooks the extension's functions to MediaWiki events. public static function setup() { global $wgLinkTitlesParseOnEdit; global $wgLinkTitlesParseOnRender; global $wgHooks; if ( $wgLinkTitlesParseOnEdit ) { $wgHooks['PageContentSave'][] = 'LinkTitles::onPageContentSave'; }; if ( $wgLinkTitlesParseOnRender ) { $wgHooks['InternalParseBeforeLinks'][] = 'LinkTitles::onInternalParseBeforeLinks'; }; $wgHooks['GetDoubleUnderscoreIDs'][] = 'LinkTitles::onGetDoubleUnderscoreIDs'; } /// Event handler that is hooked to the PageContentSave event. public static function onPageContentSave( &$wikiPage, &$user, &$content, &$summary, $isMinor, $isWatch, $section, &$flags, &$status ) { if ( ! $isMinor ) { $title = $wikiPage->getTitle(); $text = $content->getContentHandler()->serializeContent($content); $newText = self::parseContent( $title, $text ); if ( $newText != $text ) { $content = $content->getContentHandler()->unserializeContent( $newText ); } }; return true; } /// Event handler that is hooked to the InternalParseBeforeLinks event. /// @param Parser $parser Parser that raised the event. /// @param $text Preprocessed text of the page. public static function onInternalParseBeforeLinks( Parser &$parser, &$text ) { $title = $parser->getTitle(); $text = self::parseContent( $title, $text ); return true; } /// Core function of the extension, performs the actual parsing of the content. /// @param Title $title Title of the page being parsed /// @param $text String that holds the article content /// @returns string: parsed text with links added if needed private static function parseContent( Title &$title, &$text ) { // If the page contains the magic word '__NOAUTOLINKS__', do not parse it. if ( MagicWord::get('MAG_LINKTITLES_NOAUTOLINKS')->match( $text ) ) { return true; } // 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 $wgLinkTitlesSmartMode; global $wgCapitalLinks; ( $wgLinkTitlesWordStartOnly ) ? $wordStartDelim = '\b' : $wordStartDelim = ''; ( $wgLinkTitlesWordEndOnly ) ? $wordEndDelim = '\b' : $wordEndDelim = ''; ( $wgLinkTitlesPreferShortTitles ) ? $sort_order = 'ASC' : $sort_order = 'DESC'; ( $wgLinkTitlesFirstOnly ) ? $limit = 1 : $limit = -1; if ( $wgLinkTitlesSkipTemplates ) { $templatesDelimiter = '{{.+?}}|'; } else { $templatesDelimiter = '{{[^|]+?}}|{{.+\||'; }; LinkTitles::$currentTitle = $title; $newText = $text; // 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
				'