From c12578ed92864b523ff6fa99003a463a006cf931 Mon Sep 17 00:00:00 2001 From: Daniel Kraus Date: Tue, 22 May 2012 18:59:22 +0200 Subject: [PATCH] Implement option to not parse headings --- HISTORY | 24 ------------------------ LinkTitles.body.php | 36 +++++++++--------------------------- LinkTitles.php | 14 ++++++++------ NEWS | 29 +++++++++++++++++++++++++++++ 4 files changed, 46 insertions(+), 57 deletions(-) delete mode 100644 HISTORY create mode 100644 NEWS diff --git a/HISTORY b/HISTORY deleted file mode 100644 index 41d5f5e..0000000 --- a/HISTORY +++ /dev/null @@ -1,24 +0,0 @@ -Extension:LinkTitles - -0.0.7: 2012-05-20 -* Fix version information - -0.0.6: 2012-05-20 -* Fix bug: Minimum length title - -0.0.5: 2012-05-20 -* Add $wgLinkTitlesMinimumTitleLength configuration variable. - -0.0.4: 2012-05-20 -* Add $wgLinkTitlesPreferShortTitles configuration variable. - -0.0.3: 2012-05-20 -* Only look for page titles from 'normal' pages (namespace = 0). - -0.0.2: 2012-05-20 -* Prevent generation of self-references. -* Escape slashes in page titles before using them in a regexp. - -0.0.1: 2012-05-20 -* Initial release. - diff --git a/LinkTitles.body.php b/LinkTitles.body.php index 65c022c..01c0678 100755 --- a/LinkTitles.body.php +++ b/LinkTitles.body.php @@ -51,7 +51,7 @@ // 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, $content ); + return $minor or self::parseContent( $article, $text ); } /// Called when an ArticleAfterFetchContent event occurs; this requires the @@ -80,7 +80,8 @@ $myTitle = $article->getTitle()->getText(); ( $wgLinkTitlesPreferShortTitles ) ? $sort_order = 'DESC' : $sort_order = ''; - + ( $wgLinkTitlesParseHeadings ) ? $delimiter = '' : $delimiter = '=+.+?=+|'; + $delimiter = '/(' . $delimiter . '\[\[.*?\]\])/i'; // Build an SQL query and fetch all page titles ordered // by length from shortest to longest. @@ -95,7 +96,6 @@ array( 'ORDER BY' => 'CHAR_LENGTH(page_title) ' . $sort_order )); // Iterate through the page titles - $newText = $text; foreach( $res as $row ) { // Page titles are stored in the database with spaces // replaced by underscores. Therefore we now convert @@ -104,38 +104,20 @@ if ( $title != $myTitle ) { // split the string by [[...]] groups - $arr = preg_split( '/(\[\[.*?\]\])/', $newText, -1, PREG_SPLIT_DELIM_CAPTURE ); + // credits to inhan @ StackOverflow for suggesting preg_split + // see http://stackoverflow.com/questions/10672286 + $arr = preg_split( $delimiter, $text, -1, PREG_SPLIT_DELIM_CAPTURE ); + // dump( $arr ); $safeTitle = str_replace( '/', '\/', $title ); for ( $i = 0; $i < count( $arr ); $i+=2 ) { // even indexes will point to text that is not enclosed by brackets $arr[$i] = preg_replace( '/\b(' . $safeTitle . ')\b/i', '[[$1]]', $arr[$i] ); }; - $newText = implode( '', $arr ); + $text = implode( '', $arr ); }; // if $title != $myTitle }; // foreach $res as $row - if ( $newText != '' ) { - $text = $newText; - }; return true; } - - /* - * The following function was initially used, but it does not replace - * every occurrence of the title words in the page text. - * - public static function parse1( &$newText ) { - // Now look for every occurrence of $title in the - // page $text and enclose it in double square brackets, - // unless it is already enclosed in brackets (directly - // adjacent or remotely, see http://stackoverflow.com/questions/10672286 - // Regex built with the help from Eugene @ Stackoverflow - // http://stackoverflow.com/a/10672440/270712 - $newText = preg_replace( - '/(\b' . str_replace('/', '\/', $title) . '\b)([^\]]+(\[|$))/ium', - '[[$1]]$2', - $newText ); - return true; - } - */ } + // vim: ts=2:sw=2:noet diff --git a/LinkTitles.php b/LinkTitles.php index 65726fe..eb3c6fa 100755 --- a/LinkTitles.php +++ b/LinkTitles.php @@ -23,11 +23,13 @@ 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; + /* + error_reporting(E_ALL); + ini_set('display_errors', 'Off'); + ini_set('error_log', 'php://stderr'); + $wgMainCacheType = CACHE_NONE; + $wgCacheDirectory = false; + */ // Configuration variables $wgLinkTitlesPreferShortTitles = false; @@ -41,7 +43,7 @@ 'name' => 'LinkTitles', 'author' => '[http://www.mediawiki.org/wiki/User:Bovender Daniel Kraus]', 'url' => 'http://www.mediawiki.org/wiki/Extension:LinkTitles', - 'version' => '0.0.7', + 'version' => '1.0.0', 'descriptionmsg' => 'linktitles-desc' ); diff --git a/NEWS b/NEWS new file mode 100644 index 0000000..c792833 --- /dev/null +++ b/NEWS @@ -0,0 +1,29 @@ +LinkTitles 1.0.0: 2012-05-22 +* Added new option to parse or not parse headings +* Added new option to parse pages on saving edits +* Added new option to parse pages on viewing (unless the page is retrieved + from cache) +* Minor performance improvement + +LinkTitles 0.0.7: 2012-05-20 +* Fix version information + +LinkTitles 0.0.6: 2012-05-20 +* Fix bug: Minimum length title + +LinkTitles 0.0.5: 2012-05-20 +* Add $wgLinkTitlesMinimumTitleLength configuration variable. + +LinkTitles 0.0.4: 2012-05-20 +* Add $wgLinkTitlesPreferShortTitles configuration variable. + +LinkTitles 0.0.3: 2012-05-20 +* Only look for page titles from 'normal' pages (namespace = 0). + +LinkTitles 0.0.2: 2012-05-20 +* Prevent generation of self-references. +* Escape slashes in page titles before using them in a regexp. + +LinkTitles 0.0.1: 2012-05-20 +* Initial release. +