"MDL-18848, add default module to curl class"
[moodle.git] / mod / glossary / db / upgrade.php
CommitLineData
b8a342d7 1<?php //$Id$
ecfeb03a 2
3// This file keeps track of upgrades to
4// the glossary module
5//
6// Sometimes, changes between versions involve
7// alterations to database structures and other
8// major things that may break installations.
9//
10// The upgrade function in this file will attempt
11// to perform all the necessary actions to upgrade
12// your older installtion to the current version.
13//
14// If there's something it cannot do itself, it
15// will tell you what you need to do.
16//
17// The commands in here will all be database-neutral,
b1f93b15 18// using the methods of database_manager class
775f811a 19//
20// Please do not forget to use upgrade_set_timeout()
21// before any action that may take longer time to finish.
ecfeb03a 22
775f811a 23function xmldb_glossary_upgrade($oldversion) {
24 global $CFG, $DB;
ecfeb03a 25
775f811a 26 $dbman = $DB->get_manager();
ecfeb03a 27 $result = true;
28
219f652b 29//===== 1.9.0 upgrade line ======//
ecfeb03a 30
6b04ddc5 31 if ($result && $oldversion < 2008081900) {
49bcd737 32
33 /////////////////////////////////////
34 /// new file storage upgrade code ///
35 /////////////////////////////////////
36
37 $fs = get_file_storage();
38
39 $empty = $DB->sql_empty(); // silly oracle empty string handling workaround
40
41 $sqlfrom = "FROM {glossary_entries} ge
42 JOIN {glossary} g ON g.id = ge.glossaryid
43 JOIN {modules} m ON m.name = 'glossary'
44 JOIN {course_modules} cm ON (cm.module = m.id AND cm.instance = g.id)
c48068f8 45 WHERE ge.attachment <> '$empty' AND ge.attachment <> '1'";
49bcd737 46
47 $count = $DB->count_records_sql("SELECT COUNT('x') $sqlfrom");
48
c48068f8 49 if ($rs = $DB->get_recordset_sql("SELECT ge.id, ge.userid, ge.attachment, ge.glossaryid, ge.sourceglossaryid, g.course, cm.id AS cmid $sqlfrom ORDER BY g.course, g.id")) {
49bcd737 50
51 $pbar = new progress_bar('migrateglossaryfiles', 500, true);
52
49bcd737 53 $i = 0;
54 foreach ($rs as $entry) {
55 $i++;
56 upgrade_set_timeout(60); // set up timeout, may also abort execution
57 $pbar->update($i, $count, "Migrating glossary entries - $i/$count.");
58
59 $filepath = "$CFG->dataroot/$entry->course/$CFG->moddata/glossary/$entry->glossaryid/$entry->id/$entry->attachment";
0d18a9d6 60 if ($entry->sourceglossaryid and !is_readable($filepath)) {
49bcd737 61 //eh - try the second possible location
49bcd737 62 $filepath = "$CFG->dataroot/$entry->course/$CFG->moddata/glossary/$entry->sourceglossaryid/$entry->id/$entry->attachment";
63
64 }
65 if (!is_readable($filepath)) {
66 //file missing??
0d18a9d6 67 notify("File not readable, skipping: $filepath");
49bcd737 68 $entry->attachment = '';
69 $DB->update_record('glossary_entries', $entry);
70 continue;
71 }
72 $context = get_context_instance(CONTEXT_MODULE, $entry->cmid);
73
74 $filearea = 'glossary_attachment';
75 $filename = clean_param($entry->attachment, PARAM_FILE);
76 if ($filename === '') {
77 notify("Unsupported entry filename, skipping: ".$filepath);
78 $entry->attachment = '';
79 $DB->update_record('glossary_entries', $entry);
80 continue;
81 }
82 if (!$fs->file_exists($context->id, $filearea, $entry->id, '/', $filename)) {
83 $file_record = array('contextid'=>$context->id, 'filearea'=>$filearea, 'itemid'=>$entry->id, 'filepath'=>'/', 'filename'=>$filename, 'userid'=>$entry->userid);
84 if ($fs->create_file_from_pathname($file_record, $filepath)) {
85 $entry->attachment = '1';
86 if ($DB->update_record('glossary_entries', $entry)) {
87 unlink($filepath);
88 }
89 }
90 }
91
92 // remove dirs if empty
93 @rmdir("$CFG->dataroot/$entry->course/$CFG->moddata/glossary/$entry->glossaryid/$entry->id");
94 @rmdir("$CFG->dataroot/$entry->course/$CFG->moddata/glossary/$entry->glossaryid");
95 @rmdir("$CFG->dataroot/$entry->course/$CFG->moddata/glossary");
96 }
49bcd737 97 $rs->close();
98 }
99
6b04ddc5 100 upgrade_mod_savepoint($result, 2008081900, 'glossary');
49bcd737 101 }
102
ecfeb03a 103 return $result;
104}
105
106?>