weekly release 2.1dev
[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
EL
73 /**
74 * Dispatch grouped chunks safely once their end tag happens.
75 * Also notify end of path if selected and not under grouped
4c7f6ac6
EL
76 */
77 public function after_path($path) {
78 if ($this->path_is_grouped($path)) {
79 // Any accumulated information must be in
80 // currentdata, properly built
81 $data = $this->currentdata[$path];
82 unset($this->currentdata[$path]);
83 // TODO: If running under DEBUG_DEVELOPER notice about >1MB grouped chunks
84 $this->dispatch_chunk($data);
85 }
14e6710d
EL
86 // Normal notification of path end
87 // Only if path is selected and not child of grouped
88 if ($this->path_is_selected($path) && !$this->grouped_parent_exists($path)) {
89 parent::after_path($path);
90 }
4c7f6ac6
EL
91 }
92
93// Protected API starts here
94
95 /**
96 * Override this method so grouping will be happening here
97 * also deciding between accumulating/dispatching
98 */
99 protected function postprocess_chunk($data) {
100 $path = $data['path'];
101 // If the chunk is a grouped one, simply put it into currentdata
102 if ($this->path_is_grouped($path)) {
8298beda 103 $this->notify_path_start($path);
4c7f6ac6
EL
104 $this->currentdata[$path] = $data;
105
106 // If the chunk is child of grouped one, add it to currentdata
107 } else if ($grouped = $this->grouped_parent_exists($path)) {
108 $this->build_currentdata($grouped, $data);
109 $this->chunks--; // not counted, as it's accumulated
110
111 // No grouped nor child of grouped, dispatch it
112 } else {
8298beda 113 $this->notify_path_start($path);
4c7f6ac6
EL
114 $this->dispatch_chunk($data);
115 }
116 }
117
118 protected function path_is_grouped($path) {
119 return in_array($path, $this->groupedpaths);
120 }
121
4c7f6ac6
EL
122 /**
123 * Function that will look for any grouped
124 * parent for the given path, returning it if found,
125 * false if not
126 */
127 protected function grouped_parent_exists($path) {
e521039d 128 $parentpath = progressive_parser::dirname($path);
4c7f6ac6
EL
129 while ($parentpath != '/') {
130 if ($this->path_is_grouped($parentpath)) {
131 return $parentpath;
132 }
e521039d 133 $parentpath = progressive_parser::dirname($parentpath);
4c7f6ac6
EL
134 }
135 return false;
136 }
137
138 /**
139 * Function that will look for any grouped
140 * child for the given path, returning it if found,
141 * false if not
142 */
143 protected function grouped_child_exists($path) {
144 $childpath = $path . '/';
145 foreach ($this->groupedpaths as $groupedpath) {
146 if (strpos($groupedpath, $childpath) === 0) {
147 return $groupedpath;
148 }
149 }
150 return false;
151 }
152
153 /**
154 * This function will accumulate the chunk into the specified
155 * grouped element for later dispatching once it is complete
156 */
157 protected function build_currentdata($grouped, $data) {
158 // Check the grouped already exists into currentdata
159 if (!array_key_exists($grouped, $this->currentdata)) {
160 $a = new stdclass();
161 $a->grouped = $grouped;
162 $a->child = $data['path'];
163 throw new progressive_parser_exception('xml_cannot_add_to_grouped', $a);
164 }
165 $this->add_missing_sub($grouped, $data['path'], $data['tags']);
166 }
167
168 /**
169 * Add non-existing subarray elements
170 */
171 protected function add_missing_sub($grouped, $path, $tags) {
172
173 // Remember tag being processed
174 $processedtag = basename($path);
175
176 $info =& $this->currentdata[$grouped]['tags'];
177 $hierarchyarr = explode('/', str_replace($grouped . '/', '', $path));
178
179 $previouselement = '';
180 $currentpath = '';
181
182 foreach ($hierarchyarr as $index => $element) {
183
184 $currentpath = $currentpath . '/' . $element;
185
186 // If element is already set and it's not
187 // the processed one (with tags) fast move the $info
188 // pointer and continue
189 if ($element !== $processedtag && isset($info[$element])) {
190 $previouselement = $element;
191 $info =& $info[$element];
192 continue;
193 }
194
195 // If previous element already has occurrences
196 // we move $info pointer there (only if last is
197 // numeric occurrence)
198 if (!empty($previouselement) && is_array($info) && count($info) > 0) {
199 end($info);
200 $key = key($info);
201 if ((int) $key === $key) {
202 $info =& $info[$key];
203 }
204 }
205
206 // Create element if not defined
207 if (!isset($info[$element])) {
208 // First into last element if present
209 $info[$element] = array();
210 }
211
212 // If element is the current one, add information
213 if ($element === $processedtag) {
214 $info[$element][] = $tags;
215 }
216
217 $previouselement = $element;
218 $info =& $info[$element];
219 }
220 }
221}