Skip to content

Commit

Permalink
MDL-40079 repositories: Replaced const with configs
Browse files Browse the repository at this point in the history
  • Loading branch information
Rajesh Taneja committed Dec 2, 2013
1 parent f7434db commit c1b3ac4
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 21 deletions.
5 changes: 4 additions & 1 deletion admin/settings/plugins.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,10 @@

// Add common settings page
$temp = new admin_settingpage('managerepositoriescommon', new lang_string('commonrepositorysettings', 'repository'));
$temp->add(new admin_setting_configtext('repositorycacheexpire', new lang_string('cacheexpire', 'repository'), new lang_string('configcacheexpire', 'repository'), 120));
$temp->add(new admin_setting_configtext('repositorycacheexpire', new lang_string('cacheexpire', 'repository'), new lang_string('configcacheexpire', 'repository'), 120, PARAM_INT));
$temp->add(new admin_setting_configtext('repositorygetfiletimeout', new lang_string('getfiletimeout', 'repository'), new lang_string('configgetfiletimeout', 'repository'), 30, PARAM_INT));
$temp->add(new admin_setting_configtext('repositorysyncfiletimeout', new lang_string('syncfiletimeout', 'repository'), new lang_string('configsyncfiletimeout', 'repository'), 1, PARAM_INT));
$temp->add(new admin_setting_configtext('repositorysyncimagetimeout', new lang_string('syncimagetimeout', 'repository'), new lang_string('configsyncimagetimeout', 'repository'), 3, PARAM_INT));
$temp->add(new admin_setting_configcheckbox('repositoryallowexternallinks', new lang_string('allowexternallinks', 'repository'), new lang_string('configallowexternallinks', 'repository'), 1));
$temp->add(new admin_setting_configcheckbox('legacyfilesinnewcourses', new lang_string('legacyfilesinnewcourses', 'admin'), new lang_string('legacyfilesinnewcourses_help', 'admin'), 0));
$temp->add(new admin_setting_configcheckbox('legacyfilesaddallowed', new lang_string('legacyfilesaddallowed', 'admin'), new lang_string('legacyfilesaddallowed_help', 'admin'), 1));
Expand Down
6 changes: 6 additions & 0 deletions lang/en/repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@
$string['commonrepositorysettings'] = 'Common repository settings';
$string['configallowexternallinks'] = 'This option enables all users to choose whether or not external media is copied into Moodle or not. If this is off then media is always copied into Moodle (this is usually best for overall data integrity and security). If this is on then users can choose each time they add media to a text.';
$string['configcacheexpire'] = 'The amount of time that file listings are cached locally (in seconds) when browsing external repositories.';
$string['configgetfiletimeout'] = 'Timeout in seconds for downloading the external file into moodle.';
$string['configsaved'] = 'Configuration saved!';
$string['configsyncfiletimeout'] = 'Timeout in seconds for syncronising the external file size.';
$string['configsyncimagetimeout'] = 'Timeout in seconds for downloading an image file from external repository during syncronisation.';
$string['confirmdelete'] = 'Are you sure you want to delete this repository - {$a}? If you choose "Continue and download", file references to external contents will be downloaded to moodle, but it could take long time to process.';
$string['confirmdeletefile'] = 'Are you sure you want to delete this file?';
$string['confirmrenamefile'] = 'Are you sure you want to rename/move this file? There are {$a} alias/shortcut files that use this file as their source. If you proceed then those aliases will be converted to true copies.';
Expand Down Expand Up @@ -122,6 +125,7 @@
$string['foldernotfound'] = 'Folder not found';
$string['folderrecurse'] = 'Folder can not be moved to it\'s own subfolder';
$string['getfile'] = 'Select this file';
$string['getfiletimeout'] = 'Get file timeout';
$string['hidden'] = 'Hidden';
$string['help'] = 'Help';
$string['choosealink'] = 'Choose a link...';
Expand Down Expand Up @@ -210,6 +214,8 @@
$string['size'] = 'Size';
$string['submit'] = 'Submit';
$string['sync'] = 'Sync';
$string['syncfiletimeout'] = 'Sync file timeout';
$string['syncimagetimeout'] = 'Sync image timeout';
$string['thumbview'] = 'View as icons';
$string['title'] = 'Choose a file...';
$string['type'] = 'Type';
Expand Down
4 changes: 2 additions & 2 deletions repository/boxnet/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ public function sync_reference(stored_file $file) {
$url = $reference->downloadurl;
if (file_extension_in_typegroup($file->get_filename(), 'web_image')) {
$path = $this->prepare_file('');
$result = $c->download_one($url, null, array('filepath' => $path, 'timeout' => self::SYNCIMAGE_TIMEOUT));
$result = $c->download_one($url, null, array('filepath' => $path, 'timeout' => $CFG->repositorysyncimagetimeout));
$info = $c->get_info();
if ($result === true && isset($info['http_code']) && $info['http_code'] == 200) {
$fs = get_file_storage();
Expand All @@ -439,7 +439,7 @@ public function sync_reference(stored_file $file) {
return true;
}
}
$c->get($url, null, array('timeout' => self::SYNCIMAGE_TIMEOUT, 'followlocation' => true, 'nobody' => true));
$c->get($url, null, array('timeout' => $CFG->repositorysyncimagetimeout, 'followlocation' => true, 'nobody' => true));
$info = $c->get_info();
if (isset($info['http_code']) && $info['http_code'] == 200 &&
array_key_exists('download_content_length', $info) &&
Expand Down
22 changes: 13 additions & 9 deletions repository/dropbox/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,12 +269,13 @@ public function get_listing($path = '', $page = '1') {
* @param string $string
*/
public function send_thumbnail($source) {
global $CFG;
$saveas = $this->prepare_file('');
try {
$access_key = get_user_preferences($this->setting.'_access_key', '');
$access_secret = get_user_preferences($this->setting.'_access_secret', '');
$this->dropbox->set_access_token($access_key, $access_secret);
$this->dropbox->get_thumbnail($source, $saveas, self::SYNCIMAGE_TIMEOUT);
$this->dropbox->get_thumbnail($source, $saveas, $CFG->repositorysyncimagetimeout);
$content = file_get_contents($saveas);
unlink($saveas);
// set 30 days lifetime for the image. If the image is changed in dropbox it will have
Expand Down Expand Up @@ -346,10 +347,11 @@ public function get_option($config = '') {
* @param string $reference contents of DB field files_reference.reference
*/
public function fix_old_style_reference($reference) {
global $CFG;
$ref = unserialize($reference);
if (!isset($ref->url)) {
$this->dropbox->set_access_token($ref->access_key, $ref->access_secret);
$ref->url = $this->dropbox->get_file_share_link($ref->path, self::GETFILE_TIMEOUT);
$ref->url = $this->dropbox->get_file_share_link($ref->path, $CFG->repositorygetfiletimeout);
if (!$ref->url) {
// some error occurred, do not fix reference for now
return $reference;
Expand Down Expand Up @@ -419,15 +421,16 @@ private function get_file_download_link($sharedurl) {
* url: URL to the source (from parameters)
*/
public function get_file($reference, $saveas = '') {
global $CFG;
$ref = unserialize($reference);
$saveas = $this->prepare_file($saveas);
if (isset($ref->access_key) && isset($ref->access_secret) && isset($ref->path)) {
$this->dropbox->set_access_token($ref->access_key, $ref->access_secret);
return $this->dropbox->get_file($ref->path, $saveas, self::GETFILE_TIMEOUT);
return $this->dropbox->get_file($ref->path, $saveas, $CFG->repositorygetfiletimeout);
} else if (isset($ref->url)) {
$c = new curl;
$url = $this->get_file_download_link($ref->url);
$result = $c->download_one($url, null, array('filepath' => $saveas, 'timeout' => self::GETFILE_TIMEOUT, 'followlocation' => true));
$result = $c->download_one($url, null, array('filepath' => $saveas, 'timeout' => $CFG->repositorygetfiletimeout, 'followlocation' => true));
$info = $c->get_info();
if ($result !== true || !isset($info['http_code']) || $info['http_code'] != 200) {
throw new moodle_exception('errorwhiledownload', 'repository', '', $result);
Expand Down Expand Up @@ -507,10 +510,11 @@ public function supported_returntypes() {
* @return string
*/
public function get_link($reference) {
global $CFG;
$ref = unserialize($reference);
if (!isset($ref->url)) {
$this->dropbox->set_access_token($ref->access_key, $ref->access_secret);
$ref->url = $this->dropbox->get_file_share_link($ref->path, self::GETFILE_TIMEOUT);
$ref->url = $this->dropbox->get_file_share_link($ref->path, $CFG->repositorygetfiletimeout);
}
return $this->get_file_download_link($ref->url);
}
Expand All @@ -522,7 +526,7 @@ public function get_link($reference) {
* @return string file referece
*/
public function get_file_reference($source) {
global $USER;
global $USER, $CFG;
$reference = new stdClass;
$reference->path = $source;
$reference->userid = $USER->id;
Expand All @@ -536,7 +540,7 @@ public function get_file_reference($source) {
$usefilereference = optional_param('usefilereference', false, PARAM_BOOL);
if ($usefilereference) {
$this->dropbox->set_access_token($reference->access_key, $reference->access_secret);
$url = $this->dropbox->get_file_share_link($source, self::GETFILE_TIMEOUT);
$url = $this->dropbox->get_file_share_link($source, $CFG->repositorygetfiletimeout);
if ($url) {
unset($reference->access_key);
unset($reference->access_secret);
Expand Down Expand Up @@ -564,7 +568,7 @@ public function sync_reference(stored_file $file) {
if (file_extension_in_typegroup($ref->path, 'web_image')) {
$saveas = $this->prepare_file('');
try {
$result = $c->download_one($url, array(), array('filepath' => $saveas, 'timeout' => self::SYNCIMAGE_TIMEOUT, 'followlocation' => true));
$result = $c->download_one($url, array(), array('filepath' => $saveas, 'timeout' => $CFG->repositorysyncimagetimeout, 'followlocation' => true));
$info = $c->get_info();
if ($result === true && isset($info['http_code']) && $info['http_code'] == 200) {
$fs = get_file_storage();
Expand All @@ -574,7 +578,7 @@ public function sync_reference(stored_file $file) {
}
} catch (Exception $e) {}
}
$c->get($url, null, array('timeout' => self::SYNCIMAGE_TIMEOUT, 'followlocation' => true, 'nobody' => true));
$c->get($url, null, array('timeout' => $CFG->repositorysyncimagetimeout, 'followlocation' => true, 'nobody' => true));
$info = $c->get_info();
if (isset($info['http_code']) && $info['http_code'] == 200 &&
array_key_exists('download_content_length', $info) &&
Expand Down
8 changes: 4 additions & 4 deletions repository/equella/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ private function connection_result($errno = null) {
* url: URL to the source (from parameters)
*/
public function get_file($reference, $filename = '') {
global $USER;
global $USER, $CFG;
$ref = @unserialize(base64_decode($reference));
if (!isset($ref->url) || !($url = $this->appendtoken($ref->url))) {
// Occurs when the user isn't known..
Expand All @@ -179,7 +179,7 @@ public function get_file($reference, $filename = '') {
$path = $this->prepare_file($filename);
$cookiepathname = $this->prepare_file($USER->id. '_'. uniqid('', true). '.cookie');
$c = new curl(array('cookie'=>$cookiepathname));
$result = $c->download_one($url, null, array('filepath' => $path, 'followlocation' => true, 'timeout' => self::GETFILE_TIMEOUT));
$result = $c->download_one($url, null, array('filepath' => $path, 'followlocation' => true, 'timeout' => $CFG->repositorygetfiletimeout));
// Delete cookie jar.
if (file_exists($cookiepathname)) {
unlink($cookiepathname);
Expand Down Expand Up @@ -208,15 +208,15 @@ public function sync_reference(stored_file $file) {
$c = new curl(array('cookie' => $cookiepathname));
if (file_extension_in_typegroup($ref->filename, 'web_image')) {
$path = $this->prepare_file('');
$result = $c->download_one($url, null, array('filepath' => $path, 'followlocation' => true, 'timeout' => self::SYNCIMAGE_TIMEOUT));
$result = $c->download_one($url, null, array('filepath' => $path, 'followlocation' => true, 'timeout' => $CFG->repositorysyncimagetimeout));
if ($result === true) {
$fs = get_file_storage();
list($contenthash, $filesize, $newfile) = $fs->add_file_to_pool($path);
$file->set_synchronized($contenthash, $filesize);
return true;
}
} else {
$result = $c->head($url, array('followlocation' => true, 'timeout' => self::SYNCFILE_TIMEOUT));
$result = $c->head($url, array('followlocation' => true, 'timeout' => $CFG->repositorysyncfiletimeout));
}
// Delete cookie jar.
if (file_exists($cookiepathname)) {
Expand Down
12 changes: 7 additions & 5 deletions repository/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -493,13 +493,12 @@ public static function wake_from_cache($data) {
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class repository implements cacheable_object {
/** Timeout in seconds for downloading the external file into moodle */
/** Deprecated since Moodle 2.6, please use $CFG->repositorygetfiletimeout instead */
const GETFILE_TIMEOUT = 30;
/** Timeout in seconds for syncronising the external file size */
/** Deprecated since Moodle 2.6, please use $CFG->repositorysyncfiletimeout instead */
const SYNCFILE_TIMEOUT = 1;
/** Timeout in seconds for downloading an image file from external repository during syncronisation */
/** Deprecated since Moodle 2.6, please use $CFG->repositorysyncimagetimeout instead */
const SYNCIMAGE_TIMEOUT = 3;

// $disabled can be set to true to disable a plugin by force
// example: self::$disabled = true
/** @var bool force disable repository instance */
Expand Down Expand Up @@ -1724,9 +1723,12 @@ public function get_link($url) {
* url: URL to the source (from parameters)
*/
public function get_file($url, $filename = '') {
global $CFG;

$path = $this->prepare_file($filename);
$c = new curl;
$result = $c->download_one($url, null, array('filepath' => $path, 'timeout' => self::GETFILE_TIMEOUT));

$result = $c->download_one($url, null, array('filepath' => $path, 'timeout' => $CFG->repositorygetfiletimeout));
if ($result !== true) {
throw new moodle_exception('errorwhiledownload', 'repository', '', $result);
}
Expand Down
3 changes: 3 additions & 0 deletions repository/upgrade.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ http://docs.moodle.org/dev/Repository_API
reset_caches(). Instead there is one method repository::sync_reference() - this simplifies
the repositories API and reduces the number of DB queries.

* Deprecated const GETFILE_TIMEOUT, SYNCFILE_TIMEOUT and SYNCIMAGE_TIMEOUT and replaced them with
config variables repositorygetfiletimeout, repositorysyncfiletimeout and repositorysyncimagetimeout.

=== 2.5 ===

* repository::append_suffix() has been deprecated, use repository::get_unused_filename() if you need
Expand Down

0 comments on commit c1b3ac4

Please sign in to comment.