Skip to content

Commit

Permalink
Fix infinite loop for self-redirects with variants conversion
Browse files Browse the repository at this point in the history
The redirect targets another variant, but the target does not exist.
It causes infinite loop when querying the page with the redirect
and converttitles flags are both set.
Check the mConvertTitles to stop re-resolving the same title.

Bug: T333050
Change-Id: I64c5886dbd0a9148eeff033ba56306bcb7f2eb8c
  • Loading branch information
Xi-Plus committed Mar 25, 2023
1 parent 078a685 commit 24c3ef2
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
4 changes: 3 additions & 1 deletion includes/api/ApiPageSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -1281,7 +1281,9 @@ private function loadRedirectTargets() {
unset( $this->mPendingRedirectIDs[$rdfrom] );
if ( $to->isExternal() ) {
$this->mInterwikiTitles[$to->getPrefixedText()] = $to->getInterwiki();
} elseif ( !isset( $this->mAllPages[$to->getNamespace()][$to->getDBkey()] ) ) {
} elseif ( !isset( $this->mAllPages[$to->getNamespace()][$to->getDBkey()] )
&& !( $this->mConvertTitles && isset( $this->mConvertedTitles[$to->getPrefixedText()] ) )
) {
$titlesToResolve[] = $to;
}
$this->mRedirectTitles[$from] = $to;
Expand Down
59 changes: 59 additions & 0 deletions tests/phpunit/includes/api/ApiPageSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,65 @@ public function testHandleNormalization() {
);
}

public static function provideConversionWithRedirects() {
return [
'convert, redirect, convert' => [
[
[ '維基百科1', '#REDIRECT [[维基百科2]]' ],
[ '維基百科2', '' ],
],
[ 'titles' => '维基百科1', 'converttitles' => 1, 'redirects' => 1 ],
[ [ 'from' => '维基百科1', 'to' => '維基百科1' ], [ 'from' => '维基百科2', 'to' => '維基百科2' ] ],
[ [ 'from' => '維基百科1', 'to' => '维基百科2' ] ],
],

'redirect, convert, redirect' => [
[
[ '維基百科3', '#REDIRECT [[维基百科4]]' ],
[ '維基百科4', '#REDIRECT [[維基百科5]]' ],
],
[ 'titles' => '維基百科3', 'converttitles' => 1, 'redirects' => 1 ],
[ [ 'from' => '维基百科4', 'to' => '維基百科4' ] ],
[ [ 'from' => '維基百科3', 'to' => '维基百科4' ], [ 'from' => '維基百科4', 'to' => '維基百科5' ] ],
],

'hans redirects to hant with converttitles' => [
[
[ '维基百科6', '#REDIRECT [[維基百科6]]' ],
],
[ 'titles' => '维基百科6', 'converttitles' => 1, 'redirects' => 1 ],
[ [ 'from' => '維基百科6', 'to' => '维基百科6' ] ],
[ [ 'from' => '维基百科6', 'to' => '維基百科6' ] ],
],

'hans redirects to hant without converttitles' => [
[
[ '维基百科6', '#REDIRECT [[維基百科6]]' ],
],
[ 'titles' => '维基百科6', 'redirects' => 1 ],
[],
[ [ 'from' => '维基百科6', 'to' => '維基百科6' ] ],
],
];
}

/**
* @dataProvider provideConversionWithRedirects
*/
public function testHandleConversionWithRedirects( $pages, $params, $expectConversion, $exceptRedirects ) {
$this->overrideConfigValue( MainConfigNames::LanguageCode, 'zh' );

foreach ( $pages as $page ) {
$this->editPage( $page[0], $page[1] );
}

$pageSet = $this->newApiPageSet( $params );
$pageSet->execute();

$this->assertSame( $expectConversion, $pageSet->getConvertedTitlesAsResult() );
$this->assertSame( $exceptRedirects, $pageSet->getRedirectTitlesAsResult() );
}

public function testSpecialRedirects() {
$id1 = $this->editPage( 'UTApiPageSet', 'UTApiPageSet in the default language' )
->getNewRevision()->getPageId();
Expand Down

0 comments on commit 24c3ef2

Please sign in to comment.