Cleanup forum_ tables:
authorFlorent Bruneau <florent.bruneau@polytechnique.org>
Sat, 27 Dec 2008 21:20:44 +0000 (22:20 +0100)
committerFlorent Bruneau <florent.bruneau@polytechnique.org>
Sat, 27 Dec 2008 21:20:44 +0000 (22:20 +0100)
* move tables to x4dat.forum_ namespace (database forums is dead).
* remove references to auth_user_quick banana_last.

Signed-off-by: Florent Bruneau <florent.bruneau@polytechnique.org>
12 files changed:
classes/xorgsession.php
configs/platal.ini
include/banana/forum.inc.php
include/banana/hooks.inc.php
include/banana/ml.inc.php
include/user.func.inc.php
modules/admin.php
modules/forums.php
modules/register.php
upgrade/account/02_forums.sql [new file with mode: 0644]
upgrade/account/99_insertion.sql [moved from upgrade/account/02_insertion.sql with 76% similarity]
upgrade/account/README [new file with mode: 0644]

index 73739c9..76bc5b0 100644 (file)
@@ -188,13 +188,14 @@ class XorgSession extends PlSession
         $res  = XDB::query("SELECT  a.uid, a.hruid, a.display_name, a.full_name, a.password,
                                     a.sex = 'female' AS femme, a.email_format,
                                     a.token, FIND_IN_SET('watch', a.flags) AS watch_account,
-                                    UNIX_TIMESTAMP(q.banana_last) AS banana_last, q.watch_last,
+                                    UNIX_TIMESTAMP(fp.last_seen) AS banana_last, q.watch_last,
                                     q.last_version, g.g_account_name IS NOT NULL AS googleapps,
                                     UNIX_TIMESTAMP(s.start) AS lastlogin, s.host,
                                     a.is_admin, at.perms
                               FROM  accounts        AS a
                         INNER JOIN  account_types   AS at ON(a.type = at.type)
                         INNER JOIN  auth_user_quick AS q  ON(a.uid = q.user_id)
+                         LEFT JOIN  forum_profiles  AS fp ON(fp.uid = a.uid)
                          LEFT JOIN  gapps_accounts  AS g  ON(a.uid = g.l_userid AND g.g_status = 'active')
                          LEFT JOIN  logger.last_sessions AS ls ON (ls.uid = a.uid)
                          LEFT JOIN  logger.sessions AS s  ON(s.id = ls.id)
index c6980ec..4cc0ca5 100644 (file)
@@ -17,7 +17,6 @@ password = "***"
 web_user = "***"
 web_pass = "***"
 
-table_prefix = "banana_"
 spool_root   = "/var/spool/banana"
 mbox_helper  = "/usr/bin/banana-mbox-helper"
 
index 4d5704b..aa6b428 100644 (file)
@@ -62,16 +62,16 @@ class ForumsBanana extends Banana
         $time = null;
         if (!is_null($this->params) && isset($this->params['updateall'])) {
             $time = intval($this->params['updateall']);
-            $_SESSION['banana_last']     = $time;
+            S::set('banana_last', $time);
         }
 
         // Get user profile from SQL
-        $req = XDB::query("SELECT  nom, mail, sig,
+        $req = XDB::query("SELECT  name, mail, sig,
                                    FIND_IN_SET('threads',flags), FIND_IN_SET('automaj',flags),
                                    tree_unread, tree_read
-                             FROM  {$globals->banana->table_prefix}profils
-                            WHERE  uid={?}", S::i('uid'));
-        if (!(list($nom, $mail, $sig, $disp, $maj, $unread, $read) = $req->fetchOneRow())) {
+                             FROM  forum_profiles
+                            WHERE  uid = {?}", $this->user->id());
+        if (!(list($nom, $mail, $sig, $disp, $maj, $unread, $read) = $req->fetchOneRow()) || !$nom) {
             $nom  = S::v('prenom')." ".S::v('nom');
             $mail = $this->user->forlifeEmail();
             $sig  = $nom." (".S::v('promo').")";
@@ -85,11 +85,10 @@ class ForumsBanana extends Banana
         }
 
         // Build user profile
-        $req = XDB::query("
-                 SELECT  nom
-                   FROM  {$globals->banana->table_prefix}abos
-              LEFT JOIN  {$globals->banana->table_prefix}list ON list.fid=abos.fid
-                  WHERE  uid={?}", S::i('uid'));
+        $req = XDB::query("SELECT  name
+                             FROM  forum_subs AS fs
+                        LEFT JOIN  forums AS f ON (f.fid = fs.fid)
+                            WHERE  uid={?}", $this->user->id());
         Banana::$profile['headers']['From']         = "$nom <$mail>";
         Banana::$profile['headers']['Organization'] = make_Organization();
         Banana::$profile['signature']               = $sig;
@@ -102,10 +101,15 @@ class ForumsBanana extends Banana
 
         // Update the "unread limit"
         if (!is_null($time)) {
-            XDB::execute("UPDATE  auth_user_quick
-                             SET  banana_last = FROM_UNIXTIME({?})
-                           WHERE  user_id={?}",
-                         $time, S::i('uid'));
+            XDB::execute('UPDATE  forum_profiles
+                             SET  last_seen = FROM_UNIXTIME({?})
+                           WHERE  uid = {?}',
+                         $time, $this->user->id());
+            if (XDB::affectedRows() == 0) {
+                XDB::execute('INSERT INTO  forum_profiles (uid, last_seen)
+                                   VALUES  ({?}, FROM_UNIXTIME({?}))',
+                             $this->user->id(), $time);
+            }
         }
 
         if (!empty($GLOBALS['IS_XNET_SITE'])) {
@@ -135,11 +139,7 @@ class ForumsBanana extends Banana
     public function post($dest, $reply, $subject, $body)
     {
         global $globals;
-        $res = XDB::query('SELECT  nom, prenom, promo
-                             FROM  auth_user_md5 AS u
-                            WHERE  u.user_id = {?}', $this->user->id());
-        list($nom, $prenom, $promo) = $res->fetchOneRow();
-        Banana::$profile['headers']['From']         = "$prenom $nom ($promo) <{$this->user->bestEmail()}>";
+        Banana::$profile['headers']['From']         = $this->user->fullName() .  ' <' . $this->user->bestEmail() . '>';
         Banana::$profile['headers']['Organization'] = make_Organization();
         return parent::post($dest, $reply, $subject, $body);
     }
@@ -147,28 +147,32 @@ class ForumsBanana extends Banana
     protected function action_saveSubs($groups)
     {
         global $globals;
-        $uid = S::v('uid');
+        $uid = $this->user->id();
 
         Banana::$profile['subscribe'] = array();
-        XDB::execute("DELETE FROM {$globals->banana->table_prefix}abos WHERE uid={?}", $uid);
+        XDB::execute('DELETE FROM  forum_subs
+                            WHERE  uid = {?}', $this->user->id());
         if (!count($groups)) {
             return true;
         }
 
-        $req  = XDB::iterRow("SELECT fid,nom FROM {$globals->banana->table_prefix}list");
+        $req  = XDB::iterRow('SELECT  fid, name
+                                FROM  forums');
         $fids = array();
-        while (list($fid,$fnom) = $req->next()) {
+        while (list($fid, $fnom) = $req->next()) {
             $fids[$fnom] = $fid;
         }
 
         $diff = array_diff($groups, array_keys($fids));
         foreach ($diff as $g) {
-            XDB::execute("INSERT INTO {$globals->banana->table_prefix}list (nom) VALUES ({?})", $g);
+            XDB::execute('INSERT INTO  forums (name)
+                               VALUES  ({?})', $g);
             $fids[$g] = XDB::insertId();
         }
 
         foreach ($groups as $g) {
-            XDB::execute("INSERT INTO {$globals->banana->table_prefix}abos (fid,uid) VALUES ({?},{?})",
+            XDB::execute('INSERT INTO  forum_subs (fid, uid)
+                               VALUES  ({?}, {?})',
                          $fids[$g], $uid);
             Banana::$profile['subscribe'][] = $g;
         }
@@ -177,7 +181,7 @@ class ForumsBanana extends Banana
     protected function action_updateProfile()
     {
         global $globals;
-        $page = Platal::page();
+        $page =& Platal::page();
 
         $colors = glob(dirname(__FILE__) . '/../../htdocs/images/banana/m2*.gif');
         foreach ($colors as $key=>$path) {
@@ -188,7 +192,7 @@ class ForumsBanana extends Banana
 
         if (Post::has('action') && Post::v('action') == 'Enregistrer') {
             S::assert_xsrf_token();
-            $flags = new FlagSet();
+            $flags = new PlFlagSet();
             if (Post::b('bananadisplay')) {
                 $flags->addFlag('threads');
             }
@@ -202,30 +206,35 @@ class ForumsBanana extends Banana
             $read = Post::s('read');
             if (!in_array($unread, $colors) || !in_array($read, $colors)) {
                 $page->trigError('Le choix de type pour l\'arborescence est invalide');
-            } elseif (XDB::execute("REPLACE INTO  forums.profils (uid, sig, mail, nom, flags, tree_unread, tree_read)
-                                           VALUES  ({?}, {?}, {?}, {?}, {?}, {?}, {?})",
-                                    S::v('uid'), Post::v('bananasig'),
-                                    Post::v('bananamail'), Post::v('banananame'),
-                                    $flags, $unread, $read)) {
-                $page->trigSuccess("Ton profil a été enregistré avec succès.");
             } else {
-                $page->trigError("Une erreur s'est produite lors de l'enregistrement de ton profil");
+                $last_seen = XDB::query('SELECT  last_seen
+                                           FROM  forum_profiles
+                                          WHERE  uid = {?}', $this->user->id());
+                if ($last_seen->numRows() > 0) {
+                    $last_seen = $last_seen->fetchOneCell();
+                } else {
+                    $last_seen = '0000-00-00';
+                }
+                XDB::execute('REPLACE INTO  forum_profiles (uid, sig, mail, name, flags, tree_unread, tree_read, last_seen)
+                                    VALUES  ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?})',
+                             $this->user->id(), Post::v('bananasig'),
+                             Post::v('bananamail'), Post::v('banananame'),
+                             $flags, $unread, $read, $last_seen);
+                $page->trigSuccess('Ton profil a été mis à jour');
             }
         }
 
-        $req = XDB::query("
-            SELECT  nom, mail, sig,
-                    FIND_IN_SET('threads', flags),
-                    FIND_IN_SET('automaj', flags),
-                    FIND_IN_SET('xface', flags),
-                    tree_unread,
-                    tree_read
-              FROM  forums.profils
-             WHERE  uid = {?}", S::v('uid'));
-        if (!(list($nom, $mail, $sig, $disp, $maj, $xface, $unread, $read) = $req->fetchOneRow())) {
-            $nom   = S::v('prenom').' '.S::v('nom');
-            $mail  = S::user()->forlifeEmail();
-            $sig   = $nom.' ('.S::v('promo').')';
+        $req = XDB::query('SELECT  name, mail, sig,
+                                   FIND_IN_SET(\'threads\', flags),
+                                   FIND_IN_SET(\'automaj\', flags),
+                                   FIND_IN_SET(\'xface\', flags),
+                                   tree_unread, tree_read
+                             FROM  forum_profiles
+                            WHERE  uid = {?}', $this->user->id());
+        if (!((list($nom, $mail, $sig, $disp, $maj, $xface, $unread, $read) = $req->fetchOneRow())) || !$nom) {
+            $nom   = $this->user->fullName();
+            $mail  = $this->user->forlifeEmail();
+            $sig   = $this->user->displayName();
             $disp  = 0;
             $maj   = 0;
             $xface = 0;
index 98960f0..4c70d63 100644 (file)
@@ -177,20 +177,25 @@ function hook_hasXFace($headers)
 
 function hook_getXFace($headers)
 {
-    $login = @$headers['x-org-id'];
-    if (!$login) {
-        @list($login, ) = explode('@', $headers['x-org-mail']);
+    $login = null;
+    foreach (array('x-org-id', 'x-org-mail') as $key) {
+        if (isset($headers[$key])) {
+            $login = $headers[$key];
+            break;
+        }
     }
-    if (!$login) {
+    if (is_null($login)) {
+        // No login, fallback to default handler
         return false;
     }
     if (isset($headers['x-face'])) {
-        $res = XDB::query("SELECT  p.uid
-                             FROM  forums.profils AS p
-                       INNER JOIN  aliases AS a ON (p.uid = a.id)
-                            WHERE  FIND_IN_SET('xface', p.flags) AND a.alias = {?}",
-                          $login);
+        $user = User::getSilent($login);
+        $res = XDB::query('SELECT  pf.uid
+                             FROM  forum_profiles AS pf
+                            WHERE  pf.uid = {?} AND FIND_IN_SET(\'xface\', pf.flags)',
+                          $user->id());
         if ($res->numRows()) {
+            // User wants his xface to be showed, fallback to default handler
             return false;
         }
     }
index 6e00cb1..09632eb 100644 (file)
@@ -81,9 +81,9 @@ class MLBanana extends Banana
         Banana::$msgedit_headers['X-Org-Mail'] = $this->user->forlifeEmail();
 
         // Tree color
-        $req = XDB::query("SELECT  tree_unread, tree_read
-                             FROM  {$globals->banana->table_prefix}profils
-                            WHERE  uid={?}", S::i('uid'));
+        $req = XDB::query('SELECT  tree_unread, tree_read
+                             FROM  forum_profiles
+                            WHERE  uid= {?}', $this->user->id());
         if (!(list($unread, $read) = $req->fetchOneRow())) {
             $unread = 'o';
             $read = 'dg';
index 126569a..a839455 100644 (file)
@@ -42,7 +42,7 @@ function user_clear_all_subs($user_id, $really_del=true)
 
     if ($really_del) {
         array_push($tables_to_clear['uid'], 'emails', 'groupex.membres', 'contacts', 'adresses', 'profile_phones',
-                                            'photo', 'perte_pass', 'langues_ins', 'forums.abos', 'forums.profils');
+                                            'photo', 'perte_pass', 'langues_ins', 'forum_subs', 'forum_profiles');
         array_push($tables_to_clear['user_id'], 'newsletter_ins', 'auth_user_quick', 'binets_ins');
         $tables_to_clear['id'] = array('aliases');
         $tables_to_clear['contact'] = array('contacts');
index 9534e99..f87987f 100644 (file)
@@ -645,9 +645,9 @@ class AdminModule extends PLModule
 
                 // Forum ban update.
                 case "b_edit":
-                    XDB::execute("DELETE FROM forums.innd WHERE uid = {?}", $user->id());
+                    XDB::execute("DELETE FROM forum_innd WHERE uid = {?}", $user->id());
                     if (Env::v('write_perm') != "" || Env::v('read_perm') != ""  || Env::v('commentaire') != "" ) {
-                        XDB::execute("INSERT INTO  forums.innd
+                        XDB::execute("INSERT INTO  forum_innd
                                               SET  ipmin = '0', ipmax = '4294967295',
                                                    write_perm = {?}, read_perm = {?},
                                                    comment = {?}, priority = '200', uid = {?}",
@@ -694,7 +694,7 @@ class AdminModule extends PLModule
 
         // Displays forum bans.
         $res = XDB::query("SELECT  write_perm, read_perm, comment
-                             FROM  forums.innd
+                             FROM  forum_innd
                             WHERE  uid = {?}", $user->id());
         $bans = $res->fetchOneAssoc();
         $page->assign('bans', $bans);
index 2999659..a193f64 100644 (file)
@@ -67,7 +67,7 @@ class ForumsModule extends PLModule
     {
         $page->setTitle('Administration - Bannissements des forums');
         $page->assign('title', 'Gestion des mises au ban');
-        $table_editor = new PLTableEditor('admin/forums','forums.innd','id_innd');
+        $table_editor = new PLTableEditor('admin/forums','forum_innd','id_innd');
         $table_editor->add_sort_field('priority', true, true);
         $table_editor->describe('read_perm','lecture',true);
         $table_editor->describe('write_perm','écriture',true);
index 2654fd0..5680799 100644 (file)
@@ -324,8 +324,10 @@ class RegisterModule extends PLModule
         $p_for = 'xorg.promo.x' . $promo;
         $cible = array('xorg.general', 'xorg.pa.divers', 'xorg.pa.logements', $p_for);
         foreach ($cible as $val) {
-            XDB::execute("INSERT INTO  forums.abos (fid,uid)
-                               SELECT  fid, {?} FROM forums.list WHERE nom={?}", $uid, $val);
+            XDB::execute('INSERT INTO  forum_subs (fid,uid)
+                               SELECT  fid, {?}
+                                 FROM  forum
+                                WHERE  name = {?}', $uid, $val);
             if (XDB::affectedRows() == 0 && $val == $p_for) {
                 $res = XDB::query("SELECT  SUM(perms IN ('admin','user') AND deces = 0), COUNT(*)
                                      FROM  auth_user_md5
diff --git a/upgrade/account/02_forums.sql b/upgrade/account/02_forums.sql
new file mode 100644 (file)
index 0000000..26f18f4
--- /dev/null
@@ -0,0 +1,47 @@
+# Move forums tables to x4dat
+#RENAME  forums.list
+#    TO  x4dat.forums;
+#RENAME  forums.abos
+#    TO  x4dat.forum_subs;
+#RENAME  forums.innd
+#    TO  x4dat.forum_innd;
+#RENAME  forums.porfils
+#    TO  x4dat.forum_profiles;
+#DROP DATABASE forums;
+
+## Dev version of previous line
+# (non destructive)
+CREATE TABLE  x4dat.forums
+        LIKE  forums.list;
+INSERT INTO  x4dat.forums
+     SELECT  *
+       FROM  forums.list;
+
+CREATE TABLE  x4dat.forum_subs
+        LIKE  forums.abos;
+INSERT INTO  x4dat.forum_subs
+     SELECT  *
+       FROM  forums.abos;
+
+CREATE TABLE  x4dat.forum_innd
+        LIKE  forums.innd;
+INSERT INTO  x4dat.forum_innd
+     SELECT  *
+       FROM  forums.innd;
+
+CREATE TABLE  x4dat.forum_profiles
+        LIKE  forums.profils;
+INSERT INTO  x4dat.forum_profiles
+     SELECT  *
+       FROM  forums.profils;
+
+
+# Conform to naming convention.
+  ALTER TABLE  forums
+CHANGE COLUMN  nom name VARCHAR(64) NOT NULL;
+
+  ALTER TABLE  forum_profiles
+CHANGE COLUMN  nom name VARCHAR(64) NOT NULL,
+   ADD COLUMN  last_seen TIMESTAMP NOT NULL DEFAULT '0000-00-00';
+
+# vim:set syntax=mysql:
similarity index 76%
rename from upgrade/account/02_insertion.sql
rename to upgrade/account/99_insertion.sql
index f0b7256..f4512d5 100644 (file)
@@ -34,4 +34,15 @@ insert into account_profiles
        from auth_user_md5
       where hruid is not null;
 
+# Update banana last_seen timetamp
+    update  forum_profiles as fp
+inner join  auth_user_quick as q ON (q.user_id = fp.uid)
+       set  fp.uid = fp.uid, fp.tree_unread = fp.tree_unread, fp.tree_read = fp.tree_read,
+            fp.last_seen = q.banana_last;
+
+insert ignore into  forum_profiles (uid, last_seen)
+            select  user_id as uid, banana_last as last_seen
+              from  auth_user_quick
+             where  banana_last >= DATE_SUB(NOW(), INTERVAL 6 MONTH);
+
 # vim:set syntax=mysql:
diff --git a/upgrade/account/README b/upgrade/account/README
new file mode 100644 (file)
index 0000000..15e599c
--- /dev/null
@@ -0,0 +1,19 @@
+DataBase upgrade:
+-----------------
+
+* Activate forums database drop
+
+
+Configuration changes:
+----------------------
+
+[Banana]
+* table_prefix deprecated
+
+
+Affected services:
+------------------
+
+News:
+* authentication must use account + account_types with weakpass.
+* forums base dropped and moved to forum_ namespace.