From 7f87b19eeb3af1d6afb4dbca771e261cc49b8caa Mon Sep 17 00:00:00 2001
From: Dries Buytaert <dries@buytaert.net>
Date: Sun, 8 Jan 2006 12:49:51 +0000
Subject: [PATCH] - Patch #42886 by chx: critical feature: allow modules to be
 reordered.

---
 database/database.mysql  |  4 +++-
 database/database.pgsql  |  2 ++
 database/updates.inc     | 16 ++++++++++++++++
 includes/module.inc      |  5 ++---
 modules/node.module      |  2 +-
 modules/node/node.module |  2 +-
 6 files changed, 25 insertions(+), 6 deletions(-)

diff --git a/database/database.mysql b/database/database.mysql
index e84d4518bc98..68671f14bd6c 100644
--- a/database/database.mysql
+++ b/database/database.mysql
@@ -685,7 +685,9 @@ CREATE TABLE system (
   throttle tinyint(1) DEFAULT '0' NOT NULL,
   bootstrap int(2) NOT NULL default '0',
   schema_version smallint(2) unsigned NOT NULL default 0,
-  PRIMARY KEY (filename)
+  weight int(2) NOT NULL default '0',
+  PRIMARY KEY (filename),
+  KEY (weight)
 ) TYPE=MyISAM;
 
 --
diff --git a/database/database.pgsql b/database/database.pgsql
index 8b7659f5a679..1529901dd70a 100644
--- a/database/database.pgsql
+++ b/database/database.pgsql
@@ -680,8 +680,10 @@ CREATE TABLE system (
   throttle smallint NOT NULL default '0',
   bootstrap integer NOT NULL default '0',
   schema_version smallint NOT NULL default 0,
+  weight smallint NOT NULL default 0,
   PRIMARY KEY (filename)
 );
+CREATE INDEX system_weight_idx ON system(weight);
 
 --
 -- Table structure for term_data
diff --git a/database/updates.inc b/database/updates.inc
index d4b440f3d631..10a73f3d7557 100644
--- a/database/updates.inc
+++ b/database/updates.inc
@@ -1397,3 +1397,19 @@ function system_update_166() {
 
   return $ret;
 }
+
+function system_update_167() {
+  $ret = array();
+  switch ($GLOBALS['db_type']) {
+    case 'pgsql':
+      db_add_column($ret, 'system', 'weight', 'smallint', array('not null' => TRUE, 'default' => 0));
+      $ret[] = update_sql('CREATE INDEX {system}_weight_idx ON {system} (weight)');
+      break;
+    case 'mysql':
+    case 'mysqli':
+      $ret[] = update_sql("ALTER TABLE {system} ADD weight tinyint(3) unsigned default '0' NOT NULL, ADD KEY (weight)");
+      break;
+  }
+
+  return $ret;
+}
diff --git a/includes/module.inc b/includes/module.inc
index f4f5907725d7..e4956999f0b2 100644
--- a/includes/module.inc
+++ b/includes/module.inc
@@ -50,10 +50,10 @@ function module_list($refresh = FALSE, $bootstrap = TRUE) {
   if (!$list) {
     $list = array('filter' => 'filter', 'system' => 'system', 'user' => 'user', 'watchdog' => 'watchdog');
     if ($bootstrap) {
-      $result = db_query("SELECT name, filename, throttle, bootstrap FROM {system} WHERE type = 'module' AND status = 1 AND bootstrap = 1");
+      $result = db_query("SELECT name, filename, throttle, bootstrap FROM {system} WHERE type = 'module' AND status = 1 AND bootstrap = 1 ORDER BY weight ASC, filename ASC");
     }
     else {
-      $result = db_query("SELECT name, filename, throttle, bootstrap FROM {system} WHERE type = 'module' AND status = 1");
+      $result = db_query("SELECT name, filename, throttle, bootstrap FROM {system} WHERE type = 'module' AND status = 1 ORDER BY weight ASC, filename ASC");
     }
     while ($module = db_fetch_object($result)) {
       if (file_exists($module->filename)) {
@@ -67,7 +67,6 @@ function module_list($refresh = FALSE, $bootstrap = TRUE) {
         }
       }
     }
-    asort($list);
   }
   return $list;
 }
diff --git a/modules/node.module b/modules/node.module
index 3c7283b0e3f1..f7e998d131b1 100644
--- a/modules/node.module
+++ b/modules/node.module
@@ -2169,7 +2169,7 @@ function node_form_alter($form_id, &$form) {
  *   - "delete"
  *   - "create"
  * @param $node
- *   The node object (or node array) on which the operation is to be performed, 
+ *   The node object (or node array) on which the operation is to be performed,
  *   or node type (e.g. 'forum') for "create" operation.
  * @param $uid
  *   The user ID on which the operation is to be performed.
diff --git a/modules/node/node.module b/modules/node/node.module
index 3c7283b0e3f1..f7e998d131b1 100644
--- a/modules/node/node.module
+++ b/modules/node/node.module
@@ -2169,7 +2169,7 @@ function node_form_alter($form_id, &$form) {
  *   - "delete"
  *   - "create"
  * @param $node
- *   The node object (or node array) on which the operation is to be performed, 
+ *   The node object (or node array) on which the operation is to be performed,
  *   or node type (e.g. 'forum') for "create" operation.
  * @param $uid
  *   The user ID on which the operation is to be performed.
-- 
GitLab