weekly release 2.1.7+
[moodle.git] / lib / dtl / file_xml_database_importer.php
bloba4362f347fe676a22a2340a859636abe74f28f0a
1 <?php
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.
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/>.
19 /**
20 * XML format importer class from file storage
22 * @package core
23 * @subpackage dtl
24 * @copyright 2008 Andrei Bautu
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 defined('MOODLE_INTERNAL') || die();
30 /**
31 * XML format importer class from file storage.
33 class file_xml_database_importer extends xml_database_importer {
34 /** Path to the XML data file. */
35 protected $filepath;
37 /**
38 * Object constructor.
40 * @param string $filepath - path to the XML data file. Use 'php://input' for PHP
41 * input stream.
42 * @param moodle_database $mdb Connection to the target database
43 * @see xml_database_importer::__construct()
44 * @param boolean $check_schema - whether or not to check that XML database
45 * @see xml_database_importer::__construct()
47 public function __construct($filepath, moodle_database $mdb, $check_schema=true) {
48 $this->filepath = $filepath;
49 parent::__construct($mdb, $check_schema);
52 /**
53 * Common import method: it opens the file storage, creates the parser, feeds
54 * the XML parser with data, releases the parser and closes the file storage.
55 * @return void
57 public function import_database() {
58 $file = fopen($this->filepath, 'r');
59 $parser = $this->get_parser();
60 while ($data = fread($file, 65536)) {
61 if (!xml_parse($parser, $data, feof($file))) {
62 throw new dbtransfer_exception('malformedxmlexception');
65 xml_parser_free($parser);
66 fclose($file);