MDL-19156: auth/fc: clarifying the licensing terms as per author's email
[moodle.git] / lib / db / upgradelib.php
CommitLineData
42ff9ce6 1<?php //$Id$
2
3/*
4 * This file is used for special upgrade functions - for example groups and gradebook.
56a1a882 5 * These functions must use SQL and database related functions only- no other Moodle API,
42ff9ce6 6 * because it might depend on db structures that are not yet present during upgrade.
7 * (Do not use functions from accesslib.php, grades classes or group functions at all!)
8 */
9
863850d2 10function upgrade_fix_category_depths() {
f33e1ed4 11 global $CFG, $DB;
863850d2 12
13 // first fix incorrect parents
14 $sql = "SELECT c.id
f33e1ed4 15 FROM {course_categories} c
16 WHERE c.parent > 0 AND c.parent NOT IN (SELECT pc.id FROM {course_categories} pc)";
17 if ($rs = $DB->get_recordset_sql($sql)) {
18 foreach ($rs as $cat) {
863850d2 19 $cat->depth = 1;
20 $cat->path = '/'.$cat->id;
21 $cat->parent = 0;
f33e1ed4 22 $DB->update_record('course_categories', $cat);
863850d2 23 }
f33e1ed4 24 $rs->close();
863850d2 25 }
26
27 // now add path and depth to top level categories
f33e1ed4 28 $sql = "UPDATE {course_categories}
245ac557 29 SET depth = 1, path = ".$DB->sql_concat("'/'", "id")."
863850d2 30 WHERE parent = 0";
f33e1ed4 31 $DB->execute($sql);
863850d2 32
33 // now fix all other levels - slow but works in all supported dbs
34 $parentdepth = 1;
f33e1ed4 35 while ($DB->record_exists('course_categories', array('depth'=>0))) {
863850d2 36 $sql = "SELECT c.id, pc.path
f33e1ed4 37 FROM {course_categories} c, {course_categories} pc
38 WHERE c.parent=pc.id AND c.depth=0 AND pc.depth=?";
39 if ($rs = $DB->get_recordset_sql($sql, array($parentdepth))) {
f33e1ed4 40 foreach ($rs as $cat) {
863850d2 41 $cat->depth = $parentdepth+1;
42 $cat->path = $cat->path.'/'.$cat->id;
f33e1ed4 43 $DB->update_record('course_categories', $cat);
863850d2 44 }
f33e1ed4 45 $rs->close();
863850d2 46 }
47 $parentdepth++;
48 if ($parentdepth > 100) {
49 //something must have gone wrong - nobody can have more than 100 levels of categories, right?
50 debugging('Unknown error fixing category depths');
51 break;
52 }
53 }
863850d2 54}
55
172dd12c 56/**
57 * Moves all course files except the moddata to new file storage
58 *
59 * Unfortunately this function uses core file related functions - it might be necessary to tweak it if something changes there :-(
60 */
61function upgrade_migrate_files_courses() {
62 global $DB, $CFG;
63 require_once($CFG->libdir.'/filelib.php');
64
65 $count = $DB->count_records('course');
66 $pbar = new progress_bar('migratecoursefiles', 500, true);
67
68 $rs = $DB->get_recordset('course');
172dd12c 69 $i = 0;
70 foreach ($rs as $course) {
71 $i++;
775f811a 72 upgrade_set_timeout(60*5); // set up timeout, may also abort execution
172dd12c 73 $context = get_context_instance(CONTEXT_COURSE, $course->id);
74 upgrade_migrate_files_course($context, '/', true);
75 $pbar->update($i, $count, "Migrated course files - course $i/$count.");
76 }
172dd12c 77 $rs->close();
78
79 return true;
80}
81
82/**
83 * Internal function - do not use directly
84 */
85function upgrade_migrate_files_course($context, $path, $delete) {
86 global $CFG;
87
88 $fullpathname = $CFG->dataroot.'/'.$context->instanceid.$path;
89 if (!file_exists($fullpathname)) {
90 return;
91 }
92 $items = new DirectoryIterator($fullpathname);
93 $fs = get_file_storage();
94
95 foreach ($items as $item) {
96 if ($item->isDot()) {
97 continue;
98 }
99
100 if ($item->isLink()) {
775f811a 101 // do not delete symbolic links or its children
172dd12c 102 $delete_this = false;
103 } else {
104 $delete_this = $delete;
105 }
106
107 if (strpos($path, '/backupdata/') === 0) {
108 $filearea = 'course_backup';
109 $filepath = substr($path, strlen('/backupdata'));
110 } else {
111 $filearea = 'course_content';
112 $filepath = $path;
113 }
114
115 if ($item->isFile()) {
116 if (!$item->isReadable()) {
117 notify(" File not readable, skipping: ".$fullpathname.$item->getFilename());
118 continue;
119 }
120
121 $filepath = clean_param($filepath, PARAM_PATH);
122 $filename = clean_param($item->getFilename(), PARAM_FILE);
123
124 if ($filename === '') {
125 continue;
126 }
127
128 if (!$fs->file_exists($context->id, $filearea, '0', $filepath, $filename)) {
129 $file_record = array('contextid'=>$context->id, 'filearea'=>$filearea, 'itemid'=>0, 'filepath'=>$filepath, 'filename'=>$filename,
130 'timecreated'=>$item->getCTime(), 'timemodified'=>$item->getMTime());
131 if ($fs->create_file_from_pathname($file_record, $fullpathname.$item->getFilename())) {
132 if ($delete_this) {
133 @unlink($fullpathname.$item->getFilename());
134 }
135 }
136 }
137
138 } else {
139 if ($path == '/' and $item->getFilename() == 'moddata') {
140 continue; // modules are responsible
141 }
142
143 $filepath = clean_param($filepath.$item->getFilename().'/', PARAM_PATH);
144 if ($filepath !== '/backupdata/') {
145 $fs->create_directory($context->id, $filearea, 0, $filepath);
146 }
147
148 //migrate recursively all subdirectories
149 upgrade_migrate_files_course($context, $path.$item->getFilename().'/', $delete_this);
150 if ($delete_this) {
151 // delete dir if empty
152 @rmdir($fullpathname.$item->getFilename());
153 }
154 }
155 }
156 unset($items); //release file handles
157}
158
159/**
160 * Moves all block attachments
161 *
162 * Unfortunately this function uses core file related functions - it might be necessary to tweak it if something changes there :-(
163 */
164function upgrade_migrate_files_blog() {
165 global $DB, $CFG;
166
167 $fs = get_file_storage();
168
a1442d4e 169 $count = $DB->count_records_select('post', "module='blog' AND attachment IS NOT NULL AND attachment <> '1'");
172dd12c 170
a1442d4e 171 if ($rs = $DB->get_recordset_select('post', "module='blog' AND attachment IS NOT NULL AND attachment <> '1'")) {
172dd12c 172
775f811a 173 upgrade_set_timeout(60*20); // set up timeout, may also abort execution
174
172dd12c 175 $pbar = new progress_bar('migrateblogfiles', 500, true);
176
172dd12c 177 $i = 0;
178 foreach ($rs as $entry) {
179 $i++;
180 $pathname = "$CFG->dataroot/blog/attachments/$entry->id/$entry->attachment";
181 if (!file_exists($pathname)) {
775f811a 182 $entry->attachment = NULL;
183 $DB->update_record('post', $entry);
172dd12c 184 continue;
185 }
186
187 $filename = clean_param($entry->attachment, PARAM_FILE);
188 if ($filename === '') {
189 // weird file name, ignore it
190 $entry->attachment = NULL;
191 $DB->update_record('post', $entry);
192 continue;
193 }
194
195 if (!is_readable($pathname)) {
196 notify(" File not readable, skipping: ".$pathname);
197 continue;
198 }
199
200 if (!$fs->file_exists(SYSCONTEXTID, 'blog', $entry->id, '/', $filename)) {
201 $file_record = array('contextid'=>SYSCONTEXTID, 'filearea'=>'blog', 'itemid'=>$entry->id, 'filepath'=>'/', 'filename'=>$filename,
775f811a 202 'timecreated'=>filectime($pathname), 'timemodified'=>filemtime($pathname), 'userid'=>$entry->userid);
172dd12c 203 $fs->create_file_from_pathname($file_record, $pathname);
204 }
205 @unlink($pathname);
206 @rmdir("$CFG->dataroot/blog/attachments/$entry->id/");
207
208 $entry->attachment = 1; // file name not needed there anymore
209 $DB->update_record('post', $entry);
210 $pbar->update($i, $count, "Migrated blog attachments - $i/$count.");
211 }
172dd12c 212 $rs->close();
213 }
775f811a 214
172dd12c 215 @rmdir("$CFG->dataroot/blog/attachments/");
216 @rmdir("$CFG->dataroot/blog/");
217}