Merge branch 'MDL-26896' of git://github.com/stronk7/moodle

This commit is contained in:
Sam Hemelryk 2011-04-11 14:48:01 +08:00
commit e1ce7c7e7b
9 changed files with 187 additions and 18 deletions

View File

@ -53,4 +53,12 @@ class restore_inforef_parser_processor extends grouped_parser_processor {
$itemid = $data['tags']['id']; $itemid = $data['tags']['id'];
restore_dbops::set_backup_ids_record($this->restoreid, $itemname, $itemid); 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
}
} }

View File

@ -51,6 +51,14 @@ class restore_moodlexml_parser_processor extends grouped_parser_processor {
$this->accumchunks[] = $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() { public function get_all_chunks() {
return $this->accumchunks; return $this->accumchunks;
} }

View File

@ -75,6 +75,14 @@ class restore_questions_parser_processor extends grouped_parser_processor {
} }
} }
protected function notify_path_start($path) {
// nothing to do
}
protected function notify_path_end($path) {
// nothing to do
}
/** /**
* Provide NULL decoding * Provide NULL decoding
*/ */

View File

@ -55,6 +55,14 @@ class restore_roles_parser_processor extends grouped_parser_processor {
} }
} }
protected function notify_path_start($path) {
// nothing to do
}
protected function notify_path_end($path) {
// nothing to do
}
/** /**
* Provide NULL decoding * Provide NULL decoding
*/ */

View File

@ -102,4 +102,12 @@ class restore_structure_parser_processor extends grouped_parser_processor {
protected function dispatch_chunk($data) { protected function dispatch_chunk($data) {
$this->step->process($data); $this->step->process($data);
} }
protected function notify_path_start($path) {
// nothing to do
}
protected function notify_path_end($path) {
// nothing to do
}
} }

View File

@ -66,6 +66,14 @@ class restore_users_parser_processor extends grouped_parser_processor {
} }
} }
protected function notify_path_start($path) {
// nothing to do
}
protected function notify_path_end($path) {
// nothing to do
}
/** /**
* Provide NULL decoding * Provide NULL decoding
*/ */

View File

@ -71,7 +71,18 @@ abstract class grouped_parser_processor extends simplified_parser_processor {
} }
/** /**
* 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) { public function after_path($path) {
if ($this->path_is_grouped($path)) { if ($this->path_is_grouped($path)) {
@ -82,6 +93,11 @@ abstract class grouped_parser_processor extends simplified_parser_processor {
// TODO: If running under DEBUG_DEVELOPER notice about >1MB grouped chunks // TODO: If running under DEBUG_DEVELOPER notice about >1MB grouped chunks
$this->dispatch_chunk($data); $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 // Protected API starts here
@ -111,23 +127,6 @@ abstract class grouped_parser_processor extends simplified_parser_processor {
return in_array($path, $this->groupedpaths); 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 * Function that will look for any grouped
* parent for the given path, returning it if found, * parent for the given path, returning it if found,

View File

@ -63,6 +63,16 @@ abstract class simplified_parser_processor extends progressive_parser_processor
*/ */
abstract protected function dispatch_chunk($data); 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 * Get one chunk of parsed data and make it simpler
* adding attributes as tags and delegating to * adding attributes as tags and delegating to
@ -139,6 +149,24 @@ abstract class simplified_parser_processor extends progressive_parser_processor
return true; 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 API starts here
protected function postprocess_chunk($data) { protected function postprocess_chunk($data) {
@ -152,4 +180,18 @@ abstract class simplified_parser_processor extends progressive_parser_processor
protected function path_is_selected_parent($path) { protected function path_is_selected_parent($path) {
return in_array($path, $this->parentpaths); 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;
}
} }

View File

@ -307,6 +307,29 @@ class progressive_parser_test extends UnitTestCase {
$this->assertEqual(count($tags), 2); $this->assertEqual(count($tags), 2);
$this->assertEqual($tags['name'], 4); $this->assertEqual($tags['name'], 4);
$this->assertEqual($tags['value'], 5); $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 @@ class progressive_parser_test extends UnitTestCase {
$this->assertEqual(count($othertest[0]), 2); $this->assertEqual(count($othertest[0]), 2);
$this->assertEqual($othertest[0]['name'], 4); $this->assertEqual($othertest[0]['name'], 4);
$this->assertEqual($othertest[0]['value'], 5); $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 @@ class mock_parser_processor extends progressive_parser_processor {
class mock_simplified_parser_processor extends simplified_parser_processor { class mock_simplified_parser_processor extends simplified_parser_processor {
private $chunksarr = array(); // To accumulate the found chunks 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) { public function dispatch_chunk($data) {
$this->chunksarr[] = $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() { public function get_chunks() {
return $this->chunksarr; return $this->chunksarr;
} }
public function get_start_notifications() {
return $this->startarr;
}
public function get_end_notifications() {
return $this->endarr;
}
} }
/* /*
@ -542,12 +604,30 @@ class mock_simplified_parser_processor extends simplified_parser_processor {
class mock_grouped_parser_processor extends grouped_parser_processor { class mock_grouped_parser_processor extends grouped_parser_processor {
private $chunksarr = array(); // To accumulate the found chunks 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) { public function dispatch_chunk($data) {
$this->chunksarr[] = $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() { public function get_chunks() {
return $this->chunksarr; return $this->chunksarr;
} }
public function get_start_notifications() {
return $this->startarr;
}
public function get_end_notifications() {
return $this->endarr;
}
} }