b5e3787a85404ff71f92c0fb648b8847fdf1399a
4 * Base class for file repositories
5 * Do not instantiate, use a derived class.
7 abstract class FileRepo
{
8 const DELETE_SOURCE
= 1;
10 var $thumbScriptUrl, $transformVia404;
11 var $descBaseUrl, $scriptDirUrl, $articleUrl, $fetchDescription, $initialCapital;
14 * Factory functions for creating new files
15 * Override these in the base class
17 var $fileFactory = false, $oldFileFactory = false;
19 function __construct( $info ) {
21 $this->name
= $info['name'];
24 $this->initialCapital
= true; // by default
25 foreach ( array( 'descBaseUrl', 'scriptDirUrl', 'articleUrl', 'fetchDescription',
26 'thumbScriptUrl', 'initialCapital' ) as $var )
28 if ( isset( $info[$var] ) ) {
29 $this->$var = $info[$var];
32 $this->transformVia404
= !empty( $info['transformVia404'] );
36 * Determine if a string is an mwrepo:// URL
38 static function isVirtualUrl( $url ) {
39 return substr( $url, 0, 9 ) == 'mwrepo://';
43 * Create a new File object from the local repository
44 * @param mixed $title Title object or string
45 * @param mixed $time Time at which the image is supposed to have existed.
46 * If this is specified, the returned object will be an
47 * instance of the repository's old file class instead of
48 * a current file. Repositories not supporting version
49 * control should return false if this parameter is set.
51 function newFile( $title, $time = false ) {
52 if ( !($title instanceof Title
) ) {
53 $title = Title
::makeTitleSafe( NS_IMAGE
, $title );
54 if ( !is_object( $title ) ) {
59 if ( $this->oldFileFactory
) {
60 return call_user_func( $this->oldFileFactory
, $title, $this, $time );
65 return call_user_func( $this->fileFactory
, $title, $this );
70 * Find an instance of the named file that existed at the specified time
71 * Returns false if the file did not exist. Repositories not supporting
72 * version control should return false if the time is specified.
74 * @param mixed $time 14-character timestamp, or false for the current version
76 function findFile( $title, $time = false ) {
77 # First try the current version of the file to see if it precedes the timestamp
78 $img = $this->newFile( $title );
82 if ( $img->exists() && ( !$time ||
$img->getTimestamp() <= $time ) ) {
85 # Now try an old version of the file
86 $img = $this->newFile( $title, $time );
87 if ( $img->exists() ) {
93 * Get the URL of thumb.php
95 function getThumbScriptUrl() {
96 return $this->thumbScriptUrl
;
100 * Returns true if the repository can transform files via a 404 handler
102 function canTransformVia404() {
103 return $this->transformVia404
;
107 * Get the name of an image from its title object
109 function getNameFromTitle( $title ) {
110 global $wgCapitalLinks;
111 if ( $this->initialCapital
!= $wgCapitalLinks ) {
113 $name = $title->getUserCaseDBKey();
114 if ( $this->initialCapital
) {
115 $name = $wgContLang->ucfirst( $name );
118 $name = $title->getDBkey();
123 static function getHashPathForLevel( $name, $levels ) {
124 if ( $levels == 0 ) {
127 $hash = md5( $name );
129 for ( $i = 1; $i <= $levels; $i++
) {
130 $path .= substr( $hash, 0, $i ) . '/';
137 * Get the name of this repository, as specified by $info['name]' to the constructor
144 * Get the file description page base URL, or false if there isn't one.
147 function getDescBaseUrl() {
148 if ( is_null( $this->descBaseUrl
) ) {
149 if ( !is_null( $this->articleUrl
) ) {
150 $this->descBaseUrl
= str_replace( '$1',
151 wfUrlencode( Namespace::getCanonicalName( NS_IMAGE
) ) . ':', $this->articleUrl
);
152 } elseif ( !is_null( $this->scriptDirUrl
) ) {
153 $this->descBaseUrl
= $this->scriptDirUrl
. '/index.php?title=' .
154 wfUrlencode( Namespace::getCanonicalName( NS_IMAGE
) ) . ':';
156 $this->descBaseUrl
= false;
159 return $this->descBaseUrl
;
163 * Get the URL of an image description page. May return false if it is
164 * unknown or not applicable. In general this should only be called by the
165 * File class, since it may return invalid results for certain kinds of
166 * repositories. Use File::getDescriptionUrl() in user code.
168 * In particular, it uses the article paths as specified to the repository
169 * constructor, whereas local repositories use the local Title functions.
171 function getDescriptionUrl( $name ) {
172 $base = $this->getDescBaseUrl();
174 return $base . wfUrlencode( $name );
181 * Get the URL of the content-only fragment of the description page. For
182 * MediaWiki this means action=render. This should only be called by the
183 * repository's file class, since it may return invalid results. User code
184 * should use File::getDescriptionText().
186 function getDescriptionRenderUrl( $name ) {
187 if ( isset( $this->scriptDirUrl
) ) {
188 return $this->scriptDirUrl
. '/index.php?title=' .
189 wfUrlencode( Namespace::getCanonicalName( NS_IMAGE
) . ':' . $name ) .
192 $descBase = $this->getDescBaseUrl();
194 return wfAppendQuery( $descBase . wfUrlencode( $name ), 'action=render' );
202 * Store a file to a given destination.
204 abstract function store( $srcPath, $dstZone, $dstRel, $flags = 0 );
207 * Pick a random name in the temp zone and store a file to it.
208 * Returns the URL, or a WikiError on failure.
209 * @param string $originalName The base name of the file as specified
210 * by the user. The file extension will be maintained.
211 * @param string $srcPath The current location of the file.
213 abstract function storeTemp( $originalName, $srcPath );
216 * Remove a temporary file or mark it for garbage collection
217 * @param string $virtualUrl The virtual URL returned by storeTemp
218 * @return boolean True on success, false on failure
221 function freeTemp( $virtualUrl ) {
226 * Copy or move a file either from the local filesystem or from an mwrepo://
227 * virtual URL, into this repository at the specified destination location.
229 * @param string $srcPath The source path or URL
230 * @param string $dstRel The destination relative path
231 * @param string $archiveRel The relative path where the existing file is to
232 * be archived, if there is one. Relative to the public zone root.
233 * @param integer $flags Bitfield, may be FileRepo::DELETE_SOURCE to indicate
234 * that the source file should be deleted if possible
236 abstract function publish( $srcPath, $dstRel, $archiveRel, $flags = 0 );
239 * Get properties of a file with a given virtual URL
240 * The virtual URL must refer to this repo
241 * Properties should ultimately be obtained via File::getPropsFromPath()
243 abstract function getFileProps( $virtualUrl );
246 * Call a callback function for every file in the repository
247 * May use either the database or the filesystem
250 function enumFiles( $callback ) {
251 throw new MWException( 'enumFiles is not supported by ' . get_class( $this ) );
255 * Determine if a relative path is valid, i.e. not blank or involving directory traveral
257 function validateFilename( $filename ) {
258 if ( strval( $filename ) == '' ) {
261 if ( wfIsWindows() ) {
262 $filename = strtr( $filename, '\\', '/' );
265 * Use the same traversal protection as Title::secureAndSplit()
267 if ( strpos( $filename, '.' ) !== false &&
268 ( $filename === '.' ||
$filename === '..' ||
269 strpos( $filename, './' ) === 0 ||
270 strpos( $filename, '../' ) === 0 ||
271 strpos( $filename, '/./' ) !== false ||
272 strpos( $filename, '/../' ) !== false ) )