diff --git a/LinkTitles.body.php b/LinkTitles.body.php index ddfcc73..65c022c 100755 --- a/LinkTitles.body.php +++ b/LinkTitles.body.php @@ -29,6 +29,18 @@ }; 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'; + }; + } /// This function is hooked to the ArticleSave event. /// It will be called whenever a page is about to be @@ -39,19 +51,25 @@ // 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. - if ( !$minor ) { - return parseContent( $article, $content ); - }; + return $minor or self::parseContent( $article, $content ); } /// Called when an ArticleAfterFetchContent event occurs; this requires the /// $wgLinkTitlesParseOnRender option to be set to 'true' public static function onArticleAfterFetchContent( &$article, &$content ) { - return parseContent( $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; } /// This function performs the actual parsing of the content. - static function parseContent( &$article, &$content ) { + static function parseContent( &$article, &$text ) { // Configuration variables need to be defined here as globals. global $wgLinkTitlesPreferShortTitles; global $wgLinkTitlesMinimumTitleLength; @@ -98,6 +116,7 @@ if ( $newText != '' ) { $text = $newText; }; + return true; } /* diff --git a/LinkTitles.php b/LinkTitles.php index b419e90..65726fe 100755 --- a/LinkTitles.php +++ b/LinkTitles.php @@ -23,17 +23,16 @@ die( 'Not an entry point.' ); } - /* error_reporting(E_ALL); ini_set('display_errors', 'Off'); ini_set('error_log', 'php://stderr'); $wgMainCacheType = CACHE_NONE; $wgCacheDirectory = false; - */ // Configuration variables $wgLinkTitlesPreferShortTitles = false; $wgLinkTitlesMinimumTitleLength = 3; + $wgLinkTitlesParseHeadings = false; $wgLinkTitlesParseOnEdit = true; $wgLinkTitlesParseOnRender = false; @@ -48,14 +47,7 @@ $wgExtensionMessagesFiles['LinkTitles'] = dirname( __FILE__ ) . '/LinkTitles.i18n.php'; $wgAutoloadClasses['LinkTitles'] = dirname(__FILE__) . '/LinkTitles.body.php'; + $wgExtensionFunctions[] = 'LinkTitles::setup'; - // Hook up our custom function to the ArticleSave event. - if ( $wgLinkTitlesParseOnEdit ) { - $wgHooks['ArticleSave'][] = 'LinkTitles::onArticleSave'; - }; - if ( $wgLinkTitlesParseOnRender ) { - $wgHooks['ArticleAfterFetchContent'][] = 'LinkTitles::onArticleAfterFetchContent'; - }; - // vim: ts=2:sw=2:noet