Use preg_replace_callback() flag introduced in PHP 7.4

Should improve performance by avoiding the need to run the regex
twice.

Change-Id: Ic737c1c2189c1bfc046cdcb099cdcbc0edde0a97
This commit is contained in:
Tim Starling 2021-12-03 16:27:16 +11:00
parent fd123da0a3
commit 33ee32f448

View file

@ -122,27 +122,26 @@ class ParserFileProcessingHookHandlers implements
return;
}
// Find our special comments
// Find and remove our special comments
$images = [];
if ( $wgPageImagesLeadSectionOnly ) {
$sectionText = strstr( $text, '<mw:editsection', true );
if ( $sectionText === false ) {
$sectionText = $text;
}
$leadEndPos = strpos( $text, '<mw:editsection' );
} else {
$sectionText = $text;
$leadEndPos = false;
}
$matches = [];
preg_match_all( self::CANDIDATE_REGEX, $sectionText, $matches );
foreach ( $matches[1] as $id ) {
$id = intval( $id );
if ( isset( $allImages[$id] ) ) {
$images[] = PageImageCandidate::newFromArray( $allImages[$id] );
}
}
// Remove the comments
$text = preg_replace( self::CANDIDATE_REGEX, '', $text );
$text = preg_replace_callback(
self::CANDIDATE_REGEX,
static function ( $m ) use ( $allImages, &$images, $leadEndPos ) {
$offset = $m[0][1];
$id = intval( $m[1][0] );
$inLead = $leadEndPos === false || $offset < $leadEndPos;
if ( $inLead && isset( $allImages[$id] ) ) {
$images[] = PageImageCandidate::newFromArray( $allImages[$id] );
}
return '';
},
$text, -1, $count, PREG_OFFSET_CAPTURE
);
list( $bestImageName, $freeImageName ) = $this->findBestImages( $images );