b5e3787a85404ff71f92c0fb648b8847fdf1399a
[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 */
7 abstract class FileRepo {
8 const DELETE_SOURCE = 1;
9
10 var $thumbScriptUrl, $transformVia404;
11 var $descBaseUrl, $scriptDirUrl, $articleUrl, $fetchDescription, $initialCapital;
12
13 /**
14 * Factory functions for creating new files
15 * Override these in the base class
16 */
17 var $fileFactory = false, $oldFileFactory = false;
18
19 function __construct( $info ) {
20 // Required settings
21 $this->name = $info['name'];
22
23 // Optional settings
24 $this->initialCapital = true; // by default
25 foreach ( array( 'descBaseUrl', 'scriptDirUrl', 'articleUrl', 'fetchDescription',
26 'thumbScriptUrl', 'initialCapital' ) as $var )
27 {
28 if ( isset( $info[$var] ) ) {
29 $this->$var = $info[$var];
30 }
31 }
32 $this->transformVia404 = !empty( $info['transformVia404'] );
33 }
34
35 /**
36 * Determine if a string is an mwrepo:// URL
37 */
38 static function isVirtualUrl( $url ) {
39 return substr( $url, 0, 9 ) == 'mwrepo://';
40 }
41
42 /**
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.
50 */
51 function newFile( $title, $time = false ) {
52 if ( !($title instanceof Title) ) {
53 $title = Title::makeTitleSafe( NS_IMAGE, $title );
54 if ( !is_object( $title ) ) {
55 return null;
56 }
57 }
58 if ( $time ) {
59 if ( $this->oldFileFactory ) {
60 return call_user_func( $this->oldFileFactory, $title, $this, $time );
61 } else {
62 return false;
63 }
64 } else {
65 return call_user_func( $this->fileFactory, $title, $this );
66 }
67 }
68
69 /**
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.
73 *
74 * @param mixed $time 14-character timestamp, or false for the current version
75 */
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 );
79 if ( !$img ) {
80 return false;
81 }
82 if ( $img->exists() && ( !$time || $img->getTimestamp() <= $time ) ) {
83 return $img;
84 }
85 # Now try an old version of the file
86 $img = $this->newFile( $title, $time );
87 if ( $img->exists() ) {
88 return $img;
89 }
90 }
91
92 /**
93 * Get the URL of thumb.php
94 */
95 function getThumbScriptUrl() {
96 return $this->thumbScriptUrl;
97 }
98
99 /**
100 * Returns true if the repository can transform files via a 404 handler
101 */
102 function canTransformVia404() {
103 return $this->transformVia404;
104 }
105
106 /**
107 * Get the name of an image from its title object
108 */
109 function getNameFromTitle( $title ) {
110 global $wgCapitalLinks;
111 if ( $this->initialCapital != $wgCapitalLinks ) {
112 global $wgContLang;
113 $name = $title->getUserCaseDBKey();
114 if ( $this->initialCapital ) {
115 $name = $wgContLang->ucfirst( $name );
116 }
117 } else {
118 $name = $title->getDBkey();
119 }
120 return $name;
121 }
122
123 static function getHashPathForLevel( $name, $levels ) {
124 if ( $levels == 0 ) {
125 return '';
126 } else {
127 $hash = md5( $name );
128 $path = '';
129 for ( $i = 1; $i <= $levels; $i++ ) {
130 $path .= substr( $hash, 0, $i ) . '/';
131 }
132 return $path;
133 }
134 }
135
136 /**
137 * Get the name of this repository, as specified by $info['name]' to the constructor
138 */
139 function getName() {
140 return $this->name;
141 }
142
143 /**
144 * Get the file description page base URL, or false if there isn't one.
145 * @private
146 */
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 ) ) . ':';
155 } else {
156 $this->descBaseUrl = false;
157 }
158 }
159 return $this->descBaseUrl;
160 }
161
162 /**
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.
167 *
168 * In particular, it uses the article paths as specified to the repository
169 * constructor, whereas local repositories use the local Title functions.
170 */
171 function getDescriptionUrl( $name ) {
172 $base = $this->getDescBaseUrl();
173 if ( $base ) {
174 return $base . wfUrlencode( $name );
175 } else {
176 return false;
177 }
178 }
179
180 /**
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().
185 */
186 function getDescriptionRenderUrl( $name ) {
187 if ( isset( $this->scriptDirUrl ) ) {
188 return $this->scriptDirUrl . '/index.php?title=' .
189 wfUrlencode( Namespace::getCanonicalName( NS_IMAGE ) . ':' . $name ) .
190 '&action=render';
191 } else {
192 $descBase = $this->getDescBaseUrl();
193 if ( $descBase ) {
194 return wfAppendQuery( $descBase . wfUrlencode( $name ), 'action=render' );
195 } else {
196 return false;
197 }
198 }
199 }
200
201 /**
202 * Store a file to a given destination.
203 */
204 abstract function store( $srcPath, $dstZone, $dstRel, $flags = 0 );
205
206 /**
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.
212 */
213 abstract function storeTemp( $originalName, $srcPath );
214
215 /**
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
219 * STUB
220 */
221 function freeTemp( $virtualUrl ) {
222 return true;
223 }
224
225 /**
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.
228 *
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
235 */
236 abstract function publish( $srcPath, $dstRel, $archiveRel, $flags = 0 );
237
238 /**
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()
242 */
243 abstract function getFileProps( $virtualUrl );
244
245 /**
246 * Call a callback function for every file in the repository
247 * May use either the database or the filesystem
248 * STUB
249 */
250 function enumFiles( $callback ) {
251 throw new MWException( 'enumFiles is not supported by ' . get_class( $this ) );
252 }
253
254 /**
255 * Determine if a relative path is valid, i.e. not blank or involving directory traveral
256 */
257 function validateFilename( $filename ) {
258 if ( strval( $filename ) == '' ) {
259 return false;
260 }
261 if ( wfIsWindows() ) {
262 $filename = strtr( $filename, '\\', '/' );
263 }
264 /**
265 * Use the same traversal protection as Title::secureAndSplit()
266 */
267 if ( strpos( $filename, '.' ) !== false &&
268 ( $filename === '.' || $filename === '..' ||
269 strpos( $filename, './' ) === 0 ||
270 strpos( $filename, '../' ) === 0 ||
271 strpos( $filename, '/./' ) !== false ||
272 strpos( $filename, '/../' ) !== false ) )
273 {
274 return false;
275 } else {
276 return true;
277 }
278 }
279 }
280