RepoGroup: replace Title in method signatures

Bug: T278459
Change-Id: Icee70d3743b321b25c046429e8738fffa6a657de
This commit is contained in:
daniel 2021-04-09 17:18:14 +02:00 committed by Daniel Kinzler
parent 085a1ef4ce
commit e0000ce176
3 changed files with 62 additions and 6 deletions

View file

@ -21,7 +21,9 @@
* @ingroup FileRepo
*/
use MediaWiki\Linker\LinkTarget;
use MediaWiki\MediaWikiServices;
use MediaWiki\Page\PageIdentity;
/**
* Prioritized list of file repositories
@ -105,7 +107,7 @@ class RepoGroup {
/**
* Search repositories for an image.
*
* @param Title|string $title Title object or string
* @param PageIdentity|LinkTarget|string $title The file to find
* @param array $options Associative array of options:
* time: requested time for an archived image, or false for the
* current version. An image object will be returned which was
@ -223,14 +225,16 @@ class RepoGroup {
/**
* Interface for FileRepo::checkRedirect()
* @param Title $title
* @param PageIdentity|LinkTarget|string $title
* @return bool|Title
*/
public function checkRedirect( Title $title ) {
public function checkRedirect( $title ) {
if ( !$this->reposInitialised ) {
$this->initialiseRepos();
}
$title = File::normalizeTitle( $title );
$redir = $this->localRepo->checkRedirect( $title );
if ( $redir ) {
return $redir;
@ -463,11 +467,13 @@ class RepoGroup {
/**
* Clear RepoGroup process cache used for finding a file
* @param Title|null $title Title of the file or null to clear all files
* @param PageIdentity|string|null $title File page or file name, or null to clear all files
*/
public function clearCache( Title $title = null ) {
public function clearCache( $title = null ) {
if ( $title == null ) {
$this->cache->clear();
} elseif ( is_string( $title ) ) {
$this->cache->clear( $title );
} else {
$this->cache->clear( $title->getDBkey() );
}

View file

@ -6,7 +6,9 @@
* Represents files in a repository.
*/
use MediaWiki\HookContainer\ProtectedHookAccessorTrait;
use MediaWiki\Linker\LinkTarget;
use MediaWiki\MediaWikiServices;
use MediaWiki\Page\PageIdentity;
use Wikimedia\AtEase\AtEase;
/**
@ -191,13 +193,22 @@ abstract class File implements IDBAccessObject {
* Given a string or Title object return either a
* valid Title object with namespace NS_FILE or null
*
* @param Title|string $title
* @param PageIdentity|LinkTarget|string $title
* @param string|bool $exception Use 'exception' to throw an error on bad titles
* @throws MWException
* @return Title|null
*/
public static function normalizeTitle( $title, $exception = false ) {
$ret = $title;
if ( !$ret instanceof Title ) {
if ( $ret instanceof PageIdentity ) {
$ret = Title::castFromPageIdentity( $ret );
} elseif ( $ret instanceof LinkTarget ) {
$ret = Title::castFromLinkTarget( $ret );
}
}
if ( $ret instanceof Title ) {
# Normalize NS_MEDIA -> NS_FILE
if ( $ret->getNamespace() === NS_MEDIA ) {

View file

@ -1,5 +1,7 @@
<?php
use MediaWiki\Page\PageIdentityValue;
class FileTest extends MediaWikiMediaTestCase {
/**
@ -442,4 +444,41 @@ class FileTest extends MediaWikiMediaTestCase {
],
];
}
public function provideNormalizeTitle() {
yield [ 'some name.jpg', 'Some_name.jpg' ];
yield [ new TitleValue( NS_FILE, 'Some_name.jpg' ), 'Some_name.jpg' ];
yield [ new TitleValue( NS_MEDIA, 'Some_name.jpg' ), 'Some_name.jpg' ];
yield [ new PageIdentityValue( 0, NS_FILE, 'Some_name.jpg', false ), 'Some_name.jpg' ];
}
/**
* @covers File::normalizeTitle
* @dataProvider provideNormalizeTitle
*/
public function testNormalizeTitle( $title, $expected ) {
$actual = File::normalizeTitle( $title );
$this->assertSame( NS_FILE, $actual->getNamespace() );
$this->assertSame( $expected, $actual->getDBkey() );
}
public function provideNormalizeTitleFails() {
yield [ '' ];
yield [ '#' ];
yield [ new TitleValue( NS_USER, 'Some_name.jpg' ) ];
yield [ new PageIdentityValue( 0, NS_USER, 'Some_name.jpg', false ) ];
}
/**
* @covers File::normalizeTitle
* @dataProvider provideNormalizeTitleFails
*/
public function testNormalizeTitleFails( $title ) {
$actual = File::normalizeTitle( $title );
$this->assertNull( $actual );
$this->expectException( MWException::class );
File::normalizeTitle( $title, 'exception' );
}
}