fixed docs typos
[moodle.git] / mod / resource / db / upgradelib.php
CommitLineData
aa54ed7b 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 * Resource module upgrade related helper functions
20 *
21 * @package mod-resource
22 * @copyright 2009 Petr Skoda (http://skodak.org)
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
aa54ed7b 26/**
27 * Migrate resource module data from 1.9 resource_old table to new resource table
28 * @return void
29 */
30function resource_20_migrate() {
31 global $CFG, $DB;
32
33 require_once("$CFG->libdir/filelib.php");
34 require_once("$CFG->libdir/resourcelib.php");
35 require_once("$CFG->dirroot/course/lib.php");
36
37 $fs = get_file_storage();
38
39 $withrelativelinks = array('text/html', 'text/xml', 'application/xhtml+xml', 'application/x-shockwave-flash');
40 // note: pdf doc and other types may contain links too, but we do not support relative links there
41
42 if (!$candidates = $DB->get_recordset('resource_old', array('type'=>'file', 'migrated'=>0))) {
43 return;
44 }
45
46 foreach ($candidates as $candidate) {
47 upgrade_set_timeout();
48
49 $path = $candidate->reference;
50 $siteid = get_site()->id;
51 $fs = get_file_storage();
52
53 if (strpos($path, 'LOCALPATH') === 0) {
54 // ignore not maintained local files - sorry
55 continue;
56
57 } else if (preg_match("|$CFG->wwwroot/file.php(\?file=)?/$siteid(/[^\s'\"&\?#]+)|", $path, $matches)) {
58 // public site files
59 $path = $matches[2];
60
61 $resource = new object();
62 $resource->id = $candidate->oldid;
63 $resource->tobemigrated = 0;
64 $resource->mainfile = $path;
65 $resource->filterfiles = $CFG->filteruploadedfiles;
66 $resource->legacyfiles = RESOURCELIB_LEGACYFILES_NO; // on-demand-migration not possible for site files, sorry
67
68 $context = get_context_instance(CONTEXT_MODULE, $candidate->cmid);
69 $sitecontext = get_context_instance(CONTEXT_COURSE, $siteid);
64f93798
PS
70 $file_record = array('contextid'=>$context->id, 'component'=>'mod_resourse', 'filearea'=>'content', 'itemid'=>0);
71 if ($file = $fs->get_file_by_hash(sha1("/$sitecontext->id/course/legacy/content/0".$path))) {
aa54ed7b 72 try {
73 $fs->create_file_from_storedfile($file_record, $file);
74 } catch (Exception $x) {
75 }
76 $resource->mainfile = $file->get_filepath().$file->get_filename();
77 }
78
79 } else if (preg_match("|$CFG->wwwroot/file.php(\?file=)?/$candidate->course(/[^\s'\"&\?#]+)|", $path, $matches)) {
80 // current course files
81 $path = $matches[2];
82
83 $resource = new object();
84 $resource->id = $candidate->oldid;
85 $resource->tobemigrated = 0;
86 $resource->mainfile = $path;
87 $resource->filterfiles = $CFG->filteruploadedfiles;
88 $mimetype = mimeinfo('type', $resource->mainfile);
89 if (in_array($mimetype, $withrelativelinks)) {
90 $resource->legacyfiles = RESOURCELIB_LEGACYFILES_ACTIVE;
91 } else {
92 $resource->legacyfiles = RESOURCELIB_LEGACYFILES_NO;
93 }
94
95 // try migration of main file - ignore if does not exist
64f93798 96 if ($file = resourcelib_try_file_migration($resource->mainfile, $candidate->cmid, $candidate->course, 'mod_resource', 'content', 0)) {
aa54ed7b 97 $resource->mainfile = $file->get_filepath().$file->get_filename();
98 }
99
100 } else if (strpos($path, '://') or strpos($path, '/') === 0) {
101 // http:// https:// ftp:// OR starts with slash - to be converted to link resource
102 continue;
103
104 } else {
105 // current course files
106 // cleanup old path first
107 $path = '/'.trim(trim($path), '/');
108 if (strpos($path, '?forcedownload=1') !== false) {
109 // eliminate old force download tricks
110 $candidate->options = 'forcedownload';
111 $path = str_replace('?forcedownload=1', '', $path);
112 }
113 // get rid of any extra url parameters, sorry we can not support these
114 preg_match("/^[^?#]+/", $path, $matches);
115 $parts = $matches[0];
116
117 $resource = new object();
118 $resource->id = $candidate->oldid;
119 $resource->tobemigrated = 0;
120 $resource->mainfile = $path;
121 $resource->filterfiles = $CFG->filteruploadedfiles;
122 $mimetype = mimeinfo('type', $resource->mainfile);
123 if (in_array($mimetype, $withrelativelinks)) {
124 $resource->legacyfiles = RESOURCELIB_LEGACYFILES_ACTIVE;
125 } else {
126 $resource->legacyfiles = RESOURCELIB_LEGACYFILES_NO;
127 }
128
129 // try migration of main file - ignore if does not exist
64f93798 130 if ($file = resourcelib_try_file_migration($resource->mainfile, $candidate->cmid, $candidate->course, 'mod_resource', 'content', 0)) {
aa54ed7b 131 $resource->mainfile = $file->get_filepath().$file->get_filename();
132 }
133 }
134
135 $options = array('printheading'=>0, 'printintro'=>1);
136 if ($candidate->options == 'frame') {
137 $resource->display = RESOURCELIB_DISPLAY_FRAME;
138
139 } else if ($candidate->options == 'objectframe') {
140 $resource->display = RESOURCELIB_DISPLAY_EMBED;
141
142 } else if ($candidate->options == 'forcedownload') {
143 $resource->display = RESOURCELIB_DISPLAY_DOWNLOAD;
144
145 } else if ($candidate->popup) {
146 $resource->display = RESOURCELIB_DISPLAY_POPUP;
147 if ($candidate->popup) {
148 $rawoptions = explode(',', $candidate->popup);
149 foreach ($rawoptions as $rawoption) {
150 list($name, $value) = explode('=', trim($rawoption), 2);
151 if ($value > 0 and ($name == 'width' or $name == 'height')) {
152 $options['popup'.$name] = $value;
153 continue;
154 }
155 }
156 }
157
158 } else {
159 $resource->display = RESOURCELIB_DISPLAY_AUTO;
160 }
161 $resource->displayoptions = serialize($options);
162
163 // update resource instance and mark as migrated
164 $DB->update_record('resource', $resource);
165 $candidate->newmodule = 'resource';
166 $candidate->newid = $candidate->oldid;
167 $candidate->migrated = time();
168 $DB->update_record('resource_old', $candidate);
169 }
170
171 $candidates->close();
172
173 // clear all course modinfo caches
174 rebuild_course_cache(0, true);
175}
176
177/**
6489e361 178 * This function creates resource_old table and copies all data
aa54ed7b 179 * from resource table, this functions has to be called from
6489e361 180 * all modules that are successors of old resource module types.
aa54ed7b 181 * @return bool true if migration required, false if not
182 */
183function resource_20_prepare_migration() {
184 global $DB;
185
186 $dbman = $DB->get_manager();
187
6489e361 188 // If the resource not created yet, this is probably a new install
aa54ed7b 189 $table = new xmldb_table('resource');
190 if (!$dbman->table_exists($table)) {
191 return false;
192 }
193
194 // Define table resource_old to be created
195 $table = new xmldb_table('resource_old');
196
197 if ($dbman->table_exists($table)) {
198 //already executed
199 return true;
200 }
201
202 // Adding fields to table resource_old
203 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
204 $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
205 $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
206 $table->add_field('type', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null);
207 $table->add_field('reference', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
208 $table->add_field('intro', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
209 $table->add_field('introformat', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
210 $table->add_field('alltext', XMLDB_TYPE_TEXT, 'medium', null, XMLDB_NOTNULL, null, null);
211 $table->add_field('popup', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null);
212 $table->add_field('options', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
213 $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
214 $table->add_field('oldid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
215 $table->add_field('cmid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
216 $table->add_field('newmodule', XMLDB_TYPE_CHAR, '50', null, null, null, null);
217 $table->add_field('newid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
218 $table->add_field('migrated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
219
220 // Adding keys to table resource_old
221 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
222
223 // Adding indexes to table resource_old
224 $table->add_index('oldid', XMLDB_INDEX_UNIQUE, array('oldid'));
225 $table->add_index('cmid', XMLDB_INDEX_NOTUNIQUE, array('cmid'));
226
227 // Launch create table for resource_old
228 $dbman->create_table($table);
229
230 $module = $DB->get_field('modules', 'id', array('name'=>'resource'));
231
232 if (!$DB->count_records('resource')) {
233 // upgrade of fresh new server from 1.9 - no upgrade needed
234 return false;
235 }
236
237 // find out if resource contains new 2.0dev fields already
238 $table = new xmldb_table('resource');
239 $field = new xmldb_field('introformat', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'intro');
240 if ($dbman->field_exists($table, $field)) {
241 $sql = "INSERT INTO {resource_old} (oldid, course, name, type, reference, intro, introformat, alltext, popup, options, timemodified, cmid)
242 SELECT r.id, r.course, r.name, r.type, r.reference, r.intro, r.introformat, r.alltext, r.popup, r.options, r.timemodified, cm.id
243 FROM {resource} r
244 LEFT JOIN {course_modules} cm ON (r.id = cm.instance AND cm.module = :module)";
245 } else {
246 $sql = "INSERT INTO {resource_old} (oldid, course, name, type, reference, intro, introformat, alltext, popup, options, timemodified, cmid)
247 SELECT r.id, r.course, r.name, r.type, r.reference, r.summary, 0, r.alltext, r.popup, r.options, r.timemodified, cm.id
248 FROM {resource} r
249 LEFT JOIN {course_modules} cm ON (r.id = cm.instance AND cm.module = :module)";
250 }
251
252 $DB->execute($sql, array('module'=>$module));
253
254 return true;
255}
256
257/**
258 * Migrate old resource type to new module, updates all related info, keeps the context id.
259 * @param string $modname name of new module
260 * @param object $candidate old instance from resource_old
6489e361 261 * @param object $newinstance new module instance to be inserted into db
aa54ed7b 262 * @return mixed false if error, object new instance with id if success
263 */
264function resource_migrate_to_module($modname, $candidate, $newinstance) {
265 global $DB;
266
267 if (!$cm = get_coursemodule_from_id('resource', $candidate->cmid)) {
268 return false;
269 }
270
271 if (!$module = $DB->get_record('modules', array('name'=>$modname))) {
272 return false;
273 }
274
275 if (!$resource = $DB->get_record('resource', array('id'=>$candidate->oldid))) {
276 return false;
277 }
278
279 // insert new instance
280 $newinstance->id = $DB->insert_record($modname, $newinstance);
281
282 // update course modules
283 $cm->module = $module->id;
284 $cm->instance = $newinstance->id;
285 $DB->update_record('course_modules', $cm);
286
287 //delete old record
288 $DB->delete_records('resource', array('id'=>$resource->id));
289
290 //mark as migrated
291 $candidate->newmodule = $modname;
292 $candidate->newid = $newinstance->id;
293 $candidate->migrated = time();
294 $DB->update_record('resource_old', $candidate);
295
296 //no need to upgrade data in logs because resource module is able to redirect to migrated instances
297 return $newinstance;
298}