[Wiki] changed: DatabaseCleanup

Wiki Guest <[email protected]>
Newsgroups gmane.comp.horde.cvs
Message-ID <[email protected]>
guest [193.50.125.9]  Mon, 12 Jan 2009 11:02:59 -0500

Modified page: http://wiki.horde.org/DatabaseCleanup
New Revision:  1.3
Change log:  add a sample using an api to purge old userdata

@@ -1,4 +1,83 @@
+January 12th 2009
+Another script in PHP using API removeUserData dom.lalot à gmail.com
+Sorry for my poor coding, I'm a Perl programmer not a PHP one.
+
+<code>
+#!/usr/bin/env php
+<?php
+@define('AUTH_HANDLER', true);
+@define('HORDE_BASE', dirname(__FILE__));
+# require_once HORDE_BASE . '/lib/base.php';
+// Do CLI checks and environment setup first.
+require_once HORDE_BASE . '/lib/core.php';
+require_once 'Horde/CLI.php';
+// Make sure no one runs this from the web.
+if (!Horde_CLI::runningFromCLI()) {
+    exit("Must be run from the command line\n");
+}
+// Load the CLI environment - make sure there's no time limit, init some
+// variables, etc.
+$cli = &Horde_CLI::singleton();
+$cli->init();
+// Include needed libraries.
+require_once HORDE_BASE . '/lib/base.php';
+// Authenticate as administrator.
+if (!count($conf['auth']['admins'])) {
+    exit("You must have at least one administrator configured to run  
the alarms.php script.\n");
+}
+$auth = &Auth::singleton($conf['auth']['driver']);
+$auth->setAuth($conf['auth']['admins'][0], array());
+require_once HORDE_BASE . '/lib/Horde/Auth.php';
+global $conf;
+require_once 'DB.php';
+$db = &DB::connect($conf['sql']);
+if (is_a($db, 'PEAR_Error')) {
+    Horde::fatal($db, __FILE__, __LINE__);
+}
+$validusers=$auth->listUsers();
+$valides=count($validusers);
+if ($valides==0){
+   echo "Can't list users, your auth driver has no listusers capability\n";
+   exit;
+}
+echo "$valides valid users found\n";
+$valides=array();
+foreach($validusers as $value){
+   $valides[$value]=1;
+}
+// Looking at prefs to get most of old user data
+$db->setOption('portability', DB_PORTABILITY_LOWERCASE |  
DB_PORTABILITY_ERRORS);
+$result = $db->query('SELECT pref_uid FROM horde_prefs WHERE pref_uid  
like \'%\'');
+if (is_a($result, 'PEAR_Error')) {
+   $cli->message($result->toString(), 'cli.error');
+   exit;
+}
+
+$uid = array();
+while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC)) {
+   $login=$row['pref_uid'];
+    $uid[$login] = $login;
+}
+$total=count($uid);
+echo "$total data user found\n";
+exit; # Drop this line if you want to remove data
+
+//now compare valid and data found and purge
+foreach ($uid as $value) {
+   if (!array_key_exists($value,$valides)){
+      echo "Not Found $value\n";
+      Auth::removeUserData($value);
+      $supp++;
+   }
+}
+echo "suppressed $supp\n";
+?>
+
+
+</code>
+
+
  Here is a  database cleanup script that was first  posted on the  
Horde list by Jacques Beaudoin <[email protected]>
   -
  I changed it a little.
  It really should be PHP-ized and get the database information from  
the configs

--
To unsubscribe, mail: [email protected]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.