[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/backup/util/helper/ -> restore_log_rule.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-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 used to restore logs, converting all the information as needed
  29   *
  30   * This class allows each restore task to specify which logs (by action) will
  31   * be handled on restore and which transformations will be performed in order
  32   * to accomodate them into their new destination
  33   *
  34   * TODO: Complete phpdocs
  35   */
  36  class restore_log_rule implements processable {
  37  
  38      protected $module;   // module of the log record
  39      protected $action;   // action of the log record
  40  
  41      protected $urlread;   // url format of the log record in backup file
  42      protected $inforead;  // info format of the log record in backup file
  43  
  44      protected $modulewrite;// module of the log record to be written (defaults to $module if not specified)
  45      protected $actionwrite;// action of the log record to be written (defaults to $action if not specified)
  46  
  47      protected $urlwrite; // url format of the log record to be written (defaults to $urlread if not specified)
  48      protected $infowrite;// info format of the log record to be written (defaults to $inforead if not specified)
  49  
  50      protected $urlreadregexp; // Regexps for extracting information from url and info
  51      protected $inforeadregexp;
  52  
  53      protected $allpairs; // to acummulate all tokens and values pairs on each log record restored
  54  
  55      protected $urltokens; // tokens present int the $urlread attribute
  56      protected $infotokens;// tokens present in the $inforead attribute
  57  
  58      protected $fixedvalues;    // Some values that will have precedence over mappings to save tons of DB mappings
  59  
  60      protected $restoreid;
  61  
  62      public function __construct($module, $action, $urlread, $inforead,
  63                                  $modulewrite = null, $actionwrite = null, $urlwrite = null, $infowrite = null) {
  64          $this->module    = $module;
  65          $this->action    = $action;
  66          $this->urlread   = $urlread;
  67          $this->inforead  = $inforead;
  68          $this->modulewrite = is_null($modulewrite) ? $module : $modulewrite;
  69          $this->actionwrite= is_null($actionwrite) ? $action : $actionwrite;
  70          $this->urlwrite = is_null($urlwrite) ? $urlread : $urlwrite;
  71          $this->infowrite= is_null($infowrite) ? $inforead : $infowrite;
  72          $this->allpairs = array();
  73          $this->urltokens = array();
  74          $this->infotokens= array();
  75          $this->urlreadregexp = null;
  76          $this->inforeadregexp = null;
  77          $this->fixedvalues = array();
  78          $this->restoreid = null;
  79  
  80          // TODO: validate module, action are valid => exception
  81  
  82          // Calculate regexps and tokens, both for urlread and inforead
  83          $this->calculate_url_regexp($this->urlread);
  84          $this->calculate_info_regexp($this->inforead);
  85      }
  86  
  87      public function set_restoreid($restoreid) {
  88          $this->restoreid = $restoreid;
  89      }
  90  
  91      public function set_fixed_values($values) {
  92          //TODO: check $values is array => exception
  93          $this->fixedvalues = $values;
  94      }
  95  
  96      public function get_key_name() {
  97          return $this->module . '-' . $this->action;
  98      }
  99  
 100      public function process($log) {
 101  
 102          // Reset the allpairs array
 103          $this->allpairs = array();
 104  
 105          $urlmatches  = array();
 106          $infomatches = array();
 107          // Apply urlreadregexp to the $log->url if necessary
 108          if ($this->urlreadregexp) {
 109              preg_match($this->urlreadregexp, $log->url, $urlmatches);
 110              if (empty($urlmatches)) {
 111                  return false;
 112              }
 113          } else {
 114              if (!is_null($this->urlread)) { // If not null, use it (null means unmodified)
 115                  $log->url = $this->urlread;
 116              }
 117          }
 118          // Apply inforeadregexp to the $log->info if necessary
 119          if ($this->inforeadregexp) {
 120              preg_match($this->inforeadregexp, $log->info, $infomatches);
 121              if (empty($infomatches)) {
 122                  return false;
 123              }
 124          } else {
 125              if (!is_null($this->inforead)) { // If not null, use it (null means unmodified)
 126                  $log->info = $this->inforead;
 127              }
 128          }
 129  
 130          // If there are $urlmatches, let's process them
 131          if (!empty($urlmatches)) {
 132              array_shift($urlmatches); // Take out first element
 133              if (count($urlmatches) !== count($this->urltokens)) { // Number of matches must be number of tokens
 134                  return false;
 135              }
 136              // Let's process all the tokens and matches, using them to parse the urlwrite
 137              $log->url = $this->parse_tokens_and_matches($this->urltokens, $urlmatches, $this->urlwrite);
 138          }
 139  
 140          // If there are $infomatches, let's process them
 141          if (!empty($infomatches)) {
 142              array_shift($infomatches); // Take out first element
 143              if (count($infomatches) !== count($this->infotokens)) { // Number of matches must be number of tokens
 144                  return false;
 145              }
 146              // Let's process all the tokens and matches, using them to parse the infowrite
 147              $log->info = $this->parse_tokens_and_matches($this->infotokens, $infomatches, $this->infowrite);
 148          }
 149  
 150          // Arrived here, if there is any pending token in $log->url or $log->info, stop
 151          if ($this->extract_tokens($log->url) || $this->extract_tokens($log->info)) {
 152              return false;
 153          }
 154  
 155          // Finally, set module and action
 156          $log->module = $this->modulewrite;
 157          $log->action = $this->actionwrite;
 158  
 159          return $log;
 160      }
 161  
 162  // Protected API starts here
 163  
 164      protected function parse_tokens_and_matches($tokens, $values, $content) {
 165  
 166          $pairs = array_combine($tokens, $values);
 167          ksort($pairs); // First literals, then mappings
 168          foreach ($pairs as $token => $value) {
 169              // If one token has already been processed, continue
 170              if (array_key_exists($token, $this->allpairs)) {
 171                  continue;
 172              }
 173  
 174              // If the pair is one literal token, just keep it unmodified
 175              if (substr($token, 0, 1) == '[') {
 176                  $this->allpairs[$token] = $value;
 177  
 178              // If the pair is one mapping token, let's process it
 179              } else if (substr($token, 0, 1) == '{') {
 180                  $ctoken = $token;
 181  
 182                  // First, resolve mappings to literals if necessary
 183                  if (substr($token, 1, 1) == '[') {
 184                      $literaltoken = trim($token, '{}');
 185                      if (array_key_exists($literaltoken, $this->allpairs)) {
 186                          $ctoken = '{' . $this->allpairs[$literaltoken] . '}';
 187                      }
 188                  }
 189  
 190                  // Look for mapping in fixedvalues before going to DB
 191                  $plaintoken = trim($ctoken, '{}');
 192                  if (array_key_exists($plaintoken, $this->fixedvalues)) {
 193                      $this->allpairs[$token] = $this->fixedvalues[$plaintoken];
 194  
 195                   // Last chance, fetch value from backup_ids_temp, via mapping
 196                  } else {
 197                      if ($mapping = restore_dbops::get_backup_ids_record($this->restoreid, $plaintoken, $value)) {
 198                          $this->allpairs[$token] = $mapping->newitemid;
 199                      }
 200                  }
 201              }
 202          }
 203  
 204          // Apply all the conversions array (allpairs) to content
 205          krsort($this->allpairs); // First mappings, then literals
 206          $content = str_replace(array_keys($this->allpairs), $this->allpairs, $content);
 207  
 208          return $content;
 209      }
 210  
 211      protected function calculate_url_regexp($urlexpression) {
 212          // Detect all the tokens in the expression
 213          if ($tokens = $this->extract_tokens($urlexpression)) {
 214              $this->urltokens = $tokens;
 215              // Now, build the regexp
 216              $this->urlreadregexp = $this->build_regexp($urlexpression, $this->urltokens);
 217          }
 218      }
 219  
 220      protected function calculate_info_regexp($infoexpression) {
 221          // Detect all the tokens in the expression
 222          if ($tokens = $this->extract_tokens($infoexpression)) {
 223              $this->infotokens = $tokens;
 224              // Now, build the regexp
 225              $this->inforeadregexp = $this->build_regexp($infoexpression, $this->infotokens);
 226          }
 227      }
 228  
 229      protected function extract_tokens($expression) {
 230          // Extract all the tokens enclosed in square and curly brackets
 231          preg_match_all('~\[[^\]]+\]|\{[^\}]+\}~', $expression, $matches);
 232          return $matches[0];
 233      }
 234  
 235      protected function build_regexp($expression, $tokens) {
 236          // Replace to temp (and preg_quote() safe) placeholders
 237          foreach ($tokens as $token) {
 238              $expression = preg_replace('~' . preg_quote($token, '~') . '~', '#@@#@@#', $expression, 1);
 239          }
 240          // quote the expression
 241          $expression = preg_quote($expression, '~');
 242          // Replace all the placeholders
 243          $expression = preg_replace('~#@@#@@#~', '(.*)', $expression);
 244          return '~' . $expression . '~';
 245      }
 246  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1