MDL-24759 fixing incorrect skipping on non-url file type resources, credit goes to...
[moodle.git] / mod / url / db / upgradelib.php
CommitLineData
58a27a74 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 * URL module upgrade related helper functions
20 *
2b641d15
PS
21 * @package mod
22 * @subpackage url
23 * @copyright 2009 Petr Skoda {@link http://skodak.org}
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
58a27a74 25 */
26
2b641d15
PS
27defined('MOODLE_INTERNAL') || die;
28
58a27a74 29/**
30 * Migrate url module data from 1.9 resource_old table to new url table
31 * @return void
32 */
33function url_20_migrate() {
34 global $CFG, $DB;
35
36 require_once("$CFG->libdir/filelib.php");
37 require_once("$CFG->libdir/resourcelib.php");
38 require_once("$CFG->dirroot/course/lib.php");
39
40 if (!file_exists("$CFG->dirroot/mod/resource/db/upgradelib.php")) {
41 // bad luck, somebody deleted resource module
42 return;
43 }
44
45 require_once("$CFG->dirroot/mod/resource/db/upgradelib.php");
46
47 // create resource_old table and copy resource table there if needed
48 if (!resource_20_prepare_migration()) {
49 // no modules or fresh install
50 return;
51 }
52
53 if (!$candidates = $DB->get_recordset('resource_old', array('type'=>'file', 'migrated'=>0))) {
54 return;
55 }
58a27a74 56
57 foreach ($candidates as $candidate) {
58 $path = $candidate->reference;
59 $siteid = get_site()->id;
60
61 if (strpos($path, 'LOCALPATH') === 0) {
62 // ignore not maintained local files - sorry
63 continue;
64 } else if (!strpos($path, '://')) {
65 // not URL
113072bd 66 continue;
58a27a74 67 } else if (preg_match("|$CFG->wwwroot/file.php(\?file=)?/$siteid(/[^\s'\"&\?#]+)|", $path, $matches)) {
68 // handled by resource module
113072bd 69 continue;
58a27a74 70 } else if (preg_match("|$CFG->wwwroot/file.php(\?file=)?/$candidate->course(/[^\s'\"&\?#]+)|", $path, $matches)) {
71 // handled by resource module
113072bd 72 continue;
58a27a74 73 }
74
75 upgrade_set_timeout();
76
b8091a0b
PS
77 if ($CFG->texteditors !== 'textarea') {
78 $intro = text_to_html($candidate->intro, false, false, true);
79 $introformat = FORMAT_HTML;
80 } else {
81 $intro = $candidate->intro;
82 $introformat = FORMAT_MOODLE;
83 }
84
39790bd8 85 $url = new stdClass();
58a27a74 86 $url->course = $candidate->course;
87 $url->name = $candidate->name;
b8091a0b
PS
88 $url->intro = $intro;
89 $url->introformat = $introformat;
58a27a74 90 $url->externalurl = $path;
91 $url->timemodified = time();
92
93 $options = array('printheading'=>0, 'printintro'=>1);
94 $parameters = array();
95 if ($candidate->options == 'frame') {
96 $url->display = RESOURCELIB_DISPLAY_FRAME;
97
98 } else if ($candidate->options == 'objectframe') {
99 $url->display = RESOURCELIB_DISPLAY_EMBED;
100
101 } else if ($candidate->popup) {
102 $url->display = RESOURCELIB_DISPLAY_POPUP;
103 if ($candidate->popup) {
104 $rawoptions = explode(',', $candidate->popup);
105 foreach ($rawoptions as $rawoption) {
106 list($name, $value) = explode('=', trim($rawoption), 2);
107 if ($value > 0 and ($name == 'width' or $name == 'height')) {
108 $options['popup'.$name] = $value;
109 continue;
110 }
111 }
112 }
113
114 } else {
115 $url->display = RESOURCELIB_DISPLAY_AUTO;
116 }
117 $url->displayoptions = serialize($options);
118
119 if ($candidate->alltext) {
120 $rawoptions = explode(',', $candidate->alltext);
121 foreach ($rawoptions as $rawoption) {
122 list($variable, $parameter) = explode('=', trim($rawoption), 2);
123 $parameters[$parameter] = $variable;
124 }
125 }
126
127 $url->parameters = serialize($parameters);
128
129 if (!$url = resource_migrate_to_module('url', $candidate, $url)) {
130 continue;
131 }
132 }
133
134 $candidates->close();
135
136 // clear all course modinfo caches
137 rebuild_course_cache(0, true);
138}