mirrored from git://git.moodle.org/moodle.git
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MDL-22699 xml parser - to be used by restore
- Loading branch information
Showing
13 changed files
with
1,167 additions
and
0 deletions.
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
backup/util/xml/parser/processors/findpaths_parser_processor.class.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
35
backup/util/xml/parser/processors/null_parser_processor.class.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) { | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
backup/util/xml/parser/processors/progressive_parser_processor.class.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |
53 changes: 53 additions & 0 deletions
53
backup/util/xml/parser/processors/selective_exact_parser_processor.class.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
backup/util/xml/parser/processors/selective_like_parser_processor.class.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
backup/util/xml/parser/processors/simple_parser_processor.class.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |
Oops, something went wrong.