MDL-35146 mod_wiki: fixed issue where pages for deleted groups caused an error on...
[moodle.git] / mod / wiki / backup / moodle2 / restore_wiki_stepslib.php
CommitLineData
36ee2cae
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 * @package moodlecore
20 * @subpackage backup-moodle2
21 * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25/**
26 * Define all the restore steps that will be used by the restore_wiki_activity_task
27 */
28
29/**
30 * Structure step to restore one wiki activity
31 */
32class restore_wiki_activity_structure_step extends restore_activity_structure_step {
33
34 protected function define_structure() {
35
36 $paths = array();
37 $userinfo = $this->get_setting_value('userinfo');
38
39 $paths[] = new restore_path_element('wiki', '/activity/wiki');
40 if ($userinfo) {
41 $paths[] = new restore_path_element('wiki_subwiki', '/activity/wiki/subwikis/subwiki');
42 $paths[] = new restore_path_element('wiki_page', '/activity/wiki/subwikis/subwiki/pages/page');
43 $paths[] = new restore_path_element('wiki_version', '/activity/wiki/subwikis/subwiki/pages/page/versions/version');
07f7954d 44 $paths[] = new restore_path_element('wiki_tag', '/activity/wiki/subwikis/subwiki/pages/page/tags/tag');
36ee2cae
DM
45 $paths[] = new restore_path_element('wiki_synonym', '/activity/wiki/subwikis/subwiki/synonyms/synonym');
46 $paths[] = new restore_path_element('wiki_link', '/activity/wiki/subwikis/subwiki/links/link');
36ee2cae
DM
47 }
48
49 // Return the paths wrapped into standard activity structure
50 return $this->prepare_activity_structure($paths);
51 }
52
53 protected function process_wiki($data) {
54 global $DB;
55
56 $data = (object)$data;
57 $oldid = $data->id;
58 $data->course = $this->get_courseid();
59
60 $data->editbegin = $this->apply_date_offset($data->editbegin);
61 $data->editend = $this->apply_date_offset($data->editend);
26cf184c 62 $data->timemodified = $this->apply_date_offset($data->timemodified);
36ee2cae
DM
63
64 // insert the wiki record
65 $newitemid = $DB->insert_record('wiki', $data);
034ef761 66 // immediately after inserting "activity" record, call this
36ee2cae
DM
67 $this->apply_activity_instance($newitemid);
68 }
69
70 protected function process_wiki_subwiki($data) {
71 global $DB;
72
73
74 $data = (object)$data;
75 $oldid = $data->id;
76 $data->wikiid = $this->get_new_parentid('wiki');
19b2d3fa
AF
77 if($data->groupid !== '0') $data->groupid = $this->get_mappingid('group', $data->groupid);
78 if($date->userid !== '0') $data->userid = $this->get_mappingid('user', $data->userid);
36ee2cae 79
19b2d3fa
AF
80 if($data->groupid !== false && $data->userid !== false) {
81 $newitemid = $DB->insert_record('wiki_subwikis', $data);
82 } else {
83 $newitemid = false;
84 }
36ee2cae
DM
85 $this->set_mapping('wiki_subwiki', $oldid, $newitemid);
86 }
87 protected function process_wiki_page($data) {
88 global $DB;
89
90 $data = (object)$data;
91 $oldid = $data->id;
92 $data->subwikiid = $this->get_new_parentid('wiki_subwiki');
93 $data->userid = $this->get_mappingid('user', $data->userid);
26cf184c
DM
94 $data->timemodified = $this->apply_date_offset($data->timemodified);
95 $data->timecreated = $this->apply_date_offset($data->timecreated);
96 $data->timerendered = $this->apply_date_offset($data->timerendered);
36ee2cae 97
19b2d3fa
AF
98 if($data->subwikiid !== false) {
99 $newitemid = $DB->insert_record('wiki_pages', $data);
100 } else {
101 $newitemid = false;
102 }
07f7954d 103 $this->set_mapping('wiki_page', $oldid, $newitemid, true); // There are files related to this
36ee2cae
DM
104 }
105 protected function process_wiki_version($data) {
106 global $DB;
107
108 $data = (object)$data;
109 $oldid = $data->id;
110 $data->pageid = $this->get_new_parentid('wiki_page');
111 $data->userid = $this->get_mappingid('user', $data->userid);
26cf184c 112 $data->timecreated = $this->apply_date_offset($data->timecreated);
36ee2cae
DM
113
114 $newitemid = $DB->insert_record('wiki_versions', $data);
26cf184c 115 $this->set_mapping('wiki_version', $oldid, $newitemid);
36ee2cae
DM
116 }
117 protected function process_wiki_synonym($data) {
118 global $DB;
119
120 $data = (object)$data;
121 $oldid = $data->id;
122 $data->subwikiid = $this->get_new_parentid('wiki_subwiki');
123 $data->pageid = $this->get_mappingid('wiki_page', $data->pageid);
124
125 $newitemid = $DB->insert_record('wiki_synonyms', $data);
126 // No need to save this mapping as far as nothing depend on it
127 // (child paths, file areas nor links decoder)
128 }
129 protected function process_wiki_link($data) {
130 global $DB;
131
132 $data = (object)$data;
133 $oldid = $data->id;
134 $data->subwikiid = $this->get_new_parentid('wiki_subwiki');
135 $data->frompageid = $this->get_mappingid('wiki_page', $data->frompageid);
136 $data->topageid = $this->get_mappingid('wiki_page', $data->topageid);
07f7954d 137
36ee2cae
DM
138 $newitemid = $DB->insert_record('wiki_links', $data);
139 // No need to save this mapping as far as nothing depend on it
140 // (child paths, file areas nor links decoder)
141 }
142
07f7954d
EL
143 protected function process_wiki_tag($data) {
144 global $CFG, $DB;
36ee2cae
DM
145
146 $data = (object)$data;
147 $oldid = $data->id;
36ee2cae 148
07f7954d
EL
149 if (empty($CFG->usetags)) { // tags disabled in server, nothing to process
150 return;
151 }
36ee2cae 152
07f7954d
EL
153 $tag = $data->rawname;
154 $itemid = $this->get_new_parentid('wiki_page');
f0f77dfa 155 tag_set_add('wiki_pages', $itemid, $tag);
07f7954d 156 }
36ee2cae
DM
157
158 protected function after_execute() {
159 // Add wiki related files, no need to match by itemname (just internally handled context)
160 $this->add_related_files('mod_wiki', 'intro', null);
07f7954d 161 $this->add_related_files('mod_wiki', 'attachments', 'wiki_page');
36ee2cae
DM
162 }
163}