MDL-40584 backup: Query db once per category in precheck
[moodle.git] / backup / util / dbops / restore_dbops.class.php
1 <?php
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/>.
18 /**
19  * @package    moodlecore
20  * @subpackage backup-dbops
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  */
25 /**
26  * Base abstract class for all the helper classes providing DB operations
27  *
28  * TODO: Finish phpdocs
29  */
30 abstract class restore_dbops {
31     /**
32      * Keep cache of backup records.
33      * @var array
34      * @todo MDL-25290 static should be replaced with MUC code.
35      */
36     private static $backupidscache = array();
37     /**
38      * Keep track of backup ids which are cached.
39      * @var array
40      * @todo MDL-25290 static should be replaced with MUC code.
41      */
42     private static $backupidsexist = array();
43     /**
44      * Count is expensive, so manually keeping track of
45      * backupidscache, to avoid memory issues.
46      * @var int
47      * @todo MDL-25290 static should be replaced with MUC code.
48      */
49     private static $backupidscachesize = 2048;
50     /**
51      * Count is expensive, so manually keeping track of
52      * backupidsexist, to avoid memory issues.
53      * @var int
54      * @todo MDL-25290 static should be replaced with MUC code.
55      */
56     private static $backupidsexistsize = 10240;
57     /**
58      * Slice backupids cache to add more data.
59      * @var int
60      * @todo MDL-25290 static should be replaced with MUC code.
61      */
62     private static $backupidsslice = 512;
64     /**
65      * Return one array containing all the tasks that have been included
66      * in the restore process. Note that these tasks aren't built (they
67      * haven't steps nor ids data available)
68      */
69     public static function get_included_tasks($restoreid) {
70         $rc = restore_controller_dbops::load_controller($restoreid);
71         $tasks = $rc->get_plan()->get_tasks();
72         $includedtasks = array();
73         foreach ($tasks as $key => $task) {
74             // Calculate if the task is being included
75             $included = false;
76             // blocks, based in blocks setting and parent activity/course
77             if ($task instanceof restore_block_task) {
78                 if (!$task->get_setting_value('blocks')) { // Blocks not included, continue
79                     continue;
80                 }
81                 $parent = basename(dirname(dirname($task->get_taskbasepath())));
82                 if ($parent == 'course') { // Parent is course, always included if present
83                     $included = true;
85                 } else { // Look for activity_included setting
86                     $included = $task->get_setting_value($parent . '_included');
87                 }
89             // ativities, based on included setting
90             } else if ($task instanceof restore_activity_task) {
91                 $included = $task->get_setting_value('included');
93             // sections, based on included setting
94             } else if ($task instanceof restore_section_task) {
95                 $included = $task->get_setting_value('included');
97             // course always included if present
98             } else if ($task instanceof restore_course_task) {
99                 $included = true;
100             }
102             // If included, add it
103             if ($included) {
104                 $includedtasks[] = $task;
105             }
106         }
107         return $includedtasks;
108     }
110     /**
111      * Load one inforef.xml file to backup_ids table for future reference
112      */
113     public static function load_inforef_to_tempids($restoreid, $inforeffile) {
115         if (!file_exists($inforeffile)) { // Shouldn't happen ever, but...
116             throw new backup_helper_exception('missing_inforef_xml_file', $inforeffile);
117         }
118         // Let's parse, custom processor will do its work, sending info to DB
119         $xmlparser = new progressive_parser();
120         $xmlparser->set_file($inforeffile);
121         $xmlprocessor = new restore_inforef_parser_processor($restoreid);
122         $xmlparser->set_processor($xmlprocessor);
123         $xmlparser->process();
124     }
126     /**
127      * Load the needed role.xml file to backup_ids table for future reference
128      */
129     public static function load_roles_to_tempids($restoreid, $rolesfile) {
131         if (!file_exists($rolesfile)) { // Shouldn't happen ever, but...
132             throw new backup_helper_exception('missing_roles_xml_file', $rolesfile);
133         }
134         // Let's parse, custom processor will do its work, sending info to DB
135         $xmlparser = new progressive_parser();
136         $xmlparser->set_file($rolesfile);
137         $xmlprocessor = new restore_roles_parser_processor($restoreid);
138         $xmlparser->set_processor($xmlprocessor);
139         $xmlparser->process();
140     }
142     /**
143      * Precheck the loaded roles, return empty array if everything is ok, and
144      * array with 'errors', 'warnings' elements (suitable to be used by restore_prechecks)
145      * with any problem found. At the same time, store all the mapping into backup_ids_temp
146      * and also put the information into $rolemappings (controller->info), so it can be reworked later by
147      * post-precheck stages while at the same time accept modified info in the same object coming from UI
148      */
149     public static function precheck_included_roles($restoreid, $courseid, $userid, $samesite, $rolemappings) {
150         global $DB;
152         $problems = array(); // To store warnings/errors
154         // Get loaded roles from backup_ids
155         $rs = $DB->get_recordset('backup_ids_temp', array('backupid' => $restoreid, 'itemname' => 'role'), '', 'itemid');
156         foreach ($rs as $recrole) {
157             // If the rolemappings->modified flag is set, that means that we are coming from
158             // manually modified mappings (by UI), so accept those mappings an put them to backup_ids
159             if ($rolemappings->modified) {
160                 $target = $rolemappings->mappings[$recrole->itemid]->targetroleid;
161                 self::set_backup_ids_record($restoreid, 'role', $recrole->itemid, $target);
163             // Else, we haven't any info coming from UI, let's calculate the mappings, matching
164             // in multiple ways and checking permissions. Note mapping to 0 means "skip"
165             } else {
166                 $role = (object)self::get_backup_ids_record($restoreid, 'role', $recrole->itemid)->info;
167                 $match = self::get_best_assignable_role($role, $courseid, $userid, $samesite);
168                 // Send match to backup_ids
169                 self::set_backup_ids_record($restoreid, 'role', $recrole->itemid, $match);
170                 // Build the rolemappings element for controller
171                 unset($role->id);
172                 unset($role->nameincourse);
173                 unset($role->nameincourse);
174                 $role->targetroleid = $match;
175                 $rolemappings->mappings[$recrole->itemid] = $role;
176                 // Prepare warning if no match found
177                 if (!$match) {
178                     $problems['warnings'][] = get_string('cannotfindassignablerole', 'backup', $role->name);
179                 }
180             }
181         }
182         $rs->close();
183         return $problems;
184     }
186     /**
187      * Return cached backup id's
188      *
189      * @param int $restoreid id of backup
190      * @param string $itemname name of the item
191      * @param int $itemid id of item
192      * @return array backup id's
193      * @todo MDL-25290 replace static backupids* with MUC code
194      */
195     protected static function get_backup_ids_cached($restoreid, $itemname, $itemid) {
196         global $DB;
198         $key = "$itemid $itemname $restoreid";
200         // If record exists in cache then return.
201         if (isset(self::$backupidsexist[$key]) && isset(self::$backupidscache[$key])) {
202             // Return a copy of cached data, to avoid any alterations in cached data.
203             return clone self::$backupidscache[$key];
204         }
206         // Clean cache, if it's full.
207         if (self::$backupidscachesize <= 0) {
208             // Remove some records, to keep memory in limit.
209             self::$backupidscache = array_slice(self::$backupidscache, self::$backupidsslice, null, true);
210             self::$backupidscachesize = self::$backupidscachesize + self::$backupidsslice;
211         }
212         if (self::$backupidsexistsize <= 0) {
213             self::$backupidsexist = array_slice(self::$backupidsexist, self::$backupidsslice, null, true);
214             self::$backupidsexistsize = self::$backupidsexistsize + self::$backupidsslice;
215         }
217         // Retrive record from database.
218         $record = array(
219             'backupid' => $restoreid,
220             'itemname' => $itemname,
221             'itemid'   => $itemid
222         );
223         if ($dbrec = $DB->get_record('backup_ids_temp', $record)) {
224             self::$backupidsexist[$key] = $dbrec->id;
225             self::$backupidscache[$key] = $dbrec;
226             self::$backupidscachesize--;
227             self::$backupidsexistsize--;
228             return $dbrec;
229         } else {
230             return false;
231         }
232     }
234     /**
235      * Cache backup ids'
236      *
237      * @param int $restoreid id of backup
238      * @param string $itemname name of the item
239      * @param int $itemid id of item
240      * @param array $extrarecord extra record which needs to be updated
241      * @return void
242      * @todo MDL-25290 replace static BACKUP_IDS_* with MUC code
243      */
244     protected static function set_backup_ids_cached($restoreid, $itemname, $itemid, $extrarecord) {
245         global $DB;
247         $key = "$itemid $itemname $restoreid";
249         $record = array(
250             'backupid' => $restoreid,
251             'itemname' => $itemname,
252             'itemid'   => $itemid,
253         );
255         // If record is not cached then add one.
256         if (!isset(self::$backupidsexist[$key])) {
257             // If we have this record in db, then just update this.
258             if ($existingrecord = $DB->get_record('backup_ids_temp', $record)) {
259                 self::$backupidsexist[$key] = $existingrecord->id;
260                 self::$backupidsexistsize--;
261                 self::update_backup_cached_record($record, $extrarecord, $key, $existingrecord);
262             } else {
263                 // Add new record to cache and db.
264                 $recorddefault = array (
265                     'newitemid' => 0,
266                     'parentitemid' => null,
267                     'info' => null);
268                 $record = array_merge($record, $recorddefault, $extrarecord);
269                 $record['id'] = $DB->insert_record('backup_ids_temp', $record);
270                 self::$backupidsexist[$key] = $record['id'];
271                 self::$backupidsexistsize--;
272                 if (self::$backupidscachesize > 0) {
273                     // Cache new records if we haven't got many yet.
274                     self::$backupidscache[$key] = (object) $record;
275                     self::$backupidscachesize--;
276                 }
277             }
278         } else {
279             self::update_backup_cached_record($record, $extrarecord, $key);
280         }
281     }
283     /**
284      * Updates existing backup record
285      *
286      * @param array $record record which needs to be updated
287      * @param array $extrarecord extra record which needs to be updated
288      * @param string $key unique key which is used to identify cached record
289      * @param stdClass $existingrecord (optional) existing record
290      */
291     protected static function update_backup_cached_record($record, $extrarecord, $key, $existingrecord = null) {
292         global $DB;
293         // Update only if extrarecord is not empty.
294         if (!empty($extrarecord)) {
295             $extrarecord['id'] = self::$backupidsexist[$key];
296             $DB->update_record('backup_ids_temp', $extrarecord);
297             // Update existing cache or add new record to cache.
298             if (isset(self::$backupidscache[$key])) {
299                 $record = array_merge((array)self::$backupidscache[$key], $extrarecord);
300                 self::$backupidscache[$key] = (object) $record;
301             } else if (self::$backupidscachesize > 0) {
302                 if ($existingrecord) {
303                     self::$backupidscache[$key] = $existingrecord;
304                 } else {
305                     // Retrive record from database and cache updated records.
306                     self::$backupidscache[$key] = $DB->get_record('backup_ids_temp', $record);
307                 }
308                 $record = array_merge((array)self::$backupidscache[$key], $extrarecord);
309                 self::$backupidscache[$key] = (object) $record;
310                 self::$backupidscachesize--;
311             }
312         }
313     }
315     /**
316      * Reset the ids caches completely
317      *
318      * Any destructive operation (partial delete, truncate, drop or recreate) performed
319      * with the backup_ids table must cause the backup_ids caches to be
320      * invalidated by calling this method. See MDL-33630.
321      *
322      * Note that right now, the only operation of that type is the recreation
323      * (drop & restore) of the table that may happen once the prechecks have ended. All
324      * the rest of operations are always routed via {@link set_backup_ids_record()}, 1 by 1,
325      * keeping the caches on sync.
326      *
327      * @todo MDL-25290 static should be replaced with MUC code.
328      */
329     public static function reset_backup_ids_cached() {
330         // Reset the ids cache.
331         $cachetoadd = count(self::$backupidscache);
332         self::$backupidscache = array();
333         self::$backupidscachesize = self::$backupidscachesize + $cachetoadd;
334         // Reset the exists cache.
335         $existstoadd = count(self::$backupidsexist);
336         self::$backupidsexist = array();
337         self::$backupidsexistsize = self::$backupidsexistsize + $existstoadd;
338     }
340     /**
341      * Given one role, as loaded from XML, perform the best possible matching against the assignable
342      * roles, using different fallback alternatives (shortname, archetype, editingteacher => teacher, defaultcourseroleid)
343      * returning the id of the best matching role or 0 if no match is found
344      */
345     protected static function get_best_assignable_role($role, $courseid, $userid, $samesite) {
346         global $CFG, $DB;
348         // Gather various information about roles
349         $coursectx = context_course::instance($courseid);
350         $assignablerolesshortname = get_assignable_roles($coursectx, ROLENAME_SHORT, false, $userid);
352         // Note: under 1.9 we had one function restore_samerole() that performed one complete
353         // matching of roles (all caps) and if match was found the mapping was availabe bypassing
354         // any assignable_roles() security. IMO that was wrong and we must not allow such
355         // mappings anymore. So we have left that matching strategy out in 2.0
357         // Empty assignable roles, mean no match possible
358         if (empty($assignablerolesshortname)) {
359             return 0;
360         }
362         // Match by shortname
363         if ($match = array_search($role->shortname, $assignablerolesshortname)) {
364             return $match;
365         }
367         // Match by archetype
368         list($in_sql, $in_params) = $DB->get_in_or_equal(array_keys($assignablerolesshortname));
369         $params = array_merge(array($role->archetype), $in_params);
370         if ($rec = $DB->get_record_select('role', "archetype = ? AND id $in_sql", $params, 'id', IGNORE_MULTIPLE)) {
371             return $rec->id;
372         }
374         // Match editingteacher to teacher (happens a lot, from 1.9)
375         if ($role->shortname == 'editingteacher' && in_array('teacher', $assignablerolesshortname)) {
376             return array_search('teacher', $assignablerolesshortname);
377         }
379         // No match, return 0
380         return 0;
381     }
384     /**
385      * Process the loaded roles, looking for their best mapping or skipping
386      * Any error will cause exception. Note this is one wrapper over
387      * precheck_included_roles, that contains all the logic, but returns
388      * errors/warnings instead and is executed as part of the restore prechecks
389      */
390      public static function process_included_roles($restoreid, $courseid, $userid, $samesite, $rolemappings) {
391         global $DB;
393         // Just let precheck_included_roles() to do all the hard work
394         $problems = self::precheck_included_roles($restoreid, $courseid, $userid, $samesite, $rolemappings);
396         // With problems of type error, throw exception, shouldn't happen if prechecks executed
397         if (array_key_exists('errors', $problems)) {
398             throw new restore_dbops_exception('restore_problems_processing_roles', null, implode(', ', $problems['errors']));
399         }
400     }
402     /**
403      * Load the needed users.xml file to backup_ids table for future reference
404      */
405     public static function load_users_to_tempids($restoreid, $usersfile) {
407         if (!file_exists($usersfile)) { // Shouldn't happen ever, but...
408             throw new backup_helper_exception('missing_users_xml_file', $usersfile);
409         }
410         // Let's parse, custom processor will do its work, sending info to DB
411         $xmlparser = new progressive_parser();
412         $xmlparser->set_file($usersfile);
413         $xmlprocessor = new restore_users_parser_processor($restoreid);
414         $xmlparser->set_processor($xmlprocessor);
415         $xmlparser->process();
416     }
418     /**
419      * Load the needed questions.xml file to backup_ids table for future reference
420      */
421     public static function load_categories_and_questions_to_tempids($restoreid, $questionsfile) {
423         if (!file_exists($questionsfile)) { // Shouldn't happen ever, but...
424             throw new backup_helper_exception('missing_questions_xml_file', $questionsfile);
425         }
426         // Let's parse, custom processor will do its work, sending info to DB
427         $xmlparser = new progressive_parser();
428         $xmlparser->set_file($questionsfile);
429         $xmlprocessor = new restore_questions_parser_processor($restoreid);
430         $xmlparser->set_processor($xmlprocessor);
431         $xmlparser->process();
432     }
434     /**
435      * Check all the included categories and questions, deciding the action to perform
436      * for each one (mapping / creation) and returning one array of problems in case
437      * something is wrong.
438      *
439      * There are some basic rules that the method below will always try to enforce:
440      *
441      * Rule1: Targets will be, always, calculated for *whole* question banks (a.k.a. contexid source),
442      *     so, given 2 question categories belonging to the same bank, their target bank will be
443      *     always the same. If not, we can be incurring into "fragmentation", leading to random/cloze
444      *     problems (qtypes having "child" questions).
445      *
446      * Rule2: The 'moodle/question:managecategory' and 'moodle/question:add' capabilities will be
447      *     checked before creating any category/question respectively and, if the cap is not allowed
448      *     into upper contexts (system, coursecat)) but in lower ones (course), the *whole* question bank
449      *     will be created there.
450      *
451      * Rule3: Coursecat question banks not existing in the target site will be created as course
452      *     (lower ctx) question banks, never as "guessed" coursecat question banks base on depth or so.
453      *
454      * Rule4: System question banks will be created at system context if user has perms to do so. Else they
455      *     will created as course (lower ctx) question banks (similary to rule3). In other words, course ctx
456      *     if always a fallback for system and coursecat question banks.
457      *
458      * Also, there are some notes to clarify the scope of this method:
459      *
460      * Note1: This method won't create any question category nor question at all. It simply will calculate
461      *     which actions (create/map) must be performed for each element and where, validating that all those
462      *     actions are doable by the user executing the restore operation. Any problem found will be
463      *     returned in the problems array, causing the restore process to stop with error.
464      *
465      * Note2: To decide if one question bank (all its question categories and questions) is going to be remapped,
466      *     then all the categories and questions must exist in the same target bank. If able to do so, missing
467      *     qcats and qs will be created (rule2). But if, at the end, something is missing, the whole question bank
468      *     will be recreated at course ctx (rule1), no matter if that duplicates some categories/questions.
469      *
470      * Note3: We'll be using the newitemid column in the temp_ids table to store the action to be performed
471      *     with each question category and question. newitemid = 0 means the qcat/q needs to be created and
472      *     any other value means the qcat/q is mapped. Also, for qcats, parentitemid will contain the target
473      *     context where the categories have to be created (but for module contexts where we'll keep the old
474      *     one until the activity is created)
475      *
476      * Note4: All these "actions" will be "executed" later by {@link restore_create_categories_and_questions}
477      */
478     public static function precheck_categories_and_questions($restoreid, $courseid, $userid, $samesite) {
480         $problems = array();
482         // TODO: Check all qs, looking their qtypes are restorable
484         // Precheck all qcats and qs looking for target contexts / warnings / errors
485         list($syserr, $syswarn) = self::prechek_precheck_qbanks_by_level($restoreid, $courseid, $userid, $samesite, CONTEXT_SYSTEM);
486         list($caterr, $catwarn) = self::prechek_precheck_qbanks_by_level($restoreid, $courseid, $userid, $samesite, CONTEXT_COURSECAT);
487         list($couerr, $couwarn) = self::prechek_precheck_qbanks_by_level($restoreid, $courseid, $userid, $samesite, CONTEXT_COURSE);
488         list($moderr, $modwarn) = self::prechek_precheck_qbanks_by_level($restoreid, $courseid, $userid, $samesite, CONTEXT_MODULE);
490         // Acummulate and handle errors and warnings
491         $errors   = array_merge($syserr, $caterr, $couerr, $moderr);
492         $warnings = array_merge($syswarn, $catwarn, $couwarn, $modwarn);
493         if (!empty($errors)) {
494             $problems['errors'] = $errors;
495         }
496         if (!empty($warnings)) {
497             $problems['warnings'] = $warnings;
498         }
499         return $problems;
500     }
502     /**
503      * This function will process all the question banks present in restore
504      * at some contextlevel (from CONTEXT_SYSTEM to CONTEXT_MODULE), finding
505      * the target contexts where each bank will be restored and returning
506      * warnings/errors as needed.
507      *
508      * Some contextlevels (system, coursecat), will delegate process to
509      * course level if any problem is found (lack of permissions, non-matching
510      * target context...). Other contextlevels (course, module) will
511      * cause return error if some problem is found.
512      *
513      * At the end, if no errors were found, all the categories in backup_temp_ids
514      * will be pointing (parentitemid) to the target context where they must be
515      * created later in the restore process.
516      *
517      * Note: at the time these prechecks are executed, activities haven't been
518      * created yet so, for CONTEXT_MODULE banks, we keep the old contextid
519      * in the parentitemid field. Once the activity (and its context) has been
520      * created, we'll update that context in the required qcats
521      *
522      * Caller {@link precheck_categories_and_questions} will, simply, execute
523      * this function for all the contextlevels, acting as a simple controller
524      * of warnings and errors.
525      *
526      * The function returns 2 arrays, one containing errors and another containing
527      * warnings. Both empty if no errors/warnings are found.
528      */
529     public static function prechek_precheck_qbanks_by_level($restoreid, $courseid, $userid, $samesite, $contextlevel) {
530         global $CFG, $DB;
532         // To return any errors and warnings found
533         $errors   = array();
534         $warnings = array();
536         // Specify which fallbacks must be performed
537         $fallbacks = array(
538             CONTEXT_SYSTEM => CONTEXT_COURSE,
539             CONTEXT_COURSECAT => CONTEXT_COURSE);
541         // For any contextlevel, follow this process logic:
542         //
543         // 0) Iterate over each context (qbank)
544         // 1) Iterate over each qcat in the context, matching by stamp for the found target context
545         //     2a) No match, check if user can create qcat and q
546         //         3a) User can, mark the qcat and all dependent qs to be created in that target context
547         //         3b) User cannot, check if we are in some contextlevel with fallback
548         //             4a) There is fallback, move ALL the qcats to fallback, warn. End qcat loop
549         //             4b) No fallback, error. End qcat loop.
550         //     2b) Match, mark qcat to be mapped and iterate over each q, matching by stamp and version
551         //         5a) No match, check if user can add q
552         //             6a) User can, mark the q to be created
553         //             6b) User cannot, check if we are in some contextlevel with fallback
554         //                 7a) There is fallback, move ALL the qcats to fallback, warn. End qcat loop
555         //                 7b) No fallback, error. End qcat loop
556         //         5b) Match, mark q to be mapped
558         // Get all the contexts (question banks) in restore for the given contextlevel
559         $contexts = self::restore_get_question_banks($restoreid, $contextlevel);
561         // 0) Iterate over each context (qbank)
562         foreach ($contexts as $contextid => $contextlevel) {
563             // Init some perms
564             $canmanagecategory = false;
565             $canadd            = false;
566             // get categories in context (bank)
567             $categories = self::restore_get_question_categories($restoreid, $contextid);
568             // cache permissions if $targetcontext is found
569             if ($targetcontext = self::restore_find_best_target_context($categories, $courseid, $contextlevel)) {
570                 $canmanagecategory = has_capability('moodle/question:managecategory', $targetcontext, $userid);
571                 $canadd            = has_capability('moodle/question:add', $targetcontext, $userid);
572             }
573             // 1) Iterate over each qcat in the context, matching by stamp for the found target context
574             foreach ($categories as $category) {
575                 $matchcat = false;
576                 if ($targetcontext) {
577                     $matchcat = $DB->get_record('question_categories', array(
578                                     'contextid' => $targetcontext->id,
579                                     'stamp' => $category->stamp));
580                 }
581                 // 2a) No match, check if user can create qcat and q
582                 if (!$matchcat) {
583                     // 3a) User can, mark the qcat and all dependent qs to be created in that target context
584                     if ($canmanagecategory && $canadd) {
585                         // Set parentitemid to targetcontext, BUT for CONTEXT_MODULE categories, where
586                         // we keep the source contextid unmodified (for easier matching later when the
587                         // activities are created)
588                         $parentitemid = $targetcontext->id;
589                         if ($contextlevel == CONTEXT_MODULE) {
590                             $parentitemid = null; // null means "not modify" a.k.a. leave original contextid
591                         }
592                         self::set_backup_ids_record($restoreid, 'question_category', $category->id, 0, $parentitemid);
593                         // Nothing else to mark, newitemid = 0 means create
595                     // 3b) User cannot, check if we are in some contextlevel with fallback
596                     } else {
597                         // 4a) There is fallback, move ALL the qcats to fallback, warn. End qcat loop
598                         if (array_key_exists($contextlevel, $fallbacks)) {
599                             foreach ($categories as $movedcat) {
600                                 $movedcat->contextlevel = $fallbacks[$contextlevel];
601                                 self::set_backup_ids_record($restoreid, 'question_category', $movedcat->id, 0, $contextid, $movedcat);
602                                 // Warn about the performed fallback
603                                 $warnings[] = get_string('qcategory2coursefallback', 'backup', $movedcat);
604                             }
606                         // 4b) No fallback, error. End qcat loop.
607                         } else {
608                             $errors[] = get_string('qcategorycannotberestored', 'backup', $category);
609                         }
610                         break; // out from qcat loop (both 4a and 4b), we have decided about ALL categories in context (bank)
611                     }
613                 // 2b) Match, mark qcat to be mapped and iterate over each q, matching by stamp and version
614                 } else {
615                     self::set_backup_ids_record($restoreid, 'question_category', $category->id, $matchcat->id, $targetcontext->id);
616                     $questions = self::restore_get_questions($restoreid, $category->id);
618                     // Collect all the questions for this category into memory so we only talk to the DB once.
619                     $questioncache = $DB->get_records_sql_menu("SELECT ".$DB->sql_concat('stamp', "' '", 'version').", id
620                                                                   FROM {question}
621                                                                  WHERE category = ?", array($matchcat->id));
623                     foreach ($questions as $question) {
624                         if (isset($questioncache[$question->stamp." ".$question->version])) {
625                             $matchqid = $questioncache[$question->stamp." ".$question->version];
626                         } else {
627                             $matchqid = false;
628                         }
629                         // 5a) No match, check if user can add q
630                         if (!$matchqid) {
631                             // 6a) User can, mark the q to be created
632                             if ($canadd) {
633                                 // Nothing to mark, newitemid means create
635                              // 6b) User cannot, check if we are in some contextlevel with fallback
636                             } else {
637                                 // 7a) There is fallback, move ALL the qcats to fallback, warn. End qcat loo
638                                 if (array_key_exists($contextlevel, $fallbacks)) {
639                                     foreach ($categories as $movedcat) {
640                                         $movedcat->contextlevel = $fallbacks[$contextlevel];
641                                         self::set_backup_ids_record($restoreid, 'question_category', $movedcat->id, 0, $contextid, $movedcat);
642                                         // Warn about the performed fallback
643                                         $warnings[] = get_string('question2coursefallback', 'backup', $movedcat);
644                                     }
646                                 // 7b) No fallback, error. End qcat loop
647                                 } else {
648                                     $errors[] = get_string('questioncannotberestored', 'backup', $question);
649                                 }
650                                 break 2; // out from qcat loop (both 7a and 7b), we have decided about ALL categories in context (bank)
651                             }
653                         // 5b) Match, mark q to be mapped
654                         } else {
655                             self::set_backup_ids_record($restoreid, 'question', $question->id, $matchqid);
656                         }
657                     }
658                 }
659             }
660         }
662         return array($errors, $warnings);
663     }
665     /**
666      * Return one array of contextid => contextlevel pairs
667      * of question banks to be checked for one given restore operation
668      * ordered from CONTEXT_SYSTEM downto CONTEXT_MODULE
669      * If contextlevel is specified, then only banks corresponding to
670      * that level are returned
671      */
672     public static function restore_get_question_banks($restoreid, $contextlevel = null) {
673         global $DB;
675         $results = array();
676         $qcats = $DB->get_records_sql("SELECT itemid, parentitemid AS contextid
677                                          FROM {backup_ids_temp}
678                                        WHERE backupid = ?
679                                          AND itemname = 'question_category'", array($restoreid));
680         foreach ($qcats as $qcat) {
681             // If this qcat context haven't been acummulated yet, do that
682             if (!isset($results[$qcat->contextid])) {
683                 $temprec = self::get_backup_ids_record($restoreid, 'question_category', $qcat->itemid);
684                 // Filter by contextlevel if necessary
685                 if (is_null($contextlevel) || $contextlevel == $temprec->info->contextlevel) {
686                     $results[$qcat->contextid] = $temprec->info->contextlevel;
687                 }
688             }
689         }
690         // Sort by value (contextlevel from CONTEXT_SYSTEM downto CONTEXT_MODULE)
691         asort($results);
692         return $results;
693     }
695     /**
696      * Return one array of question_category records for
697      * a given restore operation and one restore context (question bank)
698      */
699     public static function restore_get_question_categories($restoreid, $contextid) {
700         global $DB;
702         $results = array();
703         $qcats = $DB->get_records_sql("SELECT itemid
704                                          FROM {backup_ids_temp}
705                                         WHERE backupid = ?
706                                           AND itemname = 'question_category'
707                                           AND parentitemid = ?", array($restoreid, $contextid));
708         foreach ($qcats as $qcat) {
709             $temprec = self::get_backup_ids_record($restoreid, 'question_category', $qcat->itemid);
710             $results[$qcat->itemid] = $temprec->info;
711         }
712         return $results;
713     }
715     /**
716      * Calculates the best context found to restore one collection of qcats,
717      * al them belonging to the same context (question bank), returning the
718      * target context found (object) or false
719      */
720     public static function restore_find_best_target_context($categories, $courseid, $contextlevel) {
721         global $DB;
723         $targetcontext = false;
725         // Depending of $contextlevel, we perform different actions
726         switch ($contextlevel) {
727              // For system is easy, the best context is the system context
728              case CONTEXT_SYSTEM:
729                  $targetcontext = context_system::instance();
730                  break;
732              // For coursecat, we are going to look for stamps in all the
733              // course categories between CONTEXT_SYSTEM and CONTEXT_COURSE
734              // (i.e. in all the course categories in the path)
735              //
736              // And only will return one "best" target context if all the
737              // matches belong to ONE and ONLY ONE context. If multiple
738              // matches are found, that means that there is some annoying
739              // qbank "fragmentation" in the categories, so we'll fallback
740              // to create the qbank at course level
741              case CONTEXT_COURSECAT:
742                  // Build the array of stamps we are going to match
743                  $stamps = array();
744                  foreach ($categories as $category) {
745                      $stamps[] = $category->stamp;
746                  }
747                  $contexts = array();
748                  // Build the array of contexts we are going to look
749                  $systemctx = context_system::instance();
750                  $coursectx = context_course::instance($courseid);
751                  $parentctxs = $coursectx->get_parent_context_ids();
752                  foreach ($parentctxs as $parentctx) {
753                      // Exclude system context
754                      if ($parentctx == $systemctx->id) {
755                          continue;
756                      }
757                      $contexts[] = $parentctx;
758                  }
759                  if (!empty($stamps) && !empty($contexts)) {
760                      // Prepare the query
761                      list($stamp_sql, $stamp_params) = $DB->get_in_or_equal($stamps);
762                      list($context_sql, $context_params) = $DB->get_in_or_equal($contexts);
763                      $sql = "SELECT contextid
764                                FROM {question_categories}
765                               WHERE stamp $stamp_sql
766                                 AND contextid $context_sql";
767                      $params = array_merge($stamp_params, $context_params);
768                      $matchingcontexts = $DB->get_records_sql($sql, $params);
769                      // Only if ONE and ONLY ONE context is found, use it as valid target
770                      if (count($matchingcontexts) == 1) {
771                          $targetcontext = context::instance_by_id(reset($matchingcontexts)->contextid);
772                      }
773                  }
774                  break;
776              // For course is easy, the best context is the course context
777              case CONTEXT_COURSE:
778                  $targetcontext = context_course::instance($courseid);
779                  break;
781              // For module is easy, there is not best context, as far as the
782              // activity hasn't been created yet. So we return context course
783              // for them, so permission checks and friends will work. Note this
784              // case is handled by {@link prechek_precheck_qbanks_by_level}
785              // in an special way
786              case CONTEXT_MODULE:
787                  $targetcontext = context_course::instance($courseid);
788                  break;
789         }
790         return $targetcontext;
791     }
793     /**
794      * Return one array of question records for
795      * a given restore operation and one question category
796      */
797     public static function restore_get_questions($restoreid, $qcatid) {
798         global $DB;
800         $results = array();
801         $qs = $DB->get_records_sql("SELECT itemid
802                                       FROM {backup_ids_temp}
803                                      WHERE backupid = ?
804                                        AND itemname = 'question'
805                                        AND parentitemid = ?", array($restoreid, $qcatid));
806         foreach ($qs as $q) {
807             $temprec = self::get_backup_ids_record($restoreid, 'question', $q->itemid);
808             $results[$q->itemid] = $temprec->info;
809         }
810         return $results;
811     }
813     /**
814      * Given one component/filearea/context and
815      * optionally one source itemname to match itemids
816      * put the corresponding files in the pool
817      *
818      * @param string $basepath the full path to the root of unzipped backup file
819      * @param string $restoreid the restore job's identification
820      * @param string $component
821      * @param string $filearea
822      * @param int $oldcontextid
823      * @param int $dfltuserid default $file->user if the old one can't be mapped
824      * @param string|null $itemname
825      * @param int|null $olditemid
826      * @param int|null $forcenewcontextid explicit value for the new contextid (skip mapping)
827      * @param bool $skipparentitemidctxmatch
828      * @return array of result object
829      */
830     public static function send_files_to_pool($basepath, $restoreid, $component, $filearea, $oldcontextid, $dfltuserid, $itemname = null, $olditemid = null, $forcenewcontextid = null, $skipparentitemidctxmatch = false) {
831         global $DB, $CFG;
833         $backupinfo = backup_general_helper::get_backup_information(basename($basepath));
834         $includesfiles = $backupinfo->include_files;
836         $results = array();
838         if ($forcenewcontextid) {
839             // Some components can have "forced" new contexts (example: questions can end belonging to non-standard context mappings,
840             // with questions originally at system/coursecat context in source being restored to course context in target). So we need
841             // to be able to force the new contextid
842             $newcontextid = $forcenewcontextid;
843         } else {
844             // Get new context, must exist or this will fail
845             if (!$newcontextid = self::get_backup_ids_record($restoreid, 'context', $oldcontextid)->newitemid) {
846                 throw new restore_dbops_exception('unknown_context_mapping', $oldcontextid);
847             }
848         }
850         // Sometimes it's possible to have not the oldcontextids stored into backup_ids_temp->parentitemid
851         // columns (because we have used them to store other information). This happens usually with
852         // all the question related backup_ids_temp records. In that case, it's safe to ignore that
853         // matching as far as we are always restoring for well known oldcontexts and olditemids
854         $parentitemctxmatchsql = ' AND i.parentitemid = f.contextid ';
855         if ($skipparentitemidctxmatch) {
856             $parentitemctxmatchsql = '';
857         }
859         // Important: remember how files have been loaded to backup_files_temp
860         //   - info: contains the whole original object (times, names...)
861         //   (all them being original ids as loaded from xml)
863         // itemname = null, we are going to match only by context, no need to use itemid (all them are 0)
864         if ($itemname == null) {
865             $sql = "SELECT id AS bftid, contextid, component, filearea, itemid, itemid AS newitemid, info
866                       FROM {backup_files_temp}
867                      WHERE backupid = ?
868                        AND contextid = ?
869                        AND component = ?
870                        AND filearea  = ?";
871             $params = array($restoreid, $oldcontextid, $component, $filearea);
873         // itemname not null, going to join with backup_ids to perform the old-new mapping of itemids
874         } else {
875             $sql = "SELECT f.id AS bftid, f.contextid, f.component, f.filearea, f.itemid, i.newitemid, f.info
876                       FROM {backup_files_temp} f
877                       JOIN {backup_ids_temp} i ON i.backupid = f.backupid
878                                               $parentitemctxmatchsql
879                                               AND i.itemid = f.itemid
880                      WHERE f.backupid = ?
881                        AND f.contextid = ?
882                        AND f.component = ?
883                        AND f.filearea = ?
884                        AND i.itemname = ?";
885             $params = array($restoreid, $oldcontextid, $component, $filearea, $itemname);
886             if ($olditemid !== null) { // Just process ONE olditemid intead of the whole itemname
887                 $sql .= ' AND i.itemid = ?';
888                 $params[] = $olditemid;
889             }
890         }
892         $fs = get_file_storage();         // Get moodle file storage
893         $basepath = $basepath . '/files/';// Get backup file pool base
894         $rs = $DB->get_recordset_sql($sql, $params);
895         foreach ($rs as $rec) {
896             $file = (object)unserialize(base64_decode($rec->info));
898             // ignore root dirs (they are created automatically)
899             if ($file->filepath == '/' && $file->filename == '.') {
900                 continue;
901             }
903             // set the best possible user
904             $mappeduser = self::get_backup_ids_record($restoreid, 'user', $file->userid);
905             $mappeduserid = !empty($mappeduser) ? $mappeduser->newitemid : $dfltuserid;
907             // dir found (and not root one), let's create it
908             if ($file->filename == '.') {
909                 $fs->create_directory($newcontextid, $component, $filearea, $rec->newitemid, $file->filepath, $mappeduserid);
910                 continue;
911             }
913             // The file record to restore.
914             $file_record = array(
915                 'contextid'   => $newcontextid,
916                 'component'   => $component,
917                 'filearea'    => $filearea,
918                 'itemid'      => $rec->newitemid,
919                 'filepath'    => $file->filepath,
920                 'filename'    => $file->filename,
921                 'timecreated' => $file->timecreated,
922                 'timemodified'=> $file->timemodified,
923                 'userid'      => $mappeduserid,
924                 'author'      => $file->author,
925                 'license'     => $file->license,
926                 'sortorder'   => $file->sortorder
927             );
929             if (empty($file->repositoryid)) {
930                 // this is a regular file, it must be present in the backup pool
931                 $backuppath = $basepath . backup_file_manager::get_backup_content_file_location($file->contenthash);
933                 // Some file types do not include the files as they should already be
934                 // present. We still need to create entries into the files table.
935                 if ($includesfiles) {
936                     // The file is not found in the backup.
937                     if (!file_exists($backuppath)) {
938                         $result = new stdClass();
939                         $result->code = 'file_missing_in_backup';
940                         $result->message = sprintf('missing file %s%s in backup', $file->filepath, $file->filename);
941                         $result->level = backup::LOG_WARNING;
942                         $results[] = $result;
943                         continue;
944                     }
946                     // create the file in the filepool if it does not exist yet
947                     if (!$fs->file_exists($newcontextid, $component, $filearea, $rec->newitemid, $file->filepath, $file->filename)) {
949                         // If no license found, use default.
950                         if ($file->license == null){
951                             $file->license = $CFG->sitedefaultlicense;
952                         }
954                         $fs->create_file_from_pathname($file_record, $backuppath);
955                     }
956                 } else {
957                     // This backup does not include the files - they should be available in moodle filestorage already.
959                     // Even if a file has been deleted since the backup was made, the file metadata will remain in the
960                     // files table, and the file will not be moved to the trashdir.
961                     // Files are not cleared from the files table by cron until several days after deletion.
962                     if ($foundfiles = $DB->get_records('files', array('contenthash' => $file->contenthash))) {
963                         // Only grab one of the foundfiles - the file content should be the same for all entries.
964                         $foundfile = reset($foundfiles);
965                         $fs->create_file_from_storedfile($file_record, $foundfile->id);
966                     } else {
967                         // A matching existing file record was not found in the database.
968                         $result = new stdClass();
969                         $result->code = 'file_missing_in_backup';
970                         $result->message = sprintf('missing file %s%s in backup', $file->filepath, $file->filename);
971                         $result->level = backup::LOG_WARNING;
972                         $results[] = $result;
973                         continue;
974                     }
975                 }
977                 // store the the new contextid and the new itemid in case we need to remap
978                 // references to this file later
979                 $DB->update_record('backup_files_temp', array(
980                     'id' => $rec->bftid,
981                     'newcontextid' => $newcontextid,
982                     'newitemid' => $rec->newitemid), true);
984             } else {
985                 // this is an alias - we can't create it yet so we stash it in a temp
986                 // table and will let the final task to deal with it
987                 if (!$fs->file_exists($newcontextid, $component, $filearea, $rec->newitemid, $file->filepath, $file->filename)) {
988                     $info = new stdClass();
989                     // oldfile holds the raw information stored in MBZ (including reference-related info)
990                     $info->oldfile = $file;
991                     // newfile holds the info for the new file_record with the context, user and itemid mapped
992                     $info->newfile = (object) $file_record;
994                     restore_dbops::set_backup_ids_record($restoreid, 'file_aliases_queue', $file->id, 0, null, $info);
995                 }
996             }
997         }
998         $rs->close();
999         return $results;
1000     }
1002     /**
1003      * Given one restoreid, create in DB all the users present
1004      * in backup_ids having newitemid = 0, as far as
1005      * precheck_included_users() have left them there
1006      * ready to be created. Also, annotate their newids
1007      * once created for later reference
1008      */
1009     public static function create_included_users($basepath, $restoreid, $userid) {
1010         global $CFG, $DB;
1012         $authcache = array(); // Cache to get some bits from authentication plugins
1013         $languages = get_string_manager()->get_list_of_translations(); // Get languages for quick search later
1014         $themes    = get_list_of_themes(); // Get themes for quick search later
1016         // Iterate over all the included users with newitemid = 0, have to create them
1017         $rs = $DB->get_recordset('backup_ids_temp', array('backupid' => $restoreid, 'itemname' => 'user', 'newitemid' => 0), '', 'itemid, parentitemid');
1018         foreach ($rs as $recuser) {
1019             $user = (object)self::get_backup_ids_record($restoreid, 'user', $recuser->itemid)->info;
1021             // if user lang doesn't exist here, use site default
1022             if (!array_key_exists($user->lang, $languages)) {
1023                 $user->lang = $CFG->lang;
1024             }
1026             // if user theme isn't available on target site or they are disabled, reset theme
1027             if (!empty($user->theme)) {
1028                 if (empty($CFG->allowuserthemes) || !in_array($user->theme, $themes)) {
1029                     $user->theme = '';
1030                 }
1031             }
1033             // if user to be created has mnet auth and its mnethostid is $CFG->mnet_localhost_id
1034             // that's 100% impossible as own server cannot be accesed over mnet. Change auth to email/manual
1035             if ($user->auth == 'mnet' && $user->mnethostid == $CFG->mnet_localhost_id) {
1036                 // Respect registerauth
1037                 if ($CFG->registerauth == 'email') {
1038                     $user->auth = 'email';
1039                 } else {
1040                     $user->auth = 'manual';
1041                 }
1042             }
1043             unset($user->mnethosturl); // Not needed anymore
1045             // Disable pictures based on global setting
1046             if (!empty($CFG->disableuserimages)) {
1047                 $user->picture = 0;
1048             }
1050             // We need to analyse the AUTH field to recode it:
1051             //   - if the auth isn't enabled in target site, $CFG->registerauth will decide
1052             //   - finally, if the auth resulting isn't enabled, default to 'manual'
1053             if (!is_enabled_auth($user->auth)) {
1054                 if ($CFG->registerauth == 'email') {
1055                     $user->auth = 'email';
1056                 } else {
1057                     $user->auth = 'manual';
1058                 }
1059             }
1060             if (!is_enabled_auth($user->auth)) { // Final auth check verify, default to manual if not enabled
1061                 $user->auth = 'manual';
1062             }
1064             // Now that we know the auth method, for users to be created without pass
1065             // if password handling is internal and reset password is available
1066             // we set the password to "restored" (plain text), so the login process
1067             // will know how to handle that situation in order to allow the user to
1068             // recover the password. MDL-20846
1069             if (empty($user->password)) { // Only if restore comes without password
1070                 if (!array_key_exists($user->auth, $authcache)) { // Not in cache
1071                     $userauth = new stdClass();
1072                     $authplugin = get_auth_plugin($user->auth);
1073                     $userauth->preventpassindb = $authplugin->prevent_local_passwords();
1074                     $userauth->isinternal      = $authplugin->is_internal();
1075                     $userauth->canresetpwd     = $authplugin->can_reset_password();
1076                     $authcache[$user->auth] = $userauth;
1077                 } else {
1078                     $userauth = $authcache[$user->auth]; // Get from cache
1079                 }
1081                 // Most external plugins do not store passwords locally
1082                 if (!empty($userauth->preventpassindb)) {
1083                     $user->password = AUTH_PASSWORD_NOT_CACHED;
1085                 // If Moodle is responsible for storing/validating pwd and reset functionality is available, mark
1086                 } else if ($userauth->isinternal and $userauth->canresetpwd) {
1087                     $user->password = 'restored';
1088                 }
1089             }
1091             // Creating new user, we must reset the policyagreed always
1092             $user->policyagreed = 0;
1094             // Set time created if empty
1095             if (empty($user->timecreated)) {
1096                 $user->timecreated = time();
1097             }
1099             // Done, let's create the user and annotate its id
1100             $newuserid = $DB->insert_record('user', $user);
1101             self::set_backup_ids_record($restoreid, 'user', $recuser->itemid, $newuserid);
1102             // Let's create the user context and annotate it (we need it for sure at least for files)
1103             // but for deleted users that don't have a context anymore (MDL-30192). We are done for them
1104             // and nothing else (custom fields, prefs, tags, files...) will be created.
1105             if (empty($user->deleted)) {
1106                 $newuserctxid = $user->deleted ? 0 : context_user::instance($newuserid)->id;
1107                 self::set_backup_ids_record($restoreid, 'context', $recuser->parentitemid, $newuserctxid);
1109                 // Process custom fields
1110                 if (isset($user->custom_fields)) { // if present in backup
1111                     foreach($user->custom_fields['custom_field'] as $udata) {
1112                         $udata = (object)$udata;
1113                         // If the profile field has data and the profile shortname-datatype is defined in server
1114                         if ($udata->field_data) {
1115                             if ($field = $DB->get_record('user_info_field', array('shortname'=>$udata->field_name, 'datatype'=>$udata->field_type))) {
1116                             /// Insert the user_custom_profile_field
1117                                 $rec = new stdClass();
1118                                 $rec->userid  = $newuserid;
1119                                 $rec->fieldid = $field->id;
1120                                 $rec->data    = $udata->field_data;
1121                                 $DB->insert_record('user_info_data', $rec);
1122                             }
1123                         }
1124                     }
1125                 }
1127                 // Process tags
1128                 if (!empty($CFG->usetags) && isset($user->tags)) { // if enabled in server and present in backup
1129                     $tags = array();
1130                     foreach($user->tags['tag'] as $usertag) {
1131                         $usertag = (object)$usertag;
1132                         $tags[] = $usertag->rawname;
1133                     }
1134                     tag_set('user', $newuserid, $tags);
1135                 }
1137                 // Process preferences
1138                 if (isset($user->preferences)) { // if present in backup
1139                     foreach($user->preferences['preference'] as $preference) {
1140                         $preference = (object)$preference;
1141                         // Prepare the record and insert it
1142                         $preference->userid = $newuserid;
1143                         $status = $DB->insert_record('user_preferences', $preference);
1144                     }
1145                 }
1147                 // Create user files in pool (profile, icon, private) by context
1148                 restore_dbops::send_files_to_pool($basepath, $restoreid, 'user', 'icon', $recuser->parentitemid, $userid);
1149                 restore_dbops::send_files_to_pool($basepath, $restoreid, 'user', 'profile', $recuser->parentitemid, $userid);
1150             }
1151         }
1152         $rs->close();
1153     }
1155     /**
1156     * Given one user object (from backup file), perform all the neccesary
1157     * checks is order to decide how that user will be handled on restore.
1158     *
1159     * Note the function requires $user->mnethostid to be already calculated
1160     * so it's caller responsibility to set it
1161     *
1162     * This function is used both by @restore_precheck_users() and
1163     * @restore_create_users() to get consistent results in both places
1164     *
1165     * It returns:
1166     *   - one user object (from DB), if match has been found and user will be remapped
1167     *   - boolean true if the user needs to be created
1168     *   - boolean false if some conflict happened and the user cannot be handled
1169     *
1170     * Each test is responsible for returning its results and interrupt
1171     * execution. At the end, boolean true (user needs to be created) will be
1172     * returned if no test has interrupted that.
1173     *
1174     * Here it's the logic applied, keep it updated:
1175     *
1176     *  If restoring users from same site backup:
1177     *      1A - Normal check: If match by id and username and mnethost  => ok, return target user
1178     *      1B - If restoring an 'anonymous' user (created via the 'Anonymize user information' option) try to find a
1179     *           match by username only => ok, return target user MDL-31484
1180     *      1C - Handle users deleted in DB and "alive" in backup file:
1181     *           If match by id and mnethost and user is deleted in DB and
1182     *           (match by username LIKE 'backup_email.%' or by non empty email = md5(username)) => ok, return target user
1183     *      1D - Handle users deleted in backup file and "alive" in DB:
1184     *           If match by id and mnethost and user is deleted in backup file
1185     *           and match by email = email_without_time(backup_email) => ok, return target user
1186     *      1E - Conflict: If match by username and mnethost and doesn't match by id => conflict, return false
1187     *      1F - None of the above, return true => User needs to be created
1188     *
1189     *  if restoring from another site backup (cannot match by id here, replace it by email/firstaccess combination):
1190     *      2A - Normal check: If match by username and mnethost and (email or non-zero firstaccess) => ok, return target user
1191     *      2B - Handle users deleted in DB and "alive" in backup file:
1192     *           2B1 - If match by mnethost and user is deleted in DB and not empty email = md5(username) and
1193     *                 (username LIKE 'backup_email.%' or non-zero firstaccess) => ok, return target user
1194     *           2B2 - If match by mnethost and user is deleted in DB and
1195     *                 username LIKE 'backup_email.%' and non-zero firstaccess) => ok, return target user
1196     *                 (to cover situations were md5(username) wasn't implemented on delete we requiere both)
1197     *      2C - Handle users deleted in backup file and "alive" in DB:
1198     *           If match mnethost and user is deleted in backup file
1199     *           and by email = email_without_time(backup_email) and non-zero firstaccess=> ok, return target user
1200     *      2D - Conflict: If match by username and mnethost and not by (email or non-zero firstaccess) => conflict, return false
1201     *      2E - None of the above, return true => User needs to be created
1202     *
1203     * Note: for DB deleted users email is stored in username field, hence we
1204     *       are looking there for emails. See delete_user()
1205     * Note: for DB deleted users md5(username) is stored *sometimes* in the email field,
1206     *       hence we are looking there for usernames if not empty. See delete_user()
1207     */
1208     protected static function precheck_user($user, $samesite) {
1209         global $CFG, $DB;
1211         // Handle checks from same site backups
1212         if ($samesite && empty($CFG->forcedifferentsitecheckingusersonrestore)) {
1214             // 1A - If match by id and username and mnethost => ok, return target user
1215             if ($rec = $DB->get_record('user', array('id'=>$user->id, 'username'=>$user->username, 'mnethostid'=>$user->mnethostid))) {
1216                 return $rec; // Matching user found, return it
1217             }
1219             // 1B - If restoring an 'anonymous' user (created via the 'Anonymize user information' option) try to find a
1220             // match by username only => ok, return target user MDL-31484
1221             // This avoids username / id mis-match problems when restoring subsequent anonymized backups.
1222             if (backup_anonymizer_helper::is_anonymous_user($user)) {
1223                 if ($rec = $DB->get_record('user', array('username' => $user->username))) {
1224                     return $rec; // Matching anonymous user found - return it
1225                 }
1226             }
1228             // 1C - Handle users deleted in DB and "alive" in backup file
1229             // Note: for DB deleted users email is stored in username field, hence we
1230             //       are looking there for emails. See delete_user()
1231             // Note: for DB deleted users md5(username) is stored *sometimes* in the email field,
1232             //       hence we are looking there for usernames if not empty. See delete_user()
1233             // If match by id and mnethost and user is deleted in DB and
1234             // match by username LIKE 'backup_email.%' or by non empty email = md5(username) => ok, return target user
1235             if ($rec = $DB->get_record_sql("SELECT *
1236                                               FROM {user} u
1237                                              WHERE id = ?
1238                                                AND mnethostid = ?
1239                                                AND deleted = 1
1240                                                AND (
1241                                                        UPPER(username) LIKE UPPER(?)
1242                                                     OR (
1243                                                            ".$DB->sql_isnotempty('user', 'email', false, false)."
1244                                                        AND email = ?
1245                                                        )
1246                                                    )",
1247                                            array($user->id, $user->mnethostid, $user->email.'.%', md5($user->username)))) {
1248                 return $rec; // Matching user, deleted in DB found, return it
1249             }
1251             // 1D - Handle users deleted in backup file and "alive" in DB
1252             // If match by id and mnethost and user is deleted in backup file
1253             // and match by email = email_without_time(backup_email) => ok, return target user
1254             if ($user->deleted) {
1255                 // Note: for DB deleted users email is stored in username field, hence we
1256                 //       are looking there for emails. See delete_user()
1257                 // Trim time() from email
1258                 $trimemail = preg_replace('/(.*?)\.[0-9]+.?$/', '\\1', $user->username);
1259                 if ($rec = $DB->get_record_sql("SELECT *
1260                                                   FROM {user} u
1261                                                  WHERE id = ?
1262                                                    AND mnethostid = ?
1263                                                    AND UPPER(email) = UPPER(?)",
1264                                                array($user->id, $user->mnethostid, $trimemail))) {
1265                     return $rec; // Matching user, deleted in backup file found, return it
1266                 }
1267             }
1269             // 1E - If match by username and mnethost and doesn't match by id => conflict, return false
1270             if ($rec = $DB->get_record('user', array('username'=>$user->username, 'mnethostid'=>$user->mnethostid))) {
1271                 if ($user->id != $rec->id) {
1272                     return false; // Conflict, username already exists and belongs to another id
1273                 }
1274             }
1276         // Handle checks from different site backups
1277         } else {
1279             // 2A - If match by username and mnethost and
1280             //     (email or non-zero firstaccess) => ok, return target user
1281             if ($rec = $DB->get_record_sql("SELECT *
1282                                               FROM {user} u
1283                                              WHERE username = ?
1284                                                AND mnethostid = ?
1285                                                AND (
1286                                                        UPPER(email) = UPPER(?)
1287                                                     OR (
1288                                                            firstaccess != 0
1289                                                        AND firstaccess = ?
1290                                                        )
1291                                                    )",
1292                                            array($user->username, $user->mnethostid, $user->email, $user->firstaccess))) {
1293                 return $rec; // Matching user found, return it
1294             }
1296             // 2B - Handle users deleted in DB and "alive" in backup file
1297             // Note: for DB deleted users email is stored in username field, hence we
1298             //       are looking there for emails. See delete_user()
1299             // Note: for DB deleted users md5(username) is stored *sometimes* in the email field,
1300             //       hence we are looking there for usernames if not empty. See delete_user()
1301             // 2B1 - If match by mnethost and user is deleted in DB and not empty email = md5(username) and
1302             //       (by username LIKE 'backup_email.%' or non-zero firstaccess) => ok, return target user
1303             if ($rec = $DB->get_record_sql("SELECT *
1304                                               FROM {user} u
1305                                              WHERE mnethostid = ?
1306                                                AND deleted = 1
1307                                                AND ".$DB->sql_isnotempty('user', 'email', false, false)."
1308                                                AND email = ?
1309                                                AND (
1310                                                        UPPER(username) LIKE UPPER(?)
1311                                                     OR (
1312                                                            firstaccess != 0
1313                                                        AND firstaccess = ?
1314                                                        )
1315                                                    )",
1316                                            array($user->mnethostid, md5($user->username), $user->email.'.%', $user->firstaccess))) {
1317                 return $rec; // Matching user found, return it
1318             }
1320             // 2B2 - If match by mnethost and user is deleted in DB and
1321             //       username LIKE 'backup_email.%' and non-zero firstaccess) => ok, return target user
1322             //       (this covers situations where md5(username) wasn't being stored so we require both
1323             //        the email & non-zero firstaccess to match)
1324             if ($rec = $DB->get_record_sql("SELECT *
1325                                               FROM {user} u
1326                                              WHERE mnethostid = ?
1327                                                AND deleted = 1
1328                                                AND UPPER(username) LIKE UPPER(?)
1329                                                AND firstaccess != 0
1330                                                AND firstaccess = ?",
1331                                            array($user->mnethostid, $user->email.'.%', $user->firstaccess))) {
1332                 return $rec; // Matching user found, return it
1333             }
1335             // 2C - Handle users deleted in backup file and "alive" in DB
1336             // If match mnethost and user is deleted in backup file
1337             // and match by email = email_without_time(backup_email) and non-zero firstaccess=> ok, return target user
1338             if ($user->deleted) {
1339                 // Note: for DB deleted users email is stored in username field, hence we
1340                 //       are looking there for emails. See delete_user()
1341                 // Trim time() from email
1342                 $trimemail = preg_replace('/(.*?)\.[0-9]+.?$/', '\\1', $user->username);
1343                 if ($rec = $DB->get_record_sql("SELECT *
1344                                                   FROM {user} u
1345                                                  WHERE mnethostid = ?
1346                                                    AND UPPER(email) = UPPER(?)
1347                                                    AND firstaccess != 0
1348                                                    AND firstaccess = ?",
1349                                                array($user->mnethostid, $trimemail, $user->firstaccess))) {
1350                     return $rec; // Matching user, deleted in backup file found, return it
1351                 }
1352             }
1354             // 2D - If match by username and mnethost and not by (email or non-zero firstaccess) => conflict, return false
1355             if ($rec = $DB->get_record_sql("SELECT *
1356                                               FROM {user} u
1357                                              WHERE username = ?
1358                                                AND mnethostid = ?
1359                                            AND NOT (
1360                                                        UPPER(email) = UPPER(?)
1361                                                     OR (
1362                                                            firstaccess != 0
1363                                                        AND firstaccess = ?
1364                                                        )
1365                                                    )",
1366                                            array($user->username, $user->mnethostid, $user->email, $user->firstaccess))) {
1367                 return false; // Conflict, username/mnethostid already exist and belong to another user (by email/firstaccess)
1368             }
1369         }
1371         // Arrived here, return true as the user will need to be created and no
1372         // conflicts have been found in the logic above. This covers:
1373         // 1E - else => user needs to be created, return true
1374         // 2E - else => user needs to be created, return true
1375         return true;
1376     }
1378     /**
1379      * Check all the included users, deciding the action to perform
1380      * for each one (mapping / creation) and returning one array
1381      * of problems in case something is wrong (lack of permissions,
1382      * conficts)
1383      */
1384     public static function precheck_included_users($restoreid, $courseid, $userid, $samesite) {
1385         global $CFG, $DB;
1387         // To return any problem found
1388         $problems = array();
1390         // We are going to map mnethostid, so load all the available ones
1391         $mnethosts = $DB->get_records('mnet_host', array(), 'wwwroot', 'wwwroot, id');
1393         // Calculate the context we are going to use for capability checking
1394         $context = context_course::instance($courseid);
1396         // Calculate if we have perms to create users, by checking:
1397         // to 'moodle/restore:createuser' and 'moodle/restore:userinfo'
1398         // and also observe $CFG->disableusercreationonrestore
1399         $cancreateuser = false;
1400         if (has_capability('moodle/restore:createuser', $context, $userid) and
1401             has_capability('moodle/restore:userinfo', $context, $userid) and
1402             empty($CFG->disableusercreationonrestore)) { // Can create users
1404             $cancreateuser = true;
1405         }
1407         // Iterate over all the included users
1408         $rs = $DB->get_recordset('backup_ids_temp', array('backupid' => $restoreid, 'itemname' => 'user'), '', 'itemid');
1409         foreach ($rs as $recuser) {
1410             $user = (object)self::get_backup_ids_record($restoreid, 'user', $recuser->itemid)->info;
1412             // Find the correct mnethostid for user before performing any further check
1413             if (empty($user->mnethosturl) || $user->mnethosturl === $CFG->wwwroot) {
1414                 $user->mnethostid = $CFG->mnet_localhost_id;
1415             } else {
1416                 // fast url-to-id lookups
1417                 if (isset($mnethosts[$user->mnethosturl])) {
1418                     $user->mnethostid = $mnethosts[$user->mnethosturl]->id;
1419                 } else {
1420                     $user->mnethostid = $CFG->mnet_localhost_id;
1421                 }
1422             }
1424             // Now, precheck that user and, based on returned results, annotate action/problem
1425             $usercheck = self::precheck_user($user, $samesite);
1427             if (is_object($usercheck)) { // No problem, we have found one user in DB to be mapped to
1428                 // Annotate it, for later process. Set newitemid to mapping user->id
1429                 self::set_backup_ids_record($restoreid, 'user', $recuser->itemid, $usercheck->id);
1431             } else if ($usercheck === false) { // Found conflict, report it as problem
1432                  $problems[] = get_string('restoreuserconflict', '', $user->username);
1434             } else if ($usercheck === true) { // User needs to be created, check if we are able
1435                 if ($cancreateuser) { // Can create user, set newitemid to 0 so will be created later
1436                     self::set_backup_ids_record($restoreid, 'user', $recuser->itemid, 0, null, (array)$user);
1438                 } else { // Cannot create user, report it as problem
1439                     $problems[] = get_string('restorecannotcreateuser', '', $user->username);
1440                 }
1442             } else { // Shouldn't arrive here ever, something is for sure wrong. Exception
1443                 throw new restore_dbops_exception('restore_error_processing_user', $user->username);
1444             }
1445         }
1446         $rs->close();
1447         return $problems;
1448     }
1450     /**
1451      * Process the needed users in order to decide
1452      * which action to perform with them (create/map)
1453      *
1454      * Just wrap over precheck_included_users(), returning
1455      * exception if any problem is found
1456      */
1457     public static function process_included_users($restoreid, $courseid, $userid, $samesite) {
1458         global $DB;
1460         // Just let precheck_included_users() to do all the hard work
1461         $problems = self::precheck_included_users($restoreid, $courseid, $userid, $samesite);
1463         // With problems, throw exception, shouldn't happen if prechecks were originally
1464         // executed, so be radical here.
1465         if (!empty($problems)) {
1466             throw new restore_dbops_exception('restore_problems_processing_users', null, implode(', ', $problems));
1467         }
1468     }
1470     /**
1471      * Process the needed question categories and questions
1472      * to check all them, deciding about the action to perform
1473      * (create/map) and target.
1474      *
1475      * Just wrap over precheck_categories_and_questions(), returning
1476      * exception if any problem is found
1477      */
1478     public static function process_categories_and_questions($restoreid, $courseid, $userid, $samesite) {
1479         global $DB;
1481         // Just let precheck_included_users() to do all the hard work
1482         $problems = self::precheck_categories_and_questions($restoreid, $courseid, $userid, $samesite);
1484         // With problems of type error, throw exception, shouldn't happen if prechecks were originally
1485         // executed, so be radical here.
1486         if (array_key_exists('errors', $problems)) {
1487             throw new restore_dbops_exception('restore_problems_processing_questions', null, implode(', ', $problems['errors']));
1488         }
1489     }
1491     public static function set_backup_files_record($restoreid, $filerec) {
1492         global $DB;
1494         // Store external files info in `info` field
1495         $filerec->info     = base64_encode(serialize($filerec)); // Serialize the whole rec in info
1496         $filerec->backupid = $restoreid;
1497         $DB->insert_record('backup_files_temp', $filerec);
1498     }
1500     public static function set_backup_ids_record($restoreid, $itemname, $itemid, $newitemid = 0, $parentitemid = null, $info = null) {
1501         // Build conditionally the extra record info
1502         $extrarecord = array();
1503         if ($newitemid != 0) {
1504             $extrarecord['newitemid'] = $newitemid;
1505         }
1506         if ($parentitemid != null) {
1507             $extrarecord['parentitemid'] = $parentitemid;
1508         }
1509         if ($info != null) {
1510             $extrarecord['info'] = base64_encode(serialize($info));
1511         }
1513         self::set_backup_ids_cached($restoreid, $itemname, $itemid, $extrarecord);
1514     }
1516     public static function get_backup_ids_record($restoreid, $itemname, $itemid) {
1517         $dbrec = self::get_backup_ids_cached($restoreid, $itemname, $itemid);
1519         if ($dbrec && isset($dbrec->info) && is_string($dbrec->info)) {
1520             $dbrec->info = unserialize(base64_decode($dbrec->info));
1521         }
1523         return $dbrec;
1524     }
1526     /**
1527      * Given on courseid, fullname and shortname, calculate the correct fullname/shortname to avoid dupes
1528      */
1529     public static function calculate_course_names($courseid, $fullname, $shortname) {
1530         global $CFG, $DB;
1532         $currentfullname = '';
1533         $currentshortname = '';
1534         $counter = 0;
1535         // Iteratere while the name exists
1536         do {
1537             if ($counter) {
1538                 $suffixfull  = ' ' . get_string('copyasnoun') . ' ' . $counter;
1539                 $suffixshort = '_' . $counter;
1540             } else {
1541                 $suffixfull  = '';
1542                 $suffixshort = '';
1543             }
1544             $currentfullname = $fullname.$suffixfull;
1545             $currentshortname = substr($shortname, 0, 100 - strlen($suffixshort)).$suffixshort; // < 100cc
1546             $coursefull  = $DB->get_record_select('course', 'fullname = ? AND id != ?',
1547                     array($currentfullname, $courseid), '*', IGNORE_MULTIPLE);
1548             $courseshort = $DB->get_record_select('course', 'shortname = ? AND id != ?', array($currentshortname, $courseid));
1549             $counter++;
1550         } while ($coursefull || $courseshort);
1552         // Return results
1553         return array($currentfullname, $currentshortname);
1554     }
1556     /**
1557      * For the target course context, put as many custom role names as possible
1558      */
1559     public static function set_course_role_names($restoreid, $courseid) {
1560         global $DB;
1562         // Get the course context
1563         $coursectx = context_course::instance($courseid);
1564         // Get all the mapped roles we have
1565         $rs = $DB->get_recordset('backup_ids_temp', array('backupid' => $restoreid, 'itemname' => 'role'), '', 'itemid');
1566         foreach ($rs as $recrole) {
1567             // Get the complete temp_ids record
1568             $role = (object)self::get_backup_ids_record($restoreid, 'role', $recrole->itemid);
1569             // If it's one mapped role and we have one name for it
1570             if (!empty($role->newitemid) && !empty($role->info['nameincourse'])) {
1571                 // If role name doesn't exist, add it
1572                 $rolename = new stdclass();
1573                 $rolename->roleid = $role->newitemid;
1574                 $rolename->contextid = $coursectx->id;
1575                 if (!$DB->record_exists('role_names', (array)$rolename)) {
1576                     $rolename->name = $role->info['nameincourse'];
1577                     $DB->insert_record('role_names', $rolename);
1578                 }
1579             }
1580         }
1581         $rs->close();
1582     }
1584     /**
1585      * Creates a skeleton record within the database using the passed parameters
1586      * and returns the new course id.
1587      *
1588      * @global moodle_database $DB
1589      * @param string $fullname
1590      * @param string $shortname
1591      * @param int $categoryid
1592      * @return int The new course id
1593      */
1594     public static function create_new_course($fullname, $shortname, $categoryid) {
1595         global $DB;
1596         $category = $DB->get_record('course_categories', array('id'=>$categoryid), '*', MUST_EXIST);
1598         $course = new stdClass;
1599         $course->fullname = $fullname;
1600         $course->shortname = $shortname;
1601         $course->category = $category->id;
1602         $course->sortorder = 0;
1603         $course->timecreated  = time();
1604         $course->timemodified = $course->timecreated;
1605         // forcing skeleton courses to be hidden instead of going by $category->visible , until MDL-27790 is resolved.
1606         $course->visible = 0;
1608         $courseid = $DB->insert_record('course', $course);
1610         $category->coursecount++;
1611         $DB->update_record('course_categories', $category);
1613         return $courseid;
1614     }
1616     /**
1617      * Deletes all of the content associated with the given course (courseid)
1618      * @param int $courseid
1619      * @param array $options
1620      * @return bool True for success
1621      */
1622     public static function delete_course_content($courseid, array $options = null) {
1623         return remove_course_contents($courseid, false, $options);
1624     }
1627 /*
1628  * Exception class used by all the @dbops stuff
1629  */
1630 class restore_dbops_exception extends backup_exception {
1632     public function __construct($errorcode, $a=NULL, $debuginfo=null) {
1633         parent::__construct($errorcode, 'error', '', $a, null, $debuginfo);
1634     }