From 14e6710d99cbf5ca9c239c537e475f44302d5d14 Mon Sep 17 00:00:00 2001 From: "Eloy Lafuente (stronk7)" Date: Mon, 11 Apr 2011 02:16:58 +0200 Subject: [PATCH] MDL-26896 parser - add start/end path notifications --- ...restore_inforef_parser_processor.class.php | 8 ++ ...store_moodlexml_parser_processor.class.php | 8 ++ ...store_questions_parser_processor.class.php | 8 ++ .../restore_roles_parser_processor.class.php | 8 ++ ...store_structure_parser_processor.class.php | 8 ++ .../restore_users_parser_processor.class.php | 8 ++ .../grouped_parser_processor.class.php | 35 ++++---- .../simplified_parser_processor.class.php | 42 ++++++++++ .../util/xml/parser/simpletest/testparser.php | 80 +++++++++++++++++++ 9 files changed, 187 insertions(+), 18 deletions(-) diff --git a/backup/util/helper/restore_inforef_parser_processor.class.php b/backup/util/helper/restore_inforef_parser_processor.class.php index 2e308e4dc360e..3cf5ee033117d 100644 --- a/backup/util/helper/restore_inforef_parser_processor.class.php +++ b/backup/util/helper/restore_inforef_parser_processor.class.php @@ -53,4 +53,12 @@ protected function dispatch_chunk($data) { $itemid = $data['tags']['id']; restore_dbops::set_backup_ids_record($this->restoreid, $itemname, $itemid); } + + protected function notify_path_start($path) { + // nothing to do + } + + protected function notify_path_end($path) { + // nothing to do + } } diff --git a/backup/util/helper/restore_moodlexml_parser_processor.class.php b/backup/util/helper/restore_moodlexml_parser_processor.class.php index f95dfb0e9fd21..04a49698b19e0 100644 --- a/backup/util/helper/restore_moodlexml_parser_processor.class.php +++ b/backup/util/helper/restore_moodlexml_parser_processor.class.php @@ -51,6 +51,14 @@ protected function dispatch_chunk($data) { $this->accumchunks[] = $data; } + protected function notify_path_start($path) { + // nothing to do + } + + protected function notify_path_end($path) { + // nothing to do + } + public function get_all_chunks() { return $this->accumchunks; } diff --git a/backup/util/helper/restore_questions_parser_processor.class.php b/backup/util/helper/restore_questions_parser_processor.class.php index c15a9f2d3dad6..4e94468ca9606 100644 --- a/backup/util/helper/restore_questions_parser_processor.class.php +++ b/backup/util/helper/restore_questions_parser_processor.class.php @@ -75,6 +75,14 @@ protected function dispatch_chunk($data) { } } + protected function notify_path_start($path) { + // nothing to do + } + + protected function notify_path_end($path) { + // nothing to do + } + /** * Provide NULL decoding */ diff --git a/backup/util/helper/restore_roles_parser_processor.class.php b/backup/util/helper/restore_roles_parser_processor.class.php index 2729ae09337dc..f2172cf5a0fce 100644 --- a/backup/util/helper/restore_roles_parser_processor.class.php +++ b/backup/util/helper/restore_roles_parser_processor.class.php @@ -55,6 +55,14 @@ protected function dispatch_chunk($data) { } } + protected function notify_path_start($path) { + // nothing to do + } + + protected function notify_path_end($path) { + // nothing to do + } + /** * Provide NULL decoding */ diff --git a/backup/util/helper/restore_structure_parser_processor.class.php b/backup/util/helper/restore_structure_parser_processor.class.php index 222fa0d3cdc24..0cfdfe7499baa 100644 --- a/backup/util/helper/restore_structure_parser_processor.class.php +++ b/backup/util/helper/restore_structure_parser_processor.class.php @@ -102,4 +102,12 @@ protected function postprocess_chunk($data) { protected function dispatch_chunk($data) { $this->step->process($data); } + + protected function notify_path_start($path) { + // nothing to do + } + + protected function notify_path_end($path) { + // nothing to do + } } diff --git a/backup/util/helper/restore_users_parser_processor.class.php b/backup/util/helper/restore_users_parser_processor.class.php index c999ad002efc3..41ceb1b446541 100644 --- a/backup/util/helper/restore_users_parser_processor.class.php +++ b/backup/util/helper/restore_users_parser_processor.class.php @@ -66,6 +66,14 @@ protected function dispatch_chunk($data) { } } + protected function notify_path_start($path) { + // nothing to do + } + + protected function notify_path_end($path) { + // nothing to do + } + /** * Provide NULL decoding */ diff --git a/backup/util/xml/parser/processors/grouped_parser_processor.class.php b/backup/util/xml/parser/processors/grouped_parser_processor.class.php index 7d70106493373..ca5d3f494ffe8 100644 --- a/backup/util/xml/parser/processors/grouped_parser_processor.class.php +++ b/backup/util/xml/parser/processors/grouped_parser_processor.class.php @@ -71,7 +71,18 @@ public function add_path($path, $grouped = false) { } /** - * Dispatch grouped chunks safely once their end tag happens + * Notify start of path if selected and not under grouped + */ + public function before_path($path) { + if ($this->path_is_selected($path) && !$this->grouped_parent_exists($path)) { + parent::before_path($path); + } + } + + + /** + * Dispatch grouped chunks safely once their end tag happens. + * Also notify end of path if selected and not under grouped */ public function after_path($path) { if ($this->path_is_grouped($path)) { @@ -82,6 +93,11 @@ public function after_path($path) { // TODO: If running under DEBUG_DEVELOPER notice about >1MB grouped chunks $this->dispatch_chunk($data); } + // Normal notification of path end + // Only if path is selected and not child of grouped + if ($this->path_is_selected($path) && !$this->grouped_parent_exists($path)) { + parent::after_path($path); + } } // Protected API starts here @@ -111,23 +127,6 @@ protected function path_is_grouped($path) { return in_array($path, $this->groupedpaths); } - /** - * Function that will look for any - * parent for the given path, returning it if found, - * false if not - */ - protected function processed_parent_exists($path) { - $parentpath = progressive_parser::dirname($path); - while ($parentpath != '/') { - if ($this->path_is_selected($parentpath)) { - return $parentpath; - } - $parentpath = progressive_parser::dirname($parentpath); - } - return false; - } - - /** * Function that will look for any grouped * parent for the given path, returning it if found, diff --git a/backup/util/xml/parser/processors/simplified_parser_processor.class.php b/backup/util/xml/parser/processors/simplified_parser_processor.class.php index e87ac9d556fc5..e4432af9b48d9 100644 --- a/backup/util/xml/parser/processors/simplified_parser_processor.class.php +++ b/backup/util/xml/parser/processors/simplified_parser_processor.class.php @@ -63,6 +63,16 @@ public function add_path($path) { */ abstract protected function dispatch_chunk($data); + /** + * Get one selected path and notify about start + */ + abstract protected function notify_path_start($path); + + /** + * Get one selected path and notify about end + */ + abstract protected function notify_path_end($path); + /** * Get one chunk of parsed data and make it simpler * adding attributes as tags and delegating to @@ -139,6 +149,24 @@ public function process_chunk($data) { return true; } + /** + * The parser fires this each time one path is going to be parsed + */ + public function before_path($path) { + if ($this->path_is_selected($path)) { + $this->notify_path_start($path); + } + } + + /** + * The parser fires this each time one path has been parsed + */ + public function after_path($path) { + if ($this->path_is_selected($path)) { + $this->notify_path_end($path); + } + } + // Protected API starts here protected function postprocess_chunk($data) { @@ -152,4 +180,18 @@ protected function path_is_selected($path) { protected function path_is_selected_parent($path) { return in_array($path, $this->parentpaths); } + + /** + * Returns the first selected parent if available or false + */ + protected function selected_parent_exists($path) { + $parentpath = progressive_parser::dirname($path); + while ($parentpath != '/') { + if ($this->path_is_selected($parentpath)) { + return $parentpath; + } + $parentpath = progressive_parser::dirname($parentpath); + } + return false; + } } diff --git a/backup/util/xml/parser/simpletest/testparser.php b/backup/util/xml/parser/simpletest/testparser.php index eeb71a3b8cfad..eccc2c44741b8 100644 --- a/backup/util/xml/parser/simpletest/testparser.php +++ b/backup/util/xml/parser/simpletest/testparser.php @@ -307,6 +307,29 @@ function test_simplified_parser_results() { $this->assertEqual(count($tags), 2); $this->assertEqual($tags['name'], 4); $this->assertEqual($tags['value'], 5); + + // Now check start notifications + $snotifs = $pr->get_start_notifications(); + // Check we have received the correct number of notifications + $this->assertEqual(count($snotifs), 12); + // Check first, sixth and last notifications + $this->assertEqual($snotifs[0], '/activity'); + $this->assertEqual($snotifs[5], '/activity/glossary/entries/entry'); + $this->assertEqual($snotifs[11], '/activity/glossary/othertest'); + + // Now check end notifications + $enotifs = $pr->get_end_notifications(); + // Check we have received the correct number of notifications + $this->assertEqual(count($snotifs), 12); + // Check first, sixth and last notifications + $this->assertEqual($enotifs[0], '/activity/glossary/entries/entry/aliases/alias'); + $this->assertEqual($enotifs[5], '/activity/glossary/entries/entry/ratings/rating'); + $this->assertEqual($enotifs[11], '/activity'); + + // Check start and end notifications are balanced + sort($snotifs); + sort($enotifs); + $this->assertEqual($snotifs, $enotifs); } /* @@ -454,6 +477,27 @@ function test_grouped_parser_results() { $this->assertEqual(count($othertest[0]), 2); $this->assertEqual($othertest[0]['name'], 4); $this->assertEqual($othertest[0]['value'], 5); + + // Now check start notifications + $snotifs = $pr->get_start_notifications(); + // Check we have received the correct number of notifications + $this->assertEqual(count($snotifs), 2); + // Check first and last notifications + $this->assertEqual($snotifs[0], '/activity'); + $this->assertEqual($snotifs[1], '/activity/glossary'); + + // Now check end notifications + $enotifs = $pr->get_end_notifications(); + // Check we have received the correct number of notifications + $this->assertEqual(count($snotifs), 2); + // Check first, and last notifications + $this->assertEqual($enotifs[0], '/activity/glossary'); + $this->assertEqual($enotifs[1], '/activity'); + + // Check start and end notifications are balanced + sort($snotifs); + sort($enotifs); + $this->assertEqual($snotifs, $enotifs); } } @@ -526,14 +570,32 @@ public function get_chunks() { class mock_simplified_parser_processor extends simplified_parser_processor { private $chunksarr = array(); // To accumulate the found chunks + private $startarr = array(); // To accumulate all the notified path starts + private $endarr = array(); // To accumulate all the notified path ends public function dispatch_chunk($data) { $this->chunksarr[] = $data; } + public function notify_path_start($path) { + $this->startarr[] = $path; + } + + public function notify_path_end($path) { + $this->endarr[] = $path; + } + public function get_chunks() { return $this->chunksarr; } + + public function get_start_notifications() { + return $this->startarr; + } + + public function get_end_notifications() { + return $this->endarr; + } } /* @@ -542,12 +604,30 @@ public function get_chunks() { class mock_grouped_parser_processor extends grouped_parser_processor { private $chunksarr = array(); // To accumulate the found chunks + private $startarr = array(); // To accumulate all the notified path starts + private $endarr = array(); // To accumulate all the notified path ends public function dispatch_chunk($data) { $this->chunksarr[] = $data; } + public function notify_path_start($path) { + $this->startarr[] = $path; + } + + public function notify_path_end($path) { + $this->endarr[] = $path; + } + public function get_chunks() { return $this->chunksarr; } + + public function get_start_notifications() { + return $this->startarr; + } + + public function get_end_notifications() { + return $this->endarr; + } }