*/
/**
- * @var FileRepo
+ * @var FileRepo|false
*/
var $repo;
protected $canRender, $isSafeFile;
/**
- * Call this constructor from child classes
+ * @var string Required Repository class type
+ */
+ protected $repoClass = 'FileRepo';
+
+ /**
+ * Call this constructor from child classes.
+ *
+ * Both $title and $repo are optional, though some functions
+ * may return false or throw exceptions if they are not set.
+ * Most subclasses will want to call assertRepoDefined() here.
*
- * @param $title Title|false
+ * @param $title Title|string|false
* @param $repo FileRepo|false
*/
function __construct( $title, $repo ) {
- if ( $title !== false ) { // account for UnregisteredLocalFile et all
+ if ( $title !== false ) { // subclasses may not use MW titles
$title = self::normalizeTitle( $title, 'exception' );
}
$this->title = $title;
*/
public function getName() {
if ( !isset( $this->name ) ) {
+ $this->assertRepoDefined();
$this->name = $this->repo->getNameFromTitle( $this->title );
}
return $this->name;
/**
* Return the associated title object
- * @return Title
+ * @return Title|false
*/
public function getTitle() { return $this->title; }
*/
public function getUrl() {
if ( !isset( $this->url ) ) {
+ $this->assertRepoDefined();
$this->url = $this->repo->getZoneUrl( 'public' ) . '/' . $this->getUrlRel();
}
return $this->url;
*/
public function getPath() {
if ( !isset( $this->path ) ) {
- $this->path = $this->repo->getZonePath('public') . '/' . $this->getRel();
+ $this->assertRepoDefined();
+ $this->path = $this->repo->getZonePath( 'public' ) . '/' . $this->getRel();
}
return $this->path;
}
*/
function getHashPath() {
if ( !isset( $this->hashPath ) ) {
+ $this->assertRepoDefined();
$this->hashPath = $this->repo->getHashPath( $this->getName() );
}
return $this->hashPath;
* @return string
*/
function getArchivePath( $suffix = false ) {
+ $this->assertRepoDefined();
return $this->repo->getZonePath( 'public' ) . '/' . $this->getArchiveRel( $suffix );
}
* @return string
*/
function getArchiveThumbPath( $archiveName, $suffix = false ) {
- return $this->repo->getZonePath( 'thumb' ) . '/' . $this->getArchiveThumbRel( $archiveName, $suffix );
+ $this->assertRepoDefined();
+ return $this->repo->getZonePath( 'thumb' ) . '/' .
+ $this->getArchiveThumbRel( $archiveName, $suffix );
}
/**
* @return string
*/
function getThumbPath( $suffix = false ) {
+ $this->assertRepoDefined();
$path = $this->repo->getZonePath( 'thumb' ) . '/' . $this->getRel();
if ( $suffix !== false ) {
$path .= '/' . $suffix;
* @return string
*/
function getArchiveUrl( $suffix = false ) {
- $path = $this->repo->getZoneUrl('public') . '/archive/' . $this->getHashPath();
+ $this->assertRepoDefined();
+ $path = $this->repo->getZoneUrl( 'public' ) . '/archive/' . $this->getHashPath();
if ( $suffix === false ) {
$path = substr( $path, 0, -1 );
} else {
* @return string
*/
function getArchiveThumbUrl( $archiveName, $suffix = false ) {
- $path = $this->repo->getZoneUrl('thumb') . '/archive/' . $this->getHashPath() . rawurlencode( $archiveName ) . "/";
+ $this->assertRepoDefined();
+ $path = $this->repo->getZoneUrl( 'thumb' ) . '/archive/' .
+ $this->getHashPath() . rawurlencode( $archiveName ) . "/";
if ( $suffix === false ) {
$path = substr( $path, 0, -1 );
} else {
* @return path
*/
function getThumbUrl( $suffix = false ) {
+ $this->assertRepoDefined();
$path = $this->repo->getZoneUrl('thumb') . '/' . $this->getUrlRel();
if ( $suffix !== false ) {
$path .= '/' . rawurlencode( $suffix );
* @return string
*/
function getArchiveVirtualUrl( $suffix = false ) {
+ $this->assertRepoDefined();
$path = $this->repo->getVirtualUrl() . '/public/archive/' . $this->getHashPath();
if ( $suffix === false ) {
$path = substr( $path, 0, -1 );
* @return string
*/
function getThumbVirtualUrl( $suffix = false ) {
+ $this->assertRepoDefined();
$path = $this->repo->getVirtualUrl() . '/thumb/' . $this->getUrlRel();
if ( $suffix !== false ) {
$path .= '/' . rawurlencode( $suffix );
* @return string
*/
function getVirtualUrl( $suffix = false ) {
+ $this->assertRepoDefined();
$path = $this->repo->getVirtualUrl() . '/public/' . $this->getUrlRel();
if ( $suffix !== false ) {
$path .= '/' . rawurlencode( $suffix );
* @return bool
*/
function isHashed() {
+ $this->assertRepoDefined();
return $this->repo->isHashed();
}
/**
* Returns the repository
*
- * @return FileRepo
+ * @return FileRepo|false
*/
function getRepo() {
return $this->repo;
*/
function getDescriptionText() {
global $wgMemc, $wgLang;
- if ( !$this->repo->fetchDescription ) {
+ if ( !$this->repo || !$this->repo->fetchDescription ) {
return false;
}
$renderUrl = $this->repo->getDescriptionRenderUrl( $this->getName(), $wgLang->getCode() );
function isMissing() {
return false;
}
+
+ /**
+ * Assert that $this->repo is set to a valid FileRepo instance
+ * @throws MWException
+ */
+ protected function assertRepoDefined() {
+ if ( !( $this->repo instanceof $this->repoClass ) ) {
+ throw new MWException( "A {$this->repoClass} object is not set for this File.\n" );
+ }
+ }
+
+ /**
+ * Assert that $this->title is set to a Title
+ * @throws MWException
+ */
+ protected function assertTitleDefined() {
+ if ( !( $this->title instanceof Title ) ) {
+ throw new MWException( "A Title object is not set for this File.\n" );
+ }
+ }
}
/**
* Aliases for backwards compatibility with 1.6