. /** * Folder module upgrade related helper functions * * @package mod-page * @copyright 2009 Petr Skoda (http://skodak.org) * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ /** * Migrate page module data from 1.9 resource_old table to new page table * @return void */ function page_20_migrate() { global $CFG, $DB; require_once("$CFG->libdir/filelib.php"); require_once("$CFG->libdir/resourcelib.php"); require_once("$CFG->dirroot/course/lib.php"); if (!file_exists("$CFG->dirroot/mod/resource/db/upgradelib.php")) { // bad luck, somebody deleted resource module return; } require_once("$CFG->dirroot/mod/resource/db/upgradelib.php"); // create resource_old table and copy resource table there if needed if (!resource_20_prepare_migration()) { // no modules or fresh install return; } $fs = get_file_storage(); if ($candidates = $DB->get_recordset('resource_old', array('type'=>'html', 'migrated'=>0))) { foreach ($candidates as $candidate) { page_20_migrate_candidate($candidate, $fs, FORMAT_HTML); } $candidates->close(); } if ($candidates = $DB->get_recordset('resource_old', array('type'=>'text', 'migrated'=>0))) { foreach ($candidates as $candidate) { page_20_migrate_candidate($candidate, $fs, $candidate->reference); } $candidates->close(); } // clear all course modinfo caches rebuild_course_cache(0, true); } function page_20_migrate_candidate($candidate, $fs, $format) { global $CFG, $DB; upgrade_set_timeout(); $page = new object(); $page->course = $candidate->course; $page->name = $candidate->name; $page->intro = $candidate->intro; $page->introformat = $candidate->introformat; $page->content = $candidate->alltext; $page->contentformat = $format; $page->revision = 1; $page->timemodified = time(); // convert links to old course files - let the automigration do the actual job $usedfiles = array("$CFG->wwwroot/file.php/$page->course/", "$CFG->wwwroot/file.php?file=/$page->course/"); $page->content = str_ireplace($usedfiles, '@@PLUGINFILE@@/', $page->content); if (strpos($page->content, '@@PLUGINFILE@@/') === false) { $page->legacyfiles = RESOURCELIB_LEGACYFILES_NO; } else { $page->legacyfiles = RESOURCELIB_LEGACYFILES_ACTIVE; } $options = array('printheading'=>0, 'printintro'=>0); if ($candidate->popup) { $page->display = RESOURCELIB_DISPLAY_POPUP; if ($candidate->popup) { $rawoptions = explode(',', $candidate->popup); foreach ($rawoptions as $rawoption) { list($name, $value) = explode('=', trim($rawoption), 2); if ($value > 0 and ($name == 'width' or $name == 'height')) { $options['popup'.$name] = $value; continue; } } } } else { $page->display = RESOURCELIB_DISPLAY_OPEN; } $page->displayoptions = serialize($options); $page = resource_migrate_to_module('page', $candidate, $page); // now try to migrate files from site files // note: this can not work for html pages or files with other relatively linked files :-( $siteid = get_site()->id; if (preg_match_all("|$CFG->wwwroot/file.php(\?file=)?/$siteid(/[^\s'\"&\?#]+)|", $page->content, $matches)) { $context = get_context_instance(CONTEXT_MODULE, $candidate->cmid); $sitecontext = get_context_instance(CONTEXT_COURSE, $siteid); $file_record = array('contextid'=>$context->id, 'component'=>'mod_page', 'filearea'=>'content', 'itemid'=>0); $fs = get_file_storage(); foreach ($matches[2] as $i=>$sitefile) { if (!$file = $fs->get_file_by_hash(sha1("/$sitecontext->id/course/legacy/0".$sitefile))) { continue; } try { $fs->create_file_from_storedfile($file_record, $file); $page->content = str_replace($matches[0][$i], '@@PLUGINFILE@@'.$sitefile, $page->content); } catch (Exception $x) { } } $DB->update_record('page', $page); } }