[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/backup/util/dbops/ -> restore_controller_dbops.class.php (source)

   1  <?php
   2  
   3  // This file is part of Moodle - http://moodle.org/
   4  //
   5  // Moodle is free software: you can redistribute it and/or modify
   6  // it under the terms of the GNU General Public License as published by
   7  // the Free Software Foundation, either version 3 of the License, or
   8  // (at your option) any later version.
   9  //
  10  // Moodle is distributed in the hope that it will be useful,
  11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13  // GNU General Public License for more details.
  14  //
  15  // You should have received a copy of the GNU General Public License
  16  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  17  
  18  /**
  19   * @package    moodlecore
  20   * @subpackage backup-dbops
  21   * @copyright  2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  /**
  26   * Non instantiable helper class providing DB support to the @restore_controller
  27   *
  28   * This class contains various static methods available for all the DB operations
  29   * performed by the restore_controller class
  30   *
  31   * TODO: Finish phpdocs
  32   */
  33  abstract class restore_controller_dbops extends restore_dbops {
  34  
  35      /**
  36       * Send one restore controller to DB
  37       *
  38       * @param restore_controller $controller controller to send to DB
  39       * @param string $checksum hash of the controller to be checked
  40       * @param bool $includeobj to decide if the object itself must be updated (true) or no (false)
  41       * @param bool $cleanobj to decide if the object itself must be cleaned (true) or no (false)
  42       * @return int id of the controller record in the DB
  43       * @throws backup_controller_exception|restore_dbops_exception
  44       */
  45      public static function save_controller($controller, $checksum, $includeobj = true, $cleanobj = false) {
  46          global $DB;
  47          // Check we are going to save one backup_controller
  48          if (! $controller instanceof restore_controller) {
  49              throw new backup_controller_exception('restore_controller_expected');
  50          }
  51          // Check checksum is ok. Only if we are including object info. Sounds silly but it isn't ;-).
  52          if ($includeobj and !$controller->is_checksum_correct($checksum)) {
  53              throw new restore_dbops_exception('restore_controller_dbops_saving_checksum_mismatch');
  54          }
  55          // Cannot request to $includeobj and $cleanobj at the same time.
  56          if ($includeobj and $cleanobj) {
  57              throw new restore_dbops_exception('restore_controller_dbops_saving_cannot_include_and_delete');
  58          }
  59          // Get all the columns
  60          $rec = new stdclass();
  61          $rec->backupid     = $controller->get_restoreid();
  62          $rec->operation    = $controller->get_operation();
  63          $rec->type         = $controller->get_type();
  64          $rec->itemid       = $controller->get_courseid();
  65          $rec->format       = $controller->get_format();
  66          $rec->interactive  = $controller->get_interactive();
  67          $rec->purpose      = $controller->get_mode();
  68          $rec->userid       = $controller->get_userid();
  69          $rec->status       = $controller->get_status();
  70          $rec->execution    = $controller->get_execution();
  71          $rec->executiontime= $controller->get_executiontime();
  72          $rec->checksum     = $checksum;
  73          // Serialize information
  74          if ($includeobj) {
  75              $rec->controller = base64_encode(serialize($controller));
  76          } else if ($cleanobj) {
  77              $rec->controller = '';
  78          }
  79          // Send it to DB
  80          if ($recexists = $DB->get_record('backup_controllers', array('backupid' => $rec->backupid))) {
  81              $rec->id = $recexists->id;
  82              $rec->timemodified = time();
  83              $DB->update_record('backup_controllers', $rec);
  84          } else {
  85              $rec->timecreated = time();
  86              $rec->timemodified = 0;
  87              $rec->id = $DB->insert_record('backup_controllers', $rec);
  88          }
  89          return $rec->id;
  90      }
  91  
  92      public static function load_controller($restoreid) {
  93          global $DB;
  94          if (! $controllerrec = $DB->get_record('backup_controllers', array('backupid' => $restoreid))) {
  95              throw new backup_dbops_exception('restore_controller_dbops_nonexisting');
  96          }
  97          $controller = unserialize(base64_decode($controllerrec->controller));
  98          // Check checksum is ok. Sounds silly but it isn't ;-)
  99          if (!$controller->is_checksum_correct($controllerrec->checksum)) {
 100              throw new backup_dbops_exception('restore_controller_dbops_loading_checksum_mismatch');
 101          }
 102          return $controller;
 103      }
 104  
 105      public static function create_restore_temp_tables($restoreid) {
 106          global $CFG, $DB;
 107          $dbman = $DB->get_manager(); // We are going to use database_manager services
 108  
 109          if ($dbman->table_exists('backup_ids_temp')) { // Table exists, from restore prechecks
 110              // TODO: Improve this by inserting/selecting some record to see there is restoreid match
 111              // TODO: If not match, exception, table corresponds to another backup/restore operation
 112              return true;
 113          }
 114          backup_controller_dbops::create_backup_ids_temp_table($restoreid);
 115          backup_controller_dbops::create_backup_files_temp_table($restoreid);
 116          return false;
 117      }
 118  
 119      public static function drop_restore_temp_tables($backupid) {
 120          global $DB;
 121          $dbman = $DB->get_manager(); // We are going to use database_manager services
 122  
 123          $targettablenames = array('backup_ids_temp', 'backup_files_temp');
 124          foreach ($targettablenames as $targettablename) {
 125              $table = new xmldb_table($targettablename);
 126              $dbman->drop_table($table); // And drop it
 127          }
 128          // Invalidate the backup_ids caches.
 129          restore_dbops::reset_backup_ids_cached();
 130      }
 131  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1