Use alternative algorithm to catch all occurrences of a title

This commit is contained in:
Daniel Kraus
2012-05-20 16:50:25 +02:00
parent 8a7a44c858
commit 8ea8da6100

View File

@ -55,7 +55,7 @@
'page_title', 'page_title',
'page_namespace = 0', 'page_namespace = 0',
__METHOD__, __METHOD__,
array( 'ORDER BY' => 'length(page_title)' )); array( 'ORDER BY' => 'length(page_title) DESC' ));
// Iterate through the page titles // Iterate through the page titles
$new_text = $text; $new_text = $text;
@ -66,17 +66,15 @@
$title = str_replace('_', ' ', $row->page_title); $title = str_replace('_', ' ', $row->page_title);
if ( $title != $my_title ) { if ( $title != $my_title ) {
// split the string by [[...]] groups
// Now look for every occurrence of $title in the $arr = preg_split( '/(\[\[.*?\]\])/', $new_text, -1, PREG_SPLIT_DELIM_CAPTURE );
// page $text and enclose it in double square brackets, $safe_title = str_replace( '/', '\/', $title );
// unless it is already enclosed in brackets (directly for ( $i = 0; $i < count( $arr ); $i+=2 ) {
// adjacent or remotely, see http://stackoverflow.com/questions/10672286 // even indexes will text that is not enclosed by brackets
// Regex built with the help from Eugene @ Stackoverflow $arr[$i] = preg_replace( '/\b(' . $safe_title . ')\b/i', '[[$1]]', $arr[$i] );
// http://stackoverflow.com/a/10672440/270712 };
$new_text = preg_replace( dump( $arr );
'/(\b' . str_replace('/', '\/', $title) . '\b)([^\]]+(\[|$))/i', $new_text = implode( '', $arr );
'[[$1]]$2',
$new_text );
}; // if $title != $my_title }; // if $title != $my_title
}; // foreach $res as $row }; // foreach $res as $row
if ( $new_text != '' ) { if ( $new_text != '' ) {
@ -85,5 +83,24 @@
}; };
return true; 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 ) {
// 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(
'/(\b' . str_replace('/', '\/', $title) . '\b)([^\]]+(\[|$))/ium',
'[[$1]]$2',
$new_text );
return true;
}
*/
} }
// vim: ts=2:sw=2:noet // vim: ts=2:sw=2:noet