Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

MDL-25863 backup - take rid of dupe code for filename calculations

  • Loading branch information...
commit 02cf6919512f5ffe10330b8c83b415a1d2c7ff79 1 parent cbbc272
@stronk7 stronk7 authored
View
53 backup/util/dbops/backup_plan_dbops.class.php
@@ -147,34 +147,49 @@ public static function get_mnet_localhost_wwwroot() {
* Returns the default backup filename, based in passed params.
*
* Default format is (see MDL-22145)
- * backup word - format - type - name - date - info . mbz
+ * backup word - format - type - name - date - info . mbz
* where name is variable (course shortname, section name/id, activity modulename + cmid)
- * and info can be (nu = no user info, an = anonymized)
+ * and info can be (nu = no user info, an = anonymized). The last param $useidasname,
+ * defaulting to false, allows to replace the course shortname by the course id (used
+ * by automated backups, to avoid non-ascii chars in OS filesystem)
+ *
+ * @param string $format One of backup::FORMAT_
+ * @param string $type One of backup::TYPE_
+ * @param int $courseid/$sectionid/$cmid
+ * @param bool $users Should be true is users were included in the backup
+ * @param bool $anonymised Should be true is user information was anonymized.
+ * @param bool $useidasname true to use id, false to use strings (default)
+ * @return string The filename to use
*/
- public static function get_default_backup_filename($format, $type, $id, $users, $anonymised) {
+ public static function get_default_backup_filename($format, $type, $id, $users, $anonymised, $useidasname = false) {
global $DB;
// Calculate backup word
$backupword = str_replace(' ', '_', moodle_strtolower(get_string('backupfilename')));
$backupword = trim(clean_filename($backupword), '_');
- // Calculate proper name element (based on type)
- switch ($type) {
- case backup::TYPE_1COURSE:
- $shortname = $DB->get_field('course', 'shortname', array('id' => $id));
- break;
- case backup::TYPE_1SECTION:
- if (!$shortname = $DB->get_field('course_sections', 'name', array('id' => $id))) {
- $shortname = $DB->get_field('course_sections', 'section', array('id' => $id));
- }
- break;
- case backup::TYPE_1ACTIVITY:
- $cm = get_coursemodule_from_id(null, $id);
- $shortname = $cm->modname . $id;
- break;
+ $shortname = '';
+ // Not $useidasname, lets calculate it, else $id will be used
+ if (!$useidasname) {
+ // Calculate proper name element (based on type)
+ switch ($type) {
+ case backup::TYPE_1COURSE:
+ $shortname = $DB->get_field('course', 'shortname', array('id' => $id));
+ break;
+ case backup::TYPE_1SECTION:
+ if (!$shortname = $DB->get_field('course_sections', 'name', array('id' => $id))) {
+ $shortname = $DB->get_field('course_sections', 'section', array('id' => $id));
+ }
+ break;
+ case backup::TYPE_1ACTIVITY:
+ $cm = get_coursemodule_from_id(null, $id);
+ $shortname = $cm->modname . $id;
+ break;
+ }
+ $shortname = str_replace(' ', '_', $shortname);
+ $shortname = moodle_strtolower(trim(clean_filename($shortname), '_'));
}
- $shortname = str_replace(' ', '_', $shortname);
- $shortname = moodle_strtolower(trim(clean_filename($shortname), '_'));
+
$name = empty($shortname) ? $id : $shortname;
// Calculate date
View
36 backup/util/helper/backup_cron_helper.class.php
@@ -347,7 +347,7 @@ public static function launch_automated_backup($course, $starttime, $userid) {
$dir = null;
}
if (!empty($dir) && $storage !== 0) {
- $filename = self::get_external_filename($course->id, $format, $type, $users, $anonymised);
+ $filename = backup_plan_dbops::get_default_backup_filename($format, $type, $course->id, $users, $anonymised, true);
$outcome = $file->copy_content_to($dir.'/'.$filename);
if ($outcome && $storage === 1) {
$file->delete();
@@ -367,38 +367,6 @@ public static function launch_automated_backup($course, $starttime, $userid) {
}
/**
- * Gets the filename to use for the backup when it is being moved to an
- * external location.
- *
- * Note: we use the course id in the filename rather than the course shortname
- * because it may contain UTF-8 characters that could cause problems for the
- * recieving filesystem.
- *
- * @param int $courseid
- * @param string $format One of backup::FORMAT_
- * @param string $type One of backup::TYPE_
- * @param bool $users Should be true is users were included in the backup
- * @param bool $anonymised Should be true is user information was anonymized.
- * @return string The filename to use
- */
- public static function get_external_filename($courseid, $format, $type, $users, $anonymised) {
- $backupword = str_replace(' ', '_', moodle_strtolower(get_string('backupfilename')));
- $backupword = trim(clean_filename($backupword), '_');
- // Calculate date
- $backupdateformat = str_replace(' ', '_', get_string('backupnameformat', 'langconfig'));
- $date = userdate(time(), $backupdateformat, 99, false);
- $date = moodle_strtolower(trim(clean_filename($date), '_'));
- // Calculate info
- $info = '';
- if (!$users) {
- $info = 'nu';
- } else if ($anonymised) {
- $info = 'an';
- }
- return $backupword.'-'.$format.'-'.$type.'-'.$courseid.'-'.$date.'-'.$info.'.mbz';
- }
-
- /**
* Removes deleted courses fromn the backup_courses table so that we don't
* waste time backing them up.
*
@@ -531,4 +499,4 @@ public static function remove_excess_backups($course) {
return true;
}
-}
+}
Please sign in to comment.
Something went wrong with that request. Please try again.