MDL-32709 book: DB definition & upgrade cleanup

This commit is contained in:
Eloy Lafuente (stronk7)
2012-05-20 21:48:07 +02:00
parent 29656fe24b
commit b9a363548d
3 changed files with 24 additions and 356 deletions
+22 -22
View File
@@ -1,43 +1,43 @@
<?xml version="1.0" encoding="UTF-8" ?>
<XMLDB PATH="mod/book/db" VERSION="20111009" COMMENT="XMLDB file for Moodle mod_book"
<XMLDB PATH="mod/book/db" VERSION="20120515" COMMENT="XMLDB file for Moodle mod_book"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../lib/xmldb/xmldb.xsd"
>
<TABLES>
<TABLE NAME="book" COMMENT="Defines book" NEXT="book_chapters">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="true" NEXT="course"/>
<FIELD NAME="course" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="id" NEXT="name"/>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true" NEXT="course"/>
<FIELD NAME="course" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="id" NEXT="name"/>
<FIELD NAME="name" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" PREVIOUS="course" NEXT="intro"/>
<FIELD NAME="intro" TYPE="text" LENGTH="big" NOTNULL="false" SEQUENCE="false" PREVIOUS="name" NEXT="introformat"/>
<FIELD NAME="introformat" TYPE="int" LENGTH="4" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="intro" NEXT="numbering"/>
<FIELD NAME="numbering" TYPE="int" LENGTH="4" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="introformat" NEXT="customtitles"/>
<FIELD NAME="customtitles" TYPE="int" LENGTH="2" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="numbering" NEXT="revision"/>
<FIELD NAME="revision" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="false" DEFAULT="0" SEQUENCE="false" PREVIOUS="customtitles" NEXT="timecreated"/>
<FIELD NAME="timecreated" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="revision" NEXT="timemodified"/>
<FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="timecreated"/>
<FIELD NAME="intro" TYPE="text" NOTNULL="false" SEQUENCE="false" PREVIOUS="name" NEXT="introformat"/>
<FIELD NAME="introformat" TYPE="int" LENGTH="4" NOTNULL="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="intro" NEXT="numbering"/>
<FIELD NAME="numbering" TYPE="int" LENGTH="4" NOTNULL="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="introformat" NEXT="customtitles"/>
<FIELD NAME="customtitles" TYPE="int" LENGTH="2" NOTNULL="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="numbering" NEXT="revision"/>
<FIELD NAME="revision" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="customtitles" NEXT="timecreated"/>
<FIELD NAME="timecreated" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="revision" NEXT="timemodified"/>
<FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="timecreated"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id" />
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
</KEYS>
</TABLE>
<TABLE NAME="book_chapters" COMMENT="Defines book_chapters" PREVIOUS="book">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="false" SEQUENCE="true" NEXT="bookid"/>
<FIELD NAME="bookid" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="id" NEXT="pagenum"/>
<FIELD NAME="pagenum" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="bookid" NEXT="subchapter"/>
<FIELD NAME="subchapter" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="pagenum" NEXT="title"/>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true" NEXT="bookid"/>
<FIELD NAME="bookid" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="id" NEXT="pagenum"/>
<FIELD NAME="pagenum" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="bookid" NEXT="subchapter"/>
<FIELD NAME="subchapter" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="pagenum" NEXT="title"/>
<FIELD NAME="title" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" PREVIOUS="subchapter" NEXT="content"/>
<FIELD NAME="content" TYPE="text" LENGTH="big" NOTNULL="true" SEQUENCE="false" PREVIOUS="title" NEXT="contentformat"/>
<FIELD NAME="contentformat" TYPE="int" LENGTH="4" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="content" NEXT="hidden"/>
<FIELD NAME="hidden" TYPE="int" LENGTH="2" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="contentformat" NEXT="timecreated"/>
<FIELD NAME="timecreated" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="hidden" NEXT="timemodified"/>
<FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="timecreated" NEXT="importsrc"/>
<FIELD NAME="content" TYPE="text" NOTNULL="true" SEQUENCE="false" PREVIOUS="title" NEXT="contentformat"/>
<FIELD NAME="contentformat" TYPE="int" LENGTH="4" NOTNULL="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="content" NEXT="hidden"/>
<FIELD NAME="hidden" TYPE="int" LENGTH="2" NOTNULL="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="contentformat" NEXT="timecreated"/>
<FIELD NAME="timecreated" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="hidden" NEXT="timemodified"/>
<FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="timecreated" NEXT="importsrc"/>
<FIELD NAME="importsrc" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" PREVIOUS="timemodified"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id" />
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
</KEYS>
</TABLE>
</TABLES>
</XMLDB>
</XMLDB>
+2 -166
View File
@@ -35,172 +35,8 @@ function xmldb_book_upgrade($oldversion) {
$dbman = $DB->get_manager();
if ($oldversion < 2004081100) {
throw new upgrade_exception('mod_book', $oldversion, 'Can not upgrade such an old book module, sorry, you should have upgraded it long time ago in 1.9 already.');
}
if ($oldversion < 2007052001) {
// Changing type of field importsrc on table book_chapters to char
$table = new xmldb_table('book_chapters');
$field = new xmldb_field('importsrc', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'timemodified');
// Launch change of type for field importsrc
$dbman->change_field_type($table, $field);
upgrade_mod_savepoint(true, 2007052001, 'book');
}
// 1.9.0 upgrade line.
if ($oldversion < 2010120801) {
// Rename field summary on table book to intro
$table = new xmldb_table('book');
$field = new xmldb_field('summary', XMLDB_TYPE_TEXT, 'small', null, null, null, null, 'name');
// Launch rename field summary
if ($dbman->field_exists($table, $field)) {
$dbman->rename_field($table, $field, 'intro');
}
// book savepoint reached
upgrade_mod_savepoint(true, 2010120801, 'book');
}
if ($oldversion < 2010120802) {
// Rename field summary on table book to intro
$table = new xmldb_table('book');
$field = new xmldb_field('intro', XMLDB_TYPE_TEXT, 'big', null, null, null, null, 'name');
// Launch rename field summary
$dbman->change_field_precision($table, $field);
// book savepoint reached
upgrade_mod_savepoint(true, 2010120802, 'book');
}
if ($oldversion < 2010120803) {
// Define field introformat to be added to book
$table = new xmldb_table('book');
$field = new xmldb_field('introformat', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'intro');
// Launch add field introformat
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
// Conditionally migrate to html format in intro
if ($CFG->texteditors !== 'textarea') {
$rs = $DB->get_recordset('book', array('introformat'=>FORMAT_MOODLE), '', 'id,intro,introformat');
foreach ($rs as $b) {
$b->intro = text_to_html($b->intro, false, false, true);
$b->introformat = FORMAT_HTML;
$DB->update_record('book', $b);
upgrade_set_timeout();
}
unset($b);
$rs->close();
}
// book savepoint reached
upgrade_mod_savepoint(true, 2010120803, 'book');
}
if ($oldversion < 2010120804) {
// Define field introformat to be added to book
$table = new xmldb_table('book_chapters');
$field = new xmldb_field('contentformat', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'content');
// Launch add field introformat
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
$DB->set_field('book_chapters', 'contentformat', FORMAT_HTML, array());
// book savepoint reached
upgrade_mod_savepoint(true, 2010120804, 'book');
}
if ($oldversion < 2010120805) {
require_once("$CFG->dirroot/mod/book/db/upgradelib.php");
$sqlfrom = "FROM {book} b
JOIN {modules} m ON m.name = 'book'
JOIN {course_modules} cm ON (cm.module = m.id AND cm.instance = b.id)";
$count = $DB->count_records_sql("SELECT COUNT('x') $sqlfrom");
if ($rs = $DB->get_recordset_sql("SELECT b.id, b.course, cm.id AS cmid $sqlfrom ORDER BY b.course, b.id")) {
$pbar = new progress_bar('migratebookfiles', 500, true);
$i = 0;
foreach ($rs as $book) {
$i++;
upgrade_set_timeout(360); // set up timeout, may also abort execution
$pbar->update($i, $count, "Migrating book files - $i/$count.");
$context = context_module::instance($book->cmid);
mod_book_migrate_moddata_dir_to_legacy($book, $context, '/');
// remove dirs if empty
@rmdir("$CFG->dataroot/$book->course/$CFG->moddata/book/$book->id/");
@rmdir("$CFG->dataroot/$book->course/$CFG->moddata/book/");
@rmdir("$CFG->dataroot/$book->course/$CFG->moddata/");
@rmdir("$CFG->dataroot/$book->course/");
}
$rs->close();
}
// book savepoint reached
upgrade_mod_savepoint(true, 2010120805, 'book');
}
if ($oldversion < 2011011600) {
// Define field disableprinting to be dropped from book
$table = new xmldb_table('book');
$field = new xmldb_field('disableprinting');
// Conditionally launch drop field disableprinting
if ($dbman->field_exists($table, $field)) {
$dbman->drop_field($table, $field);
}
// book savepoint reached
upgrade_mod_savepoint(true, 2011011600, 'book');
}
if ($oldversion < 2011011601) {
unset_config('book_tocwidth');
// book savepoint reached
upgrade_mod_savepoint(true, 2011011601, 'book');
}
if ($oldversion < 2011090800) {
require_once("$CFG->dirroot/mod/book/db/upgradelib.php");
mod_book_migrate_all_areas();
upgrade_mod_savepoint(true, 2011090800, 'book');
}
if ($oldversion < 2011100900) {
// Define field revision to be added to book
$table = new xmldb_table('book');
$field = new xmldb_field('revision', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'customtitles');
// Conditionally launch add field revision
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
// book savepoint reached
upgrade_mod_savepoint(true, 2011100900, 'book');
}
// Moodle v2.2.0 release upgrade line
// Put any upgrade step following this
return true;
}
-168
View File
@@ -1,168 +0,0 @@
<?php
// This file is part of Book module for Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Book module upgrade related helper functions
*
* @package mod_book
* @copyright 2010 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die;
/**
* Migrate book files stored in moddata folders.
*
* Please note it was a big mistake to store the files there in the first place!
*
* @param stdClass $book
* @param stdClass $context
* @param string $path
* @return void
*/
function mod_book_migrate_moddata_dir_to_legacy($book, $context, $path) {
global $OUTPUT, $CFG;
$base = "$CFG->dataroot/$book->course/$CFG->moddata/book/$book->id";
$fulldir = $base.$path;
if (!is_dir($fulldir)) {
// does not exist
return;
}
$fs = get_file_storage();
$items = new DirectoryIterator($fulldir);
foreach ($items as $item) {
if ($item->isDot()) {
unset($item); // release file handle
continue;
}
if ($item->isLink()) {
// do not follow symlinks - they were never supported in moddata, sorry
unset($item); // release file handle
continue;
}
if ($item->isFile()) {
if (!$item->isReadable()) {
echo $OUTPUT->notification(" File not readable, skipping: ".$fulldir.$item->getFilename());
unset($item); // release file handle
continue;
}
$filepath = clean_param("/$CFG->moddata/book/$book->id".$path, PARAM_PATH);
$filename = clean_param($item->getFilename(), PARAM_FILE);
if ($filename === '') {
// unsupported chars, sorry
unset($item); // release file handle
continue;
}
if (textlib::strlen($filepath) > 255) {
echo $OUTPUT->notification(" File path longer than 255 chars, skipping: ".$fulldir.$item->getFilename());
unset($item); // release file handle
continue;
}
if (!$fs->file_exists($context->id, 'course', 'legacy', '0', $filepath, $filename)) {
$file_record = array('contextid'=>$context->id, 'component'=>'course', 'filearea'=>'legacy', 'itemid'=>0, 'filepath'=>$filepath, 'filename'=>$filename,
'timecreated'=>$item->getCTime(), 'timemodified'=>$item->getMTime());
$fs->create_file_from_pathname($file_record, $fulldir.$item->getFilename());
}
$oldpathname = $fulldir.$item->getFilename();
unset($item); // release file handle
@unlink($oldpathname);
} else {
// migrate recursively all subdirectories
$oldpathname = $base.$item->getFilename().'/';
$subpath = $path.$item->getFilename().'/';
unset($item); // release file handle
mod_book_migrate_moddata_dir_to_legacy($book, $context, $subpath);
@rmdir($oldpathname); // deletes dir if empty
}
}
unset($items); // release file handles
}
/**
* Migrate legacy files in intro and chapters
* @return void
*/
function mod_book_migrate_all_areas() {
global $DB;
$rsbooks = $DB->get_recordset('book');
foreach($rsbooks as $book) {
upgrade_set_timeout(360); // set up timeout, may also abort execution
$cm = get_coursemodule_from_instance('book', $book->id);
$context = context_module::instance($cm->id);
mod_book_migrate_area($book, 'intro', 'book', $book->course, $context, 'mod_book', 'intro', 0);
$rschapters = $DB->get_recordset('book_chapters', array('bookid'=>$book->id));
foreach ($rschapters as $chapter) {
mod_book_migrate_area($chapter, 'content', 'book_chapters', $book->course, $context, 'mod_book', 'chapter', $chapter->id);
}
$rschapters->close();
}
$rsbooks->close();
}
/**
* Migrate one area, this should be probably part of moodle core...
*
* @param stdClass $record object to migrate files (book, chapter)
* @param string $field field in the record we are going to migrate
* @param string $table DB table containing the information to migrate
* @param int $courseid id of the course the book module belongs to
* @param context_module $context context of the book module
* @param string $component component to be used for the migrated files
* @param string $filearea filearea to be used for the migrated files
* @param int $itemid id to be used for the migrated files
* @return void
*/
function mod_book_migrate_area($record, $field, $table, $courseid, $context, $component, $filearea, $itemid) {
global $CFG, $DB;
$fs = get_file_storage();
foreach(array(get_site()->id, $courseid) as $cid) {
$matches = null;
$ooldcontext = context_course::instance($cid);
if (preg_match_all("|$CFG->wwwroot/file.php(\?file=)?/$cid(/[^\s'\"&\?#]+)|", $record->$field, $matches)) {
$file_record = array('contextid'=>$context->id, 'component'=>$component, 'filearea'=>$filearea, 'itemid'=>$itemid);
foreach ($matches[2] as $i=>$filepath) {
if (!$file = $fs->get_file_by_hash(sha1("/$ooldcontext->id/course/legacy/0".$filepath))) {
continue;
}
try {
if (!$newfile = $fs->get_file_by_hash(sha1("/$context->id/$component/$filearea/$itemid".$filepath))) {
$fs->create_file_from_storedfile($file_record, $file);
}
$record->$field = str_replace($matches[0][$i], '@@PLUGINFILE@@'.$filepath, $record->$field);
} catch (Exception $ex) {
// ignore problems
}
$DB->set_field($table, $field, $record->$field, array('id'=>$record->id));
}
}
}
}