mirror of
https://github.com/diocloid/LinkTitles.git
synced 2025-07-13 09:49:31 +02:00
Add options to parse on editing or rendering a page.
This commit is contained in:
@ -36,74 +36,85 @@
|
|||||||
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 ) {
|
||||||
// To prevent adding self-references, we now
|
return parseContent( $article, $content );
|
||||||
// extract the current page's title.
|
};
|
||||||
$my_title = $article->getTitle()->getText();
|
}
|
||||||
|
|
||||||
( $wgLinkTitlesPreferShortTitles ) ? $sort_order = 'DESC' : $sort_order = '';
|
/// Called when an ArticleAfterFetchContent event occurs; this requires the
|
||||||
|
/// $wgLinkTitlesParseOnRender option to be set to 'true'
|
||||||
// Build an SQL query and fetch all page titles ordered
|
public static function onArticleAfterFetchContent( &$article, &$content ) {
|
||||||
// by length from shortest to longest.
|
return parseContent( $article, $content );
|
||||||
// Only titles from 'normal' pages (namespace uid = 0)
|
}
|
||||||
// are returned.
|
|
||||||
$dbr = wfGetDB( DB_SLAVE );
|
/// This function performs the actual parsing of the content.
|
||||||
$res = $dbr->select(
|
static function parseContent( &$article, &$content ) {
|
||||||
'page',
|
// Configuration variables need to be defined here as globals.
|
||||||
'page_title',
|
global $wgLinkTitlesPreferShortTitles;
|
||||||
array( 'page_namespace = 0', 'CHAR_LENGTH(page_title) >= ' . $wgLinkTitlesMinimumTitleLength ),
|
global $wgLinkTitlesMinimumTitleLength;
|
||||||
__METHOD__,
|
global $wgLinkTitlesParseHeadings;
|
||||||
array( 'ORDER BY' => 'CHAR_LENGTH(page_title) ' . $sort_order ));
|
|
||||||
|
// To prevent adding self-references, we now
|
||||||
// Iterate through the page titles
|
// extract the current page's title.
|
||||||
$new_text = $text;
|
$myTitle = $article->getTitle()->getText();
|
||||||
foreach( $res as $row ) {
|
|
||||||
// Page titles are stored in the database with spaces
|
( $wgLinkTitlesPreferShortTitles ) ? $sort_order = 'DESC' : $sort_order = '';
|
||||||
// replaced by underscores. Therefore we now convert
|
|
||||||
// the underscores back to spaces.
|
|
||||||
$title = str_replace('_', ' ', $row->page_title);
|
// Build an SQL query and fetch all page titles ordered
|
||||||
|
// by length from shortest to longest.
|
||||||
if ( $title != $my_title ) {
|
// Only titles from 'normal' pages (namespace uid = 0)
|
||||||
// split the string by [[...]] groups
|
// are returned.
|
||||||
$arr = preg_split( '/(\[\[.*?\]\])/', $new_text, -1, PREG_SPLIT_DELIM_CAPTURE );
|
$dbr = wfGetDB( DB_SLAVE );
|
||||||
$safe_title = str_replace( '/', '\/', $title );
|
$res = $dbr->select(
|
||||||
for ( $i = 0; $i < count( $arr ); $i+=2 ) {
|
'page',
|
||||||
// even indexes will text that is not enclosed by brackets
|
'page_title',
|
||||||
$arr[$i] = preg_replace( '/\b(' . $safe_title . ')\b/i', '[[$1]]', $arr[$i] );
|
array( 'page_namespace = 0', 'CHAR_LENGTH(page_title) >= ' . $wgLinkTitlesMinimumTitleLength ),
|
||||||
};
|
__METHOD__,
|
||||||
$new_text = implode( '', $arr );
|
array( 'ORDER BY' => 'CHAR_LENGTH(page_title) ' . $sort_order ));
|
||||||
}; // if $title != $my_title
|
|
||||||
}; // foreach $res as $row
|
// Iterate through the page titles
|
||||||
if ( $new_text != '' ) {
|
$newText = $text;
|
||||||
$text = $new_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 != $myTitle ) {
|
||||||
|
// split the string by [[...]] groups
|
||||||
|
$arr = preg_split( '/(\[\[.*?\]\])/', $newText, -1, PREG_SPLIT_DELIM_CAPTURE );
|
||||||
|
$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 );
|
||||||
|
}; // if $title != $myTitle
|
||||||
|
}; // foreach $res as $row
|
||||||
|
if ( $newText != '' ) {
|
||||||
|
$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;
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
@ -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.
|
||||||
$wgHooks['ArticleSave'][] = 'LinkTitles::onArticleSave';
|
if ( $wgLinkTitlesParseOnEdit ) {
|
||||||
|
$wgHooks['ArticleSave'][] = 'LinkTitles::onArticleSave';
|
||||||
// Configuration variables
|
};
|
||||||
$wgLinkTitlesPreferShortTitles = false;
|
if ( $wgLinkTitlesParseOnRender ) {
|
||||||
$wgLinkTitlesMinimumTitleLength = 3;
|
$wgHooks['ArticleAfterFetchContent'][] = 'LinkTitles::onArticleAfterFetchContent';
|
||||||
|
};
|
||||||
|
|
||||||
// vim: ts=2:sw=2:noet
|
// vim: ts=2:sw=2:noet
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user