4 # Copyright (c) STMicroelectronics, 2005. All Rights Reserved.
6 # Originally written by Jean-Philippe Giola, 2005
8 # This file is a part of codendi.
10 # codendi is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 2 of the License, or
13 # (at your option) any later version.
15 # codendi is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License along
21 # with this program; if not, write to the Free Software Foundation, Inc.,
22 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 /* This script allows the transfer of an mbox-formatted mail to ForumML database.
28 * First argument: mailing-list name
29 * Second argument: type of transfer, depending on the input
30 * '1': transfer from '/var/spool/forumml/mail_tmp_xyz' temporary file (1-message mbox file)
31 * '2': transfer from whole list archive (real mbox file)
32 * Third argument: temporary file name (used when 2nd arg = 1)
35 ini_set('max_execution_time', 0);
36 ini_set('memory_limit', -1);
37 require_once 'env.inc.php';
38 require_once 'pre.php';
39 require_once 'preplugins.php';
40 error_reporting(E_ALL);
41 require 'Mail/Mbox.php';
42 require_once(dirname(__FILE__).'/../include/ForumML_mimeDecode.class.php');
43 require_once(dirname(__FILE__).'/../include/ForumMLInsert.class.php');
44 require_once(dirname(__FILE__).'/../include/ForumML_FileStorage.class.php');
45 require_once 'mailman/www/mailman_utils.php';
46 //require_once('utils.php');
48 // get list id and group id from list name
49 $sql = sprintf('SELECT group_id, group_list_id'.
50 ' FROM mail_group_list'.
51 ' WHERE list_name = $1');
52 $res = db_query_params($sql,array($list));
53 if (db_numrows($res) > 0) {
54 $id_list = db_result($res,0,'group_list_id');
55 $gr_id = db_result($res,0,'group_id');
57 $stderr = fopen('php://stderr', 'w');
58 fwrite($stderr, "Invalid mailing-list $list \n");
62 $plugin_manager =& PluginManager::instance();
63 $p =& $plugin_manager->getPluginByName('forumml');
64 if ($p && $plugin_manager->isPluginAvailable($p) ) {
67 $forumml_arch = $GLOBALS['forumml_arch'];;
68 $mbox_file = $forumml_arch."/private/".$list.".mbox/".$list.".mbox";
69 // check if mbox file exists
70 if (! is_file($mbox_file)) {
71 $stderr = fopen('php://stderr', 'w');
72 fwrite($stderr, "Invalid mbox file $mbox_file \n");
76 // Do not import from archives if there are already messages for this list
77 $sql = 'SELECT NULL FROM plugin_forumml_message WHERE id_list = $1 LIMIT 1';
78 $res = db_query_params($sql,array($id_list));
79 if ($res && db_numrows($res) > 0) {
80 $stderr = fopen('php://stderr', 'w');
81 fwrite($stderr, "Cannot import messages from archive.\nThere are already messages in the database for $list ($mbox_file)\n");
87 $temp_file = $argv[3];
88 // get temp file parent dir
89 $forumml_tmp = $GLOBALS['forumml_tmp'];
90 $mbox_file = $forumml_tmp."/".$temp_file;
92 // Open the mail that has been temporary stored
93 $mbox = new Mail_Mbox($mbox_file);
95 if (PEAR::isError($mbox)) {
96 print "Unable to open mbox: ".$mbox->getMessage().PHP_EOL;
99 $num_msg = $mbox->size();
100 for ($i = 0; $i < $num_msg; $i++) {
101 $thisMessage = $mbox->get($i);
102 if (PEAR::isError($thisMessage)) {
103 print "Unable to get message $i: ".$thisMessage->getMessage().PHP_EOL;
106 $args['include_bodies'] = TRUE;
107 $args['decode_bodies'] = TRUE;
108 $args['decode_headers'] = TRUE;
109 $args['crlf'] = "\r\n";
110 $decoder = new ForumML_mimeDecode($thisMessage, "\r\n");
111 $structure = $decoder->decode($args);
113 // Get ForumML storage
114 $forumml_dir = $GLOBALS['forumml_dir'];
115 $forumml_storage = new ForumML_FileStorage($forumml_dir);
118 $insert = new ForumMLInsert($id_list);
119 $msgId = $insert->storeEmail($structure, $forumml_storage);
126 // Display message when importing a mail archive
128 if ($num_msg == $nbMailInserted) {
129 echo 'Operation Completed.'.$num_msg.' imported'.PHP_EOL;
131 echo '*** Error: '.$num_msg.' in '.$mbox_file.' file but '. $nbMailInserted.' stored in database'.PHP_EOL;
137 // delete temporary file
138 /*if ($argv[2] == 1) {
139 if (is_file($mbox_file)) {