Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
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 (uses SAX for speed and low memory footprint).
29
 * Provides logic for parsing XML data and calling appropriate callbacks.
30
 * Subclasses should define XML data sources.
31
 */
32
abstract class xml_database_importer extends database_importer {
33
    protected $current_table;
34
    protected $current_row;
35
    protected $current_field;
36
    protected $current_data;
37
    protected $current_data_is_null;
38
 
39
    /**
40
     * Creates and setups a SAX parser. Subclasses should use this method to
41
     * create the XML parser.
42
     *
43
     * @return resource XML parser resource.
44
     */
45
    protected function get_parser() {
46
        $parser = xml_parser_create();
1441 ariadna 47
        xml_set_element_handler($parser, [$this, 'tag_open'], [$this, 'tag_close']);
48
        xml_set_character_data_handler($parser, [$this, 'cdata']);
1 efrain 49
        xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, false);
50
        return $parser;
51
    }
52
 
53
    /**
54
     * Callback function. Called by the XML parser for opening tags processing.
55
     *
56
     * @param resource $parser XML parser resource.
57
     * @param string $tag name of opening tag
58
     * @param array $attributes set of opening tag XML attributes
59
     * @return void
60
     */
61
    protected function tag_open($parser, $tag, $attributes) {
62
        switch ($tag) {
63
            case 'moodle_database' :
64
                if (empty($attributes['version']) || empty($attributes['timestamp'])) {
65
                    throw new dbtransfer_exception('malformedxmlexception');
66
                }
67
                $this->begin_database_import($attributes['version'], $attributes['timestamp']);
68
                break;
69
            case 'table' :
70
                if (isset($this->current_table)) {
71
                    throw new dbtransfer_exception('malformedxmlexception');
72
                }
73
                if (empty($attributes['name']) || empty($attributes['schemaHash'])) {
74
                    throw new dbtransfer_exception('malformedxmlexception');
75
                }
76
                $this->current_table = $attributes['name'];
77
                $this->begin_table_import($this->current_table, $attributes['schemaHash']);
78
                break;
79
            case 'record' :
80
                if (isset($this->current_row) || !isset($this->current_table)) {
81
                    throw new dbtransfer_exception('malformedxmlexception');
82
                }
83
                $this->current_row = new stdClass();
84
                break;
85
            case 'field' :
86
                if (isset($this->current_field) || !isset($this->current_row)) {
87
                    throw new dbtransfer_exception('malformedxmlexception');
88
                }
89
                $this->current_field = $attributes['name'];
90
                $this->current_data = '';
91
                if (isset($attributes['value']) and $attributes['value'] === 'null') {
92
                    $this->current_data_is_null = true;
93
                } else {
94
                    $this->current_data_is_null = false;
95
                }
96
                break;
97
            default :
98
                throw new dbtransfer_exception('malformedxmlexception');
99
        }
100
    }
101
 
102
    /**
103
     * Callback function. Called by the XML parser for closing tags processing.
104
     *
105
     * @param resource $parser XML parser resource.
106
     * @param string $tag name of opening tag
107
     * @return void
108
     */
109
    protected function tag_close($parser, $tag) {
110
        switch ($tag) {
111
            case 'moodle_database' :
112
                $this->finish_database_import();
113
                break;
114
 
115
            case 'table' :
116
                $this->finish_table_import($this->current_table);
117
                $this->current_table = null;
118
                break;
119
 
120
            case 'record' :
121
                $this->import_table_data($this->current_table, $this->current_row);
122
                $this->current_row = null;
123
                break;
124
 
125
            case 'field' :
126
                $field = $this->current_field;
127
                if ($this->current_data_is_null) {
128
                    $this->current_row->$field = null;
129
                } else {
130
                    $this->current_row->$field = $this->current_data;
131
                }
132
                $this->current_field        = null;
133
                $this->current_data         = null;
134
                $this->current_data_is_null = null;
135
                break;
136
 
137
            default :
138
                throw new dbtransfer_exception('malformedxmlexception');
139
        }
140
    }
141
 
142
    /**
143
     * Callback function. Called by the XML parser for character data processing.
144
     *
145
     * @param resource $parser XML parser resource.
146
     * @param string $data character data to be processed
147
     * @return void
148
     */
149
    protected function cdata($parser, $data) {
150
        if (isset($this->current_field)) {
151
            $this->current_data .= $data;
152
        }
153
    }
154
}