|
Moodle
2.2.1
http://www.collinsharper.com
|
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 page_20_migrate() { 00034 global $CFG, $DB; 00035 require_once("$CFG->libdir/filelib.php"); 00036 require_once("$CFG->libdir/resourcelib.php"); 00037 require_once("$CFG->dirroot/course/lib.php"); 00038 00039 if (!file_exists("$CFG->dirroot/mod/resource/db/upgradelib.php")) { 00040 // bad luck, somebody deleted resource module 00041 return; 00042 } 00043 00044 require_once("$CFG->dirroot/mod/resource/db/upgradelib.php"); 00045 00046 // create resource_old table and copy resource table there if needed 00047 if (!resource_20_prepare_migration()) { 00048 // no modules or fresh install 00049 return; 00050 } 00051 00052 $fs = get_file_storage(); 00053 00054 $candidates = $DB->get_recordset('resource_old', array('type'=>'html', 'migrated'=>0)); 00055 foreach ($candidates as $candidate) { 00056 page_20_migrate_candidate($candidate, $fs, FORMAT_HTML); 00057 } 00058 $candidates->close(); 00059 00060 $candidates = $DB->get_recordset('resource_old', array('type'=>'text', 'migrated'=>0)); 00061 foreach ($candidates as $candidate) { 00062 //there might be some rubbish instead of format int value 00063 $format = (int)$candidate->reference; 00064 if ($format < 0 or $format > 4) { 00065 $format = FORMAT_MOODLE; 00066 } 00067 page_20_migrate_candidate($candidate, $fs, $format); 00068 } 00069 $candidates->close(); 00070 00071 // clear all course modinfo caches 00072 rebuild_course_cache(0, true); 00073 00074 } 00075 00076 function page_20_migrate_candidate($candidate, $fs, $format) { 00077 global $CFG, $DB; 00078 upgrade_set_timeout(); 00079 00080 if ($CFG->texteditors !== 'textarea') { 00081 $intro = text_to_html($candidate->intro, false, false, true); 00082 $introformat = FORMAT_HTML; 00083 } else { 00084 $intro = $candidate->intro; 00085 $introformat = FORMAT_MOODLE; 00086 } 00087 00088 $page = new stdClass(); 00089 $page->course = $candidate->course; 00090 $page->name = $candidate->name; 00091 $page->intro = $intro; 00092 $page->introformat = $introformat; 00093 $page->content = $candidate->alltext; 00094 $page->contentformat = $format; 00095 $page->revision = 1; 00096 $page->timemodified = time(); 00097 00098 // convert links to old course files - let the automigration do the actual job 00099 $usedfiles = array("$CFG->wwwroot/file.php/$page->course/", "$CFG->wwwroot/file.php?file=/$page->course/"); 00100 $page->content = str_ireplace($usedfiles, '@@PLUGINFILE@@/', $page->content); 00101 if (strpos($page->content, '@@PLUGINFILE@@/') === false) { 00102 $page->legacyfiles = RESOURCELIB_LEGACYFILES_NO; 00103 } else { 00104 $page->legacyfiles = RESOURCELIB_LEGACYFILES_ACTIVE; 00105 } 00106 00107 $options = array('printheading'=>0, 'printintro'=>0); 00108 if ($candidate->popup) { 00109 $page->display = RESOURCELIB_DISPLAY_POPUP; 00110 if ($candidate->popup) { 00111 $rawoptions = explode(',', $candidate->popup); 00112 foreach ($rawoptions as $rawoption) { 00113 list($name, $value) = explode('=', trim($rawoption), 2); 00114 if ($value > 0 and ($name == 'width' or $name == 'height')) { 00115 $options['popup'.$name] = $value; 00116 continue; 00117 } 00118 } 00119 } 00120 } else { 00121 $page->display = RESOURCELIB_DISPLAY_OPEN; 00122 } 00123 $page->displayoptions = serialize($options); 00124 00125 $page = resource_migrate_to_module('page', $candidate, $page); 00126 00127 // now try to migrate files from site files 00128 // note: this can not work for html pages or files with other relatively linked files :-( 00129 $siteid = get_site()->id; 00130 if (preg_match_all("|$CFG->wwwroot/file.php(\?file=)?/$siteid(/[^\s'\"&\?#]+)|", $page->content, $matches)) { 00131 $context = get_context_instance(CONTEXT_MODULE, $candidate->cmid); 00132 $sitecontext = get_context_instance(CONTEXT_COURSE, $siteid); 00133 $file_record = array('contextid'=>$context->id, 'component'=>'mod_page', 'filearea'=>'content', 'itemid'=>0); 00134 $fs = get_file_storage(); 00135 foreach ($matches[2] as $i=>$sitefile) { 00136 if (!$file = $fs->get_file_by_hash(sha1("/$sitecontext->id/course/legacy/0".$sitefile))) { 00137 continue; 00138 } 00139 try { 00140 $fs->create_file_from_storedfile($file_record, $file); 00141 $page->content = str_replace($matches[0][$i], '@@PLUGINFILE@@'.$sitefile, $page->content); 00142 } catch (Exception $x) { 00143 } 00144 } 00145 $DB->update_record('page', $page); 00146 } 00147 }