Skip to content

Commit

Permalink
MDL-22699 xml parser - to be used by restore
Browse files Browse the repository at this point in the history
  • Loading branch information
stronk7 committed Jun 7, 2010
1 parent 32a5446 commit be866f9
Show file tree
Hide file tree
Showing 13 changed files with 1,167 additions and 0 deletions.
@@ -0,0 +1,60 @@
<?php

// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* @package moodlecore
* @subpackage xml
* @copyright 2003 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

require_once($CFG->dirroot.'/backup/util/xml/parser/processors/progressive_parser_processor.class.php');

/**
* Find paths progressive_parser_processor that will search for all the paths present in
* the chunks being returned. Useful to know the overal structure of the XML file.
*/
class findpaths_parser_processor extends progressive_parser_processor {

protected $foundpaths; // array of paths foudn in the chunks received from the parser

public function __construct() {
parent::__construct();
$this->foundpaths = array();
}

public function process_chunk($data) {
if (isset($data['tags'])) {
foreach ($data['tags'] as $tag) {
$tagpath = $data['path'] . '/' . $tag['name'];
if (!array_key_exists($tagpath, $this->foundpaths)) {
$this->foundpaths[$tagpath] = 1;
} else {
$this->foundpaths[$tagpath]++;
}
}
}
}

public function debug_info() {
$debug = array();
foreach($this->foundpaths as $path => $chunks) {
$debug['paths'][$path] = $chunks;
}
return array_merge($debug, parent::debug_info());
}
}
35 changes: 35 additions & 0 deletions backup/util/xml/parser/processors/null_parser_processor.class.php
@@ -0,0 +1,35 @@
<?php

// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* @package moodlecore
* @subpackage xml
* @copyright 2003 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

require_once($CFG->dirroot.'/backup/util/xml/parser/processors/progressive_parser_processor.class.php');

/**
* Null progressive_parser_processor that won't process chunks at all.
* Useful for comparing memory use/execution time.
*/
class null_parser_processor extends progressive_parser_processor {

public function process_chunk($data) {
}
}
@@ -0,0 +1,79 @@
<?php

// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* @package moodlecore
* @subpackage backup-xml
* @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

/**
* This abstract class implements one progressive_parser_processor
*
* Processor that will receive chunks of data from the @progressive_parser
* and will perform all sort of operations with them (join, split, invoke
* other methods, output, whatever...
*
* You will need to extend this class to get the expected functionality
* by implementing the @process_chunk() method to handle different
* chunks of information and, optionally, the @process_cdata() to
* process each cdata piece individually before being "published" to
* the chunk processor.
*
* The "propietary array format" that the parser publishes to the @progressive_parser_procesor
* is this:
* array (
* 'path' => path where the tags belong to,
* 'level'=> level (1-based) of the tags
* 'tags => array (
* 'name' => name of the tag,
* 'attrs'=> array( name of the attr => value of the attr),
* 'cdata => cdata of the tag
* )
* )
*
* TODO: Finish phpdocs
*/
abstract class progressive_parser_processor {

protected $inittime; // Initial microtime
protected $chunks; // Number of chunks processed

public function __construct() {
$this->inittime= microtime(true);
$this->chunks = 0;
}

abstract public function process_chunk($data);

public function process_cdata($cdata) {
return $cdata;
}

public function debug_info() {
return array('memory' => memory_get_peak_usage(true),
'time' => microtime(true) - $this->inittime,
'chunks' => $this->chunks);
}

public function receive_chunk($data) {
$this->chunks++;
$this->process_chunk($data);
}

}
@@ -0,0 +1,53 @@
<?php

// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* @package moodlecore
* @subpackage xml
* @copyright 2003 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

require_once($CFG->dirroot.'/backup/util/xml/parser/processors/progressive_parser_processor.class.php');

/**
* Selective progressive_parser_processor that will send chunks straight
* to output but only for chunks matching (in an exact way) some defined paths
*/
class selective_exact_parser_processor extends progressive_parser_processor {

protected $paths; // array of paths we are interested on

public function __construct(array $paths) {
parent::__construct();
$this->paths = $paths;
}

public function process_chunk($data) {
if ($this->path_is_selected($data['path'])) {
print_r($data); // Simply output chunk, for testing purposes
} else {
$this->chunks--; // Chunk skipped
}
}

// Protected API starts here

protected function path_is_selected($path) {
return in_array($path, $this->paths);
}
}
@@ -0,0 +1,53 @@
<?php

// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* @package moodlecore
* @subpackage xml
* @copyright 2003 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

require_once($CFG->dirroot.'/backup/util/xml/parser/processors/progressive_parser_processor.class.php');

/**
* Selective progressive_parser_processor that will send chunks straight
* to output but only for chunks matching (in a left padded way - like) some defined paths
*/
class selective_like_parser_processor extends progressive_parser_processor {

protected $paths; // array of paths we are interested on

public function __construct(array $paths) {
parent::__construct();
$this->paths = '=>' . implode('=>', $paths);
}

public function process_chunk($data) {
if ($this->path_is_selected($data['path'])) {
print_r($data); // Simply output chunk, for testing purposes
} else {
$this->chunks--; // Chunk skipped
}
}

// Protected API starts here

protected function path_is_selected($path) {
return strpos('@=>' . $path, $this->paths);
}
}
@@ -0,0 +1,36 @@
<?php

// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* @package moodlecore
* @subpackage xml
* @copyright 2003 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

require_once($CFG->dirroot.'/backup/util/xml/parser/processors/progressive_parser_processor.class.php');

/**
* Simple progressive_parser_processor that will send chunks straight
* to output. Useful for testing, compare memory use/execution time.
*/
class simple_parser_processor extends progressive_parser_processor {

public function process_chunk($data) {
print_r($data); // Simply output chunk, for testing purposes
}
}

0 comments on commit be866f9

Please sign in to comment.