Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.
   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   * XML format importer class from file storage
  19   *
  20   * @package    core_dtl
  21   * @copyright  2008 Andrei Bautu
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  /**
  28   * XML format importer class from file storage.
  29   */
  30  class file_xml_database_importer extends xml_database_importer {
  31      /** @var string Path to the XML data file. */
  32      protected $filepath;
  33  
  34      /**
  35       * Object constructor.
  36       *
  37       * @param string $filepath - path to the XML data file. Use 'php://input' for PHP
  38       * input stream.
  39       * @param moodle_database $mdb Connection to the target database
  40       * @see xml_database_importer::__construct()
  41       * @param boolean $check_schema - whether or not to check that XML database
  42       * @see xml_database_importer::__construct()
  43       */
  44      public function __construct($filepath, moodle_database $mdb, $check_schema=true) {
  45          $this->filepath = $filepath;
  46          parent::__construct($mdb, $check_schema);
  47      }
  48  
  49      /**
  50       * Common import method: it opens the file storage, creates the parser, feeds
  51       * the XML parser with data, releases the parser and closes the file storage.
  52       * @return void
  53       */
  54      public function import_database() {
  55          $file = fopen($this->filepath, 'r');
  56          $parser = $this->get_parser();
  57          while ($data = fread($file, 65536)) {
  58              if (!xml_parse($parser, $data, feof($file))) {
  59                  throw new dbtransfer_exception('malformedxmlexception');
  60              }
  61          }
  62          xml_parser_free($parser);
  63          fclose($file);
  64      }
  65  }