MDL-34629 wiki: convert default format value during restore
[moodle.git] / mod / wiki / backup / moodle1 / lib.php
CommitLineData
61ff1fec
AB
1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/**
19 * Provides support for the conversion of moodle1 backup to the moodle2 format
728ebac7 20 * Based off of a template @ http://docs.moodle.org/dev/Backup_1.9_conversion_for_developers
61ff1fec
AB
21 *
22 * @package mod
23 * @subpackage wiki
24 * @copyright 2011 Aparup Banerjee <aparup@moodle.com>
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 */
27
28defined('MOODLE_INTERNAL') || die();
29
30/**
31 * Wiki conversion handler
32 */
33class moodle1_mod_wiki_handler extends moodle1_mod_handler {
c5c315e2
DM
34
35 /** @var string initial content for creating first page from the optional 1.9 wiki file */
61ff1fec 36 protected $initialcontent;
c5c315e2
DM
37
38 /** @var string */
61ff1fec
AB
39 protected $initialcontentfilename;
40
41 /** @var bool initial content page already exists */
42 protected $needinitpage = false;
43
44 /** @var array a data element transfer buffer, can be used for transfer of data between xml path levels. */
45 protected $databuf = array();
46
c5c315e2
DM
47 /** @var moodle1_file_manager */
48 protected $fileman = null;
49
50 /** @var int cmid */
51 protected $moduleid = null;
52
61ff1fec
AB
53 /**
54 * Declare the paths in moodle.xml we are able to convert
55 *
56 * The method returns list of {@link convert_path} instances.
57 * For each path returned, the corresponding conversion method must be
58 * defined.
59 *
60 * Note that the path /MOODLE_BACKUP/COURSE/MODULES/MOD/WIKI does not
61 * actually exist in the file. The last element with the module name was
62 * appended by the moodle1_converter class.
63 *
64 * @return array of {@link convert_path} instances
65 */
66 public function get_paths() {
67 return array(
68 new convert_path(
69 'wiki', '/MOODLE_BACKUP/COURSE/MODULES/MOD/WIKI',
70 array(
71 'newfields' => array(
72 'introformat' => '0',
73 'defaultformat' => 'html', //1.9 migrations default to html
74 'forceformat' => '1',
75 'editbegin' => '0',
76 'editend' => '0',
77 'timecreated' => time(), //2.x time of creation since theres no 1.9 time of creation
78 ),
79 'renamefields' => array(
80 'summary' => 'intro',
81 'format' => 'introformat',
82 'firstpagetitle' => 'pagename',
83 'wtype' => 'wikimode'
84 ),
85 'dropfields' => array(
86 'pagename', 'scaleid', 'ewikiprinttitle', 'htmlmode', 'ewikiacceptbinary', 'disablecamelcase',
87 'setpageflags', 'strippages', 'removepages', 'revertchanges'
88 )
89 )
90 ),
91 new convert_path(
92 'wiki_entries', '/MOODLE_BACKUP/COURSE/MODULES/MOD/WIKI/ENTRIES',
93 array(
94 'newfields' => array(
95 'synonyms' => '0',
96 'links' => 'collaborative',
97 ),
98 'dropfields' => array(
99 'pagename' ,'timemodified'
100 )
101 )
102 ),
103 new convert_path(
104 'wiki_entry', '/MOODLE_BACKUP/COURSE/MODULES/MOD/WIKI/ENTRIES/ENTRY'
105 ),
106 new convert_path(
107 'wiki_pages', '/MOODLE_BACKUP/COURSE/MODULES/MOD/WIKI/ENTRIES/ENTRY/PAGES'
108 ),
109 new convert_path(
110 'wiki_entry_page', '/MOODLE_BACKUP/COURSE/MODULES/MOD/WIKI/ENTRIES/ENTRY/PAGES/PAGE',
111 array(
112 'newfields' => array(
113 'cachedcontent' => '**reparse needed**',
114 'timerendered' => '0',
115 'readonly' => '0',
116 'tags' => ''
117 ),
118 'renamefields' => array(
119 'pagename' => 'title',
120 'created' => 'timecreated',
121 'lastmodified' => 'timemodified',
122 'hits' => 'pageviews'
123 ),
124 'dropfields' => array(
125 'version', 'flags', 'author', 'refs', //refs will be reparsed during rendering
126 'meta'
127 )
128 )
129 )
130 );
131 }
132
133 /**
134 * This is executed every time we have one /MOODLE_BACKUP/COURSE/MODULES/MOD/WIKI
135 * data available
136 */
137 public function process_wiki($data) {
cf162f93
CF
138 global $CFG; // We need to check a config setting.
139
61ff1fec
AB
140 if (!empty($data['initialcontent'])) {
141 //convert file in <INITIALCONTENT>filename</INITIALCONTENT> into a subwiki page if no entry created.
142 $temppath = $this->converter->get_tempdir_path();
143 $this->initialcontent = file_get_contents($temppath.'/course_files/'.$data['initialcontent']);
144 $this->initialcontentfilename = $data['initialcontent'];
145 $this->needinitpage = true;
146 }
147 unset($data['initialcontent']);
148 if ($data['wikimode'] !== 'group') {
149 $data['wikimode'] = 'individual';
150 //@todo need to create extra subwikis due to individual wikimode?
151 //this would then need to reference the users in the course that is being restored.(some parent class API needed)
152 } else {
153 $data['wikimode'] = 'collaborative';
154 }
155
156 if (empty($data['name'])) {
157 $data['name'] = 'Wiki';
158 }
159 // get the course module id and context id
c5c315e2
DM
160 $instanceid = $data['id'];
161 $cminfo = $this->get_cminfo($instanceid);
162 $this->moduleid = $cminfo['id'];
163 $contextid = $this->converter->get_contextid(CONTEXT_MODULE, $this->moduleid);
164
165 // get a fresh new file manager for this instance
166 $this->fileman = $this->converter->get_file_manager($contextid, 'mod_wiki');
167
168 // convert course files embedded into the intro
169 $this->fileman->filearea = 'intro';
170 $this->fileman->itemid = 0;
171 $data['intro'] = moodle1_converter::migrate_referenced_files($data['intro'], $this->fileman);
61ff1fec 172
cf162f93
CF
173 // convert the introformat if necessary
174 if ($CFG->texteditors !== 'textarea') {
175 $data['intro'] = text_to_html($data['intro'], false, false, true);
176 $data['introformat'] = FORMAT_HTML;
177 }
178
61ff1fec 179 // we now have all information needed to start writing into the file
c5c315e2
DM
180 $this->open_xml_writer("activities/wiki_{$this->moduleid}/wiki.xml");
181 $this->xmlwriter->begin_tag('activity', array('id' => $instanceid, 'moduleid' => $this->moduleid,
61ff1fec
AB
182 'modulename' => 'wiki', 'contextid' => $contextid));
183 $this->xmlwriter->begin_tag('wiki', array('id' => $instanceid));
184
61ff1fec 185 foreach ($data as $field => $value) {
c5c315e2
DM
186 if ($field <> 'id') {
187 $this->xmlwriter->full_tag($field, $value);
188 }
61ff1fec 189 }
c5c315e2
DM
190
191 return $data;
61ff1fec
AB
192 }
193
61ff1fec
AB
194 public function on_wiki_entries_start() {
195 $this->xmlwriter->begin_tag('subwikis');
196 $this->needinitpage = false; //backup has entries, so the initial_content file has been stored as a page in 1.9.
197 }
c5c315e2 198
61ff1fec
AB
199 public function on_wiki_entries_end() {
200 $this->xmlwriter->end_tag('subwikis');
201 }
202
61ff1fec
AB
203 public function process_wiki_entry($data) {
204 $this->xmlwriter->begin_tag('subwiki', array('id' => $data['id']));
205 unset($data['id']);
206
207 unset($data['pagename']);
208 unset($data['timemodified']);
209
210 foreach ($data as $field => $value) {
211 $this->xmlwriter->full_tag($field, $value);
212 }
213 }
c5c315e2 214
61ff1fec
AB
215 public function on_wiki_entry_end() {
216 $this->xmlwriter->end_tag('subwiki');
217 }
218
61ff1fec
AB
219 public function on_wiki_pages_start() {
220 $this->xmlwriter->begin_tag('pages');
221 }
c5c315e2 222
61ff1fec
AB
223 public function on_wiki_pages_end() {
224 $this->xmlwriter->end_tag('pages');
225 }
226
61ff1fec
AB
227 public function process_wiki_entry_page($data) {
228 // assimilate data to create later in extra virtual path page/versions/version/
229 $this->databuf['id'] = $this->converter->get_nextid();
230 $this->databuf['content'] = $data['content'];
231 unset($data['content']);
232 $this->databuf['contentformat'] = 'html';
233 $this->databuf['version'] = 0;
234 $this->databuf['timecreated'] = $data['timecreated']; //do not unset, is reused
235 $this->databuf['userid'] = $data['userid']; //do not unset, is reused
236
c5c315e2 237 // process page data (user data and also the one that is from <initialcontent>
61ff1fec
AB
238 $this->xmlwriter->begin_tag('page', array('id' => $data['id']));
239 unset($data['id']); // we already write it as attribute, do not repeat it as child element
240 foreach ($data as $field => $value) {
241 $this->xmlwriter->full_tag($field, $value);
242 }
243
c5c315e2 244 // process page content as a version.
61ff1fec
AB
245 $this->xmlwriter->begin_tag('versions');
246 $this->write_xml('version', $this->databuf, array('/version/id')); //version id from get_nextid()
247 $this->xmlwriter->end_tag('versions');
248 }
249 public function on_wiki_entry_page_end() {
250 $this->xmlwriter->end_tag('page');
251 }
252
253 /**
254 * This is executed when we reach the closing </MOD> tag of our 'wiki' path
255 */
256 public function on_wiki_end() {
257 global $USER;
c5c315e2
DM
258
259 //check if the initial content needs to be created (and if a page is already there for it)
61ff1fec
AB
260 if ($this->initialcontentfilename && $this->needinitpage) {
261 //contruct (synthetic - not for cooking) a full path for creating entries/entry/pages/page
262 $data_entry = array(
263 'id' => $this->converter->get_nextid(), //creating the first entry
264 'groupid' => 0,
265 'userid' => 0,
266 'synonyms' => '',
267 'links' => ''
268 );
269 $data_page = array(
270 'id' => $this->converter->get_nextid(), //just creating the first page in the wiki
271 'title' => $this->initialcontentfilename,
272 'content' => $this->initialcontent,
273 'userid' => $USER->id,
274 'timecreated' => time(),
275 'timemodified' => 0,
276 'pageviews' => 0,
277 'cachedcontent' => '**reparse needed**',
278 'timerendered' => 0,
279 'readonly' => 0,
280 'tags' => ''
281 );
282 //create xml with constructed page data (from initial_content file).
283 $this->on_wiki_entries_start();
c5c315e2
DM
284 $this->process_wiki_entry($data_entry);
285 $this->on_wiki_pages_start();
286 $this->process_wiki_entry_page($data_page);
287 $this->on_wiki_entry_page_end();
288 $this->on_wiki_pages_end();
289 $this->on_wiki_entry_end();
61ff1fec
AB
290 $this->on_wiki_entries_end();
291 }
292
293 //close wiki.xml
294 $this->xmlwriter->end_tag('wiki');
295 $this->xmlwriter->end_tag('activity');
296 $this->close_xml_writer();
c5c315e2
DM
297
298 // write inforef.xml
299 $this->open_xml_writer("activities/wiki_{$this->moduleid}/inforef.xml");
300 $this->xmlwriter->begin_tag('inforef');
301 $this->xmlwriter->begin_tag('fileref');
302 foreach ($this->fileman->get_fileids() as $fileid) {
303 $this->write_xml('file', array('id' => $fileid));
304 }
305 $this->xmlwriter->end_tag('fileref');
306 $this->xmlwriter->end_tag('inforef');
307 $this->close_xml_writer();
61ff1fec
AB
308 }
309}