Skip to content

Commit

Permalink
Merge branch 'MDL-26896' of git://github.com/stronk7/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Hemelryk committed Apr 11, 2011
2 parents 08c2633 + 14e6710 commit e1ce7c7
Show file tree
Hide file tree
Showing 9 changed files with 187 additions and 18 deletions.
8 changes: 8 additions & 0 deletions backup/util/helper/restore_inforef_parser_processor.class.php
Expand Up @@ -53,4 +53,12 @@ protected function dispatch_chunk($data) {
$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
}
} }
Expand Up @@ -51,6 +51,14 @@ protected function dispatch_chunk($data) {
$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;
} }
Expand Down
Expand Up @@ -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 * Provide NULL decoding
*/ */
Expand Down
8 changes: 8 additions & 0 deletions backup/util/helper/restore_roles_parser_processor.class.php
Expand Up @@ -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 * Provide NULL decoding
*/ */
Expand Down
Expand Up @@ -102,4 +102,12 @@ protected function postprocess_chunk($data) {
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
}
} }
8 changes: 8 additions & 0 deletions backup/util/helper/restore_users_parser_processor.class.php
Expand Up @@ -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 * Provide NULL decoding
*/ */
Expand Down
Expand Up @@ -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) { public function after_path($path) {
if ($this->path_is_grouped($path)) { if ($this->path_is_grouped($path)) {
Expand All @@ -82,6 +93,11 @@ public function after_path($path) {
// 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
Expand Down Expand Up @@ -111,23 +127,6 @@ protected function path_is_grouped($path) {
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,
Expand Down
Expand Up @@ -63,6 +63,16 @@ public function add_path($path) {
*/ */
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
Expand Down Expand Up @@ -139,6 +149,24 @@ public function process_chunk($data) {
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) {
Expand All @@ -152,4 +180,18 @@ protected function path_is_selected($path) {
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;
}
} }
80 changes: 80 additions & 0 deletions backup/util/xml/parser/simpletest/testparser.php
Expand Up @@ -307,6 +307,29 @@ function test_simplified_parser_results() {
$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);
} }


/* /*
Expand Down Expand Up @@ -454,6 +477,27 @@ function test_grouped_parser_results() {
$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);
} }
} }


Expand Down Expand Up @@ -526,14 +570,32 @@ public function get_chunks() {
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;
}
} }


/* /*
Expand All @@ -542,12 +604,30 @@ public function get_chunks() {
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;
}
} }

0 comments on commit e1ce7c7

Please sign in to comment.