MDL-35668 filebrowser: perf improvements for courses
[moodle.git] / repository / local / lib.php
CommitLineData
6a391ebf 1<?php
aedb0396
DC
2// This file is part of Moodle - http://moodle.org/
3//
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.
8//
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.
13//
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/>.
6a391ebf 16
96297ca2 17/**
67233725 18 * This plugin is used to access local files
96297ca2 19 *
5bcfd504 20 * @since Moodle 2.0
e2652f1a 21 * @package repository_local
67233725 22 * @copyright 2010 Dongsheng Cai {@link http://dongsheng.org}
d078f6d3 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
96297ca2 24 */
67233725 25require_once($CFG->dirroot . '/repository/lib.php');
aedb0396 26
67233725
DC
27/**
28 * repository_local class is used to browse moodle files
29 *
5bcfd504 30 * @since Moodle 2.0
67233725 31 * @package repository_local
b8de2621 32 * @copyright 2012 Marina Glancy
67233725
DC
33 * @copyright 2009 Dongsheng Cai {@link http://dongsheng.org}
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 */
be236ef5 36class repository_local extends repository {
96297ca2 37 /**
aedb0396 38 * Get file listing
96297ca2 39 *
cd312233 40 * @param string $encodedpath
b8de2621 41 * @param string $page no paging is used in repository_local
cd312233 42 * @return mixed
96297ca2 43 */
58eb9b9f 44 public function get_listing($encodedpath = '', $page = '') {
390baf46 45 global $CFG, $USER, $OUTPUT;
cd312233 46 $ret = array();
47 $ret['dynload'] = true;
7a79030f 48 $ret['nosearch'] = false;
03896eb7 49 $ret['nologin'] = true;
b8de2621
MG
50 $ret['list'] = array();
51
52 $itemid = null;
53 $filename = null;
54 $filearea = null;
55 $filepath = null;
56 $component = null;
21942470 57
a6058fed 58 if (!empty($encodedpath)) {
56964b79 59 $params = json_decode(base64_decode($encodedpath), true);
b8de2621 60 if (is_array($params) && isset($params['contextid'])) {
aff24313
PS
61 $component = is_null($params['component']) ? NULL : clean_param($params['component'], PARAM_COMPONENT);
62 $filearea = is_null($params['filearea']) ? NULL : clean_param($params['filearea'], PARAM_AREA);
3136dc16 63 $itemid = is_null($params['itemid']) ? NULL : clean_param($params['itemid'], PARAM_INT);
0e35ba6f 64 $filepath = is_null($params['filepath']) ? NULL : clean_param($params['filepath'], PARAM_PATH);
3136dc16 65 $filename = is_null($params['filename']) ? NULL : clean_param($params['filename'], PARAM_FILE);
d197ea43 66 $context = context::instance_by_id(clean_param($params['contextid'], PARAM_INT));
a6058fed 67 }
b8de2621
MG
68 }
69 if (empty($context) && !empty($this->context)) {
43aadf04 70 $context = $this->context->get_course_context(false);
b8de2621
MG
71 }
72 if (empty($context)) {
73 $context = context_system::instance();
74 }
75
76 // prepare list of allowed extensions: $extensions is either string '*'
77 // or array of lowercase extensions, i.e. array('.gif','.jpg')
78 $extensions = optional_param_array('accepted_types', '', PARAM_RAW);
79 if (empty($extensions) || $extensions === '*' || (is_array($extensions) && in_array('*', $extensions))) {
80 $extensions = '*';
a6058fed 81 } else {
b8de2621
MG
82 if (!is_array($extensions)) {
83 $extensions = array($extensions);
be85f7ab 84 }
2f1e464a 85 $extensions = array_map('core_text::strtolower', $extensions);
a6058fed 86 }
87
b8de2621 88 // build file tree
955b6e09 89 $browser = get_file_browser();
b8de2621 90 if (!($fileinfo = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename))) {
eed6f2e7 91 // if file doesn't exist, build path nodes root of current context
eed6f2e7 92 $fileinfo = $browser->get_file_info($context, null, null, null, null, null);
e2652f1a 93 }
b8de2621
MG
94 $ret['list'] = $this->get_non_empty_children($fileinfo, $extensions);
95
e2652f1a 96 // build path navigation
b8de2621
MG
97 $path = array();
98 for ($level = $fileinfo; $level; $level = $level->get_parent()) {
99 array_unshift($path, $level);
100 }
101 array_unshift($path, null);
e2652f1a 102 $ret['path'] = array();
b8de2621
MG
103 for ($i=1; $i<count($path); $i++) {
104 if ($path[$i] == $fileinfo || !$this->can_skip($path[$i], $extensions, $path[$i-1])) {
105 $ret['path'][] = $this->get_node_path($path[$i]);
eed6f2e7 106 }
6a391ebf 107 }
cd312233 108 return $ret;
6a391ebf 109 }
dbc01944 110
aedb0396 111 /**
b8de2621 112 * Tells how the file can be picked from this repository
aedb0396 113 *
2f67a9b3 114 * @return int
aedb0396 115 */
41076c58 116 public function supported_returntypes() {
be920f9a 117 return FILE_INTERNAL | FILE_REFERENCE;
41076c58 118 }
c7e4621e 119
acb70a9b 120 /**
f392caba 121 * Does this repository used to browse moodle files?
acb70a9b 122 *
f392caba 123 * @return boolean
acb70a9b 124 */
f392caba
DC
125 public function has_moodle_files() {
126 return true;
f892da72 127 }
be920f9a 128
e2652f1a 129 /**
b8de2621
MG
130 * Returns all children elements that have one of the specified extensions
131 *
43aadf04 132 * This function may skip subfolders and recursively add their children
b8de2621 133 * {@link repository_local::can_skip()}
e2652f1a
MG
134 *
135 * @param file_info $fileinfo
b8de2621
MG
136 * @param string|array $extensions, for example '*' or array('.gif','.jpg')
137 * @return array array of file_info elements
e2652f1a 138 */
b8de2621
MG
139 private function get_non_empty_children(file_info $fileinfo, $extensions) {
140 $nonemptychildren = $fileinfo->get_non_empty_children($extensions);
141 $list = array();
142 foreach ($nonemptychildren as $child) {
143 if ($this->can_skip($child, $extensions, $fileinfo)) {
144 $list = array_merge($list, $this->get_non_empty_children($child, $extensions));
145 } else {
146 $list[] = $this->get_node($child);
147 }
e2652f1a 148 }
b8de2621 149 return $list;
e2652f1a
MG
150 }
151
152 /**
43aadf04 153 * Whether this folder may be skipped in folder hierarchy
b8de2621
MG
154 *
155 * 1. Skip the name of a single filearea in a module
156 * 2. Skip course categories for non-admins who do not have navshowmycoursecategories setting
e2652f1a
MG
157 *
158 * @param file_info $fileinfo
b8de2621
MG
159 * @param string|array $extensions, for example '*' or array('.gif','.jpg')
160 * @param file_info|int $parent specify parent here if we know it to avoid creating extra objects
161 * @return bool
e2652f1a 162 */
b8de2621
MG
163 private function can_skip(file_info $fileinfo, $extensions, $parent = -1) {
164 global $CFG;
b8de2621
MG
165 if (!$fileinfo->is_directory()) {
166 // do not skip files
167 return false;
e2652f1a 168 }
b8de2621
MG
169 if ($fileinfo instanceof file_info_context_coursecat) {
170 // This is a course category. For non-admins we do not display categories
171 return empty($CFG->navshowmycoursecategories) &&
172 !has_capability('moodle/course:update', context_system::instance());
173 } else if ($fileinfo instanceof file_info_context_course ||
174 $fileinfo instanceof file_info_context_user ||
175 $fileinfo instanceof file_info_area_course_legacy ||
176 $fileinfo instanceof file_info_context_module ||
177 $fileinfo instanceof file_info_context_system) {
178 // these instances can never be filearea inside an activity, they will never be skipped
179 return false;
180 } else {
181 $params = $fileinfo->get_params();
182 if (strlen($params['filearea']) &&
183 ($params['filepath'] === '/' || empty($params['filepath'])) &&
43aadf04
MG
184 ($params['filename'] === '.' || empty($params['filename'])) &&
185 context::instance_by_id($params['contextid'])->contextlevel == CONTEXT_MODULE) {
b8de2621
MG
186 if ($parent === -1) {
187 $parent = $fileinfo->get_parent();
188 }
189 // This is a filearea inside an activity, it can be skipped if it has no non-empty siblings
190 if ($parent && ($parent instanceof file_info_context_module)) {
43aadf04 191 if ($parent->count_non_empty_children($extensions, 2) <= 1) {
b8de2621
MG
192 return true;
193 }
194 }
195 }
196 }
197 return false;
e2652f1a
MG
198 }
199
200 /**
b8de2621 201 * Converts file_info object to element of repository return list
e2652f1a 202 *
b8de2621 203 * @param file_info $fileinfo
e2652f1a
MG
204 * @return array
205 */
b8de2621 206 private function get_node(file_info $fileinfo) {
e2652f1a 207 global $OUTPUT;
56964b79 208 $encodedpath = base64_encode(json_encode($fileinfo->get_params()));
e2652f1a 209 $node = array(
b8de2621
MG
210 'title' => $fileinfo->get_visible_name(),
211 'datemodified' => $fileinfo->get_timemodified(),
212 'datecreated' => $fileinfo->get_timecreated()
5bdf63cc 213 );
b8de2621 214 if ($fileinfo->is_directory()) {
e2652f1a 215 $node['path'] = $encodedpath;
663640f5 216 $node['thumbnail'] = $OUTPUT->image_url(file_folder_icon(90))->out(false);
e2652f1a
MG
217 $node['children'] = array();
218 } else {
b8de2621
MG
219 $node['size'] = $fileinfo->get_filesize();
220 $node['author'] = $fileinfo->get_author();
221 $node['license'] = $fileinfo->get_license();
222 $node['isref'] = $fileinfo->is_external_file();
223 if ($fileinfo->get_status() == 666) {
1778f310
MG
224 $node['originalmissing'] = true;
225 }
e2652f1a 226 $node['source'] = $encodedpath;
663640f5
DW
227 $node['thumbnail'] = $OUTPUT->image_url(file_file_icon($fileinfo, 90))->out(false);
228 $node['icon'] = $OUTPUT->image_url(file_file_icon($fileinfo, 24))->out(false);
b8de2621 229 if ($imageinfo = $fileinfo->get_imageinfo()) {
dfad252c 230 // what a beautiful picture, isn't it
b8de2621
MG
231 $fileurl = new moodle_url($fileinfo->get_url());
232 $node['realthumbnail'] = $fileurl->out(false, array('preview' => 'thumb', 'oid' => $fileinfo->get_timemodified()));
233 $node['realicon'] = $fileurl->out(false, array('preview' => 'tinyicon', 'oid' => $fileinfo->get_timemodified()));
dfad252c
DM
234 $node['image_width'] = $imageinfo['width'];
235 $node['image_height'] = $imageinfo['height'];
236 }
e2652f1a
MG
237 }
238 return $node;
239 }
240
241 /**
b8de2621 242 * Converts file_info object to element of repository return path
e2652f1a 243 *
b8de2621 244 * @param file_info $fileinfo
e2652f1a
MG
245 * @return array
246 */
b8de2621 247 private function get_node_path(file_info $fileinfo) {
56964b79 248 $encodedpath = base64_encode(json_encode($fileinfo->get_params()));
e2652f1a
MG
249 return array(
250 'path' => $encodedpath,
b8de2621 251 'name' => $fileinfo->get_visible_name()
e2652f1a
MG
252 );
253 }
31581ae6 254
7a79030f
FM
255 /**
256 * Search through all the files.
257 *
258 * This method will do a raw search through the database, then will try
259 * to match with files that a user can access. A maximum of 50 files will be
260 * returned at a time, excluding possible duplicates found along the way.
261 *
262 * Queries are done in chunk of 100 files to prevent too many records to be fetched
263 * at once. When too many files are not included, or a maximum of 10 queries are
264 * performed we consider that this was the last page.
265 *
266 * @param String $q The query string.
267 * @param integer $page The page number.
268 * @return array of results.
269 */
270 public function search($q, $page = 1) {
271 global $DB, $SESSION;
272
273 // Because the repository API is weird, the first page is 0, but it should be 1.
274 if (!$page) {
275 $page = 1;
276 }
277
278 if (!isset($SESSION->repository_local_search)) {
279 $SESSION->repository_local_search = array();
280 }
281
282 $fs = get_file_storage();
283 $fb = get_file_browser();
284
285 $max = 50;
286 $limit = 100;
287 if ($page <= 1) {
288 $SESSION->repository_local_search['query'] = $q;
289 $SESSION->repository_local_search['from'] = 0;
290 $from = 0;
291 } else {
292 // Yes, the repository does not send the query again...
293 $q = $SESSION->repository_local_search['query'];
294 $from = (int) $SESSION->repository_local_search['from'];
295 }
296
297 $count = $fs->search_server_files('%' . $DB->sql_like_escape($q) . '%', null, null, true);
298 $remaining = $count - $from;
299 $maxloops = 3000;
300 $loops = 0;
301
302 $results = array();
303 while (count($results) < $max && $maxloops > 0 && $remaining > 0) {
304 if (empty($files)) {
305 $files = $fs->search_server_files('%' . $DB->sql_like_escape($q) . '%', $from, $limit);
306 $from += $limit;
307 };
308
309 $remaining--;
310 $maxloops--;
311 $loops++;
312
313 $file = array_shift($files);
314 if (!$file) {
315 // This should not happen.
316 throw new coding_exception('Unexpected end of files list.');
317 }
318
319 $key = $file->get_contenthash() . ':' . $file->get_filename();
320 if (isset($results[$key])) {
321 // We found the file with same content and same name, let's skip it.
322 continue;
323 }
324
325 $ctx = context::instance_by_id($file->get_contextid());
326 $fileinfo = $fb->get_file_info($ctx, $file->get_component(), $file->get_filearea(), $file->get_itemid(),
327 $file->get_filepath(), $file->get_filename());
328 if ($fileinfo) {
329 $results[$key] = $this->get_node($fileinfo);
330 }
331
332 }
333
334 // Save the position for the paging to work.
335 if ($maxloops > 0 && $remaining > 0) {
336 $SESSION->repository_local_search['from'] += $loops;
337 $pages = -1;
338 } else {
339 $SESSION->repository_local_search['from'] = 0;
340 $pages = 0;
341 }
342
343 $return = array(
344 'list' => array_values($results),
345 'dynload' => true,
346 'pages' => $pages,
347 'page' => $page
348 );
349
350 return $return;
351 }
352
31581ae6
FM
353 /**
354 * Is this repository accessing private data?
355 *
356 * @return bool
357 */
358 public function contains_private_data() {
359 return false;
360 }
727dac2f 361}