Commit | Line | Data |
---|---|---|
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 | * | |
21 | * @package url-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 | ||
26 | /** | |
27 | * Migrate url module data from 1.9 resource_old table to new url table | |
28 | * @return void | |
29 | */ | |
30 | function url_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 | if (!file_exists("$CFG->dirroot/mod/resource/db/upgradelib.php")) { | |
38 | // bad luck, somebody deleted resource module | |
39 | return; | |
40 | } | |
41 | ||
42 | require_once("$CFG->dirroot/mod/resource/db/upgradelib.php"); | |
43 | ||
44 | // create resource_old table and copy resource table there if needed | |
45 | if (!resource_20_prepare_migration()) { | |
46 | // no modules or fresh install | |
47 | return; | |
48 | } | |
49 | ||
50 | if (!$candidates = $DB->get_recordset('resource_old', array('type'=>'file', 'migrated'=>0))) { | |
51 | return; | |
52 | } | |
58a27a74 | 53 | |
54 | foreach ($candidates as $candidate) { | |
55 | $path = $candidate->reference; | |
56 | $siteid = get_site()->id; | |
57 | ||
58 | if (strpos($path, 'LOCALPATH') === 0) { | |
59 | // ignore not maintained local files - sorry | |
60 | continue; | |
61 | } else if (!strpos($path, '://')) { | |
62 | // not URL | |
63 | return; | |
64 | } else if (preg_match("|$CFG->wwwroot/file.php(\?file=)?/$siteid(/[^\s'\"&\?#]+)|", $path, $matches)) { | |
65 | // handled by resource module | |
66 | return; | |
67 | } else if (preg_match("|$CFG->wwwroot/file.php(\?file=)?/$candidate->course(/[^\s'\"&\?#]+)|", $path, $matches)) { | |
68 | // handled by resource module | |
69 | return; | |
70 | } | |
71 | ||
72 | upgrade_set_timeout(); | |
73 | ||
b8091a0b PS |
74 | if ($CFG->texteditors !== 'textarea') { |
75 | $intro = text_to_html($candidate->intro, false, false, true); | |
76 | $introformat = FORMAT_HTML; | |
77 | } else { | |
78 | $intro = $candidate->intro; | |
79 | $introformat = FORMAT_MOODLE; | |
80 | } | |
81 | ||
58a27a74 | 82 | $url = new object(); |
83 | $url->course = $candidate->course; | |
84 | $url->name = $candidate->name; | |
b8091a0b PS |
85 | $url->intro = $intro; |
86 | $url->introformat = $introformat; | |
58a27a74 | 87 | $url->externalurl = $path; |
88 | $url->timemodified = time(); | |
89 | ||
90 | $options = array('printheading'=>0, 'printintro'=>1); | |
91 | $parameters = array(); | |
92 | if ($candidate->options == 'frame') { | |
93 | $url->display = RESOURCELIB_DISPLAY_FRAME; | |
94 | ||
95 | } else if ($candidate->options == 'objectframe') { | |
96 | $url->display = RESOURCELIB_DISPLAY_EMBED; | |
97 | ||
98 | } else if ($candidate->popup) { | |
99 | $url->display = RESOURCELIB_DISPLAY_POPUP; | |
100 | if ($candidate->popup) { | |
101 | $rawoptions = explode(',', $candidate->popup); | |
102 | foreach ($rawoptions as $rawoption) { | |
103 | list($name, $value) = explode('=', trim($rawoption), 2); | |
104 | if ($value > 0 and ($name == 'width' or $name == 'height')) { | |
105 | $options['popup'.$name] = $value; | |
106 | continue; | |
107 | } | |
108 | } | |
109 | } | |
110 | ||
111 | } else { | |
112 | $url->display = RESOURCELIB_DISPLAY_AUTO; | |
113 | } | |
114 | $url->displayoptions = serialize($options); | |
115 | ||
116 | if ($candidate->alltext) { | |
117 | $rawoptions = explode(',', $candidate->alltext); | |
118 | foreach ($rawoptions as $rawoption) { | |
119 | list($variable, $parameter) = explode('=', trim($rawoption), 2); | |
120 | $parameters[$parameter] = $variable; | |
121 | } | |
122 | } | |
123 | ||
124 | $url->parameters = serialize($parameters); | |
125 | ||
126 | if (!$url = resource_migrate_to_module('url', $candidate, $url)) { | |
127 | continue; | |
128 | } | |
129 | } | |
130 | ||
131 | $candidates->close(); | |
132 | ||
133 | // clear all course modinfo caches | |
134 | rebuild_course_cache(0, true); | |
135 | } |