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 exporter 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 exporter class.
  29   * Provides logic for writing XML tags and data inside appropriate callbacks.
  30   * Subclasses should define XML data sinks.
  31   */
  32  abstract class xml_database_exporter extends database_exporter {
  33      /**
  34       * Generic output method. Subclasses should implement it with code specific
  35       * to the target XML sink.
  36       */
  37      protected abstract function output($text);
  38  
  39      /**
  40       * Callback function. Outputs open XML PI and moodle_database opening tag.
  41       *
  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.
  46       * @return void
  47       */
  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').'"').'>');
  52      }
  53  
  54      /**
  55       * Callback function. Outputs table opening tag.
  56       *
  57       * @param xmldb_table $table - XMLDB object for the exported table
  58       * @return void
  59       */
  60      public function begin_table_export(xmldb_table $table) {
  61          $this->output('<table name="'.$table->getName().'" schemaHash="'.$table->getHash().'">');
  62      }
  63  
  64      /**
  65       * Callback function. Outputs table closing tag.
  66       *
  67       * @param xmldb_table $table - XMLDB object for the exported table
  68       */
  69      public function finish_table_export(xmldb_table $table) {
  70          $this->output('</table>');
  71      }
  72  
  73      /**
  74       * Callback function. Outputs moodle_database closing tag.
  75       */
  76      public function finish_database_export() {
  77          $this->output('</moodle_database>');
  78      }
  79  
  80      /**
  81       * Callback function. Outputs record tag with field subtags and data.
  82       *
  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)
  85       * @return void
  86       */
  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" />');
  92              } else {
  93                  $this->output('<field name="'.$key.'">'.htmlspecialchars($value, ENT_NOQUOTES, 'UTF-8').'</field>');
  94              }
  95          }
  96          $this->output('</record>');
  97      }
  98  }