MDL-22414 Getting rid of a relict method copied from restore subsystem
[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');
a5fe5912
DM
32require_once($CFG->dirroot . '/backup/util/dbops/backup_dbops.class.php');
33require_once($CFG->dirroot . '/backup/util/dbops/backup_controller_dbops.class.php');
34require_once($CFG->dirroot . '/backup/util/dbops/restore_dbops.class.php');
96f7c7ad 35require_once($CFG->dirroot . '/backup/util/xml/contenttransformer/xml_contenttransformer.class.php');
1e2c7351
DM
36require_once(dirname(__FILE__) . '/handlerlib.php');
37
38/**
39 * Converter of Moodle 1.9 backup into Moodle 2.x format
40 */
41class moodle1_converter extends base_converter {
42
43 /** @var progressive_parser moodle.xml file parser */
44 protected $xmlparser;
45
46 /** @var moodle1_parser_processor */
47 protected $xmlprocessor;
48
49 /** @var array of {@link convert_path} to process */
50 protected $pathelements = array();
51
6cfa5a32
DM
52 /** @var null|string the current module being processed - used to expand the MOD paths */
53 protected $currentmod = null;
1e2c7351 54
6cfa5a32
DM
55 /** @var null|string the current block being processed - used to expand the BLOCK paths */
56 protected $currentblock = null;
1e2c7351
DM
57
58 /** @var string path currently locking processing of children */
59 protected $pathlock;
60
23007e5d
DM
61 /** @var int used by the serial number {@link get_nextid()} */
62 private $nextid = 1;
63
1e2c7351
DM
64 /**
65 * Instructs the dispatcher to ignore all children below path processor returning it
66 */
67 const SKIP_ALL_CHILDREN = -991399;
68
69 /**
70 * Detects the Moodle 1.9 format of the backup directory
71 *
72 * @param string $tempdir the name of the backup directory
73 * @return null|string backup::FORMAT_MOODLE1 if the Moodle 1.9 is detected, null otherwise
74 */
75 public static function detect_format($tempdir) {
76 global $CFG;
77
78 $filepath = $CFG->dataroot . '/temp/backup/' . $tempdir . '/moodle.xml';
79 if (file_exists($filepath)) {
80 // looks promising, lets load some information
81 $handle = fopen($filepath, 'r');
82 $first_chars = fread($handle, 200);
83 fclose($handle);
84
85 // check if it has the required strings
86 if (strpos($first_chars,'<?xml version="1.0" encoding="UTF-8"?>') !== false and
87 strpos($first_chars,'<MOODLE_BACKUP>') !== false and
88 strpos($first_chars,'<INFO>') !== false) {
89
90 return backup::FORMAT_MOODLE1;
91 }
92 }
93
94 return null;
95 }
96
97 /**
98 * Initialize the instance if needed, called by the constructor
99 *
100 * Here we create objects we need before the execution.
101 */
102 protected function init() {
103
104 // ask your mother first before going out playing with toys
105 parent::init();
106
107 // good boy, prepare XML parser and processor
108 $this->xmlparser = new progressive_parser();
109 $this->xmlparser->set_file($this->get_tempdir_path() . '/moodle.xml');
110 $this->xmlprocessor = new moodle1_parser_processor($this);
111 $this->xmlparser->set_processor($this->xmlprocessor);
112
113 // make sure that MOD and BLOCK paths are visited
114 $this->xmlprocessor->add_path('/MOODLE_BACKUP/COURSE/MODULES/MOD');
115 $this->xmlprocessor->add_path('/MOODLE_BACKUP/COURSE/BLOCKS/BLOCK');
116
117 // register the conversion handlers
118 foreach (moodle1_handlers_factory::get_handlers($this) as $handler) {
1e2c7351
DM
119 $this->register_handler($handler, $handler->get_paths());
120 }
121 }
122
123 /**
124 * Converts the contents of the tempdir into the target format in the workdir
125 */
126 protected function execute() {
9b5f1ad5 127 $this->create_stash_storage();
1e2c7351 128 $this->xmlparser->process();
9b5f1ad5 129 $this->drop_stash_storage();
1e2c7351
DM
130 }
131
132 /**
133 * Register a handler for the given path elements
134 */
135 protected function register_handler(moodle1_handler $handler, array $elements) {
136
137 // first iteration, push them to new array, indexed by name
138 // to detect duplicates in names or paths
139 $names = array();
140 $paths = array();
141 foreach($elements as $element) {
142 if (!$element instanceof convert_path) {
143 throw new convert_exception('path_element_wrong_class', get_class($element));
144 }
145 if (array_key_exists($element->get_name(), $names)) {
146 throw new convert_exception('path_element_name_alreadyexists', $element->get_name());
147 }
148 if (array_key_exists($element->get_path(), $paths)) {
149 throw new convert_exception('path_element_path_alreadyexists', $element->get_path());
150 }
151 $names[$element->get_name()] = true;
152 $paths[$element->get_path()] = $element;
153 }
154
155 // now, for each element not having a processing object yet, assign the handler
156 // if the element is not a memeber of a group
157 foreach($paths as $key => $element) {
158 if (is_null($element->get_processing_object()) and !$this->grouped_parent_exists($element, $paths)) {
159 $paths[$key]->set_processing_object($handler);
160 }
161 // add the element path to the processor
162 $this->xmlprocessor->add_path($element->get_path(), $element->is_grouped());
163 }
164
165 // done, store the paths (duplicates by path are discarded)
166 $this->pathelements = array_merge($this->pathelements, $paths);
167
168 // remove the injected plugin name element from the MOD and BLOCK paths
169 // and register such collapsed path, too
170 foreach ($elements as $element) {
171 $path = $element->get_path();
172 $path = preg_replace('/^\/MOODLE_BACKUP\/COURSE\/MODULES\/MOD\/(\w+)\//', '/MOODLE_BACKUP/COURSE/MODULES/MOD/', $path);
173 $path = preg_replace('/^\/MOODLE_BACKUP\/COURSE\/BLOCKS\/BLOCK\/(\w+)\//', '/MOODLE_BACKUP/COURSE/BLOCKS/BLOCK/', $path);
174 if (!empty($path) and $path != $element->get_path()) {
175 $this->xmlprocessor->add_path($path, false);
176 }
177 }
178 }
179
180 /**
181 * Helper method used by {@link self::register_handler()}
182 *
183 * @param convert_path $pelement path element
184 * @param array of convert_path instances
185 * @return bool true if grouped parent was found, false otherwise
186 */
187 protected function grouped_parent_exists($pelement, $elements) {
188
189 foreach ($elements as $element) {
190 if ($pelement->get_path() == $element->get_path()) {
191 // don't compare against itself
192 continue;
193 }
194 // if the element is grouped and it is a parent of pelement, return true
195 if ($element->is_grouped() and strpos($pelement->get_path() . '/', $element->get_path()) === 0) {
196 return true;
197 }
198 }
199
200 // no grouped parent found
201 return false;
202 }
203
204 /**
205 * Process the data obtained from the XML parser processor
206 *
207 * This methods receives one chunk of information from the XML parser
208 * processor and dispatches it, following the naming rules.
209 * We are expanding the modules and blocks paths here to include the plugin's name.
210 *
211 * @param array $data
212 */
213 public function process_chunk($data) {
214
215 $path = $data['path'];
216
217 // expand the MOD paths so that they contain the module name
218 if ($path === '/MOODLE_BACKUP/COURSE/MODULES/MOD') {
219 $this->currentmod = strtoupper($data['tags']['MODTYPE']);
220 $path = '/MOODLE_BACKUP/COURSE/MODULES/MOD/' . $this->currentmod;
221
222 } else if (strpos($path, '/MOODLE_BACKUP/COURSE/MODULES/MOD') === 0) {
223 $path = str_replace('/MOODLE_BACKUP/COURSE/MODULES/MOD', '/MOODLE_BACKUP/COURSE/MODULES/MOD/' . $this->currentmod, $path);
224 }
225
226 // expand the BLOCK paths so that they contain the module name
227 if ($path === '/MOODLE_BACKUP/COURSE/BLOCKS/BLOCK') {
228 $this->currentblock = strtoupper($data['tags']['NAME']);
229 $path = '/MOODLE_BACKUP/COURSE/BLOCKS/BLOCK/' . $this->currentblock;
230
231 } else if (strpos($path, '/MOODLE_BACKUP/COURSE/BLOCKS/BLOCK') === 0) {
232 $path = str_replace('/MOODLE_BACKUP/COURSE/BLOCKS/BLOCK', '/MOODLE_BACKUP/COURSE/BLOCKS/BLOCK/' . $this->currentmod, $path);
233 }
234
235 if ($path !== $data['path']) {
236 if (!array_key_exists($path, $this->pathelements)) {
237 // no handler registered for the transformed MOD or BLOCK path
238 // todo add this event to the convert log instead of debugging
239 //debugging('No handler registered for the path ' . $path);
240 return;
241
242 } else {
243 // pretend as if the original $data contained the tranformed path
244 $data['path'] = $path;
245 }
246 }
247
248 if (!array_key_exists($data['path'], $this->pathelements)) {
249 // path added to the processor without the handler
250 throw new convert_exception('missing_path_handler', $data['path']);
251 }
252
beb7de37
DM
253 $element = $this->pathelements[$data['path']];
254 $object = $element->get_processing_object();
255 $method = $element->get_processing_method();
256 $returned = null; // data returned by the processing method, if any
1e2c7351
DM
257
258 if (empty($object)) {
259 throw new convert_exception('missing_processing_object', $object);
260 }
261
262 // release the lock if we aren't anymore within children of it
263 if (!is_null($this->pathlock) and strpos($data['path'], $this->pathlock) === false) {
264 $this->pathlock = null;
265 }
266
267 // if the path is not locked, apply the element's recipes and dispatch
268 // the cooked tags to the processing method
269 if (is_null($this->pathlock)) {
beb7de37
DM
270 $rawdatatags = $data['tags'];
271 $data['tags'] = $element->apply_recipes($data['tags']);
46ff8b0e
DM
272
273 // if the processing method exists, give it a chance to modify data
274 if (method_exists($object, $method)) {
275 $returned = $object->$method($data['tags'], $rawdatatags);
276 }
1e2c7351
DM
277 }
278
279 // if the dispatched method returned SKIP_ALL_CHILDREN, remember the current path
280 // and lock it so that its children are not dispatched
beb7de37 281 if ($returned === self::SKIP_ALL_CHILDREN) {
1e2c7351
DM
282 // check we haven't any previous lock
283 if (!is_null($this->pathlock)) {
284 throw new convert_exception('already_locked_path', $data['path']);
285 }
286 // set the lock - nothing below the current path will be dispatched
287 $this->pathlock = $data['path'] . '/';
288
289 // if the method has returned any info, set element data to it
beb7de37
DM
290 } else if (!is_null($returned)) {
291 $element->set_data($returned);
1e2c7351
DM
292
293 // use just the cooked parsed data otherwise
294 } else {
295 $element->set_data($data);
296 }
297 }
298
299 /**
300 * Executes operations required at the start of a watched path
301 *
6cfa5a32
DM
302 * For MOD and BLOCK paths, this is supported only for the sub-paths, not the root
303 * module/block element. For the illustration:
304 *
305 * You CAN'T attach on_xxx_start() listener to a path like
306 * /MOODLE_BACKUP/COURSE/MODULES/MOD/WORKSHOP because the <MOD> must
307 * be processed first in {@link self::process_chunk()} where $this->currentmod
308 * is set.
309 *
310 * You CAN attach some on_xxx_start() listener to a path like
311 * /MOODLE_BACKUP/COURSE/MODULES/MOD/WORKSHOP/SUBMISSIONS because it is
312 * a sub-path under <MOD> and we have $this->currentmod already set when the
313 * <SUBMISSIONS> is reached.
1e2c7351 314 *
1e2c7351
DM
315 * @param string $path in the original file
316 */
317 public function path_start_reached($path) {
a5fe5912 318
6cfa5a32
DM
319 if ($path === '/MOODLE_BACKUP/COURSE/MODULES/MOD') {
320 $this->currentmod = null;
321 $forbidden = true;
322
323 } else if (strpos($path, '/MOODLE_BACKUP/COURSE/MODULES/MOD') === 0) {
324 // expand the MOD paths so that they contain the module name
325 $path = str_replace('/MOODLE_BACKUP/COURSE/MODULES/MOD', '/MOODLE_BACKUP/COURSE/MODULES/MOD/' . $this->currentmod, $path);
326 }
327
328 if ($path === '/MOODLE_BACKUP/COURSE/BLOCKS/BLOCK') {
329 $this->currentmod = null;
330 $forbidden = true;
331
332 } else if (strpos($path, '/MOODLE_BACKUP/COURSE/BLOCKS/BLOCK') === 0) {
333 // expand the BLOCK paths so that they contain the module name
334 $path = str_replace('/MOODLE_BACKUP/COURSE/BLOCKS/BLOCK', '/MOODLE_BACKUP/COURSE/BLOCKS/BLOCK/' . $this->currentmod, $path);
335 }
336
a5fe5912
DM
337 if (empty($this->pathelements[$path])) {
338 return;
339 }
340
341 $element = $this->pathelements[$path];
342 $pobject = $element->get_processing_object();
46ff8b0e 343 $method = $element->get_start_method();
a5fe5912
DM
344
345 if (method_exists($pobject, $method)) {
6cfa5a32
DM
346 if (empty($forbidden)) {
347 $pobject->$method();
348
349 } else {
350 // this path is not supported because we do not know the module/block yet
351 throw new coding_exception('Attaching the on-start event listener to the root MOD or BLOCK element is forbidden.');
352 }
a5fe5912 353 }
1e2c7351
DM
354 }
355
356 /**
357 * Executes operations required at the end of a watched path
358 *
1e2c7351
DM
359 * @param string $path in the original file
360 */
361 public function path_end_reached($path) {
a5fe5912
DM
362
363 // expand the MOD paths so that they contain the current module name
364 if ($path === '/MOODLE_BACKUP/COURSE/MODULES/MOD') {
365 $path = '/MOODLE_BACKUP/COURSE/MODULES/MOD/' . $this->currentmod;
366
367 } else if (strpos($path, '/MOODLE_BACKUP/COURSE/MODULES/MOD') === 0) {
368 $path = str_replace('/MOODLE_BACKUP/COURSE/MODULES/MOD', '/MOODLE_BACKUP/COURSE/MODULES/MOD/' . $this->currentmod, $path);
369 }
370
371 // expand the BLOCK paths so that they contain the module name
372 if ($path === '/MOODLE_BACKUP/COURSE/BLOCKS/BLOCK') {
373 $path = '/MOODLE_BACKUP/COURSE/BLOCKS/BLOCK/' . $this->currentblock;
374
375 } else if (strpos($path, '/MOODLE_BACKUP/COURSE/BLOCKS/BLOCK') === 0) {
376 $path = str_replace('/MOODLE_BACKUP/COURSE/BLOCKS/BLOCK', '/MOODLE_BACKUP/COURSE/BLOCKS/BLOCK/' . $this->currentmod, $path);
377 }
378
379 if (empty($this->pathelements[$path])) {
380 return;
381 }
382
383 $element = $this->pathelements[$path];
384 $pobject = $element->get_processing_object();
46ff8b0e 385 $method = $element->get_end_method();
1cc0e42a 386 $data = $element->get_data();
a5fe5912
DM
387
388 if (method_exists($pobject, $method)) {
1cc0e42a 389 $pobject->$method($data['tags']);
a5fe5912
DM
390 }
391 }
392
393 /**
9b5f1ad5 394 * Creates the temporary storage for stashed data
a5fe5912 395 *
9b5f1ad5 396 * This implementation uses backup_ids_temp table.
a5fe5912 397 */
9b5f1ad5
DM
398 public function create_stash_storage() {
399 backup_controller_dbops::create_backup_ids_temp_table($this->get_id());
a5fe5912
DM
400 }
401
402 /**
9b5f1ad5 403 * Drops the temporary storage of stashed data
a5fe5912 404 *
9b5f1ad5 405 * This implementation uses backup_ids_temp table.
a5fe5912 406 */
9b5f1ad5
DM
407 public function drop_stash_storage() {
408 backup_controller_dbops::drop_backup_ids_temp_table($this->get_id());
1e2c7351 409 }
a5fe5912 410
beb7de37 411 /**
9b5f1ad5 412 * Stores some information for later processing
beb7de37 413 *
9b5f1ad5
DM
414 * This implementation uses backup_ids_temp table to store data. Make
415 * sure that the $stashname + $itemid combo is unique.
beb7de37
DM
416 *
417 * @param string $stashname name of the stash
418 * @param mixed $info information to stash
9b5f1ad5 419 * @param int $itemid optional id for multiple infos within the same stashname
beb7de37 420 */
9b5f1ad5
DM
421 public function set_stash($stashname, $info, $itemid = 0) {
422 try {
423 restore_dbops::set_backup_ids_record($this->get_id(), $stashname, $itemid, 0, null, $info);
424
425 } catch (dml_exception $e) {
426 throw new moodle1_convert_storage_exception('unable_to_restore_stash', null, $e->getMessage());
427 }
beb7de37
DM
428 }
429
430 /**
431 * Restores a given stash stored previously by {@link self::set_stash()}
432 *
433 * @param string $stashname name of the stash
9b5f1ad5
DM
434 * @param int $itemid optional id for multiple infos within the same stashname
435 * @throws moodle1_convert_empty_storage_exception if the info has not been stashed previously
beb7de37
DM
436 * @return mixed stashed data
437 */
9b5f1ad5
DM
438 public function get_stash($stashname, $itemid = 0) {
439
440 $record = restore_dbops::get_backup_ids_record($this->get_id(), $stashname, $itemid);
441
442 if (empty($record)) {
6357693c 443 throw new moodle1_convert_empty_storage_exception('required_not_stashed_data', array($stashname, $itemid));
9b5f1ad5
DM
444 } else {
445 return $record->info;
446 }
beb7de37
DM
447 }
448
cd92d83b
DM
449 /**
450 * Returns the list of existing stashes
451 *
452 * @return array
453 */
454 public function get_stash_names() {
455 global $DB;
456
457 $search = array(
458 'backupid' => $this->get_id(),
459 );
460
461 return array_keys($DB->get_records('backup_ids_temp', $search, '', 'itemname'));
462 }
463
6d73f185
DM
464 /**
465 * Returns the list of stashed $itemids in the given stash
466 *
467 * @param string $stashname
468 * @return array
469 */
470 public function get_stash_itemids($stashname) {
471 global $DB;
472
473 $search = array(
474 'backupid' => $this->get_id(),
475 'itemname' => $stashname
476 );
477
478 return array_keys($DB->get_records('backup_ids_temp', $search, '', 'itemid'));
479 }
480
beb7de37
DM
481 /**
482 * Generates an artificial context id
483 *
484 * Moodle 1.9 backups do not contain any context information. But we need them
485 * in Moodle 2.x format so here we generate fictive context id for every given
486 * context level + instance combo.
487 *
26cac34a
DM
488 * CONTEXT_SYSTEM and CONTEXT_COURSE ignore the $instance as they represent a
489 * single system or the course being restored.
490 *
beb7de37
DM
491 * @see get_context_instance()
492 * @param int $level the context level, like CONTEXT_COURSE or CONTEXT_MODULE
493 * @param int $instance the instance id, for example $course->id for courses or $cm->id for activity modules
494 * @return int the context id
495 */
26cac34a 496 public function get_contextid($level, $instance = 0) {
beb7de37 497
9b5f1ad5 498 $stashname = 'context' . $level;
beb7de37 499
26cac34a
DM
500 if ($level == CONTEXT_SYSTEM or $level == CONTEXT_COURSE) {
501 $instance = 0;
502 }
503
d5d02635
DM
504 try {
505 // try the previously stashed id
506 return $this->get_stash($stashname, $instance);
beb7de37 507
d5d02635 508 } catch (moodle1_convert_empty_storage_exception $e) {
beb7de37 509 // this context level + instance is required for the first time
26cac34a
DM
510 $newid = $this->get_nextid();
511 $this->set_stash($stashname, $newid, $instance);
512 return $newid;
9b5f1ad5 513 }
beb7de37 514 }
179982a4 515
6700d288
DM
516 /**
517 * Simple autoincrement generator
518 *
519 * @return int the next number in a row of numbers
520 */
521 public function get_nextid() {
23007e5d 522 return $this->nextid++;
6700d288
DM
523 }
524
66f79e50
DM
525 /**
526 * Creates and returns new instance of the file manager
527 *
528 * @param int $contextid the default context id of the files being migrated
529 * @param string $component the default component name of the files being migrated
530 * @param string $filearea the default file area of the files being migrated
531 * @param int $itemid the default item id of the files being migrated
532 * @param int $userid initial user id of the files being migrated
533 * @return moodle1_file_manager
534 */
535 public function get_file_manager($contextid = null, $component = null, $filearea = null, $itemid = 0, $userid = null) {
536 return new moodle1_file_manager($this, $contextid, $component, $filearea, $itemid, $userid);
537 }
538
179982a4
DM
539 /**
540 * @see parent::description()
541 */
542 public static function description() {
543
544 return array(
545 'from' => backup::FORMAT_MOODLE1,
546 'to' => backup::FORMAT_MOODLE,
547 'cost' => 10,
548 );
549 }
1e2c7351
DM
550}
551
552
9b5f1ad5
DM
553/**
554 * Exception thrown by this converter
555 */
556class moodle1_convert_exception extends convert_exception {
557}
558
559
560/**
561 * Exception thrown by the temporary storage subsystem of moodle1_converter
562 */
563class moodle1_convert_storage_exception extends moodle1_convert_exception {
564}
565
566
567/**
568 * Exception thrown by the temporary storage subsystem of moodle1_converter
569 */
570class moodle1_convert_empty_storage_exception extends moodle1_convert_exception {
571}
572
573
1e2c7351 574/**
96f7c7ad 575 * XML parser processor used for processing parsed moodle.xml
1e2c7351
DM
576 */
577class moodle1_parser_processor extends grouped_parser_processor {
578
579 /** @var moodle1_converter */
580 protected $converter;
581
582 public function __construct(moodle1_converter $converter) {
583 $this->converter = $converter;
584 parent::__construct();
585 }
586
587 /**
8312ab67
DM
588 * Provides NULL decoding
589 *
590 * Note that we do not decode $@FILEPHP@$ and friends here as we are going to write them
591 * back immediately into another XML file.
1e2c7351
DM
592 */
593 public function process_cdata($cdata) {
1e2c7351 594
8312ab67 595 if ($cdata === '$@NULL@$') {
1e2c7351 596 return null;
1e2c7351 597 }
8312ab67
DM
598
599 return $cdata;
1e2c7351
DM
600 }
601
1e2c7351
DM
602 /**
603 * Dispatches the data chunk to the converter class
604 *
605 * @param array $data the chunk of parsed data
606 */
607 protected function dispatch_chunk($data) {
608 $this->converter->process_chunk($data);
609 }
610
611 /**
612 * Informs the converter at the start of a watched path
613 *
614 * @param string $path
615 */
616 protected function notify_path_start($path) {
617 $this->converter->path_start_reached($path);
618 }
619
620 /**
621 * Informs the converter at the end of a watched path
622 *
623 * @param string $path
624 */
625 protected function notify_path_end($path) {
626 $this->converter->path_end_reached($path);
627 }
628}
629
630
96f7c7ad
DM
631/**
632 * XML transformer that modifies the content of the files being written during the conversion
633 *
634 * @see backup_xml_transformer
635 */
636class moodle1_xml_transformer extends xml_contenttransformer {
637
638 /**
639 * Modify the content before it is writter to a file
640 *
641 * @param string|mixed $content
642 */
643 public function process($content) {
644
645 // the content should be a string. If array or object is given, try our best recursively
646 // but inform the developer
647 if (is_array($content)) {
648 debugging('Moodle1 XML transformer should not process arrays but plain content always', DEBUG_DEVELOPER);
649 foreach($content as $key => $plaincontent) {
650 $content[$key] = $this->process($plaincontent);
651 }
652 return $content;
653
654 } else if (is_object($content)) {
655 debugging('Moodle1 XML transformer should not process objects but plain content always', DEBUG_DEVELOPER);
656 foreach((array)$content as $key => $plaincontent) {
657 $content[$key] = $this->process($plaincontent);
658 }
659 return (object)$content;
660 }
661
662 // try to deal with some trivial cases first
663 if (is_null($content)) {
664 return '$@NULL@$';
665
666 } else if ($content === '') {
667 return '';
668
669 } else if (is_numeric($content)) {
670 return $content;
671
672 } else if (strlen($content) < 32) {
673 return $content;
674 }
675
96f7c7ad
DM
676 return $content;
677 }
678}
679
680
1e2c7351
DM
681/**
682 * Class representing a path to be converted from XML file
683 *
684 * This was created as a copy of {@link restore_path_element} and should be refactored
685 * probably.
686 */
687class convert_path {
688
689 /** @var string name of the element */
690 protected $name;
691
692 /** @var string path within the XML file this element will handle */
693 protected $path;
694
695 /** @var bool flag to define if this element will get child ones grouped or no */
696 protected $grouped;
697
698 /** @var object object instance in charge of processing this element. */
699 protected $pobject = null;
700
701 /** @var string the name of the processing method */
702 protected $pmethod = null;
703
46ff8b0e
DM
704 /** @var string the name of the path start event handler */
705 protected $smethod = null;
706
707 /** @var string the name of the path end event handler */
708 protected $emethod = null;
709
1e2c7351
DM
710 /** @var mixed last data read for this element or returned data by processing method */
711 protected $data = null;
712
a5fe5912
DM
713 /** @var array of deprecated fields that are dropped */
714 protected $dropfields = array();
1e2c7351
DM
715
716 /** @var array of fields renaming */
717 protected $renamefields = array();
718
719 /** @var array of new fields to add and their initial values */
720 protected $newfields = array();
721
722 /**
723 * Constructor
724 *
725 * @param string $name name of the element
726 * @param string $path path of the element
727 * @param array $recipe basic description of the structure conversion
728 * @param bool $grouped to gather information in grouped mode or no
729 */
730 public function __construct($name, $path, array $recipe = array(), $grouped = false) {
731
732 $this->validate_name($name);
733
734 $this->name = $name;
735 $this->path = $path;
736 $this->grouped = $grouped;
737
46ff8b0e 738 // set the default method names
1e2c7351 739 $this->set_processing_method('process_' . $name);
46ff8b0e
DM
740 $this->set_start_method('on_'.$name.'_start');
741 $this->set_end_method('on_'.$name.'_end');
1e2c7351 742
034b0e4a
DM
743 if ($grouped and !empty($recipe)) {
744 throw new convert_path_exception('recipes_not_supported_for_grouped_elements');
745 }
746
a5fe5912
DM
747 if (isset($recipe['dropfields']) and is_array($recipe['dropfields'])) {
748 $this->set_dropped_fields($recipe['dropfields']);
1e2c7351
DM
749 }
750 if (isset($recipe['renamefields']) and is_array($recipe['renamefields'])) {
751 $this->set_renamed_fields($recipe['renamefields']);
752 }
753 if (isset($recipe['newfields']) and is_array($recipe['newfields'])) {
754 $this->set_new_fields($recipe['newfields']);
755 }
756 }
757
758 /**
759 * Validates and sets the given processing object
760 *
761 * @param object $pobject processing object, must provide a method to be called
762 */
763 public function set_processing_object($pobject) {
764 $this->validate_pobject($pobject);
765 $this->pobject = $pobject;
766 }
767
768 /**
769 * Sets the name of the processing method
770 *
771 * @param string $pmethod
772 */
773 public function set_processing_method($pmethod) {
774 $this->pmethod = $pmethod;
775 }
776
46ff8b0e
DM
777 /**
778 * Sets the name of the path start event listener
779 *
780 * @param string $smethod
781 */
782 public function set_start_method($smethod) {
783 $this->smethod = $smethod;
784 }
785
786 /**
787 * Sets the name of the path end event listener
788 *
789 * @param string $emethod
790 */
791 public function set_end_method($emethod) {
792 $this->emethod = $emethod;
793 }
794
1e2c7351
DM
795 /**
796 * Sets the element data
797 *
798 * @param mixed
799 */
800 public function set_data($data) {
801 $this->data = $data;
802 }
803
804 /**
a5fe5912 805 * Sets the list of deprecated fields to drop
1e2c7351
DM
806 *
807 * @param array $fields
808 */
a5fe5912
DM
809 public function set_dropped_fields(array $fields) {
810 $this->dropfields = $fields;
1e2c7351
DM
811 }
812
813 /**
814 * Sets the required new names of the current fields
815 *
816 * @param array $fields (string)$currentname => (string)$newname
817 */
818 public function set_renamed_fields(array $fields) {
819 $this->renamefields = $fields;
820 }
821
822 /**
823 * Sets the new fields and their values
824 *
825 * @param array $fields (string)$field => (mixed)value
826 */
827 public function set_new_fields(array $fields) {
828 $this->newfields = $fields;
829 }
830
831 /**
832 * Cooks the parsed tags data by applying known recipes
833 *
834 * Recipes are used for common trivial operations like adding new fields
835 * or renaming fields. The handler's processing method receives cooked
836 * data.
837 *
838 * @param array $data the contents of the element
839 * @return array
840 */
841 public function apply_recipes(array $data) {
842
843 $cooked = array();
844
845 foreach ($data as $name => $value) {
846 // lower case rocks!
847 $name = strtolower($name);
848
034b0e4a
DM
849 if (is_array($value)) {
850 if ($this->is_grouped()) {
851 $value = $this->apply_recipes($value);
852 } else {
853 throw new convert_path_exception('non_grouped_path_with_array_values');
854 }
855 }
856
a5fe5912
DM
857 // drop legacy fields
858 if (in_array($name, $this->dropfields)) {
859 continue;
860 }
861
1e2c7351
DM
862 // fields renaming
863 if (array_key_exists($name, $this->renamefields)) {
864 $name = $this->renamefields[$name];
865 }
866
867 $cooked[$name] = $value;
868 }
869
870 // adding new fields
871 foreach ($this->newfields as $name => $value) {
872 $cooked[$name] = $value;
873 }
874
875 return $cooked;
876 }
877
878 /**
879 * @return string the element given name
880 */
881 public function get_name() {
882 return $this->name;
883 }
884
885 /**
886 * @return string the path to the element
887 */
888 public function get_path() {
889 return $this->path;
890 }
891
892 /**
893 * @return bool flag to define if this element will get child ones grouped or no
894 */
895 public function is_grouped() {
896 return $this->grouped;
897 }
898
899 /**
900 * @return object the processing object providing the processing method
901 */
902 public function get_processing_object() {
903 return $this->pobject;
904 }
905
906 /**
907 * @return string the name of the method to call to process the element
908 */
909 public function get_processing_method() {
910 return $this->pmethod;
911 }
912
46ff8b0e
DM
913 /**
914 * @return string the name of the path start event listener
915 */
916 public function get_start_method() {
917 return $this->smethod;
918 }
919
920 /**
921 * @return string the name of the path end event listener
922 */
923 public function get_end_method() {
924 return $this->emethod;
925 }
926
1e2c7351
DM
927 /**
928 * @return mixed the element data
929 */
930 public function get_data() {
931 return $this->data;
932 }
933
934
935 /// end of public API //////////////////////////////////////////////////////
936
937 /**
938 * Makes sure the given name is a valid element name
939 *
940 * Note it may look as if we used exceptions for code flow control here. That's not the case
941 * as we actually validate the code, not the user data. And the code is supposed to be
942 * correct.
943 *
944 * @param string @name the element given name
945 * @throws convert_path_exception
946 * @return void
947 */
948 protected function validate_name($name) {
949 // Validate various name constraints, throwing exception if needed
950 if (empty($name)) {
951 throw new convert_path_exception('convert_path_emptyname', $name);
952 }
953 if (preg_replace('/\s/', '', $name) != $name) {
954 throw new convert_path_exception('convert_path_whitespace', $name);
955 }
956 if (preg_replace('/[^\x30-\x39\x41-\x5a\x5f\x61-\x7a]/', '', $name) != $name) {
957 throw new convert_path_exception('convert_path_notasciiname', $name);
958 }
959 }
960
961 /**
962 * Makes sure that the given object is a valid processing object
963 *
46ff8b0e
DM
964 * The processing object must be an object providing at least element's processing method
965 * or path-reached-end event listener or path-reached-start listener method.
966 *
1e2c7351
DM
967 * Note it may look as if we used exceptions for code flow control here. That's not the case
968 * as we actually validate the code, not the user data. And the code is supposed to be
969 * correct.
970 *
971 * @param object $pobject
972 * @throws convert_path_exception
973 * @return void
974 */
975 protected function validate_pobject($pobject) {
976 if (!is_object($pobject)) {
46ff8b0e 977 throw new convert_path_exception('convert_path_no_object', get_class($pobject));
1e2c7351 978 }
46ff8b0e
DM
979 if (!method_exists($pobject, $this->get_processing_method()) and
980 !method_exists($pobject, $this->get_end_method()) and
981 !method_exists($pobject, $this->get_start_method())) {
982 throw new convert_path_exception('convert_path_missing_method', get_class($pobject));
1e2c7351
DM
983 }
984 }
985}
986
987
988/**
989 * Exception being thrown by {@link convert_path} methods
990 */
991class convert_path_exception extends moodle_exception {
992
993 /**
994 * Constructor
995 *
996 * @param string $errorcode key for the corresponding error string
997 * @param mixed $a extra words and phrases that might be required by the error string
998 * @param string $debuginfo optional debugging information
999 */
1000 public function __construct($errorcode, $a = null, $debuginfo = null) {
1001 parent::__construct($errorcode, '', '', $a, $debuginfo);
1002 }
1003}
66f79e50
DM
1004
1005
1006/**
1007 * The class responsible for files migration
1008 *
1009 * The files in Moodle 1.9 backup are stored in moddata, user_files, group_files,
1010 * course_files and site_files folders.
1011 */
1012class moodle1_file_manager {
1013
1014 /** @var moodle1_converter instance we serve to */
1015 public $converter;
1016
1017 /** @var int context id of the files being migrated */
1018 public $contextid;
1019
1020 /** @var string component name of the files being migrated */
1021 public $component;
1022
1023 /** @var string file area of the files being migrated */
1024 public $filearea;
1025
1026 /** @var int item id of the files being migrated */
1027 public $itemid = 0;
1028
1029 /** @var int user id */
1030 public $userid;
1031
214c4924
DM
1032 /** @var string the root of the converter temp directory */
1033 protected $basepath;
1034
66f79e50
DM
1035 /** @var textlib instance used during the migration */
1036 protected $textlib;
1037
1038 /** @var array of file ids that were migrated by this instance */
1039 protected $fileids = array();
1040
1041 /**
1042 * Constructor optionally accepting some default values for the migrated files
1043 *
1044 * @param moodle1_converter $converter the converter instance we serve to
1045 * @param int $contextid initial context id of the files being migrated
1046 * @param string $component initial component name of the files being migrated
1047 * @param string $filearea initial file area of the files being migrated
1048 * @param int $itemid initial item id of the files being migrated
1049 * @param int $userid initial user id of the files being migrated
1050 */
1051 public function __construct(moodle1_converter $converter, $contextid = null, $component = null, $filearea = null, $itemid = 0, $userid = null) {
214c4924 1052 // set the initial destination of the migrated files
66f79e50
DM
1053 $this->converter = $converter;
1054 $this->contextid = $contextid;
1055 $this->component = $component;
1056 $this->filearea = $filearea;
1057 $this->itemid = $itemid;
1058 $this->userid = $userid;
214c4924
DM
1059 // set other useful bits
1060 $this->basepath = $converter->get_tempdir_path();
66f79e50
DM
1061 $this->textlib = textlib_get_instance();
1062 }
1063
1064 /**
1065 * Migrates one given file stored on disk
1066 *
214c4924 1067 * @param string $sourcepath the path to the source local file within the backup archive {@example 'moddata/foobar/file.ext'}
66f79e50
DM
1068 * @param string $filepath the file path of the migrated file, defaults to the root directory '/'
1069 * @param string $filename the name of the migrated file, defaults to the same as the source file has
1070 * @param int $timecreated override the timestamp of when the migrated file should appear as created
1071 * @param int $timemodified override the timestamp of when the migrated file should appear as modified
1072 * @return int id of the migrated file
1073 */
214c4924
DM
1074 public function migrate_file($sourcepath, $filepath = '/', $filename = null, $timecreated = null, $timemodified = null) {
1075
1076 $sourcefullpath = $this->basepath.'/'.$sourcepath;
66f79e50
DM
1077
1078 if (!is_readable($sourcefullpath)) {
214c4924 1079 throw new moodle1_convert_exception('file_not_readable', $sourcefullpath);
66f79e50
DM
1080 }
1081
1082 $filepath = clean_param($filepath, PARAM_PATH);
1083
1084 if ($this->textlib->strlen($filepath) > 255) {
1085 throw new moodle1_convert_exception('file_path_longer_than_255_chars');
1086 }
1087
1088 if (is_null($filename)) {
1089 $filename = basename($sourcefullpath);
1090 }
1091
1092 $filename = clean_param($filename, PARAM_FILE);
1093
1094 if ($filename === '') {
1095 throw new moodle1_convert_exception('unsupported_chars_in_filename');
1096 }
1097
1098 if (is_null($timecreated)) {
1099 $timecreated = filectime($sourcefullpath);
1100 }
1101
1102 if (is_null($timemodified)) {
1103 $timemodified = filemtime($sourcefullpath);
1104 }
1105
1106 $filerecord = $this->make_file_record(array(
1107 'filepath' => $filepath,
1108 'filename' => $filename,
1109 'mimetype' => mimeinfo('type', $sourcefullpath),
1110 'timecreated' => $timecreated,
1111 'timemodified' => $timemodified,
1112 ));
1113
1114 list($filerecord['contenthash'], $filerecord['filesize'], $newfile) = $this->add_file_to_pool($sourcefullpath);
1115 $this->stash_file($filerecord);
1116
1117 return $filerecord['id'];
1118 }
1119
1120 /**
1121 * Migrates all files in the given directory
1122 *
214c4924 1123 * @param string $rootpath path within the backup archive to the root directory containing the files {@example 'course_files'}
66f79e50 1124 * @param string $relpath relative path used during the recursion - do not provide when calling this!
93264625 1125 * @return array ids of the migrated files, empty array if the $rootpath not found
66f79e50
DM
1126 */
1127 public function migrate_directory($rootpath, $relpath='/') {
1128
93264625
DM
1129 if (!file_exists($this->basepath.'/'.$rootpath.$relpath)) {
1130 return array();
1131 }
1132
66f79e50
DM
1133 $fileids = array();
1134
1135 // make the fake file record for the directory itself
1136 $filerecord = $this->make_file_record(array('filepath' => $relpath, 'filename' => '.'));
1137 $this->stash_file($filerecord);
1138 $fileids[] = $filerecord['id'];
1139
214c4924 1140 $items = new DirectoryIterator($this->basepath.'/'.$rootpath.$relpath);
66f79e50
DM
1141
1142 foreach ($items as $item) {
1143
1144 if ($item->isDot()) {
1145 continue;
1146 }
1147
1148 if ($item->isLink()) {
1149 throw new moodle1_convert_exception('unexpected_symlink');
1150 }
1151
1152 if ($item->isFile()) {
214c4924
DM
1153 $fileids[] = $this->migrate_file(substr($item->getPathname(), strlen($this->basepath.'/')),
1154 $relpath, $item->getFilename(), $item->getCTime(), $item->getMTime());
66f79e50
DM
1155
1156 } else {
1157 $dirname = clean_param($item->getFilename(), PARAM_PATH);
1158
1159 if ($dirname === '') {
1160 throw new moodle1_convert_exception('unsupported_chars_in_filename');
1161 }
1162
1163 // migrate subdirectories recursively
1164 $fileids = array_merge($fileids, $this->migrate_directory($rootpath, $relpath.$item->getFilename().'/'));
1165 }
1166 }
1167
1168 return $fileids;
1169 }
1170
1171 /**
1172 * Returns the list of all file ids migrated by this instance so far
1173 *
1174 * @return array of int
1175 */
1176 public function get_fileids() {
1177 return $this->fileids;
1178 }
1179
1180 /// internal implementation details ////////////////////////////////////////
1181
1182 /**
1183 * Prepares a fake record from the files table
1184 *
1185 * @param array $fileinfo explicit file data
1186 * @return array
1187 */
1188 protected function make_file_record(array $fileinfo) {
1189
1190 $defaultrecord = array(
1191 'contenthash' => 'da39a3ee5e6b4b0d3255bfef95601890afd80709', // sha1 of an empty file
1192 'contextid' => $this->contextid,
1193 'component' => $this->component,
1194 'filearea' => $this->filearea,
1195 'itemid' => $this->itemid,
1196 'filepath' => null,
1197 'filename' => null,
1198 'filesize' => 0,
1199 'userid' => $this->userid,
1200 'mimetype' => null,
1201 'status' => 0,
1202 'timecreated' => $now = time(),
1203 'timemodified' => $now,
1204 'source' => null,
1205 'author' => null,
1206 'license' => null,
1207 'sortorder' => 0,
1208 );
1209
1210 if (!array_key_exists('id', $fileinfo)) {
1211 $defaultrecord['id'] = $this->converter->get_nextid();
1212 }
1213
1214 // override the default values with the explicit data provided and return
1215 return array_merge($defaultrecord, $fileinfo);
1216 }
1217
1218 /**
1219 * Copies the given file to the pool directory
1220 *
1221 * Returns an array containing SHA1 hash of the file contents, the file size
1222 * and a flag indicating whether the file was actually added to the pool or whether
1223 * it was already there.
1224 *
1225 * @param string $pathname the full path to the file
1226 * @return array with keys (string)contenthash, (int)filesize, (bool)newfile
1227 */
1228 protected function add_file_to_pool($pathname) {
1229
1230 if (!is_readable($pathname)) {
1231 throw new moodle1_convert_exception('file_not_readable');
1232 }
1233
1234 $contenthash = sha1_file($pathname);
1235 $filesize = filesize($pathname);
1236 $hashpath = $this->converter->get_workdir_path().'/files/'.substr($contenthash, 0, 2);
1237 $hashfile = "$hashpath/$contenthash";
1238
1239 if (file_exists($hashfile)) {
1240 if (filesize($hashfile) !== $filesize) {
1241 // congratulations! you have found two files with different size and the same
1242 // content hash. or, something were wrong (which is more likely)
1243 throw new moodle1_convert_exception('same_hash_different_size');
1244 }
1245 $newfile = false;
1246
1247 } else {
1248 check_dir_exists($hashpath);
1249 $newfile = true;
1250
1251 if (!copy($pathname, $hashfile)) {
1252 throw new moodle1_convert_exception('unable_to_copy_file');
1253 }
1254
1255 if (filesize($hashfile) !== $filesize) {
1256 throw new moodle1_convert_exception('filesize_different_after_copy');
1257 }
1258 }
1259
1260 return array($contenthash, $filesize, $newfile);
1261 }
1262
1263 /**
1264 * Stashes the file record into 'files' stash and adds the record id to list of migrated files
1265 *
1266 * @param array $filerecord
1267 */
1268 protected function stash_file(array $filerecord) {
1269 $this->converter->set_stash('files', $filerecord, $filerecord['id']);
1270 $this->fileids[] = $filerecord['id'];
1271 }
1272}