Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 7336701ff1
Fetching contributors…

Cannot retrieve contributors at this time

file 92 lines (72 sloc) 2.121 kb
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
<?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();
        make_upload_directory($this->get_convertdir());
    }

    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();
    }
}
Something went wrong with that request. Please try again.