Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
   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-helper
  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  defined('MOODLE_INTERNAL') || die();
  26  
  27  /**
  28   * Helper class in charge of providing the contents to be processed by restore_decode_rules
  29   *
  30   * This class is in charge of looking (in DB) for the contents needing to be
  31   * processed by the declared restore_decode_rules. Basically it iterates over
  32   * one recordset (optimised by joining them with backup_ids records), retrieving
  33   * them from DB, delegating process to the restore_plan and storing results back
  34   * to DB.
  35   *
  36   * Implements one visitor-like pattern so the decode_processor will visit it
  37   * to get all the contents processed by its defined rules
  38   *
  39   * TODO: Complete phpdocs
  40   */
  41  class restore_decode_content implements processable {
  42  
  43      protected $tablename; // Name, without prefix, of the table we are going to retrieve contents
  44      protected $fields;    // Array of fields we are going to decode in that table (usually 1)
  45      protected $mapping;   // Mapping (itemname) in backup_ids used to determine target ids (defaults to $tablename)
  46  
  47      protected $restoreid; // Unique id of the restore operation we are running
  48      protected $iterator;  // The iterator for this content (usually one recordset)
  49  
  50      public function __construct($tablename, $fields, $mapping = null) {
  51          // TODO: check table exists
  52          // TODO: check fields exist
  53          $this->tablename = $tablename;
  54          $this->fields    = !is_array($fields) ? array($fields) : $fields; // Accept string/array
  55          $this->mapping   = is_null($mapping) ? $tablename : $mapping; // Default to tableanme
  56          $this->restoreid = 0;
  57      }
  58  
  59      public function set_restoreid($restoreid) {
  60          $this->restoreid = $restoreid;
  61      }
  62  
  63      public function process($processor) {
  64          if (!$processor instanceof restore_decode_processor) { // No correct processor, throw exception
  65              throw new restore_decode_content_exception('incorrect_restore_decode_processor', get_class($processor));
  66          }
  67          if (!$this->restoreid) { // Check restoreid is set
  68              throw new restore_decode_rule_exception('decode_content_restoreid_not_set');
  69          }
  70  
  71          // Get the iterator of contents
  72          $it = $this->get_iterator();
  73          foreach ($it as $itrow) {               // Iterate over rows
  74              $itrowarr   = (array)$itrow;        // Array-ize for clean access
  75              $rowchanged = false;                // To track changes in the row
  76              foreach ($this->fields as $field) { // Iterate for each field
  77                  $content = $this->preprocess_field($itrowarr[$field]);     // Apply potential pre-transformations
  78                  if ($result = $processor->decode_content($content)) {
  79                      $itrowarr[$field] = $this->postprocess_field($result); // Apply potential post-transformations
  80                      $rowchanged = true;
  81                  }
  82              }
  83              if ($rowchanged) { // Change detected, perform update in the row
  84                  $this->update_iterator_row($itrowarr);
  85              }
  86          }
  87          $it->close(); // Always close the iterator at the end
  88      }
  89  
  90  // Protected API starts here
  91  
  92      protected function get_iterator() {
  93          global $DB;
  94  
  95          // Build the SQL dynamically here
  96          $fieldslist = 't.' . implode(', t.', $this->fields);
  97          $sql = "SELECT t.id, $fieldslist
  98                    FROM {" . $this->tablename . "} t
  99                    JOIN {backup_ids_temp} b ON b.newitemid = t.id
 100                   WHERE b.backupid = ?
 101                     AND b.itemname = ?";
 102          $params = array($this->restoreid, $this->mapping);
 103          return ($DB->get_recordset_sql($sql, $params));
 104      }
 105  
 106      protected function update_iterator_row($row) {
 107          global $DB;
 108          $DB->update_record($this->tablename, $row);
 109      }
 110  
 111      protected function preprocess_field($field) {
 112          return $field;
 113      }
 114  
 115      protected function postprocess_field($field) {
 116          return $field;
 117      }
 118  }
 119  
 120  /*
 121   * Exception class used by all the @restore_decode_content stuff
 122   */
 123  class restore_decode_content_exception extends backup_exception {
 124  
 125      public function __construct($errorcode, $a=NULL, $debuginfo=null) {
 126          return parent::__construct($errorcode, $a, $debuginfo);
 127      }
 128  }