updated google model

This commit is contained in:
SJ 2014-04-18 21:28:53 +02:00
parent 1be641087e
commit 4e6946feca

View File

@ -46,64 +46,66 @@ class ModelGoogleGoogle extends Model {
} }
} }
// Save all Messages from selected folder // Save all Messages from selected folder
private function saveMessages($email) { private function saveMessages($email) {
$last_msg_id = -1; $last_msg_id = -1;
$from = 1; $from = 1;
$downloaded = 0; $count = 0;
$storage = $this->storage; $storage = $this->storage;
$from = $this->get_last_message_id_by_unique_id($email, $storage) + 1; $from = $this->get_last_message_id_by_unique_id($email, $storage) + 1;
//print "will download messages from: $from\n"; //print "will download messages from: $from\n";
$num = $storage->countMessages(); $num = $storage->countMessages();
$to = $from; $to = $from;
while($from <= $num) { while($from <= $num) {
if($num - $from > 9) { $delta = 9; } if($num - $from > 9) { $delta = 9; }
else { $delta = $num-$from; } else { $delta = $num-$from; }
$to = $from + $delta; $to = $from + $delta;
//print "downloading $from, $to\n"; //print "downloading $from, $to\n";
$unique_msg_id = $storage->getUniqueId($to); $unique_msg_id = $storage->getUniqueId($to);
$messages = $storage->piler_batch_fetch($from, $to); $messages = $storage->piler_batch_fetch($from, $to);
while(list($k, $v) = each($messages)) { while(list($k, $v) = each($messages)) {
$uuid = $storage->getUniqueId($k); $uuid = $storage->getUniqueId($k);
$tmpname = "piler-" . $email . "-" . $k . "-" . $uuid . ".eml"; $tmpname = "piler-" . $email . "-" . $k . "-" . $uuid . ".eml";
$f = fopen(DIR_TMP . "/" . $tmpname, "w+"); $f = fopen(DIR_TMP . "/" . $tmpname, "w+");
if($f){ if($f){
fwrite($f, $v['RFC822.HEADER'] . $v['RFC822.TEXT']); fwrite($f, $v['RFC822.HEADER'] . $v['RFC822.TEXT']);
fclose($f); fclose($f);
rename(DIR_TMP . "/" . $tmpname, DIR_IMAP . "/" . $tmpname); rename(DIR_TMP . "/" . $tmpname, DIR_IMAP . "/" . $tmpname);
$downloaded++; $count++;
}
//print "k: $k\n";
} }
//print "k: $k\n";
$this->update_imap_table($email, $unique_msg_id, $to);
$from += $delta + 1;
} }
$this->update_imap_table($email, $unique_msg_id, $to);
syslog(LOG_INFO, "downloaded $downloaded messages for $email"); $from += $delta + 1;
}
syslog(LOG_INFO, "downloaded $count messages for $email");
return $count;
} }
public function download_users_emails($email, $accessToken) { public function download_users_emails($email, $accessToken) {
$count = 0;
if(!$email || !$accessToken) { return 0; } if(!$email || !$accessToken) { return 0; }
@ -117,16 +119,17 @@ class ModelGoogleGoogle extends Model {
$folders = new RecursiveIteratorIterator($this->storage->getFolders(), $folders = new RecursiveIteratorIterator($this->storage->getFolders(),
RecursiveIteratorIterator::SELF_FIRST); RecursiveIteratorIterator::SELF_FIRST);
foreach ($folders as $localName => $folder) { foreach ($folders as $localName => $folder) {
if ($folder->isSelectable()) { if($folder->isSelectable()) {
$this->storage->selectFolder($folder); $this->storage->selectFolder($folder);
$this->saveMessages($email); $count += $this->saveMessages($email);
} }
} }
} }
return $count;
} }