Example #1
0
/**
 * Migrate folder module data from 1.9 resource_old table to new older table
 * @return void
 */
function folder_20_migrate()
{
    global $CFG, $DB;
    require_once "{$CFG->libdir}/filelib.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;
    }
    if (!($candidates = $DB->get_recordset('resource_old', array('type' => 'directory', 'migrated' => 0)))) {
        return;
    }
    $fs = get_file_storage();
    foreach ($candidates as $candidate) {
        upgrade_set_timeout();
        $directory = '/' . trim($candidate->reference, '/') . '/';
        $directory = str_replace('//', '/', $directory);
        $folder = new object();
        $folder->course = $candidate->course;
        $folder->name = $candidate->name;
        $folder->intro = $candidate->intro;
        $folder->introformat = $candidate->introformat;
        $folder->revision = 1;
        $folder->timemodified = time();
        if (!($folder = resource_migrate_to_module('folder', $candidate, $folder))) {
            continue;
        }
        // copy files in given directory, skip moddata and backups!
        $context = get_context_instance(CONTEXT_MODULE, $candidate->cmid);
        $coursecontext = get_context_instance(CONTEXT_COURSE, $candidate->course);
        $files = $fs->get_directory_files($coursecontext->id, 'course_content', 0, $directory, true, true);
        $file_record = array('contextid' => $context->id, 'filearea' => 'folder_content', 'itemid' => 0);
        foreach ($files as $file) {
            $path = $file->get_filepath();
            if (stripos($path, '/backupdata/') === 0 or stripos($path, '/moddata/') === 0) {
                // do not publish protected data!
                continue;
            }
            $relpath = substr($path, strlen($directory) - 1);
            // keep only subfolder paths
            $file_record['filepath'] = $relpath;
            $fs->create_file_from_storedfile($file_record, $file);
        }
    }
    $candidates->close();
    // clear all course modinfo caches
    rebuild_course_cache(0, true);
}
/**
 * Migrate url module data from 1.9 resource_old table to new url table
 * @return void
 */
function url_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;
    }
    $candidates = $DB->get_recordset('resource_old', array('type' => 'file', 'migrated' => 0));
    if (!$candidates->valid()) {
        $candidates->close();
        // Not going to iterate (but exit), close rs
        return;
    }
    foreach ($candidates as $candidate) {
        $path = $candidate->reference;
        $siteid = get_site()->id;
        if (strpos($path, 'LOCALPATH') === 0) {
            // ignore not maintained local files - sorry
            continue;
        } else {
            if (!strpos($path, '://')) {
                // not URL
                continue;
            } else {
                if (preg_match("|{$CFG->wwwroot}/file.php(\\?file=)?/{$siteid}(/[^\\s'\"&\\?#]+)|", $path, $matches)) {
                    // handled by resource module
                    continue;
                } else {
                    if (preg_match("|{$CFG->wwwroot}/file.php(\\?file=)?/{$candidate->course}(/[^\\s'\"&\\?#]+)|", $path, $matches)) {
                        // handled by resource module
                        continue;
                    }
                }
            }
        }
        upgrade_set_timeout();
        if ($CFG->texteditors !== 'textarea') {
            $intro = text_to_html($candidate->intro, false, false, true);
            $introformat = FORMAT_HTML;
        } else {
            $intro = $candidate->intro;
            $introformat = FORMAT_MOODLE;
        }
        $url = new stdClass();
        $url->course = $candidate->course;
        $url->name = $candidate->name;
        $url->intro = $intro;
        $url->introformat = $introformat;
        $url->externalurl = $path;
        $url->timemodified = time();
        $options = array('printheading' => 0, 'printintro' => 1);
        $parameters = array();
        if ($candidate->options == 'frame') {
            $url->display = RESOURCELIB_DISPLAY_FRAME;
        } else {
            if ($candidate->options == 'objectframe') {
                $url->display = RESOURCELIB_DISPLAY_EMBED;
            } else {
                if ($candidate->popup) {
                    $url->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 {
                    $url->display = RESOURCELIB_DISPLAY_AUTO;
                }
            }
        }
        $url->displayoptions = serialize($options);
        if ($candidate->alltext) {
            $rawoptions = explode(',', $candidate->alltext);
            foreach ($rawoptions as $rawoption) {
                list($variable, $parameter) = explode('=', trim($rawoption), 2);
                $parameters[$parameter] = $variable;
            }
        }
        $url->parameters = serialize($parameters);
        if (!($url = resource_migrate_to_module('url', $candidate, $url))) {
            continue;
        }
    }
    $candidates->close();
    // clear all course modinfo caches
    rebuild_course_cache(0, true);
}
Example #3
0
function page_20_migrate_candidate($candidate, $fs, $format)
{
    global $CFG, $DB;
    upgrade_set_timeout();
    if ($CFG->texteditors !== 'textarea') {
        $intro = text_to_html($candidate->intro, false, false, true);
        $introformat = FORMAT_HTML;
    } else {
        $intro = $candidate->intro;
        $introformat = FORMAT_MOODLE;
    }
    $page = new stdClass();
    $page->course = $candidate->course;
    $page->name = $candidate->name;
    $page->intro = $intro;
    $page->introformat = $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);
    }
}
Example #4
0
/**
 * Migrate ubikc module data from 1.9 resource_old table to new older table
 * @return void
 */
function ubikc_20_migrate()
{
    global $CFG, $DB;
    require_once "{$CFG->libdir}/filelib.php";
    require_once "{$CFG->dirroot}/course/lib.php";
    if (!file_exists("{$CFG->dirroot}/mod/resource/db/upgradelib.php")) {
        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;
    }
    $candidates = $DB->get_recordset('resource_old', array('type' => 'directory', 'migrated' => 0));
    if (!$candidates->valid()) {
        $candidates->close();
        // Not going to iterate (but exit), close rs
        return;
    }
    $fs = get_file_storage();
    foreach ($candidates as $candidate) {
        upgrade_set_timeout();
        $directory = '/' . trim($candidate->reference, '/') . '/';
        $directory = str_replace('//', '/', $directory);
        if ($CFG->texteditors !== 'textarea') {
            $intro = text_to_html($candidate->intro, false, false, true);
            $introformat = FORMAT_HTML;
        } else {
            $intro = $candidate->intro;
            $introformat = FORMAT_MOODLE;
        }
        $ubikc = new stdClass();
        $ubikc->course = $candidate->course;
        $ubikc->name = $candidate->name;
        $ubikc->intro = $intro;
        $ubikc->introformat = $introformat;
        $ubikc->timemodified = time();
        $ubikc->kcrevision = 1;
        if (!($ubikc = resource_migrate_to_module('ubikc', $candidate, $ubikc))) {
            continue;
        }
        // copy files in given directory, skip moddata and backups!
        $context = get_context_instance(CONTEXT_MODULE, $candidate->cmid);
        $coursecontext = get_context_instance(CONTEXT_COURSE, $candidate->course);
        $files = $fs->get_directory_files($coursecontext->id, 'course', 'legacy', 0, $directory, true, true);
        $file_record = array('contextid' => $context->id, 'component' => 'mod_ubikc', 'filearea' => 'content', 'itemid' => 0);
        foreach ($files as $file) {
            $path = $file->get_filepath();
            if (stripos($path, '/backupdata/') === 0 or stripos($path, '/moddata/') === 0) {
                // do not publish protected data!
                continue;
            }
            $relpath = substr($path, strlen($directory) - 1);
            $file_record['filepath'] = $relpath;
            $fs->create_file_from_storedfile($file_record, $file);
        }
    }
    $candidates->close();
    // clear all course modinfo caches
    rebuild_course_cache(0, true);
}
/**
 * Migrate imscp module data from 1.9 resource_old table to new imscp table
 * @return void
 */
function imscp_20_migrate()
{
    global $CFG, $DB, $OUTPUT;
    require_once "{$CFG->libdir}/filelib.php";
    require_once "{$CFG->dirroot}/course/lib.php";
    require_once "{$CFG->dirroot}/mod/imscp/locallib.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;
    }
    $candidates = $DB->get_recordset('resource_old', array('type' => 'ims', 'migrated' => 0));
    if (!$candidates->valid()) {
        $candidates->close();
        // Not going to iterate (but exit), close rs
        return;
    }
    $fs = get_file_storage();
    foreach ($candidates as $candidate) {
        upgrade_set_timeout(60);
        if ($CFG->texteditors !== 'textarea') {
            $intro = text_to_html($candidate->intro, false, false, true);
            $introformat = FORMAT_HTML;
        } else {
            $intro = $candidate->intro;
            $introformat = FORMAT_MOODLE;
        }
        $imscp = new stdClass();
        $imscp->course = $candidate->course;
        $imscp->name = $candidate->name;
        $imscp->intro = $intro;
        $imscp->introformat = $introformat;
        $imscp->revision = 1;
        $imscp->keepold = 1;
        $imscp->timemodified = time();
        if (!($imscp = resource_migrate_to_module('imscp', $candidate, $imscp))) {
            continue;
        }
        $context = get_context_instance(CONTEXT_MODULE, $candidate->cmid);
        $root = "{$CFG->dataroot}/{$candidate->course}/{$CFG->moddata}/resource/{$candidate->oldid}";
        // migrate package backup file
        if ($candidate->reference) {
            $package = basename($candidate->reference);
            $fullpath = $root . '/' . $package;
            if (file_exists($fullpath)) {
                $file_record = array('contextid' => $context->id, 'component' => 'mod_imscp', 'filearea' => 'backup', 'itemid' => 1, 'filepath' => '/', 'filename' => $package);
                $fs->create_file_from_pathname($file_record, $fullpath);
            }
        }
        // migrate extracted package data
        $files = imsc_migrate_get_old_files($root, '');
        if (empty($files)) {
            // if ims package doesn't exist, continue loop
            echo $OUTPUT->notification("IMS package data cannot be found, failed migrating activity: \"{$candidate->name}\", please fix it manually");
            continue;
        }
        $file_record = array('contextid' => $context->id, 'component' => 'mod_imscp', 'filearea' => 'content', 'itemid' => 1);
        $error = false;
        foreach ($files as $relname => $fullpath) {
            $parts = explode('/', $relname);
            $file_record['filename'] = array_pop($parts);
            $parts[] = '';
            // keep trailing slash
            $file_record['filepath'] = implode('/', $parts);
            try {
                $fs->create_file_from_pathname($file_record, $fullpath);
            } catch (Exception $e) {
                //continue on error, we can not recover anyway
                $error = true;
                echo $OUTPUT->notification("IMSCP: failed migrating file: {$relname}");
            }
        }
        unset($files);
        // parse manifest
        $structure = imscp_parse_structure($imscp, $context);
        $imscp->structure = is_array($structure) ? serialize($structure) : null;
        $DB->update_record('imscp', $imscp);
        // remove old moddata dir only if no error and manifest ok
        if (!$error and is_array($structure)) {
            fulldelete($root);
        }
    }
    $candidates->close();
    // clear all course modinfo caches
    rebuild_course_cache(0, true);
}