2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 * Base for all file browsing classes.
22 * @copyright 2008 Petr Skoda (http://skodak.org)
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') ||
die();
29 * Base class for things in the tree navigated by {@link file_browser}.
32 * @copyright 2008 Petr Skoda (http://skodak.org)
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 abstract class file_info
{
37 /** @var stdClass File context */
40 /** @var file_browser File browser instance */
46 * @param file_browser $browser file_browser instance
47 * @param stdClass $context
49 public function __construct($browser, $context) {
50 $this->browser
= $browser;
51 $this->context
= $context;
55 * Returns list of standard virtual file/directory identification.
56 * The difference from stored_file parameters is that null values
57 * are allowed in all fields
59 * @return array with keys contextid, component, filearea, itemid, filepath and filename
61 public function get_params() {
62 return array('contextid' => $this->context
->id
,
71 * Returns localised visible name.
75 public abstract function get_visible_name();
78 * Whether or not this is a directory
82 public abstract function is_directory();
85 * Returns list of children.
87 * @return array of file_info instances
89 public abstract function get_children();
92 * Returns parent file_info instance
94 * @return file_info or null for root
96 public abstract function get_parent();
99 * Returns array of url encoded params.
101 * @return array with numeric keys
103 public function get_params_rawencoded() {
104 $params = $this->get_params();
106 $encoded[] = 'contextid='.$params['contextid'];
107 $encoded[] = 'component='.$params['component'];
108 $encoded[] = 'filearea='.$params['filearea'];
109 $encoded[] = 'itemid='.(is_null($params['itemid']) ?
-1 : $params['itemid']);
110 $encoded[] = 'filepath='.(is_null($params['filepath']) ?
'' : rawurlencode($params['filepath']));
111 $encoded[] = 'filename='.((is_null($params['filename']) or $params['filename'] === '.') ?
'' : rawurlencode($params['filename']));
117 * Returns file download url
119 * @param bool $forcedownload whether or not force download
120 * @param bool $https whether or not force https
123 public function get_url($forcedownload=false, $https=false) {
128 * Whether or not I can read content of this file or enter directory
132 public function is_readable() {
137 * Whether or not new files or directories can be added
141 public function is_writable() {
146 * Is this info area and is it "empty"? Are there any files in subfolders?
148 * This is used mostly in repositories to reduce the
149 * number of empty folders. This method may be very slow,
154 public function is_empty_area() {
159 * Returns file size in bytes, null for directories
161 * @return int bytes or null if not known
163 public function get_filesize() {
170 * @return string mimetype or null if not known
172 public function get_mimetype() {
177 * Returns time created unix timestamp if known
179 * @return int timestamp or null
181 public function get_timecreated() {
186 * Returns time modified unix timestamp if known
188 * @return int timestamp or null
190 public function get_timemodified() {
195 * Returns the license type of the file
196 * @return string license short name or null
198 public function get_license() {
203 * Returns the author name of the file
205 * @return string author name or null
207 public function get_author() {
212 * Returns the source of the file
214 * @return string a source url or null
216 public function get_source() {
221 * Returns the sort order of the file
225 public function get_sortorder() {
230 * Create new directory, may throw exception - make sure
233 * @param string $newdirname name of new directory
234 * @param int $userid id of author, default $USER->id
235 * @return file_info new directory
237 public function create_directory($newdirname, $userid = NULL) {
242 * Create new file from string - make sure
245 * @param string $newfilename name of new file
246 * @param string $content of file
247 * @param int $userid id of author, default $USER->id
248 * @return file_info new file
250 public function create_file_from_string($newfilename, $content, $userid = NULL) {
255 * Create new file from pathname - make sure
258 * @param string $newfilename name of new file
259 * @param string $pathname location of file
260 * @param int $userid id of author, default $USER->id
261 * @return file_info new file
263 public function create_file_from_pathname($newfilename, $pathname, $userid = NULL) {
268 * Create new file from stored file - make sure
271 * @param string $newfilename name of new file
272 * @param int|stored_file $fid id or stored_file of file
273 * @param int $userid id of author, default $USER->id
274 * @return file_info new file
276 public function create_file_from_storedfile($newfilename, $fid, $userid = NULL) {
281 * Delete file, make sure file is deletable first.
283 * @return bool success
285 public function delete() {
290 * Copy content of this file to local storage, overriding current file if needed.
292 * @param int $contextid context ID
293 * @param string $component component
294 * @param string $filearea file area
295 * @param int $itemid item ID
296 * @param string $filepath file path
297 * @param string $filename file name
298 * @return boolean success
300 public function copy_to_storage($contextid, $component, $filearea, $itemid, $filepath, $filename) {
305 * Copy content of this file to local storage, overriding current file if needed.
307 * @todo MDL-31068 implement move() rename() unzip() zip()
308 * @param string $pathname real local full file name
309 * @return boolean success
311 public function copy_to_pathname($pathname) {
316 //TODO: following methods are not implemented yet ;-)
317 //public abstract function move(location params);
318 //public abstract function rename(new name);
319 //public abstract function unzip(location params);
320 //public abstract function zip(zip file, file info);