Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 400 and 402] [Versions 400 and 403]

   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      /**
  57       * Constructor
  58       */
  59      function __construct() {
  60          $this->init();
  61      }
  62  
  63      /**
  64       * Init method, every subclass will have its own,
  65       * always calling the parent one
  66       */
  67      function init() {
  68          $this->does_generate = ACTION_NONE;
  69          $this->title     = strtolower(get_class($this));
  70          $this->str       = array();
  71          $this->output    = NULL;
  72          $this->errormsg  = NULL;
  73          $this->subaction = NULL;
  74          $this->sesskey_protected = true;
  75      }
  76  
  77      /**
  78       * Returns the type of output of the file
  79       * @return bool
  80       */
  81      function getDoesGenerate() {
  82          return $this->does_generate;
  83      }
  84  
  85      /**
  86       * getError method, returns the last error string.
  87       * Used if the invoke() methods returns false
  88       * @return string
  89       */
  90      function getError() {
  91          return $this->errormsg;
  92      }
  93  
  94      /**
  95       * getOutput method, returns the output generated by the action.
  96       * Used after execution of the invoke() methods if they return true
  97       * @return string
  98       */
  99      function getOutput() {
 100          return $this->output;
 101      }
 102  
 103      /**
 104       * getPostAction method, returns the action to launch after executing
 105       * another one
 106       * @return string
 107       */
 108      function getPostAction() {
 109          return $this->postaction;
 110      }
 111  
 112      /**
 113       * getTitle method returns the title of the action (that is part
 114       * of the $str array attribute
 115       * @return string
 116       */
 117      function getTitle() {
 118          return $this->str['title'];
 119      }
 120  
 121      /**
 122       * loadStrings method, loads the required strings specified in the
 123       * array parameter
 124       * @param string[] $strings
 125       */
 126      function loadStrings($strings) {
 127          // Load some commonly used strings
 128          if (get_string_manager()->string_exists($this->title, 'tool_xmldb')) {
 129              $this->str['title'] = get_string($this->title, 'tool_xmldb');
 130          } else {
 131              $this->str['title'] = $this->title;
 132          }
 133  
 134          // Now process the $strings array loading it in the $str atribute
 135          if ($strings) {
 136              foreach ($strings as $key => $module) {
 137                  $this->str[$key] = get_string($key, $module);
 138              }
 139          }
 140      }
 141  
 142      /**
 143       * main invoke method, it sets the postaction attribute
 144       * if possible and checks sesskey_protected if needed
 145       */
 146      function invoke() {
 147  
 148          global $SESSION;
 149  
 150          // Sesskey protection
 151          if ($this->sesskey_protected) {
 152              require_sesskey();
 153          }
 154  
 155          // If we are used any dir, save it in the lastused session object
 156          // Some actions can use it to perform positioning
 157          if ($lastused = optional_param ('dir', NULL, PARAM_PATH)) {
 158              $SESSION->lastused = $lastused;
 159          }
 160  
 161          $this->postaction = optional_param ('postaction', NULL, PARAM_ALPHAEXT);
 162          // Avoid being recursive
 163          if ($this->title == $this->postaction) {
 164              $this->postaction = NULL;
 165          }
 166      }
 167  
 168      /**
 169       * launch method, used to easily call invoke methods between actions
 170       * @param string $action
 171       * @return mixed
 172       */
 173      function launch($action) {
 174  
 175          global $CFG;
 176  
 177          // Get the action path and invoke it
 178          $actionsroot = "$CFG->dirroot/$CFG->admin/tool/xmldb/actions";
 179          $actionclass = $action . '.class.php';
 180          $actionpath = "$actionsroot/$action/$actionclass";
 181  
 182          // Load and invoke the proper action
 183          $result = false;
 184          if (file_exists($actionpath) && is_readable($actionpath)) {
 185              require_once($actionpath);
 186              if ($xmldb_action = new $action) {
 187                  $result = $xmldb_action->invoke();
 188                  if ($result) {
 189                      if ($xmldb_action->does_generate != ACTION_NONE &&
 190                          $xmldb_action->getOutput()) {
 191                          $this->does_generate = $xmldb_action->does_generate;
 192                          $this->title = $xmldb_action->title;
 193                          $this->str = $xmldb_action->str;
 194                          $this->output .= $xmldb_action->getOutput();
 195                      }
 196                  } else {
 197                      $this->errormsg = $xmldb_action->getError();
 198                  }
 199              } else {
 200                  $this->errormsg = "Error: cannot instantiate class (actions/$action/$actionclass)";
 201              }
 202          } else {
 203              $this->errormsg = "Error: wrong action specified ($action)";
 204          }
 205          return $result;
 206      }
 207  
 208      /**
 209       * This function will generate the PHP code needed to
 210       * implement the upgrade_xxxx_savepoint() php calls in
 211       * upgrade code generated from the editor. It's used by
 212       * the view_structure_php and view_table_php actions
 213       *
 214       * @param xmldb_structure structure object containing all the info
 215       * @return string PHP code to be used to mark a reached savepoint
 216       */
 217      function upgrade_savepoint_php($structure) {
 218          global $CFG;
 219  
 220          // NOTE: $CFG->admin !== 'admin' is not supported in XMLDB editor, sorry.
 221  
 222          $path = $structure->getPath();
 223          $plugintype = 'error';
 224  
 225          if ($path === 'lib/db') {
 226              $plugintype = 'lib';
 227              $pluginname = null;
 228  
 229          } else {
 230              $path = dirname($path);
 231              $pluginname = basename($path);
 232              $path = dirname($path);
 233              $plugintypes = core_component::get_plugin_types();
 234              foreach ($plugintypes as $type => $fulldir) {
 235                  if ($CFG->dirroot.'/'.$path === $fulldir) {
 236                      $plugintype = $type;
 237                      break;
 238                  }
 239              }
 240          }
 241  
 242          $result = '';
 243  
 244          switch ($plugintype ) {
 245              case 'lib': // has own savepoint function
 246                  $result = XMLDB_LINEFEED .
 247                            '        // Main savepoint reached.' . XMLDB_LINEFEED .
 248                            '        upgrade_main_savepoint(true, XXXXXXXXXX);' . XMLDB_LINEFEED;
 249                  break;
 250              case 'mod': // has own savepoint function
 251                  $result = XMLDB_LINEFEED .
 252                            '        // ' . ucfirst($pluginname) . ' savepoint reached.' . XMLDB_LINEFEED .
 253                            '        upgrade_mod_savepoint(true, XXXXXXXXXX, ' . "'$pluginname'" . ');' . XMLDB_LINEFEED;
 254                  break;
 255              case 'block': // has own savepoint function
 256                  $result = XMLDB_LINEFEED .
 257                            '        // ' . ucfirst($pluginname) . ' savepoint reached.' . XMLDB_LINEFEED .
 258                            '        upgrade_block_savepoint(true, XXXXXXXXXX, ' . "'$pluginname'" . ');' . XMLDB_LINEFEED;
 259                  break;
 260              default: // rest of plugins
 261                  $result = XMLDB_LINEFEED .
 262                            '        // ' . ucfirst($pluginname) . ' savepoint reached.' . XMLDB_LINEFEED .
 263                            '        upgrade_plugin_savepoint(true, XXXXXXXXXX, ' . "'$plugintype'" . ', ' . "'$pluginname'" . ');' . XMLDB_LINEFEED;
 264          }
 265          return $result;
 266      }
 267  }