Apply parser processor new methods + observe $CFG->keeptempdirectoriesonbackup
[moodle.git] / backup / util / helper / convert_helper.class.php
CommitLineData
17252e2d
MN
1<?php
2/**
3 * General Convert Helper
4 */
5abstract class convert_helper {
6 public static function generate_id($entropy) {
7 return md5(time() . '-' . $entropy . '-' . random_string(20));
8 }
c5c8b350
MN
9
10 /**
11 * @static
12 * @throws coding_exception|restore_controller_exception
13 * @param string $tempdir The directory to convert
14 * @param string $format The current format, if already detected
15 * @return void
16 */
17 public static function to_moodle2_format($tempdir, $format = NULL) {
18 if (is_null($format)) {
19 $format = backup_general_helper::detect_backup_format($tempdir);
20 }
21 while (!in_array($format, array(backup::FORMAT_MOODLE, backup::FORMAT_UNKNOWN))) {
22 $converter = convert_factory::converter($format, $tempdir);
23
24 if (!$converter->can_convert()) {
25 throw new coding_exception('Converter detection failed, the loaded converter cannot convert this format');
26 }
27 $converter->convert();
28
29 // Re-detect format
30 $format = backup_general_helper::detect_backup_format($tempdir);
31 }
32 if ($format == backup::FORMAT_UNKNOWN) {
33 throw new restore_controller_exception('cannot_convert_from_unknown_format'); // @todo Change exception class
34 }
35 }
142ec51f
PC
36
37 /**
38 * Inserts an inforef into the conversion temp table
39 */
40 public static function set_inforef($contextid) {
41 global $DB;
eba6af75
MN
42
43
142ec51f
PC
44 }
45
46 public static function get_inforef($contextid) {
47 }
48
49 /**
50 * Converts a plain old php object (popo?) into a string...
51 * Useful for debuging failed db inserts, or anything like that
52 */
53 public static function obj_to_readable($obj) {
54 $mapper = function($field, $value) { return "$field=$value"; };
55 $fields = get_object_vars($obj);
56
57 return implode(", ", array_map($mapper, array_keys($fields), array_values($fields)));
58 }
59
6c0235b3
MN
60 /**
61 * Generate an artificial context ID
62 *
63 * @static
64 * @throws Exception
65 * @param int $instance The moodle component instance ID, same value used for get_context_instance()
66 * @param string $component The moodle component, like block_html, mod_quiz, etc
67 * @param string $converterid The converter ID
68 * @return int
62c5daf1
MN
69 * @todo Add caching?
70 * @todo Can we make the lookup faster? Not taking advantage of indexes
6c0235b3 71 */
eba6af75 72 public static function get_contextid($instance, $component = 'moodle', $converterid = NULL) {
142ec51f
PC
73 global $DB;
74
75 // Attempt to retrieve the contextid
56bd1ab0
MN
76 $contextid = $DB->get_field_select('backup_ids_temp', 'id',
77 $DB->sql_compare_text('info', 100).' = ? AND itemid = ? AND itemname = ?',
78 array($component, $instance, 'context')
79 );
eba6af75 80
e10e8879
MN
81 if (!empty($contextid)) {
82 return $contextid;
142ec51f
PC
83 }
84
85 $context = new stdClass;
eba6af75 86 $context->itemid = $instance;
142ec51f 87 $context->itemname = 'context';
eba6af75
MN
88 $context->info = $component;
89
90 if (!is_null($converterid)) {
91 $context->backupid = $converterid;
92 }
56bd1ab0 93 if ($id = $DB->insert_record('backup_ids_temp', $context)) {
142ec51f
PC
94 return $id;
95 } else {
96 $msg = self::obj_to_readable($context);
97 throw new Exception(sprintf("Could not insert context record into temp table: %s", $msg));
eba6af75 98 }
142ec51f
PC
99 }
100}