From: skodak <skodak>
Date: Sun, 15 Jun 2008 10:32:50 +0000 (+0000)
Subject: MDL-15246 moving all remaining ddl function to adminlib, keeping old lib/ddllib.php... 
X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=b1f93b15124f6428b9e92e3c2f4608fd68c40e42;p=moodle.git

MDL-15246 moving all remaining ddl function to adminlib, keeping old lib/ddllib.php only for BC withexisting code; fixed inline docs related to ddl
---

diff --git a/admin/blocks.php b/admin/blocks.php
index 162cbcfcc0..6195cb7574 100644
--- a/admin/blocks.php
+++ b/admin/blocks.php
@@ -6,7 +6,6 @@
     require_once($CFG->libdir.'/adminlib.php');
     require_once($CFG->libdir.'/blocklib.php');
     require_once($CFG->libdir.'/tablelib.php');
-    require_once($CFG->libdir.'/ddllib.php');
 
     admin_externalpage_setup('manageblocks');
 
diff --git a/admin/cliupgrade.php b/admin/cliupgrade.php
index e17821cbea..fb36a2a75c 100644
--- a/admin/cliupgrade.php
+++ b/admin/cliupgrade.php
@@ -717,7 +717,6 @@ if ( file_exists(dirname(dirname(__FILE__)) . '/config.php')) {
 
     require_once(dirname(dirname(__FILE__)) . '/config.php');
     require_once($CFG->libdir.'/adminlib.php');  // Contains various admin-only functions
-    require_once($CFG->libdir.'/ddllib.php'); // Install/upgrade related db functions
 
     /**
      * @todo check upgrade status, if upgrader is running already, notify user and exit. 
diff --git a/admin/index.php b/admin/index.php
index e04540af0f..8736dfbcd6 100644
--- a/admin/index.php
+++ b/admin/index.php
@@ -23,7 +23,6 @@
 
     require_once('../config.php');
     require_once($CFG->libdir.'/adminlib.php');  // Contains various admin-only functions
-    require_once($CFG->libdir.'/ddllib.php'); // Install/upgrade related db functions
     require_once($CFG->libdir.'/db/upgradelib.php');  // Upgrade-related functions
 
     $id             = optional_param('id', '', PARAM_TEXT);
diff --git a/admin/modules.php b/admin/modules.php
index 9c55fdf510..450b8003c5 100644
--- a/admin/modules.php
+++ b/admin/modules.php
@@ -5,7 +5,6 @@
     require_once('../course/lib.php');
     require_once($CFG->libdir.'/adminlib.php');
     require_once($CFG->libdir.'/tablelib.php');
-    require_once($CFG->libdir.'/ddllib.php');
 
     // defines
     define('MODULE_TABLE','module_administration_table');
diff --git a/admin/xmldb/actions/check_bigints/check_bigints.class.php b/admin/xmldb/actions/check_bigints/check_bigints.class.php
index 8553d61b49..bb3ef49b5a 100644
--- a/admin/xmldb/actions/check_bigints/check_bigints.class.php
+++ b/admin/xmldb/actions/check_bigints/check_bigints.class.php
@@ -81,9 +81,6 @@ class check_bigints extends XMLDBAction {
         $dbman = $DB->get_manager();
         $dbfamily = $DB->get_dbfamily();
 
-    /// And we nedd some ddl suff
-        require_once ($CFG->libdir . '/ddllib.php');
-
     /// Here we'll acummulate all the wrong fields found
         $wrong_fields = array();
 
diff --git a/admin/xmldb/actions/check_defaults/check_defaults.class.php b/admin/xmldb/actions/check_defaults/check_defaults.class.php
index 8f6cde2232..c63956d513 100644
--- a/admin/xmldb/actions/check_defaults/check_defaults.class.php
+++ b/admin/xmldb/actions/check_defaults/check_defaults.class.php
@@ -79,7 +79,6 @@ class check_defaults extends XMLDBAction {
         global $CFG, $XMLDB, $DB;
 
     /// And we nedd some ddl suff
-        require_once ($CFG->libdir . '/ddllib.php');
         $dbman = $DB->get_manager();
 
     /// Here we'll acummulate all the wrong fields found
diff --git a/admin/xmldb/actions/check_indexes/check_indexes.class.php b/admin/xmldb/actions/check_indexes/check_indexes.class.php
index 7b347faab3..a39c0ad272 100644
--- a/admin/xmldb/actions/check_indexes/check_indexes.class.php
+++ b/admin/xmldb/actions/check_indexes/check_indexes.class.php
@@ -78,9 +78,6 @@ class check_indexes extends XMLDBAction {
 
         $dbman = $DB->get_manager();
 
-    /// And we nedd some ddl suff
-        require_once ($CFG->libdir . '/ddllib.php');
-
     /// Here we'll acummulate all the missing indexes found
         $missing_indexes = array();
 
diff --git a/admin/xmldb/index.php b/admin/xmldb/index.php
index 546d4323e3..f2f7adb9fb 100644
--- a/admin/xmldb/index.php
+++ b/admin/xmldb/index.php
@@ -50,7 +50,6 @@
 /// getting "incomplete" objects
     require_once('../../config.php');
     require_once($CFG->libdir.'/adminlib.php');
-    require_once($CFG->libdir.'/ddllib.php'); // Install/upgrade related db functions
 
     admin_externalpage_setup('xmldbeditor');
 
diff --git a/backup/db/upgrade.php b/backup/db/upgrade.php
index bc1af02f28..de6597c6f4 100644
--- a/backup/db/upgrade.php
+++ b/backup/db/upgrade.php
@@ -15,7 +15,7 @@
 // will tell you what you need to do.
 //
 // The commands in here will all be database-neutral,
-// using the functions defined in lib/ddllib.php
+// using the methods of database_manager class
 
 function xmldb_backup_upgrade($oldversion=0) {
 
@@ -29,7 +29,7 @@ function xmldb_backup_upgrade($oldversion=0) {
 /// upgrade code.
 
 /// if ($result && $oldversion < YYYYMMDD00) { //New version in version.php
-///     $result = result of "/lib/ddllib.php" function calls
+///     $result = result of database_manager methods
 /// }
 
     return $result;
diff --git a/blocks/activity_modules/db/upgrade.php b/blocks/activity_modules/db/upgrade.php
index 0a0623f9ba..9cd056bdae 100644
--- a/blocks/activity_modules/db/upgrade.php
+++ b/blocks/activity_modules/db/upgrade.php
@@ -15,7 +15,7 @@
 // will tell you what you need to do.
 //
 // The commands in here will all be database-neutral,
-// using the functions defined in lib/ddllib.php
+// using the methods of database_manager class
 
 function xmldb_block_activity_modules_upgrade($oldversion=0) {
 
@@ -29,7 +29,7 @@ function xmldb_block_activity_modules_upgrade($oldversion=0) {
 /// upgrade code.
 
 /// if ($result && $oldversion < YYYYMMDD00) { //New version in version.php
-///     $result = result of "/lib/ddllib.php" function calls
+///     $result = result of database_manager methods
 /// }
 
     return $result;
diff --git a/blocks/admin/db/upgrade.php b/blocks/admin/db/upgrade.php
index 115f3ce6d0..fc6ef1403d 100644
--- a/blocks/admin/db/upgrade.php
+++ b/blocks/admin/db/upgrade.php
@@ -15,7 +15,7 @@
 // will tell you what you need to do.
 //
 // The commands in here will all be database-neutral,
-// using the functions defined in lib/ddllib.php
+// using the methods of database_manager class
 
 function xmldb_block_admin_upgrade($oldversion=0) {
 
@@ -29,7 +29,7 @@ function xmldb_block_admin_upgrade($oldversion=0) {
 /// upgrade code.
 
 /// if ($result && $oldversion < YYYYMMDD00) { //New version in version.php
-///     $result = result of "/lib/ddllib.php" function calls
+///     $result = result of database_manager methods
 /// }
 
     return $result;
diff --git a/blocks/calendar_month/db/upgrade.php b/blocks/calendar_month/db/upgrade.php
index 99ff41a448..ba3745b09e 100644
--- a/blocks/calendar_month/db/upgrade.php
+++ b/blocks/calendar_month/db/upgrade.php
@@ -15,7 +15,7 @@
 // will tell you what you need to do.
 //
 // The commands in here will all be database-neutral,
-// using the functions defined in lib/ddllib.php
+// using the methods of database_manager class
 
 function xmldb_block_calendar_month_upgrade($oldversion=0) {
 
@@ -29,7 +29,7 @@ function xmldb_block_calendar_month_upgrade($oldversion=0) {
 /// upgrade code.
 
 /// if ($result && $oldversion < YYYYMMDD00) { //New version in version.php
-///     $result = result of "/lib/ddllib.php" function calls
+///     $result = result of database_manager methods
 /// }
 
     return $result;
diff --git a/blocks/calendar_upcoming/db/upgrade.php b/blocks/calendar_upcoming/db/upgrade.php
index 2c4cefb2e9..1ee5c024f3 100644
--- a/blocks/calendar_upcoming/db/upgrade.php
+++ b/blocks/calendar_upcoming/db/upgrade.php
@@ -15,7 +15,7 @@
 // will tell you what you need to do.
 //
 // The commands in here will all be database-neutral,
-// using the functions defined in lib/ddllib.php
+// using the methods of database_manager class
 
 function xmldb_block_calendar_upcoming_upgrade($oldversion=0) {
 
@@ -29,7 +29,7 @@ function xmldb_block_calendar_upcoming_upgrade($oldversion=0) {
 /// upgrade code.
 
 /// if ($result && $oldversion < YYYYMMDD00) { //New version in version.php
-///     $result = result of "/lib/ddllib.php" function calls
+///     $result = result of database_manager methods
 /// }
 
     return $result;
diff --git a/blocks/course_list/db/upgrade.php b/blocks/course_list/db/upgrade.php
index b5c3bd8365..fb46ad1b6e 100644
--- a/blocks/course_list/db/upgrade.php
+++ b/blocks/course_list/db/upgrade.php
@@ -15,7 +15,7 @@
 // will tell you what you need to do.
 //
 // The commands in here will all be database-neutral,
-// using the functions defined in lib/ddllib.php
+// using the methods of database_manager class
 
 function xmldb_block_course_list_upgrade($oldversion=0) {
 
@@ -29,7 +29,7 @@ function xmldb_block_course_list_upgrade($oldversion=0) {
 /// upgrade code.
 
 /// if ($result && $oldversion < YYYYMMDD00) { //New version in version.php
-///     $result = result of "/lib/ddllib.php" function calls
+///     $result = result of database_manager methods
 /// }
 
     return $result;
diff --git a/blocks/course_summary/db/upgrade.php b/blocks/course_summary/db/upgrade.php
index 633555bbab..28f823ce7c 100644
--- a/blocks/course_summary/db/upgrade.php
+++ b/blocks/course_summary/db/upgrade.php
@@ -15,7 +15,7 @@
 // will tell you what you need to do.
 //
 // The commands in here will all be database-neutral,
-// using the functions defined in lib/ddllib.php
+// using the methods of database_manager class
 
 function xmldb_block_course_summary_upgrade($oldversion=0) {
 
@@ -29,7 +29,7 @@ function xmldb_block_course_summary_upgrade($oldversion=0) {
 /// upgrade code.
 
 /// if ($result && $oldversion < YYYYMMDD00) { //New version in version.php
-///     $result = result of "/lib/ddllib.php" function calls
+///     $result = result of database_manager methods
 /// }
 
     return $result;
diff --git a/blocks/db/upgrade.php b/blocks/db/upgrade.php
index 66740a17d7..784808bb43 100644
--- a/blocks/db/upgrade.php
+++ b/blocks/db/upgrade.php
@@ -15,7 +15,7 @@
 // will tell you what you need to do.
 //
 // The commands in here will all be database-neutral,
-// using the functions defined in lib/ddllib.php
+// using the methods of database_manager class
 
 function xmldb_blocks_upgrade($oldversion=0) {
 
@@ -31,7 +31,7 @@ function xmldb_blocks_upgrade($oldversion=0) {
 /// upgrade code.
 
 /// if ($result && $oldversion < YYYYMMDD00) { //New version in version.php
-///     $result = result of "/lib/ddllib.php" function calls
+///     $result = result of database_manager methods
 /// }
 
     if ($result && $oldversion < 2007081300) {
diff --git a/blocks/news_items/db/upgrade.php b/blocks/news_items/db/upgrade.php
index ee40a9d107..bd9b97562b 100644
--- a/blocks/news_items/db/upgrade.php
+++ b/blocks/news_items/db/upgrade.php
@@ -15,7 +15,7 @@
 // will tell you what you need to do.
 //
 // The commands in here will all be database-neutral,
-// using the functions defined in lib/ddllib.php
+// using the methods of database_manager class
 
 function xmldb_block_news_items_upgrade($oldversion=0) {
 
@@ -29,7 +29,7 @@ function xmldb_block_news_items_upgrade($oldversion=0) {
 /// upgrade code.
 
 /// if ($result && $oldversion < YYYYMMDD00) { //New version in version.php
-///     $result = result of "/lib/ddllib.php" function calls
+///     $result = result of database_manager methods
 /// }
 
     return $result;
diff --git a/blocks/online_users/db/upgrade.php b/blocks/online_users/db/upgrade.php
index 31f3632c57..334c81f696 100644
--- a/blocks/online_users/db/upgrade.php
+++ b/blocks/online_users/db/upgrade.php
@@ -15,7 +15,7 @@
 // will tell you what you need to do.
 //
 // The commands in here will all be database-neutral,
-// using the functions defined in lib/ddllib.php
+// using the methods of database_manager class
 
 function xmldb_block_online_users_upgrade($oldversion=0) {
 
@@ -29,7 +29,7 @@ function xmldb_block_online_users_upgrade($oldversion=0) {
 /// upgrade code.
 
 /// if ($result && $oldversion < YYYYMMDD00) { //New version in version.php
-///     $result = result of "/lib/ddllib.php" function calls
+///     $result = result of database_manager methods
 /// }
 
     return $result;
diff --git a/blocks/participants/db/upgrade.php b/blocks/participants/db/upgrade.php
index faea7eb5ff..9decca3880 100644
--- a/blocks/participants/db/upgrade.php
+++ b/blocks/participants/db/upgrade.php
@@ -15,7 +15,7 @@
 // will tell you what you need to do.
 //
 // The commands in here will all be database-neutral,
-// using the functions defined in lib/ddllib.php
+// using the methods of database_manager class
 
 function xmldb_block_participants_upgrade($oldversion=0) {
 
@@ -29,7 +29,7 @@ function xmldb_block_participants_upgrade($oldversion=0) {
 /// upgrade code.
 
 /// if ($result && $oldversion < YYYYMMDD00) { //New version in version.php
-///     $result = result of "/lib/ddllib.php" function calls
+///     $result = result of database_manager methods
 /// }
 
     return $result;
diff --git a/blocks/recent_activity/db/upgrade.php b/blocks/recent_activity/db/upgrade.php
index 36b534eb30..65082fd962 100644
--- a/blocks/recent_activity/db/upgrade.php
+++ b/blocks/recent_activity/db/upgrade.php
@@ -15,7 +15,7 @@
 // will tell you what you need to do.
 //
 // The commands in here will all be database-neutral,
-// using the functions defined in lib/ddllib.php
+// using the methods of database_manager class
 
 function xmldb_block_recent_activity_upgrade($oldversion=0) {
 
@@ -29,7 +29,7 @@ function xmldb_block_recent_activity_upgrade($oldversion=0) {
 /// upgrade code.
 
 /// if ($result && $oldversion < YYYYMMDD00) { //New version in version.php
-///     $result = result of "/lib/ddllib.php" function calls
+///     $result = result of database_manager methods
 /// }
 
     return $result;
diff --git a/blocks/rss_client/db/upgrade.php b/blocks/rss_client/db/upgrade.php
index 90996fbc78..c8261d020c 100644
--- a/blocks/rss_client/db/upgrade.php
+++ b/blocks/rss_client/db/upgrade.php
@@ -15,7 +15,7 @@
 // will tell you what you need to do.
 //
 // The commands in here will all be database-neutral,
-// using the functions defined in lib/ddllib.php
+// using the methods of database_manager class
 
 function xmldb_block_rss_client_upgrade($oldversion=0) {
 
diff --git a/blocks/search/db/upgrade.php b/blocks/search/db/upgrade.php
index ef8679042e..80ce4c4b5a 100644
--- a/blocks/search/db/upgrade.php
+++ b/blocks/search/db/upgrade.php
@@ -15,7 +15,7 @@
 // will tell you what you need to do.
 //
 // The commands in here will all be database-neutral,
-// using the functions defined in lib/ddllib.php
+// using the methods of database_manager class
 
 function xmldb_block_search_upgrade($oldversion=0) {
 
@@ -29,7 +29,7 @@ function xmldb_block_search_upgrade($oldversion=0) {
 /// upgrade code.
 
 /// if ($result && $oldversion < YYYYMMDD00) { //New version in version.php
-///     $result = result of "/lib/ddllib.php" function calls
+///     $result = result of database_manager methods
 /// }
 
 //===== 1.9.0 upgrade line ======//
diff --git a/blocks/search_forums/db/upgrade.php b/blocks/search_forums/db/upgrade.php
index ec4a9b84c3..99ae3c9ae8 100644
--- a/blocks/search_forums/db/upgrade.php
+++ b/blocks/search_forums/db/upgrade.php
@@ -15,7 +15,7 @@
 // will tell you what you need to do.
 //
 // The commands in here will all be database-neutral,
-// using the functions defined in lib/ddllib.php
+// using the methods of database_manager class
 
 function xmldb_block_search_forums_upgrade($oldversion=0) {
 
@@ -29,7 +29,7 @@ function xmldb_block_search_forums_upgrade($oldversion=0) {
 /// upgrade code.
 
 /// if ($result && $oldversion < YYYYMMDD00) { //New version in version.php
-///     $result = result of "/lib/ddllib.php" function calls
+///     $result = result of database_manager methods
 /// }
 
     return $result;
diff --git a/blocks/section_links/db/upgrade.php b/blocks/section_links/db/upgrade.php
index 67b3b1bbbd..2e1c6dfab7 100644
--- a/blocks/section_links/db/upgrade.php
+++ b/blocks/section_links/db/upgrade.php
@@ -15,7 +15,7 @@
 // will tell you what you need to do.
 //
 // The commands in here will all be database-neutral,
-// using the functions defined in lib/ddllib.php
+// using the methods of database_manager class
 
 function xmldb_block_section_links_upgrade($oldversion=0) {
 
@@ -29,7 +29,7 @@ function xmldb_block_section_links_upgrade($oldversion=0) {
 /// upgrade code.
 
 /// if ($result && $oldversion < YYYYMMDD00) { //New version in version.php
-///     $result = result of "/lib/ddllib.php" function calls
+///     $result = result of database_manager methods
 /// }
 
     return $result;
diff --git a/blocks/social_activities/db/upgrade.php b/blocks/social_activities/db/upgrade.php
index 491f241849..914bd22ca9 100644
--- a/blocks/social_activities/db/upgrade.php
+++ b/blocks/social_activities/db/upgrade.php
@@ -15,7 +15,7 @@
 // will tell you what you need to do.
 //
 // The commands in here will all be database-neutral,
-// using the functions defined in lib/ddllib.php
+// using the methods of database_manager class
 
 function xmldb_block_social_activities_upgrade($oldversion=0) {
 
@@ -29,7 +29,7 @@ function xmldb_block_social_activities_upgrade($oldversion=0) {
 /// upgrade code.
 
 /// if ($result && $oldversion < YYYYMMDD00) { //New version in version.php
-///     $result = result of "/lib/ddllib.php" function calls
+///     $result = result of database_manager methods
 /// }
 
     return $result;
diff --git a/enrol/authorize/db/upgrade.php b/enrol/authorize/db/upgrade.php
index a0ab95156e..37034a2ffa 100644
--- a/enrol/authorize/db/upgrade.php
+++ b/enrol/authorize/db/upgrade.php
@@ -15,7 +15,7 @@
 // will tell you what you need to do.
 //
 // The commands in here will all be database-neutral,
-// using the functions defined in lib/ddllib.php
+// using the methods of database_manager class
 
 function xmldb_enrol_authorize_upgrade($oldversion=0) {
 
diff --git a/enrol/paypal/db/upgrade.php b/enrol/paypal/db/upgrade.php
index 2fd5c4308a..7c39348e9a 100644
--- a/enrol/paypal/db/upgrade.php
+++ b/enrol/paypal/db/upgrade.php
@@ -15,7 +15,7 @@
 // will tell you what you need to do.
 //
 // The commands in here will all be database-neutral,
-// using the functions defined in lib/ddllib.php
+// using the methods of database_manager class
 
 function xmldb_enrol_paypal_upgrade($oldversion=0) {
 
@@ -29,7 +29,7 @@ function xmldb_enrol_paypal_upgrade($oldversion=0) {
 /// upgrade code.
 
 /// if ($result && $oldversion < YYYYMMDD00) { //New version in version.php
-///     $result = result of "/lib/ddllib.php" function calls
+///     $result = result of database_manager methods
 /// }
 
     return $result;
diff --git a/lib/accesslib.php b/lib/accesslib.php
index ccd0ef378f..e72cc41021 100755
--- a/lib/accesslib.php
+++ b/lib/accesslib.php
@@ -5125,7 +5125,6 @@ function component_level_changed($cap, $comp, $contextlevel) {
  */
 function build_context_path($force=false) {
     global $CFG, $DB;
-    require_once($CFG->libdir.'/ddllib.php');
 
     // System context
     $sitectx = get_system_context(!$force);
diff --git a/lib/adminlib.php b/lib/adminlib.php
index 6c4d2f7ba3..3cae116116 100644
--- a/lib/adminlib.php
+++ b/lib/adminlib.php
@@ -9,6 +9,22 @@
  * @package moodlecore
  */
 
+/// Add required XMLDB constants
+require_once($CFG->libdir.'/xmldb/xmldb_constants.php');
+
+/// Add required XMLDB DB classes
+require_once($CFG->libdir.'/xmldb/xmldb_object.php');
+require_once($CFG->libdir.'/xmldb/xmldb_file.php');
+require_once($CFG->libdir.'/xmldb/xmldb_structure.php');
+require_once($CFG->libdir.'/xmldb/xmldb_table.php');
+require_once($CFG->libdir.'/xmldb/xmldb_field.php');
+require_once($CFG->libdir.'/xmldb/xmldb_key.php');
+require_once($CFG->libdir.'/xmldb/xmldb_index.php');
+require_once($CFG->libdir.'/xmldb/xmldb_statement.php');
+
+/// Add other libraries
+require_once($CFG->libdir.'/xmlize.php');
+
 function upgrade_main_savepoint($result, $version) {
     global $CFG;
 
@@ -39,6 +55,146 @@ function upgrade_blocks_savepoint($result, $version, $type) {
     //TODO
 }
 
+/**
+ * Delete all plugin tables
+ * @name string name of plugin, used as table prefix
+ * @file string path to install.xml file
+ * @feedback boolean
+ */
+function drop_plugin_tables($name, $file, $feedback=true) {
+    global $CFG, $DB;
+
+    // first try normal delete
+    if ($DB->get_manager()->delete_tables_from_xmldb_file($file, $feedback)) {
+        return true;
+    }
+
+    // then try to find all tables that start with name and are not in any xml file
+    $used_tables = get_used_table_names();
+
+    $tables = $DB->get_tables();
+
+    /// Iterate over, fixing id fields as necessary
+    foreach ($tables as $table) {
+        if (in_array($table, $used_tables)) {
+            continue;
+        }
+
+        // found orphan table --> delete it
+        if ($DB->get_manager()->table_exists($table)) {
+            $xmldb_table = new xmldb_table($table);
+            $DB->get_manager()->drop_table($xmldb_table, true, $feedback);
+        }
+    }
+
+    return true;
+}
+
+/**
+ * Returns names of all known tables == tables that moodle knowns about.
+ * @return array of lowercase table names
+ */
+function get_used_table_names() {
+    $table_names = array();
+    $dbdirs = get_db_directories();
+
+    foreach ($dbdirs as $dbdir) {
+        $file = $dbdir.'/install.xml';
+
+        $xmldb_file = new xmldb_file($file);
+
+        if (!$xmldb_file->fileExists()) {
+            continue;
+        }
+
+        $loaded    = $xmldb_file->loadXMLStructure();
+        $structure =& $xmldb_file->getStructure();
+
+        if ($loaded and $tables = $structure->getTables()) {
+            foreach($tables as $table) {
+                $table_names[] = strtolower($table->name);
+            }
+        }
+    }
+
+    return $table_names;
+}
+
+/**
+ * Returns list of all directories where we expect install.xml files
+ * @return array of paths
+ */
+function get_db_directories() {
+    global $CFG;
+
+    $dbdirs = array();
+
+/// First, the main one (lib/db)
+    $dbdirs[] = $CFG->libdir.'/db';
+
+/// Now, activity modules (mod/xxx/db)
+    if ($plugins = get_list_of_plugins('mod')) {
+        foreach ($plugins as $plugin) {
+            $dbdirs[] = $CFG->dirroot.'/mod/'.$plugin.'/db';
+        }
+    }
+
+/// Now, assignment submodules (mod/assignment/type/xxx/db)
+    if ($plugins = get_list_of_plugins('mod/assignment/type')) {
+        foreach ($plugins as $plugin) {
+            $dbdirs[] = $CFG->dirroot.'/mod/assignment/type/'.$plugin.'/db';
+        }
+    }
+
+/// Now, question types (question/type/xxx/db)
+    if ($plugins = get_list_of_plugins('question/type')) {
+        foreach ($plugins as $plugin) {
+            $dbdirs[] = $CFG->dirroot.'/question/type/'.$plugin.'/db';
+        }
+    }
+
+/// Now, backup/restore stuff (backup/db)
+    $dbdirs[] = $CFG->dirroot.'/backup/db';
+
+/// Now, block system stuff (blocks/db)
+    $dbdirs[] = $CFG->dirroot.'/blocks/db';
+
+/// Now, blocks (blocks/xxx/db)
+    if ($plugins = get_list_of_plugins('blocks', 'db')) {
+        foreach ($plugins as $plugin) {
+            $dbdirs[] = $CFG->dirroot.'/blocks/'.$plugin.'/db';
+        }
+    }
+
+/// Now, course formats (course/format/xxx/db)
+    if ($plugins = get_list_of_plugins('course/format', 'db')) {
+        foreach ($plugins as $plugin) {
+            $dbdirs[] = $CFG->dirroot.'/course/format/'.$plugin.'/db';
+        }
+    }
+
+/// Now, enrolment plugins (enrol/xxx/db)
+    if ($plugins = get_list_of_plugins('enrol', 'db')) {
+        foreach ($plugins as $plugin) {
+            $dbdirs[] = $CFG->dirroot.'/enrol/'.$plugin.'/db';
+        }
+    }
+
+/// Now admin report plugins (admin/report/xxx/db)
+    if ($plugins = get_list_of_plugins($CFG->admin.'/report', 'db')) {
+        foreach ($plugins as $plugin) {
+            $dbdirs[] = $CFG->dirroot.'/'.$CFG->admin.'/report/'.$plugin.'/db';
+        }
+    }
+
+/// Local database changes, if the local folder exists.
+    if (file_exists($CFG->dirroot . '/local')) {
+        $dbdirs[] = $CFG->dirroot.'/local/db';
+    }
+
+    return $dbdirs;
+}
+
 /**
  * Upgrade plugins
  *
diff --git a/lib/db/upgrade.php b/lib/db/upgrade.php
index 52c1e3f161..ab98e01da6 100644
--- a/lib/db/upgrade.php
+++ b/lib/db/upgrade.php
@@ -14,7 +14,7 @@
 // will tell you what you need to do.
 //
 // The commands in here will all be database-neutral,
-// using the functions defined in lib/ddllib.php
+// using the methods of database_manager class
 
 
 function xmldb_main_upgrade($oldversion=0) {
diff --git a/lib/ddl/database_manager.php b/lib/ddl/database_manager.php
index b9ded9841a..a4a631a0a8 100644
--- a/lib/ddl/database_manager.php
+++ b/lib/ddl/database_manager.php
@@ -60,7 +60,6 @@ class database_manager {
     /**
      * This function will execute an array of SQL commands, returning
      * true/false if any error is found and stopping/continue as desired.
-     * It's widely used by all the ddllib.php functions
      *
      * @param array $sqlarr array of sql statements to execute
      * @param boolean $continue to specify if must continue on error (true) or stop (false)
diff --git a/lib/ddl/mssql_sql_generator.php b/lib/ddl/mssql_sql_generator.php
index f6b995d5db..c892ad96af 100644
--- a/lib/ddl/mssql_sql_generator.php
+++ b/lib/ddl/mssql_sql_generator.php
@@ -220,7 +220,7 @@ class mssql_sql_generator extends sql_generator {
 
         $results = array();  //Array where all the sentences will be stored
 
-    /// Although this is checked in ddllib - rename_field() - double check
+    /// Although this is checked in database_manager::rename_field() - double check
     /// that we aren't trying to rename one "id" field. Although it could be
     /// implemented (if adding the necessary code to rename sequences, defaults,
     /// triggers... and so on under each getRenameFieldExtraSQL() function, it's
diff --git a/lib/ddl/simpletest/testddllib.php b/lib/ddl/simpletest/testddllib.php
index 65035f72ba..0002f101a9 100755
--- a/lib/ddl/simpletest/testddllib.php
+++ b/lib/ddl/simpletest/testddllib.php
@@ -9,7 +9,7 @@ if (!defined('MOODLE_INTERNAL')) {
     die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
 }
 
-require_once($CFG->libdir . '/ddllib.php');
+require_once($CFG->libdir . '/adminlib.php');
 
 class ddllib_test extends UnitTestCase {
     private $tables = array();
diff --git a/lib/ddl/sql_generator.php b/lib/ddl/sql_generator.php
index c6881ea1e9..3608f57db5 100644
--- a/lib/ddl/sql_generator.php
+++ b/lib/ddl/sql_generator.php
@@ -767,7 +767,7 @@ abstract class sql_generator {
 
         $results = array();  //Array where all the sentences will be stored
 
-    /// Although this is checked in ddllib - rename_field() - double check
+    /// Although this is checked in database_manager::rename_field() - double check
     /// that we aren't trying to rename one "id" field. Although it could be
     /// implemented (if adding the necessary code to rename sequences, defaults,
     /// triggers... and so on under each getRenameFieldExtraSQL() function, it's
diff --git a/lib/ddllib.php b/lib/ddllib.php
index cfaa4505e0..28039cbe0c 100644
--- a/lib/ddllib.php
+++ b/lib/ddllib.php
@@ -38,332 +38,7 @@
 //
 // For further documentation, visit http://docs.moodle.org/en/DDL_functions
 
-/// Add required XMLDB constants
-require_once($CFG->libdir.'/xmldb/xmldb_constants.php');
-
-/// Add required XMLDB DB classes
-require_once($CFG->libdir.'/xmldb/xmldb_object.php');
-require_once($CFG->libdir.'/xmldb/xmldb_file.php');
-require_once($CFG->libdir.'/xmldb/xmldb_structure.php');
-require_once($CFG->libdir.'/xmldb/xmldb_table.php');
-require_once($CFG->libdir.'/xmldb/xmldb_field.php');
-require_once($CFG->libdir.'/xmldb/xmldb_key.php');
-require_once($CFG->libdir.'/xmldb/xmldb_index.php');
-require_once($CFG->libdir.'/xmldb/xmldb_statement.php');
-
-/// Add other libraries
-require_once($CFG->libdir.'/xmlize.php');
-
-/**
- * Delete all plugin tables
- * @name string name of plugin, used as table prefix
- * @file string path to install.xml file
- * @feedback boolean
- */
-function drop_plugin_tables($name, $file, $feedback=true) {
-    global $CFG, $DB;
-
-    // first try normal delete
-    if ($DB->get_manager()->delete_tables_from_xmldb_file($file, $feedback)) {
-        return true;
-    }
-
-    // then try to find all tables that start with name and are not in any xml file
-    $used_tables = get_used_table_names();
-
-    $tables = $DB->get_tables();
-
-    /// Iterate over, fixing id fields as necessary
-    foreach ($tables as $table) {
-        if (in_array($table, $used_tables)) {
-            continue;
-        }
-
-        // found orphan table --> delete it
-        if ($DB->get_manager()->table_exists($table)) {
-            $xmldb_table = new xmldb_table($table);
-            $DB->get_manager()->drop_table($xmldb_table, true, $feedback);
-        }
-    }
-
-    return true;
-}
-
-/**
- * Returns names of all known tables == tables that moodle knowns about.
- * @return array of lowercase table names
- */
-function get_used_table_names() {
-    $table_names = array();
-    $dbdirs = get_db_directories();
-
-    foreach ($dbdirs as $dbdir) {
-        $file = $dbdir.'/install.xml';
-
-        $xmldb_file = new xmldb_file($file);
-
-        if (!$xmldb_file->fileExists()) {
-            continue;
-        }
-
-        $loaded    = $xmldb_file->loadXMLStructure();
-        $structure =& $xmldb_file->getStructure();
-
-        if ($loaded and $tables = $structure->getTables()) {
-            foreach($tables as $table) {
-                $table_names[] = strtolower($table->name);
-            }
-        }
-    }
-
-    return $table_names;
-}
-
-/**
- * Returns list of all directories where we expect install.xml files
- * @return array of paths
- */
-function get_db_directories() {
-    global $CFG;
-
-    $dbdirs = array();
-
-/// First, the main one (lib/db)
-    $dbdirs[] = $CFG->libdir.'/db';
-
-/// Now, activity modules (mod/xxx/db)
-    if ($plugins = get_list_of_plugins('mod')) {
-        foreach ($plugins as $plugin) {
-            $dbdirs[] = $CFG->dirroot.'/mod/'.$plugin.'/db';
-        }
-    }
-
-/// Now, assignment submodules (mod/assignment/type/xxx/db)
-    if ($plugins = get_list_of_plugins('mod/assignment/type')) {
-        foreach ($plugins as $plugin) {
-            $dbdirs[] = $CFG->dirroot.'/mod/assignment/type/'.$plugin.'/db';
-        }
-    }
-
-/// Now, question types (question/type/xxx/db)
-    if ($plugins = get_list_of_plugins('question/type')) {
-        foreach ($plugins as $plugin) {
-            $dbdirs[] = $CFG->dirroot.'/question/type/'.$plugin.'/db';
-        }
-    }
-
-/// Now, backup/restore stuff (backup/db)
-    $dbdirs[] = $CFG->dirroot.'/backup/db';
-
-/// Now, block system stuff (blocks/db)
-    $dbdirs[] = $CFG->dirroot.'/blocks/db';
-
-/// Now, blocks (blocks/xxx/db)
-    if ($plugins = get_list_of_plugins('blocks', 'db')) {
-        foreach ($plugins as $plugin) {
-            $dbdirs[] = $CFG->dirroot.'/blocks/'.$plugin.'/db';
-        }
-    }
-
-/// Now, course formats (course/format/xxx/db)
-    if ($plugins = get_list_of_plugins('course/format', 'db')) {
-        foreach ($plugins as $plugin) {
-            $dbdirs[] = $CFG->dirroot.'/course/format/'.$plugin.'/db';
-        }
-    }
-
-/// Now, enrolment plugins (enrol/xxx/db)
-    if ($plugins = get_list_of_plugins('enrol', 'db')) {
-        foreach ($plugins as $plugin) {
-            $dbdirs[] = $CFG->dirroot.'/enrol/'.$plugin.'/db';
-        }
-    }
-
-/// Now admin report plugins (admin/report/xxx/db)
-    if ($plugins = get_list_of_plugins($CFG->admin.'/report', 'db')) {
-        foreach ($plugins as $plugin) {
-            $dbdirs[] = $CFG->dirroot.'/'.$CFG->admin.'/report/'.$plugin.'/db';
-        }
-    }
-
-/// Local database changes, if the local folder exists.
-    if (file_exists($CFG->dirroot . '/local')) {
-        $dbdirs[] = $CFG->dirroot.'/local/db';
-    }
-
-    return $dbdirs;
-}
-
-
 // DEPRECATED - to be removed soon
 
-function table_exists($table) {
-    global $DB;
-    debugging('Deprecated ddllib function used!');
-    return $DB->get_manager()->table_exists($table);
-}
-
-function field_exists($table, $field) {
-    global $DB;
-    debugging('Deprecated ddllib function used!');
-    return $DB->get_manager()->field_exists($table, $field);
-}
-
-function find_index_name($table, $index) {
-    global $DB;
-    debugging('Deprecated ddllib function used!');
-    return $DB->get_manager()->find_index_name($table, $index);
-}
-
-function index_exists($table, $index) {
-    global $DB;
-    debugging('Deprecated ddllib function used!');
-    return $DB->get_manager()->index_exists($table, $index);
-}
-
-function find_check_constraint_name($table, $field) {
-    global $DB;
-    debugging('Deprecated ddllib function used!');
-    return $DB->get_manager()->find_check_constraint_name($table, $field);
-}
-
-function check_constraint_exists($table, $field) {
-    global $DB;
-    debugging('Deprecated ddllib function used!');
-    return $DB->get_manager()->check_constraint_exists($table, $field);
-}
-
-function find_key_name($table, $xmldb_key) {
-    global $DB;
-    debugging('Deprecated ddllib function used!');
-    return $DB->get_manager()->find_key_name($table, $xmldb_key);
-}
-
-function find_sequence_name($table) {
-    global $DB;
-    debugging('Deprecated ddllib function used!');
-    return $DB->get_manager()->find_sequence_name($table);
-}
-
-function drop_table($table, $continue=true, $feedback=true) {
-    global $DB;
-    debugging('Deprecated ddllib function used!');
-    return $DB->get_manager()->drop_table($table, $continue, $feedback);
-}
-
-function install_from_xmldb_file($file) {
-    global $DB;
-    debugging('Deprecated ddllib function used!');
-    return $DB->get_manager()->install_from_xmldb_file($file);
-}
-
-function create_table($table, $continue=true, $feedback=true) {
-    global $DB;
-    debugging('Deprecated ddllib function used!');
-    return $DB->get_manager()->create_table($table, $continue, $feedback);
-}
-
-function create_temp_table($table, $continue=true, $feedback=true) {
-    global $DB;
-    debugging('Deprecated ddllib function used!');
-    return $DB->get_manager()->create_temp_table($table, $continue, $feedback);
-}
-
-function rename_table($table, $newname, $continue=true, $feedback=true) {
-    global $DB;
-    debugging('Deprecated ddllib function used!');
-    return $DB->get_manager()->rename_table($table, $newname, $continue, $feedback);
-}
-
-function add_field($table, $field, $continue=true, $feedback=true) {
-    global $DB;
-    debugging('Deprecated ddllib function used!');
-    return $DB->get_manager()->add_field($table, $field, $continue, $feedback);
-}
-
-function drop_field($table, $field, $continue=true, $feedback=true) {
-    global $DB;
-    debugging('Deprecated ddllib function used!');
-    return $DB->get_manager()->drop_field($table, $field, $continue, $feedback);
-}
-
-function change_field_type($table, $field, $continue=true, $feedback=true) {
-    global $DB;
-    debugging('Deprecated ddllib function used!');
-    return $DB->get_manager()->change_field_type($table, $field, $continue, $feedback);
-}
-
-function change_field_precision($table, $field, $continue=true, $feedback=true) {
-    global $DB;
-    debugging('Deprecated ddllib function used!');
-    return $DB->get_manager()->change_field_precision($table, $field, $continue, $feedback);
-}
-
-function change_field_unsigned($table, $field, $continue=true, $feedback=true) {
-    global $DB;
-    debugging('Deprecated ddllib function used!');
-    return $DB->get_manager()->change_field_unsigned($table, $field, $continue, $feedback);
-}
-
-function change_field_notnull($table, $field, $continue=true, $feedback=true) {
-    global $DB;
-    debugging('Deprecated ddllib function used!');
-    return $DB->get_manager()->change_field_notnull($table, $field, $continue, $feedback);
-}
-
-function change_field_enum($table, $field, $continue=true, $feedback=true) {
-    global $DB;
-    debugging('Deprecated ddllib function used!');
-    return $DB->get_manager()->change_field_enum($table, $field, $continue, $feedback);
-}
-
-function change_field_default($table, $field, $continue=true, $feedback=true) {
-    global $DB;
-    debugging('Deprecated ddllib function used!');
-    return $DB->get_manager()->change_field_default($table, $field, $continue, $feedback);
-}
-
-function rename_field($table, $field, $newname, $continue=true, $feedback=true) {
-    global $DB;
-    debugging('Deprecated ddllib function used!');
-    return $DB->get_manager()->rename_field($table, $field, $continue, $feedback);
-}
-
-function add_key($table, $key, $continue=true, $feedback=true) {
-    global $DB;
-    debugging('Deprecated ddllib function used!');
-    return $DB->get_manager()->add_key($table, $key, $continue, $feedback);
-}
-
-function drop_key($table, $key, $continue=true, $feedback=true) {
-    global $DB;
-    debugging('Deprecated ddllib function used!');
-    return $DB->get_manager()->drop_key($table, $key, $continue, $feedback);
-}
-
-function rename_key($table, $key, $newname, $continue=true, $feedback=true) {
-    global $DB;
-    debugging('Deprecated ddllib function used!');
-    return $DB->get_manager()->rename_key($table, $key, $newname, $continue, $feedback);
-}
-
-function add_index($table, $index, $continue=true, $feedback=true) {
-    global $DB;
-    debugging('Deprecated ddllib function used!');
-    return $DB->get_manager()->add_index($table, $index, $continue, $feedback);
-}
-
-function drop_index($table, $index, $continue=true, $feedback=true) {
-    global $DB;
-    debugging('Deprecated ddllib function used!');
-    return $DB->get_manager()->drop_index($table, $index, $continue, $feedback);
-}
-
-function rename_index($table, $index, $newname, $continue=true, $feedback=true) {
-    global $DB;
-    debugging('Deprecated ddllib function used!');
-    return $DB->get_manager()->rename_index($table, $index, $newname, $continue, $feedback);
-}
-
 
 ?>
diff --git a/lib/deprecatedlib.php b/lib/deprecatedlib.php
index eeb36b281d..787944384a 100644
--- a/lib/deprecatedlib.php
+++ b/lib/deprecatedlib.php
@@ -446,6 +446,176 @@ function error($message, $link='') {
 }
 
 
+/// Deprecated DDL functions, to be removed soon ///
+
+function table_exists($table) {
+    global $DB;
+    debugging('Deprecated ddllib function used!');
+    return $DB->get_manager()->table_exists($table);
+}
+
+function field_exists($table, $field) {
+    global $DB;
+    debugging('Deprecated ddllib function used!');
+    return $DB->get_manager()->field_exists($table, $field);
+}
+
+function find_index_name($table, $index) {
+    global $DB;
+    debugging('Deprecated ddllib function used!');
+    return $DB->get_manager()->find_index_name($table, $index);
+}
+
+function index_exists($table, $index) {
+    global $DB;
+    debugging('Deprecated ddllib function used!');
+    return $DB->get_manager()->index_exists($table, $index);
+}
+
+function find_check_constraint_name($table, $field) {
+    global $DB;
+    debugging('Deprecated ddllib function used!');
+    return $DB->get_manager()->find_check_constraint_name($table, $field);
+}
+
+function check_constraint_exists($table, $field) {
+    global $DB;
+    debugging('Deprecated ddllib function used!');
+    return $DB->get_manager()->check_constraint_exists($table, $field);
+}
+
+function find_key_name($table, $xmldb_key) {
+    global $DB;
+    debugging('Deprecated ddllib function used!');
+    return $DB->get_manager()->find_key_name($table, $xmldb_key);
+}
+
+function find_sequence_name($table) {
+    global $DB;
+    debugging('Deprecated ddllib function used!');
+    return $DB->get_manager()->find_sequence_name($table);
+}
+
+function drop_table($table, $continue=true, $feedback=true) {
+    global $DB;
+    debugging('Deprecated ddllib function used!');
+    return $DB->get_manager()->drop_table($table, $continue, $feedback);
+}
+
+function install_from_xmldb_file($file) {
+    global $DB;
+    debugging('Deprecated ddllib function used!');
+    return $DB->get_manager()->install_from_xmldb_file($file);
+}
+
+function create_table($table, $continue=true, $feedback=true) {
+    global $DB;
+    debugging('Deprecated ddllib function used!');
+    return $DB->get_manager()->create_table($table, $continue, $feedback);
+}
+
+function create_temp_table($table, $continue=true, $feedback=true) {
+    global $DB;
+    debugging('Deprecated ddllib function used!');
+    return $DB->get_manager()->create_temp_table($table, $continue, $feedback);
+}
+
+function rename_table($table, $newname, $continue=true, $feedback=true) {
+    global $DB;
+    debugging('Deprecated ddllib function used!');
+    return $DB->get_manager()->rename_table($table, $newname, $continue, $feedback);
+}
+
+function add_field($table, $field, $continue=true, $feedback=true) {
+    global $DB;
+    debugging('Deprecated ddllib function used!');
+    return $DB->get_manager()->add_field($table, $field, $continue, $feedback);
+}
+
+function drop_field($table, $field, $continue=true, $feedback=true) {
+    global $DB;
+    debugging('Deprecated ddllib function used!');
+    return $DB->get_manager()->drop_field($table, $field, $continue, $feedback);
+}
+
+function change_field_type($table, $field, $continue=true, $feedback=true) {
+    global $DB;
+    debugging('Deprecated ddllib function used!');
+    return $DB->get_manager()->change_field_type($table, $field, $continue, $feedback);
+}
+
+function change_field_precision($table, $field, $continue=true, $feedback=true) {
+    global $DB;
+    debugging('Deprecated ddllib function used!');
+    return $DB->get_manager()->change_field_precision($table, $field, $continue, $feedback);
+}
+
+function change_field_unsigned($table, $field, $continue=true, $feedback=true) {
+    global $DB;
+    debugging('Deprecated ddllib function used!');
+    return $DB->get_manager()->change_field_unsigned($table, $field, $continue, $feedback);
+}
+
+function change_field_notnull($table, $field, $continue=true, $feedback=true) {
+    global $DB;
+    debugging('Deprecated ddllib function used!');
+    return $DB->get_manager()->change_field_notnull($table, $field, $continue, $feedback);
+}
+
+function change_field_enum($table, $field, $continue=true, $feedback=true) {
+    global $DB;
+    debugging('Deprecated ddllib function used!');
+    return $DB->get_manager()->change_field_enum($table, $field, $continue, $feedback);
+}
+
+function change_field_default($table, $field, $continue=true, $feedback=true) {
+    global $DB;
+    debugging('Deprecated ddllib function used!');
+    return $DB->get_manager()->change_field_default($table, $field, $continue, $feedback);
+}
+
+function rename_field($table, $field, $newname, $continue=true, $feedback=true) {
+    global $DB;
+    debugging('Deprecated ddllib function used!');
+    return $DB->get_manager()->rename_field($table, $field, $continue, $feedback);
+}
+
+function add_key($table, $key, $continue=true, $feedback=true) {
+    global $DB;
+    debugging('Deprecated ddllib function used!');
+    return $DB->get_manager()->add_key($table, $key, $continue, $feedback);
+}
+
+function drop_key($table, $key, $continue=true, $feedback=true) {
+    global $DB;
+    debugging('Deprecated ddllib function used!');
+    return $DB->get_manager()->drop_key($table, $key, $continue, $feedback);
+}
+
+function rename_key($table, $key, $newname, $continue=true, $feedback=true) {
+    global $DB;
+    debugging('Deprecated ddllib function used!');
+    return $DB->get_manager()->rename_key($table, $key, $newname, $continue, $feedback);
+}
+
+function add_index($table, $index, $continue=true, $feedback=true) {
+    global $DB;
+    debugging('Deprecated ddllib function used!');
+    return $DB->get_manager()->add_index($table, $index, $continue, $feedback);
+}
+
+function drop_index($table, $index, $continue=true, $feedback=true) {
+    global $DB;
+    debugging('Deprecated ddllib function used!');
+    return $DB->get_manager()->drop_index($table, $index, $continue, $feedback);
+}
+
+function rename_index($table, $index, $newname, $continue=true, $feedback=true) {
+    global $DB;
+    debugging('Deprecated ddllib function used!');
+    return $DB->get_manager()->rename_index($table, $index, $newname, $continue, $feedback);
+}
+
 
 //////////////////////////
 /// removed functions ////
@@ -724,4 +894,4 @@ function table_column($table, $oldfield, $field, $type='integer', $size='10',
                       $signed='unsigned', $default='0', $null='not null', $after='') {
     error('table_column() was removed, please use new ddl functions');
 }
-    
\ No newline at end of file
+
diff --git a/lib/dml/moodle_database.php b/lib/dml/moodle_database.php
index 27cbe34c36..ea277f5834 100644
--- a/lib/dml/moodle_database.php
+++ b/lib/dml/moodle_database.php
@@ -381,7 +381,7 @@ abstract class moodle_database {
         global $CFG;
 
         if (!$this->database_manager) {
-            require_once($CFG->libdir.'/ddllib.php');
+            require_once($CFG->libdir.'/adminlib.php');
             require_once($CFG->libdir.'/ddl/database_manager.php');
 
             $classname = $this->get_dbfamily().'_sql_generator';
diff --git a/lib/simpletest/fixtures/gradetest.php b/lib/simpletest/fixtures/gradetest.php
index 160190e3ef..b0645054b3 100644
--- a/lib/simpletest/fixtures/gradetest.php
+++ b/lib/simpletest/fixtures/gradetest.php
@@ -34,9 +34,8 @@ if (!defined('MOODLE_INTERNAL')) {
  * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
  * @package moodlecore
  */
+require_once($CFG->libdir . '/adminlib.php');
 require_once($CFG->libdir . '/gradelib.php');
-require_once($CFG->libdir . '/dmllib.php');
-require_once($CFG->libdir . '/ddllib.php');
 
 Mock::generate('grade_item', 'mock_grade_item');
 Mock::generate('grade_scale', 'mock_grade_scale');
diff --git a/mod/assignment/db/upgrade.php b/mod/assignment/db/upgrade.php
index 3af92c8b77..ef11ef6e72 100644
--- a/mod/assignment/db/upgrade.php
+++ b/mod/assignment/db/upgrade.php
@@ -15,7 +15,7 @@
 // will tell you what you need to do.
 //
 // The commands in here will all be database-neutral,
-// using the functions defined in lib/ddllib.php
+// using the methods of database_manager class
 
 function xmldb_assignment_upgrade($oldversion=0) {
 
diff --git a/mod/chat/db/upgrade.php b/mod/chat/db/upgrade.php
index cb7cebaff9..f2876317fd 100644
--- a/mod/chat/db/upgrade.php
+++ b/mod/chat/db/upgrade.php
@@ -15,7 +15,7 @@
 // will tell you what you need to do.
 //
 // The commands in here will all be database-neutral,
-// using the functions defined in lib/ddllib.php
+// using the methods of database_manager class
 
 function xmldb_chat_upgrade($oldversion=0) {
 
diff --git a/mod/choice/db/upgrade.php b/mod/choice/db/upgrade.php
index 5545b6d6de..b8e04bc27b 100644
--- a/mod/choice/db/upgrade.php
+++ b/mod/choice/db/upgrade.php
@@ -15,7 +15,7 @@
 // will tell you what you need to do.
 //
 // The commands in here will all be database-neutral,
-// using the functions defined in lib/ddllib.php
+// using the methods of database_manager class
 
 function xmldb_choice_upgrade($oldversion=0) {
 
@@ -29,7 +29,7 @@ function xmldb_choice_upgrade($oldversion=0) {
 /// upgrade code.
 
 /// if ($result && $oldversion < YYYYMMDD00) { //New version in version.php
-///     $result = result of "/lib/ddllib.php" function calls
+///     $result = result of database_manager methods
 /// }
 
 //===== 1.9.0 upgrade line ======//
diff --git a/mod/data/db/upgrade.php b/mod/data/db/upgrade.php
index f82f8d7971..94627617a1 100644
--- a/mod/data/db/upgrade.php
+++ b/mod/data/db/upgrade.php
@@ -15,7 +15,7 @@
 // will tell you what you need to do.
 //
 // The commands in here will all be database-neutral,
-// using the functions defined in lib/ddllib.php
+// using the methods of database_manager class
 
 function xmldb_data_upgrade($oldversion=0) {
 
diff --git a/mod/feedback/db/upgrade.php b/mod/feedback/db/upgrade.php
index 5dffd1047a..6b9c847b0f 100644
--- a/mod/feedback/db/upgrade.php
+++ b/mod/feedback/db/upgrade.php
@@ -15,7 +15,7 @@
 // will tell you what you need to do.
 //
 // The commands in here will all be database-neutral,
-// using the functions defined in lib/ddllib.php
+// using the methods of database_manager class
 
 function xmldb_feedback_upgrade($oldversion=0) {
 
diff --git a/mod/forum/db/upgrade.php b/mod/forum/db/upgrade.php
index ae0c755135..acd4cea382 100644
--- a/mod/forum/db/upgrade.php
+++ b/mod/forum/db/upgrade.php
@@ -15,7 +15,7 @@
 // will tell you what you need to do.
 //
 // The commands in here will all be database-neutral,
-// using the functions defined in lib/ddllib.php
+// using the methods of database_manager class
 
 function xmldb_forum_upgrade($oldversion=0) {
 
@@ -29,7 +29,7 @@ function xmldb_forum_upgrade($oldversion=0) {
 /// upgrade code.
 
 /// if ($result && $oldversion < YYYYMMDD00) { //New version in version.php
-///     $result = result of "/lib/ddllib.php" function calls
+///     $result = result of database_manager methods
 /// }
 
 //===== 1.9.0 upgrade line ======//
diff --git a/mod/glossary/db/upgrade.php b/mod/glossary/db/upgrade.php
index 4645ef457d..ca1a399123 100644
--- a/mod/glossary/db/upgrade.php
+++ b/mod/glossary/db/upgrade.php
@@ -15,7 +15,7 @@
 // will tell you what you need to do.
 //
 // The commands in here will all be database-neutral,
-// using the functions defined in lib/ddllib.php
+// using the methods of database_manager class
 
 function xmldb_glossary_upgrade($oldversion=0) {
 
@@ -29,7 +29,7 @@ function xmldb_glossary_upgrade($oldversion=0) {
 /// upgrade code.
 
 /// if ($result && $oldversion < YYYYMMDD00) { //New version in version.php
-///     $result = result of "/lib/ddllib.php" function calls
+///     $result = result of database_manager methods
 /// }
     
 //===== 1.9.0 upgrade line ======//
diff --git a/mod/label/db/upgrade.php b/mod/label/db/upgrade.php
index 4b7e6b5300..bfa9812af9 100644
--- a/mod/label/db/upgrade.php
+++ b/mod/label/db/upgrade.php
@@ -15,7 +15,7 @@
 // will tell you what you need to do.
 //
 // The commands in here will all be database-neutral,
-// using the functions defined in lib/ddllib.php
+// using the methods of database_manager class
 
 function xmldb_label_upgrade($oldversion=0) {
     global $CFG, $THEME, $DB;
@@ -28,7 +28,7 @@ function xmldb_label_upgrade($oldversion=0) {
 /// upgrade code.
 
 /// if ($result && $oldversion < YYYYMMDD00) { //New version in version.php
-///     $result = result of "/lib/ddllib.php" function calls
+///     $result = result of database_manager methods
 /// }
 
 //===== 1.9.0 upgrade line ======//
diff --git a/mod/lesson/db/upgrade.php b/mod/lesson/db/upgrade.php
index 92cbc0a2c9..365e225093 100644
--- a/mod/lesson/db/upgrade.php
+++ b/mod/lesson/db/upgrade.php
@@ -15,7 +15,7 @@
 // will tell you what you need to do.
 //
 // The commands in here will all be database-neutral,
-// using the functions defined in lib/ddllib.php
+// using the methods of database_manager class
 
 function xmldb_lesson_upgrade($oldversion=0) {
 
diff --git a/mod/quiz/db/upgrade.php b/mod/quiz/db/upgrade.php
index 795e99fdbe..0c060d9ff0 100644
--- a/mod/quiz/db/upgrade.php
+++ b/mod/quiz/db/upgrade.php
@@ -15,7 +15,7 @@
 // will tell you what you need to do.
 //
 // The commands in here will all be database-neutral,
-// using the functions defined in lib/ddllib.php
+// using the methods of database_manager class
 
 function xmldb_quiz_upgrade($oldversion=0) {
 
diff --git a/mod/resource/db/upgrade.php b/mod/resource/db/upgrade.php
index 93c376fecd..893d862efc 100644
--- a/mod/resource/db/upgrade.php
+++ b/mod/resource/db/upgrade.php
@@ -15,7 +15,7 @@
 // will tell you what you need to do.
 //
 // The commands in here will all be database-neutral,
-// using the functions defined in lib/ddllib.php
+// using the methods of database_manager class
 
 function xmldb_resource_upgrade($oldversion=0) {
     global $CFG, $THEME, $DB;
@@ -28,7 +28,7 @@ function xmldb_resource_upgrade($oldversion=0) {
 /// upgrade code.
 
 /// if ($result && $oldversion < YYYYMMDD00) { //New version in version.php
-///     $result = result of "/lib/ddllib.php" function calls
+///     $result = result of database_manager methods
 /// }
 
 //===== 1.9.0 upgrade line ======//
diff --git a/mod/scorm/db/upgrade.php b/mod/scorm/db/upgrade.php
index 512cfcf45a..e27bd650fd 100644
--- a/mod/scorm/db/upgrade.php
+++ b/mod/scorm/db/upgrade.php
@@ -15,7 +15,7 @@
 // will tell you what you need to do.
 //
 // The commands in here will all be database-neutral,
-// using the functions defined in lib/ddllib.php
+// using the methods of database_manager class
 
 function xmldb_scorm_upgrade($oldversion=0) {
 
diff --git a/mod/survey/db/upgrade.php b/mod/survey/db/upgrade.php
index 9c652cf734..cb26e84318 100644
--- a/mod/survey/db/upgrade.php
+++ b/mod/survey/db/upgrade.php
@@ -15,7 +15,7 @@
 // will tell you what you need to do.
 //
 // The commands in here will all be database-neutral,
-// using the functions defined in lib/ddllib.php
+// using the methods of database_manager class
 
 function xmldb_survey_upgrade($oldversion=0) {
 
@@ -29,7 +29,7 @@ function xmldb_survey_upgrade($oldversion=0) {
 /// upgrade code.
 
 /// if ($result && $oldversion < YYYYMMDD00) { //New version in version.php
-///     $result = result of "/lib/ddllib.php" function calls
+///     $result = result of database_manager methods
 /// }
 
 //===== 1.9.0 upgrade line ======//
diff --git a/mod/wiki/db/upgrade.php b/mod/wiki/db/upgrade.php
index b2686a947e..24806e82c9 100644
--- a/mod/wiki/db/upgrade.php
+++ b/mod/wiki/db/upgrade.php
@@ -15,7 +15,7 @@
 // will tell you what you need to do.
 //
 // The commands in here will all be database-neutral,
-// using the functions defined in lib/ddllib.php
+// using the methods of database_manager class
 
 function xmldb_wiki_upgrade($oldversion=0) {
 
@@ -29,7 +29,7 @@ function xmldb_wiki_upgrade($oldversion=0) {
 /// upgrade code.
 
 /// if ($result && $oldversion < YYYYMMDD00) { //New version in version.php
-///     $result = result of "/lib/ddllib.php" function calls
+///     $result = result of database_manager methods
 /// }
 
 //===== 1.9.0 upgrade line ======//
diff --git a/question/type/calculated/db/upgrade.php b/question/type/calculated/db/upgrade.php
index 8cb1fe8611..05c6c2823d 100644
--- a/question/type/calculated/db/upgrade.php
+++ b/question/type/calculated/db/upgrade.php
@@ -15,7 +15,7 @@
 // will tell you what you need to do.
 //
 // The commands in here will all be database-neutral,
-// using the functions defined in lib/ddllib.php
+// using the methods of database_manager class
 
 function xmldb_qtype_calculated_upgrade($oldversion=0) {
 
@@ -29,7 +29,7 @@ function xmldb_qtype_calculated_upgrade($oldversion=0) {
 /// upgrade code.
 
 /// if ($result && $oldversion < YYYYMMDD00) { //New version in version.php
-///     $result = result of "/lib/ddllib.php" function calls
+///     $result = result of database_manager methods
 /// }
 
     return $result;
diff --git a/question/type/essay/db/upgrade.php b/question/type/essay/db/upgrade.php
index bdf553fb5e..b98d8fb5a5 100644
--- a/question/type/essay/db/upgrade.php
+++ b/question/type/essay/db/upgrade.php
@@ -15,7 +15,7 @@
 // will tell you what you need to do.
 //
 // The commands in here will all be database-neutral,
-// using the functions defined in lib/ddllib.php
+// using the methods of database_manager class
 
 function xmldb_qtype_essay_upgrade($oldversion=0) {
 
@@ -29,7 +29,7 @@ function xmldb_qtype_essay_upgrade($oldversion=0) {
 /// upgrade code.
 
 /// if ($result && $oldversion < YYYYMMDD00) { //New version in version.php
-///     $result = result of "/lib/ddllib.php" function calls
+///     $result = result of database_manager methods
 /// }
 
     return $result;
diff --git a/question/type/match/db/upgrade.php b/question/type/match/db/upgrade.php
index d18ea5fcfe..16d352b3a4 100644
--- a/question/type/match/db/upgrade.php
+++ b/question/type/match/db/upgrade.php
@@ -15,7 +15,7 @@
 // will tell you what you need to do.
 //
 // The commands in here will all be database-neutral,
-// using the functions defined in lib/ddllib.php
+// using the methods of database_manager class
 
 function xmldb_qtype_match_upgrade($oldversion=0) {
 
@@ -29,7 +29,7 @@ function xmldb_qtype_match_upgrade($oldversion=0) {
 /// upgrade code.
 
 /// if ($result && $oldversion < YYYYMMDD00) { //New version in version.php
-///     $result = result of "/lib/ddllib.php" function calls
+///     $result = result of database_manager methods
 /// }
 
     return $result;
diff --git a/question/type/multianswer/db/upgrade.php b/question/type/multianswer/db/upgrade.php
index d3b38892d2..e1d5b2ffaa 100644
--- a/question/type/multianswer/db/upgrade.php
+++ b/question/type/multianswer/db/upgrade.php
@@ -15,7 +15,7 @@
 // will tell you what you need to do.
 //
 // The commands in here will all be database-neutral,
-// using the functions defined in lib/ddllib.php
+// using the methods of database_manager class
 
 function xmldb_qtype_multianswer_upgrade($oldversion=0) {
 
@@ -29,7 +29,7 @@ function xmldb_qtype_multianswer_upgrade($oldversion=0) {
 /// upgrade code.
 
 /// if ($result && $oldversion < YYYYMMDD00) { //New version in version.php
-///     $result = result of "/lib/ddllib.php" function calls
+///     $result = result of database_manager methods
 /// }
 
     if ($result && $oldversion < 2008050800) {
diff --git a/question/type/multichoice/db/upgrade.php b/question/type/multichoice/db/upgrade.php
index ec2c5ed8fc..c2ec89247d 100644
--- a/question/type/multichoice/db/upgrade.php
+++ b/question/type/multichoice/db/upgrade.php
@@ -15,7 +15,7 @@
 // will tell you what you need to do.
 //
 // The commands in here will all be database-neutral,
-// using the functions defined in lib/ddllib.php
+// using the methods of database_manager class
 
 function xmldb_qtype_multichoice_upgrade($oldversion=0) {
 
diff --git a/question/type/numerical/db/upgrade.php b/question/type/numerical/db/upgrade.php
index eea9e9a954..75d80651ad 100644
--- a/question/type/numerical/db/upgrade.php
+++ b/question/type/numerical/db/upgrade.php
@@ -15,7 +15,7 @@
 // will tell you what you need to do.
 //
 // The commands in here will all be database-neutral,
-// using the functions defined in lib/ddllib.php
+// using the methods of database_manager class
 
 function xmldb_qtype_numerical_upgrade($oldversion=0) {
 
diff --git a/question/type/randomsamatch/db/upgrade.php b/question/type/randomsamatch/db/upgrade.php
index 8c315e1475..02ac7a2b36 100644
--- a/question/type/randomsamatch/db/upgrade.php
+++ b/question/type/randomsamatch/db/upgrade.php
@@ -15,7 +15,7 @@
 // will tell you what you need to do.
 //
 // The commands in here will all be database-neutral,
-// using the functions defined in lib/ddllib.php
+// using the methods of database_manager class
 
 function xmldb_qtype_randomsamatch_upgrade($oldversion=0) {
 
@@ -29,7 +29,7 @@ function xmldb_qtype_randomsamatch_upgrade($oldversion=0) {
 /// upgrade code.
 
 /// if ($result && $oldversion < YYYYMMDD00) { //New version in version.php
-///     $result = result of "/lib/ddllib.php" function calls
+///     $result = result of database_manager methods
 /// }
 
     return $result;
diff --git a/question/type/shortanswer/db/upgrade.php b/question/type/shortanswer/db/upgrade.php
index f245002805..d38673d9ce 100644
--- a/question/type/shortanswer/db/upgrade.php
+++ b/question/type/shortanswer/db/upgrade.php
@@ -15,7 +15,7 @@
 // will tell you what you need to do.
 //
 // The commands in here will all be database-neutral,
-// using the functions defined in lib/ddllib.php
+// using the methods of database_manager class
 
 function xmldb_qtype_shortanswer_upgrade($oldversion=0) {
 
@@ -29,7 +29,7 @@ function xmldb_qtype_shortanswer_upgrade($oldversion=0) {
 /// upgrade code.
 
 /// if ($result && $oldversion < YYYYMMDD00) { //New version in version.php
-///     $result = result of "/lib/ddllib.php" function calls
+///     $result = result of database_manager methods
 /// }
 
     return $result;
diff --git a/question/type/truefalse/db/upgrade.php b/question/type/truefalse/db/upgrade.php
index 2c71db48b7..c10321a9bb 100644
--- a/question/type/truefalse/db/upgrade.php
+++ b/question/type/truefalse/db/upgrade.php
@@ -15,7 +15,7 @@
 // will tell you what you need to do.
 //
 // The commands in here will all be database-neutral,
-// using the functions defined in lib/ddllib.php
+// using the methods of database_manager class
 
 function xmldb_qtype_truefalse_upgrade($oldversion=0) {
 
@@ -29,7 +29,7 @@ function xmldb_qtype_truefalse_upgrade($oldversion=0) {
 /// upgrade code.
 
 /// if ($result && $oldversion < YYYYMMDD00) { //New version in version.php
-///     $result = result of "/lib/ddllib.php" function calls
+///     $result = result of database_manager methods
 /// }
 
     return $result;