MDL-13766, improve repository code to store source information
authorDongsheng Cai <unoter@gmail.com>
Thu, 25 Mar 2010 07:54:19 +0000 (07:54 +0000)
committerDongsheng Cai <unoter@gmail.com>
Thu, 25 Mar 2010 07:54:19 +0000 (07:54 +0000)
14 files changed:
repository/alfresco/repository.class.php
repository/filepicker.js
repository/filepicker.php
repository/filesystem/repository.class.php
repository/flickr/repository.class.php
repository/flickr_public/repository.class.php
repository/googledocs/repository.class.php
repository/lib.php
repository/mahara/repository.class.php
repository/remotemoodle/repository.class.php
repository/repository_ajax.php
repository/s3/repository.class.php
repository/url/repository.class.php
repository/webdav/repository.class.php

index f925e06..d1758a9 100755 (executable)
@@ -199,7 +199,7 @@ class repository_alfresco extends repository {
         $fp = fopen($path, 'w');
         $c = new curl;
         $c->download(array(array('url'=>$url, 'file'=>$fp)));
-        return $path;
+        return array($path, $url);
     }
 
     public function print_search($client_id) {
index 888b03f..8569e98 100644 (file)
@@ -359,7 +359,7 @@ M.core_filepicker.init = function(Y, options) {
                 var repository_id = this.active_repo.id;
                 var title = Y.one('#newname-'+client_id).get('value');
                 var filesource = Y.one('#filesource-'+client_id).get('value');
-                var params = {'title':title, 'file':filesource, 'savepath': this.options.savepath};
+                var params = {'title':title, 'source':filesource, 'savepath': this.options.savepath};
 
                 if (this.options.env == 'editor') {
                     var linkexternal = Y.one('#linkexternal-'+client_id).get('checked');
index aecbf41..39ae149 100755 (executable)
@@ -239,10 +239,18 @@ case 'sign':
     break;
 
 case 'download':
-    $filepath = $repo->get_file($fileurl, $filename, $itemid);
-    if (!empty($filepath)) {
-        if (!is_array($filepath)) {
-            $info = repository::move_to_filepool($filepath, $filename, $itemid, $draftpath);
+    $thefile = $repo->get_file($fileurl, $filename, $itemid);
+    if (!empty($thefile)) {
+        if (!is_array($thefile)) {
+            $record = new stdclass;
+            $record->filepath = $draftpath;
+            $record->filename = $filename;
+            $record->filearea = 'user_draft';
+            $record->itemid   = $itemid;
+            $record->license  = '';
+            $record->author   = '';
+            $record->source   = $fileurl;
+            $info = repository::move_to_filepool($thefile, $record);
         }
         redirect($url, get_string('downloadsucc','repository'));
     } else {
index f5d9b96..14f865a 100644 (file)
@@ -123,7 +123,7 @@ class repository_filesystem extends repository {
         // this is a hack to prevent move_to_file deleteing files
         // in local repository
         $CFG->repository_no_delete = true;
-        return $file;
+        return array($file, '');
     }
 
     public function logout() {
index 61404b5..4df4f84 100755 (executable)
@@ -267,7 +267,7 @@ class repository_flickr extends repository {
         $c->download(array(
             array('url'=>$url, 'file'=>$fp)
         ));
-        return $path;
+        return array($path, $url);
     }
 
     /**
index 3e3f2c0..07f891f 100644 (file)
@@ -412,7 +412,7 @@ class repository_flickr_public extends repository {
             unlink($path);
             $path = $newpath;
         }
-        return $path;
+        return array($path, $url);
     }
 
     /**
index 7ebe82a..a008055 100644 (file)
@@ -120,7 +120,7 @@ class repository_googledocs extends repository {
         $gdocs = new google_docs(new google_authsub($this->subauthtoken));
         $gdocs->download_file($url, $fp);
 
-        return $path;
+        return array($path, $url);
     }
 
     public function supported_filetypes() {
index 2add7b8..a1c7489 100644 (file)
@@ -809,34 +809,29 @@ abstract class repository {
      *           id
      *           url
      */
-    public static function move_to_filepool($thefile, $name, $itemid, $filepath = '/', $filearea = 'user_draft') {
+    public static function move_to_filepool($thefile, $record) {
         global $DB, $CFG, $USER, $OUTPUT;
-        if ($filepath !== '/') {
-            $filepath = trim($filepath, '/');
-            $filepath = '/'.$filepath.'/';
+        if ($record->filepath !== '/') {
+            $record->filepath = trim($record->filepath, '/');
+            $record->filepath = '/'.$record->filepath.'/';
         }
         $context = get_context_instance(CONTEXT_USER, $USER->id);
         $now = time();
-        $entry = new object();
-        $entry->filearea  = $filearea;
-        $entry->contextid = $context->id;
-        $entry->filename  = $name;
-        $entry->filepath  = $filepath;
-        $entry->timecreated  = $now;
-        $entry->timemodified = $now;
-        $entry->userid       = $USER->id;
-        $entry->mimetype     = mimeinfo('type', $thefile);
-        if(is_numeric($itemid)) {
-            $entry->itemid = $itemid;
-        } else {
-            $entry->itemid = 0;
+
+        $record->contextid = $context->id;
+        $record->timecreated  = $now;
+        $record->timemodified = $now;
+        $record->userid       = $USER->id;
+        $record->mimetype     = mimeinfo('type', $thefile);
+        if(!is_numeric($record->itemid)) {
+            $record->itemid = 0;
         }
         $fs = get_file_storage();
         $browser = get_file_browser();
-        if ($existingfile = $fs->get_file($context->id, $filearea, $itemid, $filepath, $name)) {
+        if ($existingfile = $fs->get_file($context->id, $record->filearea, $record->itemid, $record->filepath, $record->filename)) {
             $existingfile->delete();
         }
-        if ($file = $fs->create_file_from_pathname($entry, $thefile)) {
+        if ($file = $fs->create_file_from_pathname($record, $thefile)) {
             if (empty($CFG->repository_no_delete)) {
                 $delete = unlink($thefile);
                 unset($CFG->repository_no_delete);
@@ -1176,7 +1171,7 @@ abstract class repository {
         $fp = fopen($path, 'w');
         $c = new curl;
         $c->download(array(array('url'=>$url, 'file'=>$fp)));
-        return $path;
+        return array($path, $url);
     }
 
     /**
index 078e022..cfa4229 100644 (file)
@@ -251,7 +251,7 @@ class repository_mahara extends repository {
         fwrite($fp,$content);
         fclose($fp);
 
-        return $path;
+        return array($path, '');
 
     }
 
index d1a46ea..f644dd6 100644 (file)
@@ -255,7 +255,7 @@ class repository_remotemoodle extends repository {
         fwrite($fp,$content);
         fclose($fp);
 
-        return $path;
+        return array($path, '');
 
     }
 
index a3026cf..61a7f5f 100755 (executable)
@@ -26,9 +26,9 @@
  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  */
 
-require_once('../config.php');
-require_once('../lib/filelib.php');
-require_once('lib.php');
+require_once(dirname(dirname(__FILE__)).'/config.php');
+require_once(dirname(dirname(__FILE__)).'/lib/filelib.php');
+require_once(dirname(__FILE__).'/lib.php');
 
 require_login();
 
@@ -39,14 +39,14 @@ $callback  = optional_param('callback', '', PARAM_CLEANHTML);
 $client_id = optional_param('client_id', '', PARAM_RAW);        // client ID
 $contextid = optional_param('ctx_id', SYSCONTEXTID, PARAM_INT);       // context ID
 $env       = optional_param('env', 'filepicker', PARAM_ALPHA);  // opened in editor or moodleform
-$file      = optional_param('file', '', PARAM_RAW);             // file to download
+$source    = optional_param('source', '', PARAM_RAW);           // file to download
 $itemid    = optional_param('itemid', 0, PARAM_INT);
-$title     = optional_param('title', '', PARAM_FILE);           // new file name
 $page      = optional_param('page', '', PARAM_RAW);             // page
 $maxbytes  = optional_param('maxbytes', -1, PARAM_INT);
 $req_path  = optional_param('p', '', PARAM_RAW);                // path
-$save_path = optional_param('savepath', '/', PARAM_PATH);
-$save_filearea = optional_param('filearea', 'user_draft', PARAM_TEXT);
+$saveas_filearea = optional_param('filearea', 'user_draft', PARAM_TEXT);
+$saveas_filename = optional_param('title', '', PARAM_FILE);           // new file name
+$saveas_path   = optional_param('saveaspath', '/', PARAM_PATH);
 $search_text   = optional_param('s', '', PARAM_CLEANHTML);
 $linkexternal  = optional_param('linkexternal', '', PARAM_ALPHA);
 
@@ -217,7 +217,7 @@ switch ($action) {
         try {
             // we have two special repoisitory type need to deal with
             if ($repo->options['type'] == 'local') {
-                $fileinfo = $repo->move_to_draft($file, $title, $itemid, $save_path);
+                $fileinfo = $repo->move_to_draft($source, $saveas_filename, $itemid, $saveas_path);
                 $info = array();
                 $info['client_id'] = $client_id;
                 $info['file'] = $fileinfo['title'];
@@ -242,26 +242,36 @@ switch ($action) {
 
             if ($allowexternallink and $linkexternal === 'yes' and ($repo->supported_returntypes() || FILE_EXTERNAL)) {
                 try {
-                    $link = $repo->get_link($file);
+                    $link = $repo->get_link($source);
                 } catch (repository_exception $e){
                 }
                 $info = array();
-                $info['filename'] = $title;
+                $info['filename'] = $saveas_filename;
                 $info['type'] = 'link';
                 $info['url'] = $link;
                 die(json_encode($info));
             }
 
             // get the file location
-            $filepath = $repo->get_file($file, $title, $itemid, $save_path);
-            if ($filepath === false) {
+            list($thefile, $url) = $repo->get_file($source, $saveas_filename);
+            if ($thefile === false) {
                 $err->e = get_string('cannotdownload', 'repository');
                 die(json_encode($err));
             }
-            if (($maxbytes!==-1) && (filesize($filepath) > $maxbytes)) {
+            if (($maxbytes!==-1) && (filesize($thefile) > $maxbytes)) {
                 throw new file_exception('maxbytes');
             }
-            $info = repository::move_to_filepool($filepath, $title, $itemid, $save_path, $save_filearea);
+
+            $record = new stdclass;
+            $record->filepath = $saveas_path;
+            $record->filename = $saveas_filename;
+            $record->filearea = $saveas_filearea;
+            $record->itemid   = $itemid;
+            $record->license  = '';
+            $record->author   = '';
+            $record->source   = $url;
+
+            $info = repository::move_to_filepool($thefile, $record);
             if (empty($info)) {
                 $info['e'] = get_string('error', 'moodle');
             }
index 561c824..0f3042e 100644 (file)
@@ -111,7 +111,7 @@ class repository_s3 extends repository {
         $filename = $arr[1];
         $path = $this->prepare_file($file);
         $this->s->getObject($bucket, $filename, $path);
-        return $path;
+        return array($path, '');
     }
 
     /**
index df48c50..2b335e7 100755 (executable)
@@ -53,7 +53,7 @@ class repository_url extends repository {
         $fp = fopen($path, 'w');
         $c = new curl;
         $c->download(array(array('url'=>$url, 'file'=>$fp)));
-        return $path;
+        return array($path, $url);
     }
 
     public function check_login() {
index 6f19ad9..f9ae727 100644 (file)
@@ -76,7 +76,7 @@ class repository_webdav extends repository {
         $this->wd->get($url, $buffer);
         $fp = fopen($path, 'wb');
         fwrite($fp, $buffer);
-        return $path;
+        return array($path, '');
     }
     public function global_search() {
         return false;