Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 401 and 402]

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * @package    tool_xmldb
  19   * @copyright  2003 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
  20   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21   */
  22  
  23  /**
  24   * Main xmldb action clasee
  25   *
  26   * Main xmldb action class. It implements all the basic
  27   * functionalities to be shared by each action.
  28   *
  29   * @package    tool_xmldb
  30   * @copyright  2003 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
  31   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32   */
  33  class XMLDBAction {
  34  
  35      /** @var bool Type of value returned by the invoke method, ACTION_GENERATE_HTML have contents to show, set by each specialized invoke*/
  36      protected $does_generate;
  37  
  38      /** @var string Title of the Action (class name, by default), set by parent init automatically*/
  39      protected $title;
  40  
  41      /** @var string Strings used by the action set by each specialized init, calling loadStrings*/
  42      protected $str;
  43  
  44      /** @var string  Output of the action, set by each specialized invoke, get with getOutput*/
  45      protected $output;
  46  
  47      /** @var string Last Error produced. Check when any invoke returns false, get with getError*/
  48      protected $errormsg;
  49  
  50      /** @var string Action to execute at the end of the invoke script*/
  51      protected $postaction;
  52  
  53      /** @var bool Actions must be protected by sesskey mechanism*/
  54      protected $sesskey_protected;
  55  
  56      /** @var mixed */
  57      protected $subaction;
  58  
  59      /** @var bool Set own core attributes. */
  60      protected $can_subaction;
  61  
  62      /**
  63       * Constructor
  64       */
  65      function __construct() {
  66          $this->init();
  67      }
  68  
  69      /**
  70       * Init method, every subclass will have its own,
  71       * always calling the parent one
  72       */
  73      function init() {
  74          $this->does_generate = ACTION_NONE;
  75          $this->title     = strtolower(get_class($this));
  76          $this->str       = array();
  77          $this->output    = NULL;
  78          $this->errormsg  = NULL;
  79          $this->subaction = NULL;
  80          $this->sesskey_protected = true;
  81      }
  82  
  83      /**
  84       * Returns the type of output of the file
  85       * @return bool
  86       */
  87      function getDoesGenerate() {
  88          return $this->does_generate;
  89      }
  90  
  91      /**
  92       * getError method, returns the last error string.
  93       * Used if the invoke() methods returns false
  94       * @return string
  95       */
  96      function getError() {
  97          return $this->errormsg;
  98      }
  99  
 100      /**
 101       * getOutput method, returns the output generated by the action.
 102       * Used after execution of the invoke() methods if they return true
 103       * @return string
 104       */
 105      function getOutput() {
 106          return $this->output;
 107      }
 108  
 109      /**
 110       * getPostAction method, returns the action to launch after executing
 111       * another one
 112       * @return string
 113       */
 114      function getPostAction() {
 115          return $this->postaction;
 116      }
 117  
 118      /**
 119       * getTitle method returns the title of the action (that is part
 120       * of the $str array attribute
 121       * @return string
 122       */
 123      function getTitle() {
 124          return $this->str['title'];
 125      }
 126  
 127      /**
 128       * loadStrings method, loads the required strings specified in the
 129       * array parameter
 130       * @param string[] $strings
 131       */
 132      function loadStrings($strings) {
 133          // Load some commonly used strings
 134          if (get_string_manager()->string_exists($this->title, 'tool_xmldb')) {
 135              $this->str['title'] = get_string($this->title, 'tool_xmldb');
 136          } else {
 137              $this->str['title'] = $this->title;
 138          }
 139  
 140          // Now process the $strings array loading it in the $str atribute
 141          if ($strings) {
 142              foreach ($strings as $key => $module) {
 143                  $this->str[$key] = get_string($key, $module);
 144              }
 145          }
 146      }
 147  
 148      /**
 149       * main invoke method, it sets the postaction attribute
 150       * if possible and checks sesskey_protected if needed
 151       */
 152      function invoke() {
 153  
 154          global $SESSION;
 155  
 156          // Sesskey protection
 157          if ($this->sesskey_protected) {
 158              require_sesskey();
 159          }
 160  
 161          // If we are used any dir, save it in the lastused session object
 162          // Some actions can use it to perform positioning
 163          if ($lastused = optional_param ('dir', NULL, PARAM_PATH)) {
 164              $SESSION->lastused = $lastused;
 165          }
 166  
 167          $this->postaction = optional_param ('postaction', NULL, PARAM_ALPHAEXT);
 168          // Avoid being recursive
 169          if ($this->title == $this->postaction) {
 170              $this->postaction = NULL;
 171          }
 172      }
 173  
 174      /**
 175       * launch method, used to easily call invoke methods between actions
 176       * @param string $action
 177       * @return mixed
 178       */
 179      function launch($action) {
 180  
 181          global $CFG;
 182  
 183          // Get the action path and invoke it
 184          $actionsroot = "$CFG->dirroot/$CFG->admin/tool/xmldb/actions";
 185          $actionclass = $action . '.class.php';
 186          $actionpath = "$actionsroot/$action/$actionclass";
 187  
 188          // Load and invoke the proper action
 189          $result = false;
 190          if (file_exists($actionpath) && is_readable($actionpath)) {
 191              require_once($actionpath);
 192              if ($xmldb_action = new $action) {
 193                  $result = $xmldb_action->invoke();
 194                  if ($result) {
 195                      if ($xmldb_action->does_generate != ACTION_NONE &&
 196                          $xmldb_action->getOutput()) {
 197                          $this->does_generate = $xmldb_action->does_generate;
 198                          $this->title = $xmldb_action->title;
 199                          $this->str = $xmldb_action->str;
 200                          $this->output .= $xmldb_action->getOutput();
 201                      }
 202                  } else {
 203                      $this->errormsg = $xmldb_action->getError();
 204                  }
 205              } else {
 206                  $this->errormsg = "Error: cannot instantiate class (actions/$action/$actionclass)";
 207              }
 208          } else {
 209              $this->errormsg = "Error: wrong action specified ($action)";
 210          }
 211          return $result;
 212      }
 213  
 214      /**
 215       * This function will generate the PHP code needed to
 216       * implement the upgrade_xxxx_savepoint() php calls in
 217       * upgrade code generated from the editor. It's used by
 218       * the view_structure_php and view_table_php actions
 219       *
 220       * @param xmldb_structure structure object containing all the info
 221       * @return string PHP code to be used to mark a reached savepoint
 222       */
 223      function upgrade_savepoint_php($structure) {
 224          global $CFG;
 225  
 226          // NOTE: $CFG->admin !== 'admin' is not supported in XMLDB editor, sorry.
 227  
 228          $path = $structure->getPath();
 229          $plugintype = 'error';
 230  
 231          if ($path === 'lib/db') {
 232              $plugintype = 'lib';
 233              $pluginname = null;
 234  
 235          } else {
 236              $path = dirname($path);
 237              $pluginname = basename($path);
 238              $path = dirname($path);
 239              $plugintypes = core_component::get_plugin_types();
 240              foreach ($plugintypes as $type => $fulldir) {
 241                  if ($CFG->dirroot.'/'.$path === $fulldir) {
 242                      $plugintype = $type;
 243                      break;
 244                  }
 245              }
 246          }
 247  
 248          $result = '';
 249  
 250          switch ($plugintype ) {
 251              case 'lib': // has own savepoint function
 252                  $result = XMLDB_LINEFEED .
 253                            '        // Main savepoint reached.' . XMLDB_LINEFEED .
 254                            '        upgrade_main_savepoint(true, XXXXXXXXXX);' . XMLDB_LINEFEED;
 255                  break;
 256              case 'mod': // has own savepoint function
 257                  $result = XMLDB_LINEFEED .
 258                            '        // ' . ucfirst($pluginname) . ' savepoint reached.' . XMLDB_LINEFEED .
 259                            '        upgrade_mod_savepoint(true, XXXXXXXXXX, ' . "'$pluginname'" . ');' . XMLDB_LINEFEED;
 260                  break;
 261              case 'block': // has own savepoint function
 262                  $result = XMLDB_LINEFEED .
 263                            '        // ' . ucfirst($pluginname) . ' savepoint reached.' . XMLDB_LINEFEED .
 264                            '        upgrade_block_savepoint(true, XXXXXXXXXX, ' . "'$pluginname'" . ');' . XMLDB_LINEFEED;
 265                  break;
 266              default: // rest of plugins
 267                  $result = XMLDB_LINEFEED .
 268                            '        // ' . ucfirst($pluginname) . ' savepoint reached.' . XMLDB_LINEFEED .
 269                            '        upgrade_plugin_savepoint(true, XXXXXXXXXX, ' . "'$plugintype'" . ', ' . "'$pluginname'" . ');' . XMLDB_LINEFEED;
 270          }
 271          return $result;
 272      }
 273  }