From 9f4789b8427baab411d420d7600e3818ea30d2b0 Mon Sep 17 00:00:00 2001 From: Sam Hemelryk Date: Tue, 29 May 2012 16:38:47 +1200 Subject: [PATCH 1/2] MDL-33399 files: Fixed up handling of empty sort in File API methods. The following three methods of file_storage have been patched: * get_external_files * get_area_files * get_directory_files --- lib/filestorage/file_storage.php | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/lib/filestorage/file_storage.php b/lib/filestorage/file_storage.php index edac5a52517..8a72ca684dc 100644 --- a/lib/filestorage/file_storage.php +++ b/lib/filestorage/file_storage.php @@ -366,7 +366,7 @@ class file_storage { * Returns all files belonging to given repository * * @param int $repositoryid - * @param string $sort + * @param string $sort A fragment of SQL to use for sorting */ public function get_external_files($repositoryid, $sort = 'sortorder, itemid, filepath, filename') { global $DB; @@ -374,8 +374,10 @@ class file_storage { FROM {files} f LEFT JOIN {files_reference} r ON f.referencefileid = r.id - WHERE r.repositoryid = ? - ORDER BY $sort"; + WHERE r.repositoryid = ?"; + if (!empty($sort)) { + $sql .= " ORDER BY {$sort}"; + } $result = array(); $filerecords = $DB->get_records_sql($sql, array($repositoryid)); @@ -392,11 +394,11 @@ class file_storage { * @param string $component component * @param string $filearea file area * @param int $itemid item ID or all files if not specified - * @param string $sort sort fields + * @param string $sort A fragment of SQL to use for sorting * @param bool $includedirs whether or not include directories * @return array of stored_files indexed by pathanmehash */ - public function get_area_files($contextid, $component, $filearea, $itemid = false, $sort="sortorder, itemid, filepath, filename", $includedirs = true) { + public function get_area_files($contextid, $component, $filearea, $itemid = false, $sort = "sortorder, itemid, filepath, filename", $includedirs = true) { global $DB; $conditions = array('contextid'=>$contextid, 'component'=>$component, 'filearea'=>$filearea); @@ -414,8 +416,10 @@ class file_storage { WHERE f.contextid = :contextid AND f.component = :component AND f.filearea = :filearea - $itemidsql - ORDER BY $sort"; + $itemidsql"; + if (!empty($sort)) { + $sql .= " ORDER BY {$sort}"; + } $result = array(); $filerecords = $DB->get_records_sql($sql, $conditions); @@ -489,7 +493,7 @@ class file_storage { * @param int $filepath directory path * @param bool $recursive include all subdirectories * @param bool $includedirs include files and directories - * @param string $sort sort fields + * @param string $sort A fragment of SQL to use for sorting * @return array of stored_files indexed by pathanmehash */ public function get_directory_files($contextid, $component, $filearea, $itemid, $filepath, $recursive = false, $includedirs = true, $sort = "filepath, filename") { @@ -499,6 +503,8 @@ class file_storage { return array(); } + $orderby = (!empty($sort)) ? " ORDER BY {$sort}" : ''; + if ($recursive) { $dirs = $includedirs ? "" : "AND filename <> '.'"; @@ -512,7 +518,7 @@ class file_storage { AND ".$DB->sql_substr("f.filepath", 1, $length)." = :filepath AND f.id <> :dirid $dirs - ORDER BY $sort"; + $orderby"; $params = array('contextid'=>$contextid, 'component'=>$component, 'filearea'=>$filearea, 'itemid'=>$itemid, 'filepath'=>$filepath, 'dirid'=>$directory->get_id()); $files = array(); @@ -542,7 +548,7 @@ class file_storage { AND f.itemid = :itemid AND f.filename = '.' AND ".$DB->sql_substr("f.filepath", 1, $length)." = :filepath AND f.id <> :dirid - ORDER BY $sort"; + $orderby"; $reqlevel = substr_count($filepath, '/') + 1; $filerecords = $DB->get_records_sql($sql, $params); foreach ($filerecords as $filerecord) { @@ -559,7 +565,7 @@ class file_storage { ON f.referencefileid = r.id WHERE f.contextid = :contextid AND f.component = :component AND f.filearea = :filearea AND f.itemid = :itemid AND f.filepath = :filepath AND f.filename <> '.' - ORDER BY $sort"; + $orderby"; $filerecords = $DB->get_records_sql($sql, $params); foreach ($filerecords as $filerecord) { From 849b9a6af7f8e50aed6e813c51b1230120dd3032 Mon Sep 17 00:00:00 2001 From: Sam Hemelryk Date: Tue, 29 May 2012 16:44:28 +1200 Subject: [PATCH 2/2] MDL-33399 mixed: Fixed up calls to get_area_files that used empty sorts --- lib/filebrowser/file_info_stored.php | 2 +- lib/portfolio/exporter.php | 2 +- mod/scorm/datamodels/aicclib.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/filebrowser/file_info_stored.php b/lib/filebrowser/file_info_stored.php index a5a37a3d908..2f3ea57211a 100644 --- a/lib/filebrowser/file_info_stored.php +++ b/lib/filebrowser/file_info_stored.php @@ -505,7 +505,7 @@ class file_info_stored extends file_info { if ($this->is_directory()) { $filepath = $this->lf->get_filepath(); $fs = get_file_storage(); - $storedfiles = $fs->get_area_files($this->context->id, $this->get_component(), $this->lf->get_filearea(), $this->lf->get_itemid(), ""); + $storedfiles = $fs->get_area_files($this->context->id, $this->get_component(), $this->lf->get_filearea(), $this->lf->get_itemid()); foreach ($storedfiles as $file) { if (strpos($file->get_filepath(), $filepath) === 0) { $file->delete(); diff --git a/lib/portfolio/exporter.php b/lib/portfolio/exporter.php index 81727f53ede..3baf0b5bcb3 100644 --- a/lib/portfolio/exporter.php +++ b/lib/portfolio/exporter.php @@ -809,7 +809,7 @@ class portfolio_exporter { */ public function get_tempfiles($skipfile='portfolio-export.zip') { $fs = get_file_storage(); - $files = $fs->get_area_files(SYSCONTEXTID, 'portfolio', 'exporter', $this->id, '', false); + $files = $fs->get_area_files(SYSCONTEXTID, 'portfolio', 'exporter', $this->id, 'sortorder, itemid, filepath, filename', false); if (empty($files)) { return array(); } diff --git a/mod/scorm/datamodels/aicclib.php b/mod/scorm/datamodels/aicclib.php index c926c12496e..43369fe5485 100644 --- a/mod/scorm/datamodels/aicclib.php +++ b/mod/scorm/datamodels/aicclib.php @@ -121,7 +121,7 @@ function scorm_parse_aicc($scorm) { $fs = get_file_storage(); - $files = $fs->get_area_files($context->id, 'mod_scorm', 'content', 0, '', false); + $files = $fs->get_area_files($context->id, 'mod_scorm', 'content', 0, 'sortorder, itemid, filepath, filename', false); $version = 'AICC'; $ids = array();