2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * XML format exporter class
21 * @copyright 2008 Andrei Bautu
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') ||
die();
28 * XML format exporter class.
29 * Provides logic for writing XML tags and data inside appropriate callbacks.
30 * Subclasses should define XML data sinks.
32 abstract class xml_database_exporter
extends database_exporter
{
34 * Generic output method. Subclasses should implement it with code specific
35 * to the target XML sink.
37 protected abstract function output($text);
40 * Callback function. Outputs open XML PI and moodle_database opening tag.
42 * @param float $version the version of the system which generating the data
43 * @param string $release moodle release info
44 * @param string $timestamp the timestamp of the data (in ISO 8601) format.
45 * @param string $description a user description of the data.
48 public function begin_database_export($version, $release, $timestamp, $description) {
49 $this->output('<?xml version="1.0" encoding="utf-8"?>');
50 //TODO add xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" and schema information
51 $this->output('<moodle_database version="'.$version.'" release="'.$release.'" timestamp="'.$timestamp.'"'.(empty ($description) ?
'' : ' comment="'.htmlspecialchars($description, ENT_QUOTES
, 'UTF-8').'"').'>');
55 * Callback function. Outputs table opening tag.
57 * @param xmldb_table $table - XMLDB object for the exported table
60 public function begin_table_export(xmldb_table
$table) {
61 $this->output('<table name="'.$table->getName().'" schemaHash="'.$table->getHash().'">');
65 * Callback function. Outputs table closing tag.
67 * @param xmldb_table $table - XMLDB object for the exported table
69 public function finish_table_export(xmldb_table
$table) {
70 $this->output('</table>');
74 * Callback function. Outputs moodle_database closing tag.
76 public function finish_database_export() {
77 $this->output('</moodle_database>');
81 * Callback function. Outputs record tag with field subtags and data.
83 * @param xmldb_table $table - XMLDB object of the table from which data was retrieved
84 * @param object $data - data object (fields and values from record)
87 public function export_table_data(xmldb_table
$table, $data) {
88 $this->output('<record>');
89 foreach ($data as $key => $value) {
90 if (is_null($value)) {
91 $this->output('<field name="'.$key.'" value="null" />');
93 $this->output('<field name="'.$key.'">'.htmlspecialchars($value, ENT_NOQUOTES
, 'UTF-8').'</field>');
96 $this->output('</record>');