* Broke a few long lines.
$this->dataLoaded = false;
$this->exists = false;
- if( is_object( $title ) ) {
- $this->title = $title;
+ if( $title instanceof Title ) {
+ $this->title = File::normalizeTitle( $title, 'exception' );
$this->name = $title->getDBkey();
}
$this->key = $key;
}
- if ( !$id && !$key && !is_object( $title ) ) {
+ if ( !$id && !$key && !( $title instanceof Title ) ) {
throw new MWException( "No specifications provided to ArchivedFile constructor." );
}
}
*/
/**
- * @var LocalRepo
+ * @var FileRepo
*/
var $repo;
/**
- * @var Title
+ * @var Title|false
*/
var $title;
/**
* Call this constructor from child classes
*
- * @param $title
- * @param $repo
+ * @param $title Title|false
+ * @param $repo FileRepo|false
*/
function __construct( $title, $repo ) {
+ if ( $title !== false ) { // account for UnregisteredLocalFile et all
+ $title = self::normalizeTitle( $title, 'exception' );
+ }
$this->title = $title;
$this->repo = $repo;
}
+ /**
+ * Given a string or Title object return either a
+ * valid Title object with namespace NS_FILE or null
+ * @param $title Title|string
+ * @param $exception string|false Use 'exception' to throw an error on bad titles
+ * @return Title|null
+ */
+ static function normalizeTitle( $title, $exception = false ) {
+ $ret = $title;
+ if ( $ret instanceof Title ) {
+ # Normalize NS_MEDIA -> NS_FILE
+ if ( $ret->getNamespace() == NS_MEDIA ) {
+ $ret = Title::makeTitleSafe( NS_FILE, $ret->getDBkey() );
+ # Sanity check the title namespace
+ } elseif ( $ret->getNamespace() !== NS_FILE ) {
+ $ret = null;
+ }
+ } else {
+ # Convert strings to Title objects
+ $ret = Title::makeTitleSafe( NS_FILE, (string)$ret );
+ }
+ if ( !$ret && $exception !== false ) {
+ throw new MWException( "`$title` is not a valid file title." );
+ }
+ return $ret;
+ }
+
function __get( $name ) {
$function = array( $this, 'get' . ucfirst( $name ) );
if ( !is_callable( $function ) ) {
* @param $title Title of image
* @return Bool
*/
- function checkRedirect( $title ) {
+ function checkRedirect( Title $title ) {
return false;
}
*
* @param $title Title of file
*/
- function checkRedirect( $title ) {
+ function checkRedirect( Title $title ) {
global $wgMemc;
- if( is_string( $title ) ) {
- $title = Title::newFromText( $title );
- }
- if( $title instanceof Title && $title->getNamespace() == NS_MEDIA ) {
- $title = Title::makeTitle( NS_FILE, $title->getText() );
- }
+ $title = File::normalizeTitle( $title, 'exception' );
$memcKey = $this->getSharedCacheKey( 'image_redirect', md5( $title->getDBkey() ) );
if ( $memcKey === false ) {
if ( !$this->reposInitialised ) {
$this->initialiseRepos();
}
- if ( !($title instanceof Title) ) {
- $title = Title::makeTitleSafe( NS_FILE, $title );
- if ( !is_object( $title ) ) {
- return false;
- }
- }
-
- if ( $title->getNamespace() != NS_MEDIA && $title->getNamespace() != NS_FILE ) {
- throw new MWException( __METHOD__ . ' received an Title object with incorrect namespace' );
+ $title = File::normalizeTitle( $title );
+ if ( !$title ) {
+ return false;
}
# Check the cache
if ( empty( $options['ignoreRedirect'] )
&& empty( $options['private'] )
- && empty( $options['bypassCache'] )
- && $title->getNamespace() == NS_FILE )
+ && empty( $options['bypassCache'] ) )
{
$useCache = true;
$time = isset( $options['time'] ) ? $options['time'] : '';
/**
* Interface for FileRepo::checkRedirect()
*/
- function checkRedirect( $title ) {
+ function checkRedirect( Title $title ) {
if ( !$this->reposInitialised ) {
$this->initialiseRepos();
}
/**
* @throws MWException
- * @param $title string
+ * @param $title Title|false
* @param $repo FSRepo
* @param $path string
* @param $mime string
if ( !( $title && $repo ) && !$path ) {
throw new MWException( __METHOD__.': not enough parameters, must specify title and repo, or a full path' );
}
- if ( $title ) {
- $this->title = $title;
+ if ( $title instanceof Title ) {
+ $this->title = File::normalizeTitle( $title, 'exception' );
$this->name = $repo->getNameFromTitle( $title );
} else {
$this->name = basename( $path );
- $this->title = Title::makeTitleSafe( NS_FILE, $this->name );
+ $this->title = File::normalizeTitle( $this->name, 'exception' );
}
$this->repo = $repo;
if ( $path ) {
$this->path = $path;
} else {
- $this->path = $repo->getRootDirectory() . '/' . $repo->getHashPath( $this->name ) . $this->name;
+ $this->path = $repo->getRootDirectory() . '/' .
+ $repo->getHashPath( $this->name ) . $this->name;
}
if ( $mime ) {
$this->mime = $mime;
function getURL() {
if ( $this->repo ) {
- return $this->repo->getZoneUrl( 'public' ) . '/' . $this->repo->getHashPath( $this->name ) . rawurlencode( $this->name );
+ return $this->repo->getZoneUrl( 'public' ) . '/' .
+ $this->repo->getHashPath( $this->name ) . rawurlencode( $this->name );
} else {
return false;
}