summaryrefslogtreecommitdiff
path: root/includes/filerepo/FileRepo.php
diff options
context:
space:
mode:
Diffstat (limited to 'includes/filerepo/FileRepo.php')
-rw-r--r--includes/filerepo/FileRepo.php174
1 files changed, 137 insertions, 37 deletions
diff --git a/includes/filerepo/FileRepo.php b/includes/filerepo/FileRepo.php
index ee7691a6..edfc2a99 100644
--- a/includes/filerepo/FileRepo.php
+++ b/includes/filerepo/FileRepo.php
@@ -3,9 +3,12 @@
/**
* Base class for file repositories
* Do not instantiate, use a derived class.
+ * @ingroup FileRepo
*/
abstract class FileRepo {
const DELETE_SOURCE = 1;
+ const FIND_PRIVATE = 1;
+ const FIND_IGNORE_REDIRECT = 2;
const OVERWRITE = 2;
const OVERWRITE_SAME = 4;
@@ -13,20 +16,21 @@ abstract class FileRepo {
var $descBaseUrl, $scriptDirUrl, $articleUrl, $fetchDescription, $initialCapital;
var $pathDisclosureProtection = 'paranoid';
- /**
+ /**
* Factory functions for creating new files
* Override these in the base class
*/
var $fileFactory = false, $oldFileFactory = false;
+ var $fileFactoryKey = false, $oldFileFactoryKey = false;
function __construct( $info ) {
// Required settings
$this->name = $info['name'];
-
+
// Optional settings
$this->initialCapital = true; // by default
- foreach ( array( 'descBaseUrl', 'scriptDirUrl', 'articleUrl', 'fetchDescription',
- 'thumbScriptUrl', 'initialCapital', 'pathDisclosureProtection' ) as $var )
+ foreach ( array( 'descBaseUrl', 'scriptDirUrl', 'articleUrl', 'fetchDescription',
+ 'thumbScriptUrl', 'initialCapital', 'pathDisclosureProtection', 'descriptionCacheExpiry' ) as $var )
{
if ( isset( $info[$var] ) ) {
$this->$var = $info[$var];
@@ -45,10 +49,10 @@ abstract class FileRepo {
/**
* Create a new File object from the local repository
* @param mixed $title Title object or string
- * @param mixed $time Time at which the image is supposed to have existed.
- * If this is specified, the returned object will be an
+ * @param mixed $time Time at which the image was uploaded.
+ * If this is specified, the returned object will be an
* instance of the repository's old file class instead of
- * a current file. Repositories not supporting version
+ * a current file. Repositories not supporting version
* control should return false if this parameter is set.
*/
function newFile( $title, $time = false ) {
@@ -70,13 +74,20 @@ abstract class FileRepo {
}
/**
- * Find an instance of the named file that existed at the specified time
- * Returns false if the file did not exist. Repositories not supporting
+ * Find an instance of the named file created at the specified time
+ * Returns false if the file does not exist. Repositories not supporting
* version control should return false if the time is specified.
*
+ * @param mixed $title Title object or string
* @param mixed $time 14-character timestamp, or false for the current version
*/
- function findFile( $title, $time = false ) {
+ function findFile( $title, $time = false, $flags = 0 ) {
+ if ( !($title instanceof Title) ) {
+ $title = Title::makeTitleSafe( NS_IMAGE, $title );
+ if ( !is_object( $title ) ) {
+ return false;
+ }
+ }
# First try the current version of the file to see if it precedes the timestamp
$img = $this->newFile( $title );
if ( !$img ) {
@@ -86,23 +97,100 @@ abstract class FileRepo {
return $img;
}
# Now try an old version of the file
- $img = $this->newFile( $title, $time );
- if ( $img->exists() ) {
- return $img;
+ if ( $time !== false ) {
+ $img = $this->newFile( $title, $time );
+ if ( $img->exists() ) {
+ if ( !$img->isDeleted(File::DELETED_FILE) ) {
+ return $img;
+ } else if ( ($flags & FileRepo::FIND_PRIVATE) && $img->userCan(File::DELETED_FILE) ) {
+ return $img;
+ }
+ }
}
-
+
# Now try redirects
- $redir = $this->checkRedirect( $title );
+ if ( $flags & FileRepo::FIND_IGNORE_REDIRECT ) {
+ return false;
+ }
+ $redir = $this->checkRedirect( $title );
if( $redir && $redir->getNamespace() == NS_IMAGE) {
$img = $this->newFile( $redir );
if( !$img ) {
return false;
}
if( $img->exists() ) {
- $img->redirectedFrom( $title->getText() );
+ $img->redirectedFrom( $title->getDBkey() );
return $img;
}
}
+ return false;
+ }
+
+ /*
+ * Find many files at once.
+ * @param array $titles, an array of titles
+ * @param int $flags
+ */
+ function findFiles( $titles, $flags ) {
+ $result = array();
+ foreach ( $titles as $index => $title ) {
+ $file = $this->findFile( $title, $flags );
+ if ( $file )
+ $result[$file->getTitle()->getDBkey()] = $file;
+ }
+ return $result;
+ }
+
+ /**
+ * Create a new File object from the local repository
+ * @param mixed $sha1 SHA-1 key
+ * @param mixed $time Time at which the image was uploaded.
+ * If this is specified, the returned object will be an
+ * instance of the repository's old file class instead of
+ * a current file. Repositories not supporting version
+ * control should return false if this parameter is set.
+ */
+ function newFileFromKey( $sha1, $time = false ) {
+ if ( $time ) {
+ if ( $this->oldFileFactoryKey ) {
+ return call_user_func( $this->oldFileFactoryKey, $sha1, $this, $time );
+ } else {
+ return false;
+ }
+ } else {
+ return call_user_func( $this->fileFactoryKey, $sha1, $this );
+ }
+ }
+
+ /**
+ * Find an instance of the file with this key, created at the specified time
+ * Returns false if the file does not exist. Repositories not supporting
+ * version control should return false if the time is specified.
+ *
+ * @param string $sha1 string
+ * @param mixed $time 14-character timestamp, or false for the current version
+ */
+ function findFileFromKey( $sha1, $time = false, $flags = 0 ) {
+ # First try the current version of the file to see if it precedes the timestamp
+ $img = $this->newFileFromKey( $sha1 );
+ if ( !$img ) {
+ return false;
+ }
+ if ( $img->exists() && ( !$time || $img->getTimestamp() == $time ) ) {
+ return $img;
+ }
+ # Now try an old version of the file
+ if ( $time !== false ) {
+ $img = $this->newFileFromKey( $sha1, $time );
+ if ( $img->exists() ) {
+ if ( !$img->isDeleted(File::DELETED_FILE) ) {
+ return $img;
+ } else if ( ($flags & FileRepo::FIND_PRIVATE) && $img->userCan(File::DELETED_FILE) ) {
+ return $img;
+ }
+ }
+ }
+ return false;
}
/**
@@ -163,11 +251,11 @@ abstract class FileRepo {
function getDescBaseUrl() {
if ( is_null( $this->descBaseUrl ) ) {
if ( !is_null( $this->articleUrl ) ) {
- $this->descBaseUrl = str_replace( '$1',
- wfUrlencode( Namespace::getCanonicalName( NS_IMAGE ) ) . ':', $this->articleUrl );
+ $this->descBaseUrl = str_replace( '$1',
+ wfUrlencode( MWNamespace::getCanonicalName( NS_IMAGE ) ) . ':', $this->articleUrl );
} elseif ( !is_null( $this->scriptDirUrl ) ) {
- $this->descBaseUrl = $this->scriptDirUrl . '/index.php?title=' .
- wfUrlencode( Namespace::getCanonicalName( NS_IMAGE ) ) . ':';
+ $this->descBaseUrl = $this->scriptDirUrl . '/index.php?title=' .
+ wfUrlencode( MWNamespace::getCanonicalName( NS_IMAGE ) ) . ':';
} else {
$this->descBaseUrl = false;
}
@@ -177,8 +265,8 @@ abstract class FileRepo {
/**
* Get the URL of an image description page. May return false if it is
- * unknown or not applicable. In general this should only be called by the
- * File class, since it may return invalid results for certain kinds of
+ * unknown or not applicable. In general this should only be called by the
+ * File class, since it may return invalid results for certain kinds of
* repositories. Use File::getDescriptionUrl() in user code.
*
* In particular, it uses the article paths as specified to the repository
@@ -194,15 +282,15 @@ abstract class FileRepo {
}
/**
- * Get the URL of the content-only fragment of the description page. For
- * MediaWiki this means action=render. This should only be called by the
- * repository's file class, since it may return invalid results. User code
+ * Get the URL of the content-only fragment of the description page. For
+ * MediaWiki this means action=render. This should only be called by the
+ * repository's file class, since it may return invalid results. User code
* should use File::getDescriptionText().
*/
function getDescriptionRenderUrl( $name ) {
if ( isset( $this->scriptDirUrl ) ) {
- return $this->scriptDirUrl . '/index.php?title=' .
- wfUrlencode( Namespace::getCanonicalName( NS_IMAGE ) . ':' . $name ) .
+ return $this->scriptDirUrl . '/index.php?title=' .
+ wfUrlencode( MWNamespace::getCanonicalName( NS_IMAGE ) . ':' . $name ) .
'&action=render';
} else {
$descBase = $this->getDescBaseUrl();
@@ -223,7 +311,7 @@ abstract class FileRepo {
* @param integer $flags Bitwise combination of the following flags:
* self::DELETE_SOURCE Delete the source file after upload
* self::OVERWRITE Overwrite an existing destination file instead of failing
- * self::OVERWRITE_SAME Overwrite the file if the destination exists and has the
+ * self::OVERWRITE_SAME Overwrite the file if the destination exists and has the
* same contents as the source
* @return FileRepoStatus
*/
@@ -247,7 +335,7 @@ abstract class FileRepo {
* Pick a random name in the temp zone and store a file to it.
* Returns a FileRepoStatus object with the URL in the value.
*
- * @param string $originalName The base name of the file as specified
+ * @param string $originalName The base name of the file as specified
* by the user. The file extension will be maintained.
* @param string $srcPath The current location of the file.
*/
@@ -268,7 +356,7 @@ abstract class FileRepo {
* virtual URL, into this repository at the specified destination location.
*
* Returns a FileRepoStatus object. On success, the value contains "new" or
- * "archived", to indicate whether the file was new with that name.
+ * "archived", to indicate whether the file was new with that name.
*
* @param string $srcPath The source path or URL
* @param string $dstRel The destination relative path
@@ -301,16 +389,16 @@ abstract class FileRepo {
/**
* Move a group of files to the deletion archive.
*
- * If no valid deletion archive is configured, this may either delete the
+ * If no valid deletion archive is configured, this may either delete the
* file or throw an exception, depending on the preference of the repository.
*
* The overwrite policy is determined by the repository -- currently FSRepo
- * assumes a naming scheme in the deleted zone based on content hash, as
+ * assumes a naming scheme in the deleted zone based on content hash, as
* opposed to the public zone which is assumed to be unique.
*
- * @param array $sourceDestPairs Array of source/destination pairs. Each element
+ * @param array $sourceDestPairs Array of source/destination pairs. Each element
* is a two-element array containing the source file path relative to the
- * public root in the first element, and the archive file path relative
+ * public root in the first element, and the archive file path relative
* to the deleted zone root in the second element.
* @return FileRepoStatus
*/
@@ -318,10 +406,10 @@ abstract class FileRepo {
/**
* Move a file to the deletion archive.
- * If no valid deletion archive exists, this may either delete the file
+ * If no valid deletion archive exists, this may either delete the file
* or throw an exception, depending on the preference of the repository
* @param mixed $srcRel Relative path for the file to be deleted
- * @param mixed $archiveRel Relative path for the archive location.
+ * @param mixed $archiveRel Relative path for the archive location.
* Relative to a private archive directory.
* @return WikiError object (wikitext-formatted), or true for success
*/
@@ -423,5 +511,17 @@ abstract class FileRepo {
function checkRedirect( $title ) {
return false;
}
-}
+ /**
+ * Invalidates image redirect cache related to that image
+ * STUB
+ *
+ * @param Title $title Title of image
+ */
+ function invalidateImageRedirect( $title ) {
+ }
+
+ function findBySha1( $hash ) {
+ return array();
+ }
+}