Add options to parse on editing or rendering a page.

This commit is contained in:
Daniel Kraus
2012-05-21 18:29:28 +02:00
parent 2ede5c1804
commit 1175328eba
2 changed files with 72 additions and 54 deletions

View File

@ -36,21 +36,34 @@
public static function onArticleSave( &$article, &$user, &$text, &$summary, public static function onArticleSave( &$article, &$user, &$text, &$summary,
$minor, $watchthis, $sectionanchor, &$flags, &$status ) { $minor, $watchthis, $sectionanchor, &$flags, &$status ) {
// Configuration variables need to be defined here as globals.
global $wgLinkTitlesPreferShortTitles;
global $wgLinkTitlesMinimumTitleLength;
// To prevent time-consuming parsing of the page whenever // To prevent time-consuming parsing of the page whenever
// it is edited and saved, we only parse it if the flag // it is edited and saved, we only parse it if the flag
// 'minor edits' is not set. // 'minor edits' is not set.
if ( !$minor ) { if ( !$minor ) {
return 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 );
}
/// This function performs the actual parsing of the content.
static function parseContent( &$article, &$content ) {
// Configuration variables need to be defined here as globals.
global $wgLinkTitlesPreferShortTitles;
global $wgLinkTitlesMinimumTitleLength;
global $wgLinkTitlesParseHeadings;
// To prevent adding self-references, we now // To prevent adding self-references, we now
// extract the current page's title. // extract the current page's title.
$my_title = $article->getTitle()->getText(); $myTitle = $article->getTitle()->getText();
( $wgLinkTitlesPreferShortTitles ) ? $sort_order = 'DESC' : $sort_order = ''; ( $wgLinkTitlesPreferShortTitles ) ? $sort_order = 'DESC' : $sort_order = '';
// Build an SQL query and fetch all page titles ordered // Build an SQL query and fetch all page titles ordered
// by length from shortest to longest. // by length from shortest to longest.
// Only titles from 'normal' pages (namespace uid = 0) // Only titles from 'normal' pages (namespace uid = 0)
@ -64,46 +77,44 @@
array( 'ORDER BY' => 'CHAR_LENGTH(page_title) ' . $sort_order )); array( 'ORDER BY' => 'CHAR_LENGTH(page_title) ' . $sort_order ));
// Iterate through the page titles // Iterate through the page titles
$new_text = $text; $newText = $text;
foreach( $res as $row ) { foreach( $res as $row ) {
// Page titles are stored in the database with spaces // Page titles are stored in the database with spaces
// replaced by underscores. Therefore we now convert // replaced by underscores. Therefore we now convert
// the underscores back to spaces. // the underscores back to spaces.
$title = str_replace('_', ' ', $row->page_title); $title = str_replace('_', ' ', $row->page_title);
if ( $title != $my_title ) { if ( $title != $myTitle ) {
// split the string by [[...]] groups // split the string by [[...]] groups
$arr = preg_split( '/(\[\[.*?\]\])/', $new_text, -1, PREG_SPLIT_DELIM_CAPTURE ); $arr = preg_split( '/(\[\[.*?\]\])/', $newText, -1, PREG_SPLIT_DELIM_CAPTURE );
$safe_title = str_replace( '/', '\/', $title ); $safeTitle = str_replace( '/', '\/', $title );
for ( $i = 0; $i < count( $arr ); $i+=2 ) { for ( $i = 0; $i < count( $arr ); $i+=2 ) {
// even indexes will text that is not enclosed by brackets // even indexes will point to text that is not enclosed by brackets
$arr[$i] = preg_replace( '/\b(' . $safe_title . ')\b/i', '[[$1]]', $arr[$i] ); $arr[$i] = preg_replace( '/\b(' . $safeTitle . ')\b/i', '[[$1]]', $arr[$i] );
}; };
$new_text = implode( '', $arr ); $newText = implode( '', $arr );
}; // if $title != $my_title }; // if $title != $myTitle
}; // foreach $res as $row }; // foreach $res as $row
if ( $new_text != '' ) { if ( $newText != '' ) {
$text = $new_text; $text = $newText;
}; };
};
return true;
} }
/* /*
* The following function was initially used, but it does not replace * The following function was initially used, but it does not replace
* every occurrence of the title words in the page text. * every occurrence of the title words in the page text.
* *
public static function parse1( &$new_text ) { public static function parse1( &$newText ) {
// Now look for every occurrence of $title in the // Now look for every occurrence of $title in the
// page $text and enclose it in double square brackets, // page $text and enclose it in double square brackets,
// unless it is already enclosed in brackets (directly // unless it is already enclosed in brackets (directly
// adjacent or remotely, see http://stackoverflow.com/questions/10672286 // adjacent or remotely, see http://stackoverflow.com/questions/10672286
// Regex built with the help from Eugene @ Stackoverflow // Regex built with the help from Eugene @ Stackoverflow
// http://stackoverflow.com/a/10672440/270712 // http://stackoverflow.com/a/10672440/270712
$new_text = preg_replace( $newText = preg_replace(
'/(\b' . str_replace('/', '\/', $title) . '\b)([^\]]+(\[|$))/ium', '/(\b' . str_replace('/', '\/', $title) . '\b)([^\]]+(\[|$))/ium',
'[[$1]]$2', '[[$1]]$2',
$new_text ); $newText );
return true; return true;
} }
*/ */

View File

@ -31,6 +31,12 @@
$wgCacheDirectory = false; $wgCacheDirectory = false;
*/ */
// Configuration variables
$wgLinkTitlesPreferShortTitles = false;
$wgLinkTitlesMinimumTitleLength = 3;
$wgLinkTitlesParseOnEdit = true;
$wgLinkTitlesParseOnRender = false;
$wgExtensionCredits['parserhook'][] = array( $wgExtensionCredits['parserhook'][] = array(
'path' => __FILE__, 'path' => __FILE__,
'name' => 'LinkTitles', 'name' => 'LinkTitles',
@ -44,11 +50,12 @@
$wgAutoloadClasses['LinkTitles'] = dirname(__FILE__) . '/LinkTitles.body.php'; $wgAutoloadClasses['LinkTitles'] = dirname(__FILE__) . '/LinkTitles.body.php';
// Hook up our custom function to the ArticleSave event. // Hook up our custom function to the ArticleSave event.
if ( $wgLinkTitlesParseOnEdit ) {
$wgHooks['ArticleSave'][] = 'LinkTitles::onArticleSave'; $wgHooks['ArticleSave'][] = 'LinkTitles::onArticleSave';
};
// Configuration variables if ( $wgLinkTitlesParseOnRender ) {
$wgLinkTitlesPreferShortTitles = false; $wgHooks['ArticleAfterFetchContent'][] = 'LinkTitles::onArticleAfterFetchContent';
$wgLinkTitlesMinimumTitleLength = 3; };
// vim: ts=2:sw=2:noet // vim: ts=2:sw=2:noet