Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/mod/resource/db/upgradelib.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00027 defined('MOODLE_INTERNAL') || die;
00028 
00033 function resource_20_migrate() {
00034     global $CFG, $DB;
00035 
00036     require_once("$CFG->libdir/filelib.php");
00037     require_once("$CFG->libdir/resourcelib.php");
00038     require_once("$CFG->dirroot/course/lib.php");
00039 
00040     $fs = get_file_storage();
00041 
00042     $withrelativelinks = array('text/html', 'text/xml', 'application/xhtml+xml', 'application/x-shockwave-flash');
00043     // note: pdf doc and other types may contain links too, but we do not support relative links there
00044 
00045     $candidates = $DB->get_recordset('resource_old', array('type'=>'file', 'migrated'=>0));
00046     if (!$candidates->valid()) {
00047         $candidates->close(); // Not going to iterate (but exit), close rs
00048         return;
00049     }
00050 
00051     foreach ($candidates as $candidate) {
00052         upgrade_set_timeout();
00053 
00054         $path = $candidate->reference;
00055         $siteid = get_site()->id;
00056         $fs = get_file_storage();
00057 
00058         if (empty($candidate->cmid)) {
00059             // skip borked records
00060             continue;
00061 
00062         } else if (strpos($path, 'LOCALPATH') === 0) {
00063             // ignore not maintained local files - sorry
00064             continue;
00065 
00066         } else if (preg_match("|$CFG->wwwroot/file.php(\?file=)?/$siteid(/[^\s'\"&\?#]+)|", $path, $matches)) {
00067             // public site files
00068             $path = $matches[2];
00069 
00070             $resource = new stdClass();
00071             $resource->id           = $candidate->oldid;
00072             $resource->tobemigrated = 0;
00073             $resource->mainfile     = $path;
00074             $resource->filterfiles  = $CFG->filteruploadedfiles;
00075             $resource->legacyfiles  = RESOURCELIB_LEGACYFILES_NO; // on-demand-migration not possible for site files, sorry
00076 
00077             $context     = get_context_instance(CONTEXT_MODULE, $candidate->cmid);
00078             $sitecontext = get_context_instance(CONTEXT_COURSE, $siteid);
00079             $file_record = array('contextid'=>$context->id, 'component'=>'mod_resourse', 'filearea'=>'content', 'itemid'=>0);
00080             if ($file = $fs->get_file_by_hash(sha1("/$sitecontext->id/course/legacy/content/0".$path))) {
00081                 try {
00082                     $fs->create_file_from_storedfile($file_record, $file);
00083                 } catch (Exception $x) {
00084                 }
00085                 $resource->mainfile = $file->get_filepath().$file->get_filename();
00086             }
00087 
00088         } else if (preg_match("|$CFG->wwwroot/file.php(\?file=)?/$candidate->course(/[^\s'\"&\?#]+)|", $path, $matches)) {
00089             // current course files
00090             $path = $matches[2];
00091 
00092             $resource = new stdClass();
00093             $resource->id           = $candidate->oldid;
00094             $resource->tobemigrated = 0;
00095             $resource->mainfile     = $path;
00096             $resource->filterfiles  = $CFG->filteruploadedfiles;
00097             $mimetype = mimeinfo('type', $resource->mainfile);
00098             if (in_array($mimetype, $withrelativelinks)) {
00099                 $resource->legacyfiles = RESOURCELIB_LEGACYFILES_ACTIVE;
00100             } else {
00101                 $resource->legacyfiles = RESOURCELIB_LEGACYFILES_NO;
00102             }
00103 
00104             // try migration of main file - ignore if does not exist
00105             if ($file = resourcelib_try_file_migration($resource->mainfile, $candidate->cmid, $candidate->course, 'mod_resource', 'content', 0)) {
00106                 $resource->mainfile = $file->get_filepath().$file->get_filename();
00107             }
00108 
00109         } else if (strpos($path, '://') or strpos($path, '/') === 0) {
00110             // http:// https:// ftp:// OR starts with slash - to be converted to link resource
00111             continue;
00112 
00113         } else {
00114             // current course files
00115             // cleanup old path first
00116             $path = '/'.trim(trim($path), '/');
00117             if (strpos($path, '?forcedownload=1') !== false) {
00118                 // eliminate old force download tricks
00119                 $candidate->options = 'forcedownload';
00120                 $path = str_replace('?forcedownload=1', '', $path);
00121             }
00122             // get rid of any extra url parameters, sorry we can not support these
00123             preg_match("/^[^?#]+/", $path, $matches);
00124             $parts = $matches[0];
00125 
00126             $resource = new stdClass();
00127             $resource->id           = $candidate->oldid;
00128             $resource->tobemigrated = 0;
00129             $resource->mainfile     = $path;
00130             $resource->filterfiles  = $CFG->filteruploadedfiles;
00131             $mimetype = mimeinfo('type', $resource->mainfile);
00132             if (in_array($mimetype, $withrelativelinks)) {
00133                 $resource->legacyfiles = RESOURCELIB_LEGACYFILES_ACTIVE;
00134             } else {
00135                 $resource->legacyfiles = RESOURCELIB_LEGACYFILES_NO;
00136             }
00137 
00138             // try migration of main file - ignore if does not exist
00139             if ($file = resourcelib_try_file_migration($resource->mainfile, $candidate->cmid, $candidate->course, 'mod_resource', 'content', 0)) {
00140                 $resource->mainfile = $file->get_filepath().$file->get_filename();
00141             }
00142         }
00143 
00144         $options = array('printheading'=>0, 'printintro'=>1);
00145         if ($candidate->options == 'frame') {
00146             $resource->display = RESOURCELIB_DISPLAY_FRAME;
00147 
00148         } else if ($candidate->options == 'objectframe') {
00149             $resource->display = RESOURCELIB_DISPLAY_EMBED;
00150 
00151         } else if ($candidate->options == 'forcedownload') {
00152             $resource->display = RESOURCELIB_DISPLAY_DOWNLOAD;
00153 
00154         } else if ($candidate->popup) {
00155             $resource->display = RESOURCELIB_DISPLAY_POPUP;
00156             if ($candidate->popup) {
00157                 $rawoptions = explode(',', $candidate->popup);
00158                 foreach ($rawoptions as $rawoption) {
00159                     list($name, $value) = explode('=', trim($rawoption), 2);
00160                     if ($value > 0 and ($name == 'width' or $name == 'height')) {
00161                         $options['popup'.$name] = $value;
00162                         continue;
00163                     }
00164                 }
00165             }
00166 
00167         } else {
00168             $resource->display = RESOURCELIB_DISPLAY_AUTO;
00169         }
00170         $resource->displayoptions = serialize($options);
00171 
00172         // update resource instance and mark as migrated
00173         $DB->update_record('resource', $resource);
00174         $candidate->newmodule = 'resource';
00175         $candidate->newid     = $candidate->oldid;
00176         $candidate->migrated  = time();
00177         $DB->update_record('resource_old', $candidate);
00178     }
00179 
00180     $candidates->close();
00181 
00182     // clear all course modinfo caches
00183     rebuild_course_cache(0, true);
00184 }
00185 
00192 function resource_20_prepare_migration() {
00193     global $DB;
00194 
00195     $dbman = $DB->get_manager();
00196 
00197     // If the resource not created yet, this is probably a new install
00198     $table = new xmldb_table('resource');
00199     if (!$dbman->table_exists($table)) {
00200         return false;
00201     }
00202 
00203     // Define table resource_old to be created
00204     $table = new xmldb_table('resource_old');
00205 
00206     if ($dbman->table_exists($table)) {
00207         //already executed
00208         return true;
00209     }
00210 
00211     // fix invalid NULL popup and options data in old mysql databases
00212     $sql = "UPDATE {resource} SET popup = ? WHERE popup IS NULL";
00213     $DB->execute($sql, array($DB->sql_empty()));
00214     $sql = "UPDATE {resource} SET options = ? WHERE options IS NULL";
00215     $DB->execute($sql, array($DB->sql_empty()));
00216 
00217     // Adding fields to table resource_old
00218     $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
00219     $table->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
00220     $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
00221     $table->add_field('type', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null);
00222     $table->add_field('reference', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
00223     $table->add_field('intro', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
00224     $table->add_field('introformat', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
00225     $table->add_field('alltext', XMLDB_TYPE_TEXT, 'medium', null, XMLDB_NOTNULL, null, null);
00226     $table->add_field('popup', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null);
00227     $table->add_field('options', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
00228     $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
00229     $table->add_field('oldid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
00230     $table->add_field('cmid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
00231     $table->add_field('newmodule', XMLDB_TYPE_CHAR, '50', null, null, null, null);
00232     $table->add_field('newid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
00233     $table->add_field('migrated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
00234 
00235     // Adding keys to table resource_old
00236     $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
00237 
00238     // Adding indexes to table resource_old
00239     $table->add_index('oldid', XMLDB_INDEX_UNIQUE, array('oldid'));
00240     $table->add_index('cmid', XMLDB_INDEX_NOTUNIQUE, array('cmid'));
00241 
00242     // Launch create table for resource_old
00243     $dbman->create_table($table);
00244 
00245     $module = $DB->get_field('modules', 'id', array('name'=>'resource'));
00246 
00247     if (!$DB->count_records('resource')) {
00248         // upgrade of fresh new server from 1.9 - no upgrade needed
00249         return false;
00250     }
00251 
00252     // copy old data, the intro text format was FORMAT_MOODLE==0
00253     $sql = "INSERT INTO {resource_old} (oldid, course, name, type, reference, intro, introformat, alltext, popup, options, timemodified, cmid)
00254             SELECT r.id, r.course, r.name, r.type, r.reference, r.summary, 0, r.alltext, r.popup, r.options, r.timemodified, cm.id
00255               FROM {resource} r
00256          LEFT JOIN {course_modules} cm ON (r.id = cm.instance AND cm.module = :module)";
00257 
00258     $DB->execute($sql, array('module'=>$module));
00259 
00260     return true;
00261 }
00262 
00270 function resource_migrate_to_module($modname, $candidate, $newinstance) {
00271     global $DB;
00272 
00273     if (!$cm = get_coursemodule_from_id('resource', $candidate->cmid)) {
00274         return false;
00275     }
00276 
00277     if (!$module = $DB->get_record('modules', array('name'=>$modname))) {
00278         return false;
00279     }
00280 
00281     if (!$resource = $DB->get_record('resource', array('id'=>$candidate->oldid))) {
00282         return false;
00283     }
00284 
00285     // insert new instance
00286     $newinstance->id = $DB->insert_record($modname, $newinstance);
00287 
00288     // update course modules
00289     $cm->module   = $module->id;
00290     $cm->instance = $newinstance->id;
00291     $DB->update_record('course_modules', $cm);
00292 
00293     //delete old record
00294     $DB->delete_records('resource', array('id'=>$resource->id));
00295 
00296     //mark as migrated
00297     $candidate->newmodule = $modname;
00298     $candidate->newid     = $newinstance->id;
00299     $candidate->migrated  = time();
00300     $DB->update_record('resource_old', $candidate);
00301 
00302     //no need to upgrade data in logs because resource module is able to redirect to migrated instances
00303     return $newinstance;
00304 }
 All Data Structures Namespaces Files Functions Variables Enumerations