Merge branch 'wip-MDL-33550-master' of git://github.com/marinaglancy/moodle

This commit is contained in:
Dan Poltawski 2012-06-14 16:23:08 +08:00
commit 4585e6ac98
18 changed files with 366 additions and 323 deletions

View file

@ -227,17 +227,13 @@ abstract class file_info {
}
/**
* Returns the localised human-readable name of the file together with
* virtual path
* Returns the localised human-readable name of the file together with virtual path
*
* @see file_info_stored::get_readable_fullname()
* @return string
*/
public function get_readable_fullname() {
$fpath = array();
for ($parent = $this; $parent; $parent = $parent->get_parent()) {
array_unshift($fpath, $parent->get_visible_name());
}
return join('/', $fpath);
return null;
}
/**

View file

@ -113,6 +113,46 @@ class file_info_stored extends file_info {
}
}
/**
* Returns the localised human-readable name of the file together with virtual path
*
* @return string
*/
public function get_readable_fullname() {
global $CFG;
// retrieve the readable path with all parents (excluding the top most 'System')
$fpath = array();
for ($parent = $this; $parent && $parent->get_parent(); $parent = $parent->get_parent()) {
array_unshift($fpath, $parent->get_visible_name());
}
if ($this->lf->get_component() == 'user' && $this->lf->get_filearea() == 'private') {
// use the special syntax for user private files - 'USERNAME Private files: PATH'
$username = array_shift($fpath);
array_shift($fpath); // get rid of "Private Files/" in the beginning of the path
return get_string('privatefilesof', 'repository', $username). ': '. join('/', $fpath);
} else {
// for all other files (except user private files) return 'Server files: PATH'
// first, get and cache the name of the repository_local (will be used as prefix for file names):
static $replocalname = null;
if ($replocalname === null) {
require_once($CFG->dirroot . "/repository/lib.php");
$instances = repository::get_instances(array('type' => 'local'));
if (count($instances)) {
$firstinstance = reset($instances);
$replocalname = $firstinstance->get_name();
} else if (get_string_manager()->string_exists('pluginname', 'repository_local')) {
$replocalname = get_string('pluginname', 'repository_local');
} else {
$replocalname = get_string('arearoot', 'repository');
}
}
return $replocalname. ': '. join('/', $fpath);
}
}
/**
* Returns file download url
*

View file

@ -596,6 +596,9 @@ function file_get_drafarea_files($draftitemid, $filepath = '/') {
$item->datemodified = $file->get_timemodified();
$item->datecreated = $file->get_timecreated();
$item->isref = $file->is_external_file();
if ($item->isref && $file->get_status() == 666) {
$item->originalmissing = true;
}
// find the file this draft file was created from and count all references in local
// system pointing to that file
$source = unserialize($file->get_source());
@ -2310,7 +2313,7 @@ function send_stored_file($stored_file, $lifetime=86400 , $filter=0, $forcedownl
}
// handle external resource
if ($stored_file->is_external_file()) {
if ($stored_file && $stored_file->is_external_file()) {
$stored_file->send_file($lifetime, $filter, $forcedownload, $options);
die;
}

View file

@ -1033,6 +1033,7 @@ class file_storage {
$newrecord->source = empty($filerecord->source) ? null : $filerecord->source;
$newrecord->author = empty($filerecord->author) ? null : $filerecord->author;
$newrecord->license = empty($filerecord->license) ? null : $filerecord->license;
$newrecord->status = empty($filerecord->status) ? 0 : $filerecord->status;
$newrecord->sortorder = $filerecord->sortorder;
list($newrecord->contenthash, $newrecord->filesize, $newfile) = $this->add_file_to_pool($pathname);
@ -1149,6 +1150,7 @@ class file_storage {
$newrecord->source = empty($filerecord->source) ? null : $filerecord->source;
$newrecord->author = empty($filerecord->author) ? null : $filerecord->author;
$newrecord->license = empty($filerecord->license) ? null : $filerecord->license;
$newrecord->status = empty($filerecord->status) ? 0 : $filerecord->status;
$newrecord->sortorder = $filerecord->sortorder;
list($newrecord->contenthash, $newrecord->filesize, $newfile) = $this->add_string_to_pool($content);
@ -1223,6 +1225,7 @@ class file_storage {
$filerecord->source = empty($filerecord->source) ? null : $filerecord->source;
$filerecord->author = empty($filerecord->author) ? null : $filerecord->author;
$filerecord->license = empty($filerecord->license) ? null : $filerecord->license;
$filerecord->status = empty($filerecord->status) ? 0 : $filerecord->status;
$filerecord->filepath = clean_param($filerecord->filepath, PARAM_PATH);
if (strpos($filerecord->filepath, '/') !== 0 or strrpos($filerecord->filepath, '/') !== strlen($filerecord->filepath)-1) {
// Path must start and end with '/'.
@ -1652,10 +1655,22 @@ class file_storage {
* Unpack reference field
*
* @param string $str
* @param bool $cleanparams if set to true, array elements will be passed through {@link clean_param()}
* @return array
*/
public static function unpack_reference($str) {
return unserialize(base64_decode($str));
public static function unpack_reference($str, $cleanparams = false) {
$params = unserialize(base64_decode($str));
if (is_array($params) && $cleanparams) {
$params = array(
'component' => is_null($params['component']) ? '' : clean_param($params['component'], PARAM_COMPONENT),
'filearea' => is_null($params['filearea']) ? '' : clean_param($params['filearea'], PARAM_AREA),
'itemid' => is_null($params['itemid']) ? 0 : clean_param($params['itemid'], PARAM_INT),
'filename' => is_null($params['filename']) ? null : clean_param($params['filename'], PARAM_FILE),
'filepath' => is_null($params['filepath']) ? null : clean_param($params['filepath'], PARAM_PATH),
'contextid' => is_null($params['contextid']) ? null : clean_param($params['contextid'], PARAM_INT)
);
}
return $params;
}
/**

View file

@ -522,29 +522,16 @@ class stored_file {
}
/**
* Sync external files
* Synchronize file if it is a reference and needs synchronizing
*
* @return bool true if file content changed, false if not
* Updates contenthash and filesize
*/
public function sync_external_file() {
global $CFG, $DB;
if (empty($this->file_record->referencefileid)) {
return false;
}
if (empty($this->file_record->referencelastsync) or ($this->file_record->referencelastsync + $this->file_record->referencelifetime < time())) {
global $CFG;
if (!empty($this->file_record->referencefileid)) {
require_once($CFG->dirroot.'/repository/lib.php');
if (repository::sync_external_file($this)) {
$prevcontent = $this->file_record->contenthash;
$sql = "SELECT f.*, r.repositoryid, r.reference
FROM {files} f
LEFT JOIN {files_reference} r
ON f.referencefileid = r.id
WHERE f.id = ?";
$this->file_record = $DB->get_record_sql($sql, array($this->file_record->id), MUST_EXIST);
return ($prevcontent !== $this->file_record->contenthash);
}
repository::sync_external_file($this);
}
return false;
}
/**
@ -858,7 +845,45 @@ class stored_file {
* @return string
*/
public function get_reference_details() {
return $this->repository->get_reference_details($this->get_reference());
return $this->repository->get_reference_details($this->get_reference(), $this->get_status());
}
/**
* Called after reference-file has been synchronized with the repository
*
* We update contenthash, filesize and status in files table if changed
* and we always update lastsync in files_reference table
*
* @param type $contenthash
* @param type $filesize
*/
public function set_synchronized($contenthash, $filesize, $status = 0) {
global $DB;
if (!$this->is_external_file()) {
return;
}
$now = time();
$filerecord = new stdClass();
if ($this->get_contenthash() !== $contenthash) {
$filerecord->contenthash = $contenthash;
}
if ($this->get_filesize() != $filesize) {
$filerecord->filesize = $filesize;
}
if ($this->get_status() != $status) {
$filerecord->status = $status;
}
$filerecord->referencelastsync = $now; // TODO MDL-33416 remove this
if (!empty($filerecord)) {
$this->update($filerecord);
}
$DB->set_field('files_reference', 'lastsync', $now, array('id'=>$this->get_referencefileid()));
// $this->file_record->lastsync = $now; // TODO MDL-33416 uncomment or remove
}
public function set_missingsource() {
$this->set_synchronized($this->get_contenthash(), 0, 666);
}
/**

View file

@ -159,9 +159,9 @@ class filestoragelib_testcase extends advanced_testcase {
// create user
$generator = $this->getDataGenerator();
$user = $generator->create_user();
$this->setUser($user);
$usercontext = context_user::instance($user->id);
$syscontext = context_system::instance();
$USER = $DB->get_record('user', array('id'=>$user->id));
$fs = get_file_storage();

View file

@ -527,6 +527,9 @@ M.form_filemanager.init = function(Y, options) {
if (node.refcount) {
classname = classname + ' fp-hasreferences';
}
if (node.originalmissing) {
classname = classname + ' fp-originalmissing';
}
if (node.sortorder == 1) { classname = classname + ' fp-mainfile';}
return Y.Lang.trim(classname);
}

View file

@ -612,8 +612,11 @@ class phpunit_util {
get_string_manager()->reset_caches();
events_get_handlers('reset');
textlib::reset_caches();
if (class_exists('repository')) {
repository::reset_caches();
}
$GROUPLIB_CACHE = null;
//TODO: add more resets here and probably refactor them to new core function
//TODO MDL-25290: add more resets here and probably refactor them to new core function
// purge dataroot directory
self::reset_dataroot();