4176d3a25752097ddaf65a173b9aaa64f0e9f6c6
[lhc/web/wiklou.git] / includes / filerepo / FileRepo.php
1 <?php
2
3 /**
4 * Base class for file repositories
5 * Do not instantiate, use a derived class.
6 * @ingroup FileRepo
7 */
8 abstract class FileRepo {
9 const FILES_ONLY = 1;
10 const DELETE_SOURCE = 1;
11 const FIND_PRIVATE = 1;
12 const FIND_IGNORE_REDIRECT = 2;
13 const OVERWRITE = 2;
14 const OVERWRITE_SAME = 4;
15
16 var $thumbScriptUrl, $transformVia404;
17 var $descBaseUrl, $scriptDirUrl, $articleUrl, $fetchDescription, $initialCapital;
18 var $pathDisclosureProtection = 'paranoid';
19 var $descriptionCacheExpiry, $apiThumbCacheExpiry, $hashLevels;
20
21 /**
22 * Factory functions for creating new files
23 * Override these in the base class
24 */
25 var $fileFactory = false, $oldFileFactory = false;
26 var $fileFactoryKey = false, $oldFileFactoryKey = false;
27
28 function __construct( $info ) {
29 // Required settings
30 $this->name = $info['name'];
31
32 // Optional settings
33 $this->initialCapital = true; // by default
34 foreach ( array( 'descBaseUrl', 'scriptDirUrl', 'articleUrl', 'fetchDescription',
35 'thumbScriptUrl', 'initialCapital', 'pathDisclosureProtection',
36 'descriptionCacheExpiry', 'apiThumbCacheExpiry', 'hashLevels' ) as $var )
37 {
38 if ( isset( $info[$var] ) ) {
39 $this->$var = $info[$var];
40 }
41 }
42 $this->transformVia404 = !empty( $info['transformVia404'] );
43 }
44
45 /**
46 * Determine if a string is an mwrepo:// URL
47 */
48 static function isVirtualUrl( $url ) {
49 return substr( $url, 0, 9 ) == 'mwrepo://';
50 }
51
52 /**
53 * Create a new File object from the local repository
54 * @param mixed $title Title object or string
55 * @param mixed $time Time at which the image was uploaded.
56 * If this is specified, the returned object will be an
57 * instance of the repository's old file class instead of
58 * a current file. Repositories not supporting version
59 * control should return false if this parameter is set.
60 */
61 function newFile( $title, $time = false ) {
62 if ( !($title instanceof Title) ) {
63 $title = Title::makeTitleSafe( NS_FILE, $title );
64 if ( !is_object( $title ) ) {
65 return null;
66 }
67 }
68 if ( $time ) {
69 if ( $this->oldFileFactory ) {
70 return call_user_func( $this->oldFileFactory, $title, $this, $time );
71 } else {
72 return false;
73 }
74 } else {
75 return call_user_func( $this->fileFactory, $title, $this );
76 }
77 }
78
79 /**
80 * Find an instance of the named file created at the specified time
81 * Returns false if the file does not exist. Repositories not supporting
82 * version control should return false if the time is specified.
83 *
84 * @param mixed $title Title object or string
85 * @param mixed $time 14-character timestamp, or false for the current version
86 */
87 function findFile( $title, $time = false, $flags = 0 ) {
88 if ( !($title instanceof Title) ) {
89 $title = Title::makeTitleSafe( NS_FILE, $title );
90 if ( !is_object( $title ) ) {
91 return false;
92 }
93 }
94 # First try the current version of the file to see if it precedes the timestamp
95 $img = $this->newFile( $title );
96 if ( !$img ) {
97 return false;
98 }
99 if ( $img->exists() && ( !$time || $img->getTimestamp() == $time ) ) {
100 return $img;
101 }
102 # Now try an old version of the file
103 if ( $time !== false ) {
104 $img = $this->newFile( $title, $time );
105 if ( $img && $img->exists() ) {
106 if ( !$img->isDeleted(File::DELETED_FILE) ) {
107 return $img;
108 } else if ( ($flags & FileRepo::FIND_PRIVATE) && $img->userCan(File::DELETED_FILE) ) {
109 return $img;
110 }
111 }
112 }
113
114 # Now try redirects
115 if ( $flags & FileRepo::FIND_IGNORE_REDIRECT ) {
116 return false;
117 }
118 $redir = $this->checkRedirect( $title );
119 if( $redir && $redir->getNamespace() == NS_FILE) {
120 $img = $this->newFile( $redir );
121 if( !$img ) {
122 return false;
123 }
124 if( $img->exists() ) {
125 $img->redirectedFrom( $title->getDBkey() );
126 return $img;
127 }
128 }
129 return false;
130 }
131
132 /*
133 * Find many files at once.
134 * @param array $titles, an array of titles
135 * @todo Think of a good way to optionally pass timestamps to this function.
136 */
137 function findFiles( $titles ) {
138 $result = array();
139 foreach ( $titles as $index => $title ) {
140 $file = $this->findFile( $title );
141 if ( $file )
142 $result[$file->getTitle()->getDBkey()] = $file;
143 }
144 return $result;
145 }
146
147 /**
148 * Create a new File object from the local repository
149 * @param mixed $sha1 SHA-1 key
150 * @param mixed $time Time at which the image was uploaded.
151 * If this is specified, the returned object will be an
152 * instance of the repository's old file class instead of
153 * a current file. Repositories not supporting version
154 * control should return false if this parameter is set.
155 */
156 function newFileFromKey( $sha1, $time = false ) {
157 if ( $time ) {
158 if ( $this->oldFileFactoryKey ) {
159 return call_user_func( $this->oldFileFactoryKey, $sha1, $this, $time );
160 } else {
161 return false;
162 }
163 } else {
164 return call_user_func( $this->fileFactoryKey, $sha1, $this );
165 }
166 }
167
168 /**
169 * Find an instance of the file with this key, created at the specified time
170 * Returns false if the file does not exist. Repositories not supporting
171 * version control should return false if the time is specified.
172 *
173 * @param string $sha1 string
174 * @param mixed $time 14-character timestamp, or false for the current version
175 */
176 function findFileFromKey( $sha1, $time = false, $flags = 0 ) {
177 # First try the current version of the file to see if it precedes the timestamp
178 $img = $this->newFileFromKey( $sha1 );
179 if ( !$img ) {
180 return false;
181 }
182 if ( $img->exists() && ( !$time || $img->getTimestamp() == $time ) ) {
183 return $img;
184 }
185 # Now try an old version of the file
186 if ( $time !== false ) {
187 $img = $this->newFileFromKey( $sha1, $time );
188 if ( $img->exists() ) {
189 if ( !$img->isDeleted(File::DELETED_FILE) ) {
190 return $img;
191 } else if ( ($flags & FileRepo::FIND_PRIVATE) && $img->userCan(File::DELETED_FILE) ) {
192 return $img;
193 }
194 }
195 }
196 return false;
197 }
198
199 /**
200 * Get the URL of thumb.php
201 */
202 function getThumbScriptUrl() {
203 return $this->thumbScriptUrl;
204 }
205
206 /**
207 * Returns true if the repository can transform files via a 404 handler
208 */
209 function canTransformVia404() {
210 return $this->transformVia404;
211 }
212
213 /**
214 * Get the name of an image from its title object
215 */
216 function getNameFromTitle( $title ) {
217 global $wgCapitalLinks;
218 if ( $this->initialCapital != $wgCapitalLinks ) {
219 global $wgContLang;
220 $name = $title->getUserCaseDBKey();
221 if ( $this->initialCapital ) {
222 $name = $wgContLang->ucfirst( $name );
223 }
224 } else {
225 $name = $title->getDBkey();
226 }
227 return $name;
228 }
229
230 static function getHashPathForLevel( $name, $levels ) {
231 if ( $levels == 0 ) {
232 return '';
233 } else {
234 $hash = md5( $name );
235 $path = '';
236 for ( $i = 1; $i <= $levels; $i++ ) {
237 $path .= substr( $hash, 0, $i ) . '/';
238 }
239 return $path;
240 }
241 }
242
243 /**
244 * Get a relative path including trailing slash, e.g. f/fa/
245 * If the repo is not hashed, returns an empty string
246 */
247 function getHashPath( $name ) {
248 return self::getHashPathForLevel( $name, $this->hashLevels );
249 }
250
251 /**
252 * Get the name of this repository, as specified by $info['name]' to the constructor
253 */
254 function getName() {
255 return $this->name;
256 }
257
258 /**
259 * Get the URL of an image description page. May return false if it is
260 * unknown or not applicable. In general this should only be called by the
261 * File class, since it may return invalid results for certain kinds of
262 * repositories. Use File::getDescriptionUrl() in user code.
263 *
264 * In particular, it uses the article paths as specified to the repository
265 * constructor, whereas local repositories use the local Title functions.
266 */
267 function getDescriptionUrl( $name ) {
268 $encName = wfUrlencode( $name );
269 if ( !is_null( $this->descBaseUrl ) ) {
270 # "http://example.com/wiki/Image:"
271 return $this->descBaseUrl . $encName;
272 }
273 if ( !is_null( $this->articleUrl ) ) {
274 # "http://example.com/wiki/$1"
275 #
276 # We use "Image:" as the canonical namespace for
277 # compatibility across all MediaWiki versions.
278 return str_replace( '$1',
279 "Image:$encName", $this->articleUrl );
280 }
281 if ( !is_null( $this->scriptDirUrl ) ) {
282 # "http://example.com/w"
283 #
284 # We use "Image:" as the canonical namespace for
285 # compatibility across all MediaWiki versions,
286 # and just sort of hope index.php is right. ;)
287 return $this->scriptDirUrl .
288 "/index.php?title=Image:$encName";
289 }
290 return false;
291 }
292
293 /**
294 * Get the URL of the content-only fragment of the description page. For
295 * MediaWiki this means action=render. This should only be called by the
296 * repository's file class, since it may return invalid results. User code
297 * should use File::getDescriptionText().
298 * @param string $name Name of image to fetch
299 * @param string $lang Language to fetch it in, if any.
300 */
301 function getDescriptionRenderUrl( $name, $lang = null ) {
302 $query = 'action=render';
303 if ( !is_null( $lang ) ) {
304 $query .= '&uselang=' . $lang;
305 }
306 if ( isset( $this->scriptDirUrl ) ) {
307 return $this->scriptDirUrl . '/index.php?title=' .
308 wfUrlencode( 'Image:' . $name ) .
309 "&$query";
310 } else {
311 $descUrl = $this->getDescriptionUrl( $name );
312 if ( $descUrl ) {
313 return wfAppendQuery( $descUrl, $query );
314 } else {
315 return false;
316 }
317 }
318 }
319
320 /**
321 * Store a file to a given destination.
322 *
323 * @param string $srcPath Source path or virtual URL
324 * @param string $dstZone Destination zone
325 * @param string $dstRel Destination relative path
326 * @param integer $flags Bitwise combination of the following flags:
327 * self::DELETE_SOURCE Delete the source file after upload
328 * self::OVERWRITE Overwrite an existing destination file instead of failing
329 * self::OVERWRITE_SAME Overwrite the file if the destination exists and has the
330 * same contents as the source
331 * @return FileRepoStatus
332 */
333 function store( $srcPath, $dstZone, $dstRel, $flags = 0 ) {
334 $status = $this->storeBatch( array( array( $srcPath, $dstZone, $dstRel ) ), $flags );
335 if ( $status->successCount == 0 ) {
336 $status->ok = false;
337 }
338 return $status;
339 }
340
341 /**
342 * Store a batch of files
343 *
344 * @param array $triplets (src,zone,dest) triplets as per store()
345 * @param integer $flags Flags as per store
346 */
347 abstract function storeBatch( $triplets, $flags = 0 );
348
349 /**
350 * Pick a random name in the temp zone and store a file to it.
351 * Returns a FileRepoStatus object with the URL in the value.
352 *
353 * @param string $originalName The base name of the file as specified
354 * by the user. The file extension will be maintained.
355 * @param string $srcPath The current location of the file.
356 */
357 abstract function storeTemp( $originalName, $srcPath );
358
359 /**
360 * Remove a temporary file or mark it for garbage collection
361 * @param string $virtualUrl The virtual URL returned by storeTemp
362 * @return boolean True on success, false on failure
363 * STUB
364 */
365 function freeTemp( $virtualUrl ) {
366 return true;
367 }
368
369 /**
370 * Copy or move a file either from the local filesystem or from an mwrepo://
371 * virtual URL, into this repository at the specified destination location.
372 *
373 * Returns a FileRepoStatus object. On success, the value contains "new" or
374 * "archived", to indicate whether the file was new with that name.
375 *
376 * @param string $srcPath The source path or URL
377 * @param string $dstRel The destination relative path
378 * @param string $archiveRel The relative path where the existing file is to
379 * be archived, if there is one. Relative to the public zone root.
380 * @param integer $flags Bitfield, may be FileRepo::DELETE_SOURCE to indicate
381 * that the source file should be deleted if possible
382 */
383 function publish( $srcPath, $dstRel, $archiveRel, $flags = 0 ) {
384 $status = $this->publishBatch( array( array( $srcPath, $dstRel, $archiveRel ) ), $flags );
385 if ( $status->successCount == 0 ) {
386 $status->ok = false;
387 }
388 if ( isset( $status->value[0] ) ) {
389 $status->value = $status->value[0];
390 } else {
391 $status->value = false;
392 }
393 return $status;
394 }
395
396 /**
397 * Publish a batch of files
398 * @param array $triplets (source,dest,archive) triplets as per publish()
399 * @param integer $flags Bitfield, may be FileRepo::DELETE_SOURCE to indicate
400 * that the source files should be deleted if possible
401 */
402 abstract function publishBatch( $triplets, $flags = 0 );
403
404 /**
405 * Move a group of files to the deletion archive.
406 *
407 * If no valid deletion archive is configured, this may either delete the
408 * file or throw an exception, depending on the preference of the repository.
409 *
410 * The overwrite policy is determined by the repository -- currently FSRepo
411 * assumes a naming scheme in the deleted zone based on content hash, as
412 * opposed to the public zone which is assumed to be unique.
413 *
414 * @param array $sourceDestPairs Array of source/destination pairs. Each element
415 * is a two-element array containing the source file path relative to the
416 * public root in the first element, and the archive file path relative
417 * to the deleted zone root in the second element.
418 * @return FileRepoStatus
419 */
420 abstract function deleteBatch( $sourceDestPairs );
421
422 /**
423 * Move a file to the deletion archive.
424 * If no valid deletion archive exists, this may either delete the file
425 * or throw an exception, depending on the preference of the repository
426 * @param mixed $srcRel Relative path for the file to be deleted
427 * @param mixed $archiveRel Relative path for the archive location.
428 * Relative to a private archive directory.
429 * @return WikiError object (wikitext-formatted), or true for success
430 */
431 function delete( $srcRel, $archiveRel ) {
432 return $this->deleteBatch( array( array( $srcRel, $archiveRel ) ) );
433 }
434
435 /**
436 * Get properties of a file with a given virtual URL
437 * The virtual URL must refer to this repo
438 * Properties should ultimately be obtained via File::getPropsFromPath()
439 */
440 abstract function getFileProps( $virtualUrl );
441
442 /**
443 * Call a callback function for every file in the repository
444 * May use either the database or the filesystem
445 * STUB
446 */
447 function enumFiles( $callback ) {
448 throw new MWException( 'enumFiles is not supported by ' . get_class( $this ) );
449 }
450
451 /**
452 * Determine if a relative path is valid, i.e. not blank or involving directory traveral
453 */
454 function validateFilename( $filename ) {
455 if ( strval( $filename ) == '' ) {
456 return false;
457 }
458 if ( wfIsWindows() ) {
459 $filename = strtr( $filename, '\\', '/' );
460 }
461 /**
462 * Use the same traversal protection as Title::secureAndSplit()
463 */
464 if ( strpos( $filename, '.' ) !== false &&
465 ( $filename === '.' || $filename === '..' ||
466 strpos( $filename, './' ) === 0 ||
467 strpos( $filename, '../' ) === 0 ||
468 strpos( $filename, '/./' ) !== false ||
469 strpos( $filename, '/../' ) !== false ) )
470 {
471 return false;
472 } else {
473 return true;
474 }
475 }
476
477 /**#@+
478 * Path disclosure protection functions
479 */
480 function paranoidClean( $param ) { return '[hidden]'; }
481 function passThrough( $param ) { return $param; }
482
483 /**
484 * Get a callback function to use for cleaning error message parameters
485 */
486 function getErrorCleanupFunction() {
487 switch ( $this->pathDisclosureProtection ) {
488 case 'none':
489 $callback = array( $this, 'passThrough' );
490 break;
491 default: // 'paranoid'
492 $callback = array( $this, 'paranoidClean' );
493 }
494 return $callback;
495 }
496 /**#@-*/
497
498 /**
499 * Create a new fatal error
500 */
501 function newFatal( $message /*, parameters...*/ ) {
502 $params = func_get_args();
503 array_unshift( $params, $this );
504 return call_user_func_array( array( 'FileRepoStatus', 'newFatal' ), $params );
505 }
506
507 /**
508 * Create a new good result
509 */
510 function newGood( $value = null ) {
511 return FileRepoStatus::newGood( $this, $value );
512 }
513
514 /**
515 * Delete files in the deleted directory if they are not referenced in the filearchive table
516 * STUB
517 */
518 function cleanupDeletedBatch( $storageKeys ) {}
519
520 /**
521 * Checks if there is a redirect named as $title. If there is, return the
522 * title object. If not, return false.
523 * STUB
524 *
525 * @param Title $title Title of image
526 */
527 function checkRedirect( $title ) {
528 return false;
529 }
530
531 /**
532 * Invalidates image redirect cache related to that image
533 *
534 * @param Title $title Title of image
535 */
536 function invalidateImageRedirect( $title ) {
537 global $wgMemc;
538 $memcKey = $this->getMemcKey( "image_redirect:" . md5( $title->getPrefixedDBkey() ) );
539 $wgMemc->delete( $memcKey );
540 }
541
542 function findBySha1( $hash ) {
543 return array();
544 }
545
546 /**
547 * Get the human-readable name of the repo.
548 * @return string
549 */
550 public function getDisplayName() {
551 // We don't name our own repo, return nothing
552 if ( $this->name == 'local' ) {
553 return null;
554 }
555 $repoName = wfMsg( 'shared-repo-name-' . $this->name );
556 if ( !wfEmptyMsg( 'shared-repo-name-' . $this->name, $repoName ) ) {
557 return $repoName;
558 }
559 return wfMsg( 'shared-repo' );
560 }
561
562 function getSlaveDB() {
563 return wfGetDB( DB_SLAVE );
564 }
565
566 function getMasterDB() {
567 return wfGetDB( DB_MASTER );
568 }
569
570 function getMemcKey( $key ) {
571 return wfWikiID( $this->getSlaveDB() ) . ":{$key}";
572 }
573
574 }