mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/PageImages
synced 2024-11-15 03:43:46 +00:00
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:
parent
fd123da0a3
commit
33ee32f448
|
@ -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 );
|
||||
|
||||
|
|
Loading…
Reference in a new issue