MDL-27376 MDL-27377 MDL-27378 Backup converters refactoring - work in progress
[moodle.git] / backup / converter / moodle1 / lib.php
CommitLineData
1e2c7351
DM
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 * Provides classes used by the moodle1 converter
20 *
21 * @package backup-convert
22 * @subpackage moodle1
23 * @copyright 2011 Mark Nielsen <mark@moodlerooms.com>
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26
27defined('MOODLE_INTERNAL') || die();
28
29require_once($CFG->dirroot . '/backup/converter/convertlib.php');
30require_once($CFG->dirroot . '/backup/util/xml/parser/progressive_parser.class.php');
31require_once($CFG->dirroot . '/backup/util/xml/parser/processors/grouped_parser_processor.class.php');
32require_once(dirname(__FILE__) . '/handlerlib.php');
33
34/**
35 * Converter of Moodle 1.9 backup into Moodle 2.x format
36 */
37class moodle1_converter extends base_converter {
38
39 /** @var progressive_parser moodle.xml file parser */
40 protected $xmlparser;
41
42 /** @var moodle1_parser_processor */
43 protected $xmlprocessor;
44
45 /** @var array of {@link convert_path} to process */
46 protected $pathelements = array();
47
48 /** @var string the current module being processed */
49 protected $currentmod = '';
50
51 /** @var string the current block being processed */
52 protected $currentblock = '';
53
54 /** @var string path currently locking processing of children */
55 protected $pathlock;
56
57 /**
58 * Instructs the dispatcher to ignore all children below path processor returning it
59 */
60 const SKIP_ALL_CHILDREN = -991399;
61
62 /**
63 * Detects the Moodle 1.9 format of the backup directory
64 *
65 * @param string $tempdir the name of the backup directory
66 * @return null|string backup::FORMAT_MOODLE1 if the Moodle 1.9 is detected, null otherwise
67 */
68 public static function detect_format($tempdir) {
69 global $CFG;
70
71 $filepath = $CFG->dataroot . '/temp/backup/' . $tempdir . '/moodle.xml';
72 if (file_exists($filepath)) {
73 // looks promising, lets load some information
74 $handle = fopen($filepath, 'r');
75 $first_chars = fread($handle, 200);
76 fclose($handle);
77
78 // check if it has the required strings
79 if (strpos($first_chars,'<?xml version="1.0" encoding="UTF-8"?>') !== false and
80 strpos($first_chars,'<MOODLE_BACKUP>') !== false and
81 strpos($first_chars,'<INFO>') !== false) {
82
83 return backup::FORMAT_MOODLE1;
84 }
85 }
86
87 return null;
88 }
89
90 /**
91 * Initialize the instance if needed, called by the constructor
92 *
93 * Here we create objects we need before the execution.
94 */
95 protected function init() {
96
97 // ask your mother first before going out playing with toys
98 parent::init();
99
100 // good boy, prepare XML parser and processor
101 $this->xmlparser = new progressive_parser();
102 $this->xmlparser->set_file($this->get_tempdir_path() . '/moodle.xml');
103 $this->xmlprocessor = new moodle1_parser_processor($this);
104 $this->xmlparser->set_processor($this->xmlprocessor);
105
106 // make sure that MOD and BLOCK paths are visited
107 $this->xmlprocessor->add_path('/MOODLE_BACKUP/COURSE/MODULES/MOD');
108 $this->xmlprocessor->add_path('/MOODLE_BACKUP/COURSE/BLOCKS/BLOCK');
109
110 // register the conversion handlers
111 foreach (moodle1_handlers_factory::get_handlers($this) as $handler) {
112 if (!$handler instanceof moodle1_handler) {
113 throw new convert_exception('wrong_handler_class', get_class($handler));
114 }
115 $this->register_handler($handler, $handler->get_paths());
116 }
117 }
118
119 /**
120 * Converts the contents of the tempdir into the target format in the workdir
121 */
122 protected function execute() {
123 $this->xmlparser->process();
124 }
125
126 /**
127 * Register a handler for the given path elements
128 */
129 protected function register_handler(moodle1_handler $handler, array $elements) {
130
131 // first iteration, push them to new array, indexed by name
132 // to detect duplicates in names or paths
133 $names = array();
134 $paths = array();
135 foreach($elements as $element) {
136 if (!$element instanceof convert_path) {
137 throw new convert_exception('path_element_wrong_class', get_class($element));
138 }
139 if (array_key_exists($element->get_name(), $names)) {
140 throw new convert_exception('path_element_name_alreadyexists', $element->get_name());
141 }
142 if (array_key_exists($element->get_path(), $paths)) {
143 throw new convert_exception('path_element_path_alreadyexists', $element->get_path());
144 }
145 $names[$element->get_name()] = true;
146 $paths[$element->get_path()] = $element;
147 }
148
149 // now, for each element not having a processing object yet, assign the handler
150 // if the element is not a memeber of a group
151 foreach($paths as $key => $element) {
152 if (is_null($element->get_processing_object()) and !$this->grouped_parent_exists($element, $paths)) {
153 $paths[$key]->set_processing_object($handler);
154 }
155 // add the element path to the processor
156 $this->xmlprocessor->add_path($element->get_path(), $element->is_grouped());
157 }
158
159 // done, store the paths (duplicates by path are discarded)
160 $this->pathelements = array_merge($this->pathelements, $paths);
161
162 // remove the injected plugin name element from the MOD and BLOCK paths
163 // and register such collapsed path, too
164 foreach ($elements as $element) {
165 $path = $element->get_path();
166 $path = preg_replace('/^\/MOODLE_BACKUP\/COURSE\/MODULES\/MOD\/(\w+)\//', '/MOODLE_BACKUP/COURSE/MODULES/MOD/', $path);
167 $path = preg_replace('/^\/MOODLE_BACKUP\/COURSE\/BLOCKS\/BLOCK\/(\w+)\//', '/MOODLE_BACKUP/COURSE/BLOCKS/BLOCK/', $path);
168 if (!empty($path) and $path != $element->get_path()) {
169 $this->xmlprocessor->add_path($path, false);
170 }
171 }
172 }
173
174 /**
175 * Helper method used by {@link self::register_handler()}
176 *
177 * @param convert_path $pelement path element
178 * @param array of convert_path instances
179 * @return bool true if grouped parent was found, false otherwise
180 */
181 protected function grouped_parent_exists($pelement, $elements) {
182
183 foreach ($elements as $element) {
184 if ($pelement->get_path() == $element->get_path()) {
185 // don't compare against itself
186 continue;
187 }
188 // if the element is grouped and it is a parent of pelement, return true
189 if ($element->is_grouped() and strpos($pelement->get_path() . '/', $element->get_path()) === 0) {
190 return true;
191 }
192 }
193
194 // no grouped parent found
195 return false;
196 }
197
198 /**
199 * Process the data obtained from the XML parser processor
200 *
201 * This methods receives one chunk of information from the XML parser
202 * processor and dispatches it, following the naming rules.
203 * We are expanding the modules and blocks paths here to include the plugin's name.
204 *
205 * @param array $data
206 */
207 public function process_chunk($data) {
208
209 $path = $data['path'];
210
211 // expand the MOD paths so that they contain the module name
212 if ($path === '/MOODLE_BACKUP/COURSE/MODULES/MOD') {
213 $this->currentmod = strtoupper($data['tags']['MODTYPE']);
214 $path = '/MOODLE_BACKUP/COURSE/MODULES/MOD/' . $this->currentmod;
215
216 } else if (strpos($path, '/MOODLE_BACKUP/COURSE/MODULES/MOD') === 0) {
217 $path = str_replace('/MOODLE_BACKUP/COURSE/MODULES/MOD', '/MOODLE_BACKUP/COURSE/MODULES/MOD/' . $this->currentmod, $path);
218 }
219
220 // expand the BLOCK paths so that they contain the module name
221 if ($path === '/MOODLE_BACKUP/COURSE/BLOCKS/BLOCK') {
222 $this->currentblock = strtoupper($data['tags']['NAME']);
223 $path = '/MOODLE_BACKUP/COURSE/BLOCKS/BLOCK/' . $this->currentblock;
224
225 } else if (strpos($path, '/MOODLE_BACKUP/COURSE/BLOCKS/BLOCK') === 0) {
226 $path = str_replace('/MOODLE_BACKUP/COURSE/BLOCKS/BLOCK', '/MOODLE_BACKUP/COURSE/BLOCKS/BLOCK/' . $this->currentmod, $path);
227 }
228
229 if ($path !== $data['path']) {
230 if (!array_key_exists($path, $this->pathelements)) {
231 // no handler registered for the transformed MOD or BLOCK path
232 // todo add this event to the convert log instead of debugging
233 //debugging('No handler registered for the path ' . $path);
234 return;
235
236 } else {
237 // pretend as if the original $data contained the tranformed path
238 $data['path'] = $path;
239 }
240 }
241
242 if (!array_key_exists($data['path'], $this->pathelements)) {
243 // path added to the processor without the handler
244 throw new convert_exception('missing_path_handler', $data['path']);
245 }
246
247 $element = $this->pathelements[$data['path']];
248 $object = $element->get_processing_object();
249 $method = $element->get_processing_method();
250 $rdata = null; // data returned by the processing method, if any
251
252 if (empty($object)) {
253 throw new convert_exception('missing_processing_object', $object);
254 }
255
256 // release the lock if we aren't anymore within children of it
257 if (!is_null($this->pathlock) and strpos($data['path'], $this->pathlock) === false) {
258 $this->pathlock = null;
259 }
260
261 // if the path is not locked, apply the element's recipes and dispatch
262 // the cooked tags to the processing method
263 if (is_null($this->pathlock)) {
264 $data['tags'] = $element->apply_recipes($data['tags']);
265 $rdata = $object->$method($data['tags']);
266 }
267
268 // if the dispatched method returned SKIP_ALL_CHILDREN, remember the current path
269 // and lock it so that its children are not dispatched
270 if ($rdata === self::SKIP_ALL_CHILDREN) {
271 // check we haven't any previous lock
272 if (!is_null($this->pathlock)) {
273 throw new convert_exception('already_locked_path', $data['path']);
274 }
275 // set the lock - nothing below the current path will be dispatched
276 $this->pathlock = $data['path'] . '/';
277
278 // if the method has returned any info, set element data to it
279 } else if (!is_null($rdata)) {
280 $element->set_data($rdata);
281
282 // use just the cooked parsed data otherwise
283 } else {
284 $element->set_data($data);
285 }
286 }
287
288 /**
289 * Executes operations required at the start of a watched path
290 *
291 * Note that this is called before the MOD and BLOCK paths are expanded
292 * so the current plugin is not known yet.
293 *
294 * @todo dispatch the message to the interested handlers
295 * @param string $path in the original file
296 */
297 public function path_start_reached($path) {
298 print_object("start reached: $path"); // DONOTCOMMIT
299 }
300
301 /**
302 * Executes operations required at the end of a watched path
303 *
304 * @todo dispatch the message to the interested handlers
305 * @param string $path in the original file
306 */
307 public function path_end_reached($path) {
308 print_object("end reached: $path"); // DONOTCOMMIT
309 }
310}
311
312
313/**
314 * XML parser processor
315 */
316class moodle1_parser_processor extends grouped_parser_processor {
317
318 /** @var moodle1_converter */
319 protected $converter;
320
321 public function __construct(moodle1_converter $converter) {
322 $this->converter = $converter;
323 parent::__construct();
324 }
325
326 /**
327 * Provide NULL and legacy file.php uses decoding
328 */
329 public function process_cdata($cdata) {
330 global $CFG;
331
332 if ($cdata === '$@NULL@$') { // Some cases we know we can skip complete processing
333 return null;
334 } else if ($cdata === '') {
335 return '';
336 } else if (is_numeric($cdata)) {
337 return $cdata;
338 } else if (strlen($cdata) < 32) { // Impossible to have one link in 32cc
339 return $cdata; // (http://10.0.0.1/file.php/1/1.jpg, http://10.0.0.1/mod/url/view.php?id=)
340 } else if (strpos($cdata, '$@FILEPHP@$') === false) { // No $@FILEPHP@$, nothing to convert
341 return $cdata;
342 }
343 // Decode file.php calls
344 $search = array ("$@FILEPHP@$");
345 $replace = array(get_file_url($this->courseid));
346 $result = str_replace($search, $replace, $cdata);
347 // Now $@SLASH@$ and $@FORCEDOWNLOAD@$ MDL-18799
348 $search = array('$@SLASH@$', '$@FORCEDOWNLOAD@$');
349 if ($CFG->slasharguments) {
350 $replace = array('/', '?forcedownload=1');
351 } else {
352 $replace = array('%2F', '&amp;forcedownload=1');
353 }
354 return str_replace($search, $replace, $result);
355 }
356
357 /**
358 * Override this method so we'll be able to skip
359 * dispatching some well-known chunks, like the
360 * ones being 100% part of subplugins stuff. Useful
361 * for allowing development without having all the
362 * possible restore subplugins defined
363 */
364 protected function postprocess_chunk($data) {
365
366 // Iterate over all the data tags, if any of them is
367 // not 'subplugin_XXXX' or has value, then it's a valid chunk,
368 // pass it to standard (parent) processing of chunks.
369 foreach ($data['tags'] as $key => $value) {
370 if (trim($value) !== '' || strpos($key, 'subplugin_') !== 0) {
371 parent::postprocess_chunk($data);
372 return;
373 }
374 }
375 // Arrived here, all the tags correspond to sublplugins and are empty,
376 // skip the chunk, and debug_developer notice
377 $this->chunks--; // not counted
378 debugging('Missing support on restore for ' . clean_param($data['path'], PARAM_PATH) .
379 ' subplugin (' . implode(', ', array_keys($data['tags'])) .')', DEBUG_DEVELOPER);
380 }
381
382 /**
383 * Dispatches the data chunk to the converter class
384 *
385 * @param array $data the chunk of parsed data
386 */
387 protected function dispatch_chunk($data) {
388 $this->converter->process_chunk($data);
389 }
390
391 /**
392 * Informs the converter at the start of a watched path
393 *
394 * @param string $path
395 */
396 protected function notify_path_start($path) {
397 $this->converter->path_start_reached($path);
398 }
399
400 /**
401 * Informs the converter at the end of a watched path
402 *
403 * @param string $path
404 */
405 protected function notify_path_end($path) {
406 $this->converter->path_end_reached($path);
407 }
408}
409
410
411/**
412 * Class representing a path to be converted from XML file
413 *
414 * This was created as a copy of {@link restore_path_element} and should be refactored
415 * probably.
416 */
417class convert_path {
418
419 /** @var string name of the element */
420 protected $name;
421
422 /** @var string path within the XML file this element will handle */
423 protected $path;
424
425 /** @var bool flag to define if this element will get child ones grouped or no */
426 protected $grouped;
427
428 /** @var object object instance in charge of processing this element. */
429 protected $pobject = null;
430
431 /** @var string the name of the processing method */
432 protected $pmethod = null;
433
434 /** @var mixed last data read for this element or returned data by processing method */
435 protected $data = null;
436
437 /** @var array of deprecated fields that are skipped and not converted */
438 protected $skipfields = array();
439
440 /** @var array of fields renaming */
441 protected $renamefields = array();
442
443 /** @var array of new fields to add and their initial values */
444 protected $newfields = array();
445
446 /**
447 * Constructor
448 *
449 * @param string $name name of the element
450 * @param string $path path of the element
451 * @param array $recipe basic description of the structure conversion
452 * @param bool $grouped to gather information in grouped mode or no
453 */
454 public function __construct($name, $path, array $recipe = array(), $grouped = false) {
455
456 $this->validate_name($name);
457
458 $this->name = $name;
459 $this->path = $path;
460 $this->grouped = $grouped;
461
462 // set the default processing method name
463 $this->set_processing_method('process_' . $name);
464
465 if (isset($recipe['skipfields']) and is_array($recipe['skipfields'])) {
466 $this->set_skipped_fields($recipe['skipfields']);
467 }
468 if (isset($recipe['renamefields']) and is_array($recipe['renamefields'])) {
469 $this->set_renamed_fields($recipe['renamefields']);
470 }
471 if (isset($recipe['newfields']) and is_array($recipe['newfields'])) {
472 $this->set_new_fields($recipe['newfields']);
473 }
474 }
475
476 /**
477 * Validates and sets the given processing object
478 *
479 * @param object $pobject processing object, must provide a method to be called
480 */
481 public function set_processing_object($pobject) {
482 $this->validate_pobject($pobject);
483 $this->pobject = $pobject;
484 }
485
486 /**
487 * Sets the name of the processing method
488 *
489 * @param string $pmethod
490 */
491 public function set_processing_method($pmethod) {
492 $this->pmethod = $pmethod;
493 }
494
495 /**
496 * Sets the element data
497 *
498 * @param mixed
499 */
500 public function set_data($data) {
501 $this->data = $data;
502 }
503
504 /**
505 * Sets the list of deprecated fields to skip
506 *
507 * @param array $fields
508 */
509 public function set_skipped_fields(array $fields) {
510 $this->skipfields = $fields;
511 }
512
513 /**
514 * Sets the required new names of the current fields
515 *
516 * @param array $fields (string)$currentname => (string)$newname
517 */
518 public function set_renamed_fields(array $fields) {
519 $this->renamefields = $fields;
520 }
521
522 /**
523 * Sets the new fields and their values
524 *
525 * @param array $fields (string)$field => (mixed)value
526 */
527 public function set_new_fields(array $fields) {
528 $this->newfields = $fields;
529 }
530
531 /**
532 * Cooks the parsed tags data by applying known recipes
533 *
534 * Recipes are used for common trivial operations like adding new fields
535 * or renaming fields. The handler's processing method receives cooked
536 * data.
537 *
538 * @param array $data the contents of the element
539 * @return array
540 */
541 public function apply_recipes(array $data) {
542
543 $cooked = array();
544
545 foreach ($data as $name => $value) {
546 // lower case rocks!
547 $name = strtolower($name);
548
549 // fields renaming
550 if (array_key_exists($name, $this->renamefields)) {
551 $name = $this->renamefields[$name];
552 }
553
554 $cooked[$name] = $value;
555 }
556
557 // adding new fields
558 foreach ($this->newfields as $name => $value) {
559 $cooked[$name] = $value;
560 }
561
562 return $cooked;
563 }
564
565 /**
566 * @return string the element given name
567 */
568 public function get_name() {
569 return $this->name;
570 }
571
572 /**
573 * @return string the path to the element
574 */
575 public function get_path() {
576 return $this->path;
577 }
578
579 /**
580 * @return bool flag to define if this element will get child ones grouped or no
581 */
582 public function is_grouped() {
583 return $this->grouped;
584 }
585
586 /**
587 * @return object the processing object providing the processing method
588 */
589 public function get_processing_object() {
590 return $this->pobject;
591 }
592
593 /**
594 * @return string the name of the method to call to process the element
595 */
596 public function get_processing_method() {
597 return $this->pmethod;
598 }
599
600 /**
601 * @return mixed the element data
602 */
603 public function get_data() {
604 return $this->data;
605 }
606
607
608 /// end of public API //////////////////////////////////////////////////////
609
610 /**
611 * Makes sure the given name is a valid element name
612 *
613 * Note it may look as if we used exceptions for code flow control here. That's not the case
614 * as we actually validate the code, not the user data. And the code is supposed to be
615 * correct.
616 *
617 * @param string @name the element given name
618 * @throws convert_path_exception
619 * @return void
620 */
621 protected function validate_name($name) {
622 // Validate various name constraints, throwing exception if needed
623 if (empty($name)) {
624 throw new convert_path_exception('convert_path_emptyname', $name);
625 }
626 if (preg_replace('/\s/', '', $name) != $name) {
627 throw new convert_path_exception('convert_path_whitespace', $name);
628 }
629 if (preg_replace('/[^\x30-\x39\x41-\x5a\x5f\x61-\x7a]/', '', $name) != $name) {
630 throw new convert_path_exception('convert_path_notasciiname', $name);
631 }
632 }
633
634 /**
635 * Makes sure that the given object is a valid processing object
636 *
637 * The processing object must be an object providing the element's processing method.
638 * Note it may look as if we used exceptions for code flow control here. That's not the case
639 * as we actually validate the code, not the user data. And the code is supposed to be
640 * correct.
641 *
642 * @param object $pobject
643 * @throws convert_path_exception
644 * @return void
645 */
646 protected function validate_pobject($pobject) {
647 if (!is_object($pobject)) {
648 throw new convert_path_exception('convert_path_no_object', $pobject);
649 }
650 if (!method_exists($pobject, $this->get_processing_method())) {
651 throw new convert_path_exception('convert_path_missingmethod', $this->get_processing_method());
652 }
653 }
654}
655
656
657/**
658 * Exception being thrown by {@link convert_path} methods
659 */
660class convert_path_exception extends moodle_exception {
661
662 /**
663 * Constructor
664 *
665 * @param string $errorcode key for the corresponding error string
666 * @param mixed $a extra words and phrases that might be required by the error string
667 * @param string $debuginfo optional debugging information
668 */
669 public function __construct($errorcode, $a = null, $debuginfo = null) {
670 parent::__construct($errorcode, '', '', $a, $debuginfo);
671 }
672}