|
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 00025 defined('MOODLE_INTERNAL') || die(); 00026 00041 class restore_decode_content implements processable { 00042 00043 protected $tablename; // Name, without prefix, of the table we are going to retrieve contents 00044 protected $fields; // Array of fields we are going to decode in that table (usually 1) 00045 protected $mapping; // Mapping (itemname) in backup_ids used to determine target ids (defaults to $tablename) 00046 00047 protected $restoreid; // Unique id of the restore operation we are running 00048 protected $iterator; // The iterator for this content (usually one recordset) 00049 00050 public function __construct($tablename, $fields, $mapping = null) { 00051 // TODO: check table exists 00052 // TODO: check fields exist 00053 $this->tablename = $tablename; 00054 $this->fields = !is_array($fields) ? array($fields) : $fields; // Accept string/array 00055 $this->mapping = is_null($mapping) ? $tablename : $mapping; // Default to tableanme 00056 $this->restoreid = 0; 00057 } 00058 00059 public function set_restoreid($restoreid) { 00060 $this->restoreid = $restoreid; 00061 } 00062 00063 public function process($processor) { 00064 if (!$processor instanceof restore_decode_processor) { // No correct processor, throw exception 00065 throw new restore_decode_content_exception('incorrect_restore_decode_processor', get_class($processor)); 00066 } 00067 if (!$this->restoreid) { // Check restoreid is set 00068 throw new restore_decode_rule_exception('decode_content_restoreid_not_set'); 00069 } 00070 00071 // Get the iterator of contents 00072 $it = $this->get_iterator(); 00073 foreach ($it as $itrow) { // Iterate over rows 00074 $itrowarr = (array)$itrow; // Array-ize for clean access 00075 $rowchanged = false; // To track changes in the row 00076 foreach ($this->fields as $field) { // Iterate for each field 00077 $content = $this->preprocess_field($itrowarr[$field]); // Apply potential pre-transformations 00078 if ($result = $processor->decode_content($content)) { 00079 $itrowarr[$field] = $this->postprocess_field($result); // Apply potential post-transformations 00080 $rowchanged = true; 00081 } 00082 } 00083 if ($rowchanged) { // Change detected, perform update in the row 00084 $this->update_iterator_row($itrowarr); 00085 } 00086 } 00087 $it->close(); // Always close the iterator at the end 00088 } 00089 00090 // Protected API starts here 00091 00092 protected function get_iterator() { 00093 global $DB; 00094 00095 // Build the SQL dynamically here 00096 $fieldslist = 't.' . implode(', t.', $this->fields); 00097 $sql = "SELECT t.id, $fieldslist 00098 FROM {" . $this->tablename . "} t 00099 JOIN {backup_ids_temp} b ON b.newitemid = t.id 00100 WHERE b.backupid = ? 00101 AND b.itemname = ?"; 00102 $params = array($this->restoreid, $this->mapping); 00103 return ($DB->get_recordset_sql($sql, $params)); 00104 } 00105 00106 protected function update_iterator_row($row) { 00107 global $DB; 00108 $DB->update_record($this->tablename, $row); 00109 } 00110 00111 protected function preprocess_field($field) { 00112 return $field; 00113 } 00114 00115 protected function postprocess_field($field) { 00116 return $field; 00117 } 00118 } 00119 00120 /* 00121 * Exception class used by all the @restore_decode_content stuff 00122 */ 00123 class restore_decode_content_exception extends backup_exception { 00124 00125 public function __construct($errorcode, $a=NULL, $debuginfo=null) { 00126 return parent::__construct($errorcode, $a, $debuginfo); 00127 } 00128 }