mirror of
https://github.com/diocloid/LinkTitles.git
synced 2025-07-13 01:39:30 +02:00
Add options to parse on editing or rendering a page.
This commit is contained in:
@ -36,21 +36,34 @@
|
||||
public static function onArticleSave( &$article, &$user, &$text, &$summary,
|
||||
$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
|
||||
// it is edited and saved, we only parse it if the flag
|
||||
// 'minor edits' is not set.
|
||||
|
||||
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
|
||||
// extract the current page's title.
|
||||
$my_title = $article->getTitle()->getText();
|
||||
$myTitle = $article->getTitle()->getText();
|
||||
|
||||
( $wgLinkTitlesPreferShortTitles ) ? $sort_order = 'DESC' : $sort_order = '';
|
||||
|
||||
|
||||
// Build an SQL query and fetch all page titles ordered
|
||||
// by length from shortest to longest.
|
||||
// Only titles from 'normal' pages (namespace uid = 0)
|
||||
@ -64,46 +77,44 @@
|
||||
array( 'ORDER BY' => 'CHAR_LENGTH(page_title) ' . $sort_order ));
|
||||
|
||||
// Iterate through the page titles
|
||||
$new_text = $text;
|
||||
$newText = $text;
|
||||
foreach( $res as $row ) {
|
||||
// Page titles are stored in the database with spaces
|
||||
// replaced by underscores. Therefore we now convert
|
||||
// the underscores back to spaces.
|
||||
$title = str_replace('_', ' ', $row->page_title);
|
||||
|
||||
if ( $title != $my_title ) {
|
||||
if ( $title != $myTitle ) {
|
||||
// split the string by [[...]] groups
|
||||
$arr = preg_split( '/(\[\[.*?\]\])/', $new_text, -1, PREG_SPLIT_DELIM_CAPTURE );
|
||||
$safe_title = str_replace( '/', '\/', $title );
|
||||
$arr = preg_split( '/(\[\[.*?\]\])/', $newText, -1, PREG_SPLIT_DELIM_CAPTURE );
|
||||
$safeTitle = str_replace( '/', '\/', $title );
|
||||
for ( $i = 0; $i < count( $arr ); $i+=2 ) {
|
||||
// even indexes will text that is not enclosed by brackets
|
||||
$arr[$i] = preg_replace( '/\b(' . $safe_title . ')\b/i', '[[$1]]', $arr[$i] );
|
||||
// even indexes will point to text that is not enclosed by brackets
|
||||
$arr[$i] = preg_replace( '/\b(' . $safeTitle . ')\b/i', '[[$1]]', $arr[$i] );
|
||||
};
|
||||
$new_text = implode( '', $arr );
|
||||
}; // if $title != $my_title
|
||||
$newText = implode( '', $arr );
|
||||
}; // if $title != $myTitle
|
||||
}; // foreach $res as $row
|
||||
if ( $new_text != '' ) {
|
||||
$text = $new_text;
|
||||
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( &$new_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
|
||||
$new_text = preg_replace(
|
||||
$newText = preg_replace(
|
||||
'/(\b' . str_replace('/', '\/', $title) . '\b)([^\]]+(\[|$))/ium',
|
||||
'[[$1]]$2',
|
||||
$new_text );
|
||||
$newText );
|
||||
return true;
|
||||
}
|
||||
*/
|
||||
|
@ -31,6 +31,12 @@
|
||||
$wgCacheDirectory = false;
|
||||
*/
|
||||
|
||||
// Configuration variables
|
||||
$wgLinkTitlesPreferShortTitles = false;
|
||||
$wgLinkTitlesMinimumTitleLength = 3;
|
||||
$wgLinkTitlesParseOnEdit = true;
|
||||
$wgLinkTitlesParseOnRender = false;
|
||||
|
||||
$wgExtensionCredits['parserhook'][] = array(
|
||||
'path' => __FILE__,
|
||||
'name' => 'LinkTitles',
|
||||
@ -44,11 +50,12 @@
|
||||
$wgAutoloadClasses['LinkTitles'] = dirname(__FILE__) . '/LinkTitles.body.php';
|
||||
|
||||
// Hook up our custom function to the ArticleSave event.
|
||||
if ( $wgLinkTitlesParseOnEdit ) {
|
||||
$wgHooks['ArticleSave'][] = 'LinkTitles::onArticleSave';
|
||||
|
||||
// Configuration variables
|
||||
$wgLinkTitlesPreferShortTitles = false;
|
||||
$wgLinkTitlesMinimumTitleLength = 3;
|
||||
};
|
||||
if ( $wgLinkTitlesParseOnRender ) {
|
||||
$wgHooks['ArticleAfterFetchContent'][] = 'LinkTitles::onArticleAfterFetchContent';
|
||||
};
|
||||
|
||||
// vim: ts=2:sw=2:noet
|
||||
|
||||
|
Reference in New Issue
Block a user