MDL-37893 Add unit test to demonstrate the issue
[moodle.git] / backup / util / xml / parser / processors / grouped_parser_processor.class.php
CommitLineData
4c7f6ac6
EL
1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/**
19 * @package moodlecore
20 * @subpackage xml
21 * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25require_once($CFG->dirroot.'/backup/util/xml/parser/processors/simplified_parser_processor.class.php');
26
27/**
28 * Abstract xml parser processor able to group chunks as configured
29 * and dispatch them to other arbitrary methods
30 *
31 * This @progressive_parser_processor handles the requested paths,
32 * allowing to group information under any of them, dispatching them
33 * to the methods specified
34 *
35 * Note memory increases as you group more and more paths, so use it for
36 * well-known structures being smaller enough (never to group MBs into one
37 * in-memory structure)
38 *
39 * TODO: Complete phpdocs
40 */
41abstract class grouped_parser_processor extends simplified_parser_processor {
42
43 protected $groupedpaths; // Paths we are requesting grouped
44 protected $currentdata; // Where we'll be acummulating data
45
46 public function __construct(array $paths = array()) {
47 $this->groupedpaths = array();
48 $this->currentdata = null;
49 parent::__construct($paths);
50 }
51
52 public function add_path($path, $grouped = false) {
53 if ($grouped) {
54 // Check there is no parent in the branch being grouped
55 if ($found = $this->grouped_parent_exists($path)) {
56 $a = new stdclass();
57 $a->path = $path;
58 $a->parent = $found;
59 throw new progressive_parser_exception('xml_grouped_parent_found', $a);
60 }
61 // Check there is no child in the branch being grouped
62 if ($found = $this->grouped_child_exists($path)) {
63 $a = new stdclass();
64 $a->path = $path;
65 $a->child = $found;
66 throw new progressive_parser_exception('xml_grouped_child_found', $a);
67 }
68 $this->groupedpaths[] = $path;
69 }
70 parent::add_path($path);
71 }
72
14e6710d 73 /**
54b8f334
EL
74 * The parser fires this each time one path is going to be parsed
75 *
76 * @param string $path xml path which parsing has started
77 */
78 public function before_path($path) {
79 if (!$this->grouped_parent_exists($path)) {
80 parent::before_path($path);
81 }
82 }
83
84 /**
85 * The parser fires this each time one path has been parsed
86 *
87 * @param string $path xml path which parsing has ended
4c7f6ac6
EL
88 */
89 public function after_path($path) {
54b8f334 90 // Have finished one grouped path, dispatch it
4c7f6ac6
EL
91 if ($this->path_is_grouped($path)) {
92 // Any accumulated information must be in
93 // currentdata, properly built
94 $data = $this->currentdata[$path];
95 unset($this->currentdata[$path]);
96 // TODO: If running under DEBUG_DEVELOPER notice about >1MB grouped chunks
97 $this->dispatch_chunk($data);
98 }
14e6710d
EL
99 // Normal notification of path end
100 // Only if path is selected and not child of grouped
54b8f334 101 if (!$this->grouped_parent_exists($path)) {
14e6710d
EL
102 parent::after_path($path);
103 }
4c7f6ac6
EL
104 }
105
106// Protected API starts here
107
108 /**
109 * Override this method so grouping will be happening here
110 * also deciding between accumulating/dispatching
111 */
112 protected function postprocess_chunk($data) {
113 $path = $data['path'];
114 // If the chunk is a grouped one, simply put it into currentdata
115 if ($this->path_is_grouped($path)) {
116 $this->currentdata[$path] = $data;
117
118 // If the chunk is child of grouped one, add it to currentdata
119 } else if ($grouped = $this->grouped_parent_exists($path)) {
120 $this->build_currentdata($grouped, $data);
121 $this->chunks--; // not counted, as it's accumulated
122
123 // No grouped nor child of grouped, dispatch it
124 } else {
125 $this->dispatch_chunk($data);
126 }
127 }
128
129 protected function path_is_grouped($path) {
130 return in_array($path, $this->groupedpaths);
131 }
132
4c7f6ac6
EL
133 /**
134 * Function that will look for any grouped
135 * parent for the given path, returning it if found,
136 * false if not
137 */
138 protected function grouped_parent_exists($path) {
e521039d 139 $parentpath = progressive_parser::dirname($path);
4c7f6ac6
EL
140 while ($parentpath != '/') {
141 if ($this->path_is_grouped($parentpath)) {
142 return $parentpath;
143 }
e521039d 144 $parentpath = progressive_parser::dirname($parentpath);
4c7f6ac6
EL
145 }
146 return false;
147 }
148
149 /**
150 * Function that will look for any grouped
151 * child for the given path, returning it if found,
152 * false if not
153 */
154 protected function grouped_child_exists($path) {
155 $childpath = $path . '/';
156 foreach ($this->groupedpaths as $groupedpath) {
157 if (strpos($groupedpath, $childpath) === 0) {
158 return $groupedpath;
159 }
160 }
161 return false;
162 }
163
164 /**
165 * This function will accumulate the chunk into the specified
166 * grouped element for later dispatching once it is complete
167 */
168 protected function build_currentdata($grouped, $data) {
169 // Check the grouped already exists into currentdata
170 if (!array_key_exists($grouped, $this->currentdata)) {
171 $a = new stdclass();
172 $a->grouped = $grouped;
173 $a->child = $data['path'];
174 throw new progressive_parser_exception('xml_cannot_add_to_grouped', $a);
175 }
176 $this->add_missing_sub($grouped, $data['path'], $data['tags']);
177 }
178
179 /**
180 * Add non-existing subarray elements
181 */
182 protected function add_missing_sub($grouped, $path, $tags) {
183
184 // Remember tag being processed
185 $processedtag = basename($path);
186
187 $info =& $this->currentdata[$grouped]['tags'];
188 $hierarchyarr = explode('/', str_replace($grouped . '/', '', $path));
189
190 $previouselement = '';
191 $currentpath = '';
192
193 foreach ($hierarchyarr as $index => $element) {
194
195 $currentpath = $currentpath . '/' . $element;
196
197 // If element is already set and it's not
198 // the processed one (with tags) fast move the $info
199 // pointer and continue
200 if ($element !== $processedtag && isset($info[$element])) {
201 $previouselement = $element;
202 $info =& $info[$element];
203 continue;
204 }
205
206 // If previous element already has occurrences
207 // we move $info pointer there (only if last is
208 // numeric occurrence)
209 if (!empty($previouselement) && is_array($info) && count($info) > 0) {
210 end($info);
211 $key = key($info);
212 if ((int) $key === $key) {
213 $info =& $info[$key];
214 }
215 }
216
217 // Create element if not defined
218 if (!isset($info[$element])) {
219 // First into last element if present
220 $info[$element] = array();
221 }
222
223 // If element is the current one, add information
224 if ($element === $processedtag) {
225 $info[$element][] = $tags;
226 }
227
228 $previouselement = $element;
229 $info =& $info[$element];
230 }
231 }
232}