|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00017 00027 defined('MOODLE_INTERNAL') || die(); 00028 00029 require_once($CFG->dirroot . '/backup/util/includes/convert_includes.php'); 00030 00038 abstract class base_converter implements loggable { 00039 00041 protected $id; 00042 00044 protected $tempdir; 00045 00047 protected $workdir; 00048 00050 protected $logger = null; 00051 00058 public function __construct($tempdir, $logger = null) { 00059 00060 $this->tempdir = $tempdir; 00061 $this->id = convert_helper::generate_id($tempdir); 00062 $this->workdir = $tempdir . '_' . $this->get_name() . '_' . $this->id; 00063 $this->set_logger($logger); 00064 $this->log('instantiating '.$this->get_name().' converter '.$this->get_id(), backup::LOG_DEBUG); 00065 $this->log('conversion source directory', backup::LOG_DEBUG, $this->tempdir); 00066 $this->log('conversion target directory', backup::LOG_DEBUG, $this->workdir); 00067 $this->init(); 00068 } 00069 00075 public function set_logger($logger) { 00076 if (is_null($logger) or ($logger instanceof base_logger)) { 00077 $this->logger = $logger; 00078 } 00079 } 00080 00094 public function log($message, $level, $a = null, $depth = null, $display = false) { 00095 if ($this->logger instanceof base_logger) { 00096 backup_helper::log($message, $level, $a, $depth, $display, $this->logger); 00097 } 00098 } 00099 00105 public function get_id() { 00106 return $this->id; 00107 } 00108 00114 public function get_name() { 00115 return array_shift(explode('_', get_class($this))); 00116 } 00117 00121 public function convert() { 00122 00123 try { 00124 $this->log('creating the target directory', backup::LOG_DEBUG); 00125 $this->create_workdir(); 00126 00127 $this->log('executing the conversion', backup::LOG_DEBUG); 00128 $this->execute(); 00129 00130 $this->log('replacing the source directory with the converted version', backup::LOG_DEBUG); 00131 $this->replace_tempdir(); 00132 } catch (Exception $e) { 00133 } 00134 00135 // clean-up stuff if needed 00136 $this->destroy(); 00137 00138 // eventually re-throw the execution exception 00139 if (isset($e) and ($e instanceof Exception)) { 00140 throw $e; 00141 } 00142 } 00143 00147 public function get_workdir_path() { 00148 global $CFG; 00149 00150 return "$CFG->tempdir/backup/$this->workdir"; 00151 } 00152 00156 public function get_tempdir_path() { 00157 global $CFG; 00158 00159 return "$CFG->tempdir/backup/$this->tempdir"; 00160 } 00161 00163 00172 public static function is_available() { 00173 return true; 00174 } 00175 00187 public static function detect_format($tempdir) { 00188 return null; 00189 } 00190 00199 public static function description() { 00200 00201 return array( 00202 'from' => null, 00203 'to' => null, 00204 'cost' => null, 00205 ); 00206 } 00207 00209 00213 protected function init() { 00214 } 00215 00219 protected abstract function execute(); 00220 00224 protected function create_workdir() { 00225 00226 fulldelete($this->get_workdir_path()); 00227 if (!check_dir_exists($this->get_workdir_path())) { 00228 throw new convert_exception('failed_create_workdir'); 00229 } 00230 } 00231 00238 protected function replace_tempdir() { 00239 global $CFG; 00240 00241 if (empty($CFG->keeptempdirectoriesonbackup)) { 00242 fulldelete($this->get_tempdir_path()); 00243 } else { 00244 if (!rename($this->get_tempdir_path(), $this->get_tempdir_path() . '_' . $this->get_name() . '_' . $this->id . '_source')) { 00245 throw new convert_exception('failed_rename_source_tempdir'); 00246 } 00247 } 00248 00249 if (!rename($this->get_workdir_path(), $this->get_tempdir_path())) { 00250 throw new convert_exception('failed_move_converted_into_place'); 00251 } 00252 } 00253 00260 protected function destroy() { 00261 global $CFG; 00262 00263 if (empty($CFG->keeptempdirectoriesonbackup)) { 00264 fulldelete($this->get_workdir_path()); 00265 } 00266 } 00267 } 00268 00274 class convert_exception extends moodle_exception { 00275 00283 public function __construct($errorcode, $a = null, $debuginfo = null) { 00284 parent::__construct($errorcode, '', '', $a, $debuginfo); 00285 } 00286 }