mirror of
https://github.com/diocloid/LinkTitles.git
synced 2025-07-13 01:39:30 +02:00
First working version.
This commit is contained in:
@ -39,22 +39,42 @@
|
|||||||
/// saved.
|
/// saved.
|
||||||
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 ) {
|
||||||
error_reporting(E_ALL);
|
|
||||||
ini_set('display_errors', 'Off');
|
// To prevent time-consuming parsing of the page whenever
|
||||||
ini_set('error_log', 'php://stderr');
|
// it is edited and saved, we only parse it if the flag
|
||||||
global $wgRequest;
|
// 'minor edits' is not set.
|
||||||
$params = new DerivativeRequest(
|
|
||||||
$wgRequest,
|
if ( !$minor ) {
|
||||||
array(
|
// Build an SQL query and fetch all page titles ordered
|
||||||
'action' => 'query',
|
// by length from shortest to longest.
|
||||||
'list' => 'allpages')
|
$dbr = wfGetDB( DB_SLAVE );
|
||||||
);
|
$res = $dbr->select(
|
||||||
// $api = new ApiMain( $params, false ); // false: do not edit page
|
'page',
|
||||||
// $api->execute();
|
'page_title',
|
||||||
// $data = & $api->getResultData();
|
'',
|
||||||
// dump($data);
|
__METHOD__,
|
||||||
|
array( 'ORDER BY' => 'length(page_title)' ));
|
||||||
|
|
||||||
|
// Iterate through the page titles
|
||||||
|
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);
|
||||||
|
|
||||||
|
// 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
|
||||||
|
$text = preg_replace(
|
||||||
|
'/(' . $title . ')([^\]]+(\[|$))/i',
|
||||||
|
'[[$1]]$2',
|
||||||
|
$text );
|
||||||
|
};
|
||||||
|
};
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
// vim: ts=2:sw=2:noet
|
// vim: ts=2:sw=2:noet
|
||||||
|
13
LinkTitles.i18n.php
Executable file
13
LinkTitles.i18n.php
Executable file
@ -0,0 +1,13 @@
|
|||||||
|
<?php
|
||||||
|
/*! \file LinkTitles.i18n.php
|
||||||
|
*/
|
||||||
|
|
||||||
|
$messages = array();
|
||||||
|
|
||||||
|
$messages['en'] = array(
|
||||||
|
'pubmedparser-desc' => 'Automatically adds links to existing pages when a page is saved.',
|
||||||
|
);
|
||||||
|
|
||||||
|
$messages['de'] = array(
|
||||||
|
'pubmedparser-desc' => 'Fügt beim Speichern von Seiten automatisch Querverweise zu vorhandenen Seiten ein.',
|
||||||
|
);
|
@ -41,5 +41,9 @@
|
|||||||
$wgHooks['ParserFirstCallInit'][] = 'LinkTitles::Setup';
|
$wgHooks['ParserFirstCallInit'][] = 'LinkTitles::Setup';
|
||||||
$wgHooks['ArticleSave'][] = 'LinkTitles::onArticleSave';
|
$wgHooks['ArticleSave'][] = 'LinkTitles::onArticleSave';
|
||||||
|
|
||||||
|
// error_reporting(E_ALL);
|
||||||
|
// ini_set('display_errors', 'Off');
|
||||||
|
// ini_set('error_log', 'php://stderr');
|
||||||
|
|
||||||
// vim: ts=2:sw=2:noet
|
// vim: ts=2:sw=2:noet
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user