egroupware_update_mailinglists.php
author Philipp Wagner <mail@philipp-wagner.com>
Mon Jun 21 00:23:18 2010 +0200 (2 months ago)
changeset 0 5e5168295125
permissions -rwxr-xr-x
initial commit
     1 #!/usr/bin/env php
     2 <?php
     3 /*
     4  * Update Majordomo mailinglists with data from egroupware
     5  * Each mailinglist is filled with users from one egroupware group.
     6  *
     7  * Copyright 2010  Philipp Wagner <mail@philipp-wagner.com>
     8  *
     9  * This program is free software: you can redistribute it and/or modify
    10  * it under the terms of the GNU General Public License as published by
    11  * the Free Software Foundation, either version 3 of the License, or
    12  * (at your option) any later version.
    13  *
    14  * This program is distributed in the hope that it will be useful,
    15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    17  * GNU General Public License for more details.
    18  *
    19  * You should have received a copy of the GNU General Public License
    20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    21  */
    22 require_once 'lib/Config.php';
    23 require_once 'lib/RpcService.php';
    24 require_once 'lib/WhoJob.php';
    25 require_once 'lib/SubscribeJob.php';
    26 require_once 'lib/UnsubscribeJob.php';
    27 
    28 // config
    29 $config = &Config::getInstance();
    30 $config->setConfigFile('config.ini');
    31 
    32 /**
    33  * Wait max. 120s for a result of $job
    34  * 
    35  * @param Job $job
    36  * @param string $uuid
    37  * @return mixed
    38  */
    39 function wait_for_results(Job &$job)
    40 {
    41     $cnt = 0;
    42     echo '(I) Waiting for response ';
    43     do {
    44         $res = $job->checkResults();
    45         echo '.';
    46         if ($res['status'] === Job::STATUS_NOT_READY) {
    47             sleep(1);
    48         }
    49     } while ($cnt++ < 120 && $res['status'] === Job::STATUS_NOT_READY);
    50     echo " done\n";
    51     return $res;
    52 }
    53 
    54 function p_mysql_query($query)
    55 {
    56     if (!($res = mysql_query($query))) {
    57         echo "Query failed! Query was: $query\n".
    58           "MySQL error message was: ".mysql_error()."\n";
    59     }
    60     return $res;
    61 }
    62 
    63 if (!mysql_connect($config->getValue('egroupware/db_server'), 
    64                    $config->getValue('egroupware/db_user'),
    65                    $config->getValue('egroupware/db_password'))) {
    66     echo 'DB connection failed.';
    67     exit(1);
    68 }
    69 
    70 if (!mysql_select_db($config->getValue('egroupware/db_database'))) {
    71     echo 'Unable to select DB.';
    72     exit(1);
    73 }
    74 
    75 $lists = $config->getValue(('egroupware-lists'));
    76 foreach ($lists as $listName => $groupId) {
    77     echo "(I) Processing list $listName\n";
    78     $q = "SELECT account_lid, n_family, n_given, contact_email
    79           FROM `egw_acl`
    80           LEFT JOIN egw_accounts ON acl_account = account_id
    81           LEFT JOIN egw_addressbook USING(account_id)
    82           WHERE acl_appname = 'phpgw_group' AND
    83                 `acl_location` = '-$groupId'";
    84     ($res = p_mysql_query($q)) || exit(1);
    85 
    86     $users = array();
    87     while ($user = mysql_fetch_array($res, MYSQL_ASSOC)) {
    88         $users[] = $user['contact_email'];
    89     }
    90 
    91     // get existing users on list (3 tries)
    92     $success = true;
    93     $cnt = 1;
    94     do {
    95         echo "(I) Getting existing users on the list (try $cnt)\n";
    96         $wj = new WhoJob;
    97         $uuid = $wj->go(array('list' => $listName));
    98         echo "(I) Executing job with ID $uuid.\n";
    99         $res = wait_for_results($wj);
   100         if ($res['status'] !== Job::STATUS_SUCCESS) {
   101             $success = false;
   102             echo "(E) Getting all users from list $listName failed.\n";
   103         } else {
   104             $existingUsers = $res['result'];
   105         }
   106     } while ($cnt++ < 4 && !$success);
   107 
   108     if (!$success) {
   109         echo "(E) Unable to get existing users from list in 3 tries, giving up.\n";
   110         continue;
   111     }
   112 
   113     // unsubscribe existing users
   114     if (empty($existingUsers)) {
   115         echo "(I) No existing users to unsubscribe.\n";
   116     } else {
   117         $success = true;
   118         $cnt = 1;
   119         do {
   120             echo "(I) Unsubscribing existing users (try $cnt)\n";
   121             $uj = new UnsubscribeJob;
   122             $params = array(
   123                 'list' => $listName,
   124                 'users' => $existingUsers
   125             );
   126             $uuid = $uj->go($params);
   127             echo "(I) Executing job with ID $uuid.\n";
   128             $res = wait_for_results($uj);
   129             if ($res['status'] !== Job::STATUS_SUCCESS) {
   130                 $success = false;
   131                 echo "(E) Unsubscribing all users from $listName failed.\n";
   132                 var_dump($res);
   133             }
   134         } while ($cnt++ < 4 && !$success);
   135         
   136         if (!$success) {
   137             echo "(E) Unable to unsubscribe users from list in 3 tries, giving up.\n";
   138             continue;
   139         }
   140     }
   141 
   142     // subscribe new users
   143     if (empty($users)) {
   144         echo "(I) No new users to subscribe.\n";
   145     } else {
   146         $success = true;
   147         $cnt = 1;
   148         do {
   149             echo "(I) Subscribing new users\n";
   150             $sj = new SubscribeJob;
   151             $params = array(
   152                 'list' => $listName,
   153                 'users' => $users
   154             );
   155             $uuid = $sj->go($params);
   156             echo "(I) Executing job with ID $uuid.\n";
   157             $res = wait_for_results($sj);
   158             if ($res['status'] !== Job::STATUS_SUCCESS) {
   159                 $success = false;
   160                 echo "(E) Subscribing new users to $listName failed.\n";
   161                 var_dump($res);
   162             }
   163         } while ($cnt++ < 4 && !$success);
   164 
   165         if (!$success) {
   166             echo "(E) Houston, we have a problem. All users unsubscribed, ".
   167                  "but unable to subscribe the new ones. List empty?! ".
   168                  "Stopping update! Manual intervention required.\n";
   169             exit(1);
   170         }
   171     }
   172 }
   173 
   174 
   175 echo "(I) All lists updated!\n";