mirrored from git://git.moodle.org/moodle.git
/
base_converter.class.php
94 lines (74 loc) · 2.17 KB
/
base_converter.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
/**
* Base Abstract Converter
*
* @throws backup_exception|Exception|null
*/
abstract class base_converter {
protected $id;
protected $tempdir;
protected $convertdir;
// do we want absolute path instead of tempdir?
// Do we need to create a new tempdir to convert into? EG: target...
public function __construct($tempdir) {
$this->tempdir = $tempdir;
$this->convertdir = $this->tempdir.'_'.$this->get_name();
$this->id = convert_helper::generate_id($this->convertdir);
$this->init();
}
public function init() {
}
public function get_id() {
return $this->id;
}
public function get_name() {
return array_shift(explode('_', get_class($this)));
}
public function get_convertdir() {
global $CFG;
return "$CFG->dataroot/temp/backup/$this->convertdir";
}
public function get_tempdir() {
global $CFG;
return "$CFG->dataroot/temp/backup/$this->tempdir";
}
public function delete_convertdir() {
fulldelete($this->get_convertdir());
}
public function create_convertdir() {
$this->delete_convertdir();
if (!check_dir_exists($this->get_convertdir())) {
throw new backup_exception('failedtomakeconvertdir'); // @todo Define this string
}
}
public function replace_tempdir() {
fulldelete($this->get_tempdir());
if (!rename($this->get_convertdir(), $this->get_tempdir())) {
throw new backup_exception('failedmoveconvertedintoplace'); // @todo Define this string
}
}
/**
* @abstract
* @return boolean
*/
abstract public function can_convert();
// Kicks things off
public function convert() {
$e = NULL;
try {
$this->create_convertdir();
$this->execute();
$this->replace_tempdir();
} catch (Exception $e) {
}
// Do cleanup...
$this->destroy();
if ($e instanceof Exception) {
throw $e;
}
}
abstract public function execute();
public function destroy() {
$this->delete_convertdir();
}
}