MDL-40079 repositories: Replaced const with configs

This commit is contained in:
Rajesh Taneja 2013-10-11 14:34:15 +08:00
parent f7434db109
commit c1b3ac4358
7 changed files with 39 additions and 21 deletions

View File

@ -256,7 +256,10 @@ if ($hassiteconfig) {
// 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));

View File

@ -61,7 +61,10 @@ $string['close'] = 'Close';
$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.';
@ -122,6 +125,7 @@ $string['folderexists'] = 'Folder name already being used, please use another na
$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...';
@ -210,6 +214,8 @@ $string['siteinstances'] = 'Repositories instances of the site';
$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';

View File

@ -430,7 +430,7 @@ class repository_boxnet extends repository {
$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();
@ -439,7 +439,7 @@ class repository_boxnet extends repository {
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) &&

View File

@ -269,12 +269,13 @@ class repository_dropbox extends repository {
* @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
@ -346,10 +347,11 @@ class repository_dropbox extends repository {
* @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;
@ -419,15 +421,16 @@ class repository_dropbox extends repository {
* 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);
@ -507,10 +510,11 @@ class repository_dropbox extends repository {
* @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);
}
@ -522,7 +526,7 @@ class repository_dropbox extends repository {
* @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;
@ -536,7 +540,7 @@ class repository_dropbox extends repository {
$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);
@ -564,7 +568,7 @@ class repository_dropbox extends repository {
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();
@ -574,7 +578,7 @@ class repository_dropbox extends repository {
}
} 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) &&

View File

@ -170,7 +170,7 @@ class repository_equella extends repository {
* 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..
@ -179,7 +179,7 @@ class repository_equella extends repository {
$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);
@ -208,7 +208,7 @@ class repository_equella extends repository {
$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);
@ -216,7 +216,7 @@ class repository_equella extends repository {
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)) {

View File

@ -493,13 +493,12 @@ class repository_type implements cacheable_object {
* @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 */
@ -1724,9 +1723,12 @@ abstract class repository implements cacheable_object {
* 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);
}

View File

@ -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