Permalink
Browse files

MDL-26896 parser - add start/end path notifications

  • Loading branch information...
1 parent 6b14adf commit 14e6710d99cbf5ca9c239c537e475f44302d5d14 @stronk7 stronk7 committed Apr 11, 2011
@@ -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
+ }
}
@@ -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;
}
@@ -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
*/
@@ -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
*/
@@ -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
+ }
}
@@ -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
*/
@@ -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
@@ -112,23 +128,6 @@ protected function path_is_grouped($path) {
}
/**
- * 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,
* false if not
@@ -64,6 +64,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
* dispatch_chunk() the procesing of the resulting chunk
@@ -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;
+ }
}
@@ -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;
+ }
}

0 comments on commit 14e6710

Please sign in to comment.