First working version.

This commit is contained in:
Daniel Kraus
2012-05-20 12:58:29 +02:00
parent 13238548fb
commit 142c71e723
3 changed files with 52 additions and 15 deletions

View File

@ -39,22 +39,42 @@
/// saved.
public static function onArticleSave( &$article, &$user, &$text, &$summary,
$minor, $watchthis, $sectionanchor, &$flags, &$status ) {
error_reporting(E_ALL);
ini_set('display_errors', 'Off');
ini_set('error_log', 'php://stderr');
global $wgRequest;
$params = new DerivativeRequest(
$wgRequest,
array(
'action' => 'query',
'list' => 'allpages')
);
// $api = new ApiMain( $params, false ); // false: do not edit page
// $api->execute();
// $data = & $api->getResultData();
// dump($data);
// 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 ) {
// Build an SQL query and fetch all page titles ordered
// by length from shortest to longest.
$dbr = wfGetDB( DB_SLAVE );
$res = $dbr->select(
'page',
'page_title',
'',
__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;
}
}
// vim: ts=2:sw=2:noet

13
LinkTitles.i18n.php Executable file
View 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.',
);

View File

@ -41,5 +41,9 @@
$wgHooks['ParserFirstCallInit'][] = 'LinkTitles::Setup';
$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