| | 436 | # Process all the mail items in the database that |
| | 437 | # recipients have confirmed as ham (type = 'G'). |
| | 438 | sub recalc_stats($) { |
| | 439 | my($dbh) = @_; |
| | 440 | my($sth, @row, $select); |
| | 441 | my($user_id); |
| | 442 | |
| | 443 | $select = "SELECT maia_users.id " . |
| | 444 | "FROM maia_users"; |
| | 445 | $sth = $dbh->prepare($select) |
| | 446 | or die (sprintf("Maia: [recalc_stats] Couldn't prepare query: %s", $dbh->errstr)); |
| | 447 | $sth->execute() |
| | 448 | or die (sprintf("Maia: [recalc_stats] Couldn't execute query: %s", $dbh->errstr)); |
| | 449 | while (@row = $sth->fetchrow_array()) { |
| | 450 | $user_id = $1 if $row[0] =~ /^([1-9]+[0-9]*)$/si; # untaint |
| | 451 | |
| | 452 | update_mail_stats( $dbh, $user_id, "suspected_ham"); |
| | 453 | update_mail_stats( $dbh, $user_id, "suspected_spam"); |
| | 454 | } |
| | 455 | |
| | 456 | $sth->finish; |
| | 457 | } |
| | 458 | |
| | 459 | |
| | 460 | sub update_mail_stats($$) { |
| | 461 | my($dbh, $user_id, $type) = @_; |
| | 462 | my($sth, $sth2, $sth3, $sth4, @row, $select, $update, $insert); |
| | 463 | my($token); |
| | 464 | |
| | 465 | if ($type eq "suspected_spam") { |
| | 466 | $token = "S"; |
| | 467 | } elsif ($type eq "suspected_ham") { |
| | 468 | $token = "H"; |
| | 469 | } else { |
| | 470 | $token = ""; |
| | 471 | } |
| | 472 | |
| | 473 | |
| | 474 | # Assemble a list of all the confirmed ham (G) items |
| | 475 | $select = "SELECT MIN(received_date) AS mindate, " . |
| | 476 | "MAX(received_date) AS maxdate, " . |
| | 477 | "MIN(score) AS minscore, " . |
| | 478 | "MAX(score) AS maxscore, " . |
| | 479 | "SUM(score) AS totalscore, " . |
| | 480 | "MIN(size) AS minsize, " . |
| | 481 | "MAX(size) AS maxsize, " . |
| | 482 | "SUM(size) AS totalsize, " . |
| | 483 | "COUNT(id) AS items " . |
| | 484 | "FROM maia_mail, maia_mail_recipients " . |
| | 485 | "WHERE maia_mail.id = maia_mail_recipients.mail_id " . |
| | 486 | "AND maia_mail_recipients.type = ? " . |
| | 487 | "AND maia_mail_recipients.recipient_id = ? "; |
| | 488 | $sth = $dbh->prepare($select) |
| | 489 | or die (sprintf("Maia: [update_mail_stats] Couldn't prepare query: %s", $dbh->errstr)); |
| | 490 | $sth->execute($token, $user_id) |
| | 491 | or die (sprintf("Maia: [update_mail_stats] Couldn't execute query: %s", $dbh->errstr)); |
| | 492 | |
| | 493 | my( $mindate, $maxdate, $minscore, $maxscore, $totalscore, $minsize, $maxsize, $totalsize, $items); |
| | 494 | if ( ($mindate, $maxdate, $minscore, $maxscore, $totalscore, $minsize, $maxsize, $totalsize, $items) = $sth->fetchrow() ) { |
| | 495 | $select = "SELECT user_id FROM maia_stats WHERE user_id = " . $user_id; |
| | 496 | |
| | 497 | $sth2 = $dbh->prepare($select) |
| | 498 | or die (sprintf("Maia: [update_mail_stats] Couldn't prepare query: %s", $dbh->errstr)); |
| | 499 | $sth2->execute() |
| | 500 | or die (sprintf("Maia: [update_mail_stats] Couldn't execute query: %s", $dbh->errstr)); |
| | 501 | |
| | 502 | # User already has a stats record, update it. |
| | 503 | if ($sth2->fetchrow()) { |
| | 504 | $update = "UPDATE maia_stats SET oldest_" . $type . "_date = ?, " . |
| | 505 | "newest_" . $type . "_date = ?, " . |
| | 506 | "lowest_" . $type . "_score = ?, " . |
| | 507 | "highest_" . $type . "_score = ?, " . |
| | 508 | "total_" . $type . "_score = ?, " . |
| | 509 | "smallest_" . $type . "_size = ?, " . |
| | 510 | "largest_" . $type . "_size = ?, " . |
| | 511 | "total_" . $type . "_size = ?, " . |
| | 512 | "total_" . $type . "_items = ? " . |
| | 513 | "WHERE user_id = ?"; |
| | 514 | |
| | 515 | $sth3 = $dbh->prepare( $update ) |
| | 516 | or die (sprintf("Maia: [update_mail_stats] Couldn't prepare query: %s", $dbh->errstr));; |
| | 517 | $sth3->execute( $mindate, |
| | 518 | $maxdate, |
| | 519 | (defined($minscore) ? $minscore : 0), |
| | 520 | (defined($maxscore) ? $maxscore : 0), |
| | 521 | (defined($totalscore) ? $totalscore : 0), |
| | 522 | (defined($minsize) ? $minsize : 0), |
| | 523 | (defined($maxsize) ? $maxsize : 0), |
| | 524 | (defined($totalsize) ? $totalsize : 0), |
| | 525 | (defined($items) ? $items : 0), |
| | 526 | $user_id) |
| | 527 | or die (sprintf("Maia: [update_mail_stats] Couldn't execute query: %s", $dbh->errstr)); |
| | 528 | $sth3->finish; |
| | 529 | |
| | 530 | # User doesn't have a stats record yet, create a new one for him. |
| | 531 | } else { |
| | 532 | $insert = "INSERT INTO maia_stats (oldest_" . $type . "_date, " . |
| | 533 | "newest_" . $type . "_date, " . |
| | 534 | "lowest_" . $type . "_score, " . |
| | 535 | "highest_" . $type . "_score, " . |
| | 536 | "total_" . $type . "_score, " . |
| | 537 | "smallest_" . $type . "_size, " . |
| | 538 | "largest_" . $type . "_size, " . |
| | 539 | "total_" . $type . "_size, " . |
| | 540 | "total_" . $type . "_items, " . |
| | 541 | "user_id) " . |
| | 542 | "VALUES (?,?,?,?,?,?,?,?,?,?)"; |
| | 543 | $sth4 = $dbh->prepare( $insert ) |
| | 544 | or die (sprintf("Maia: [update_mail_stats] Couldn't prepare query: %s", $dbh->errstr));; |
| | 545 | |
| | 546 | $sth4->execute( $mindate, |
| | 547 | $maxdate, |
| | 548 | (defined($minscore) ? $minscore : 0), |
| | 549 | (defined($maxscore) ? $maxscore : 0), |
| | 550 | (defined($totalscore) ? $totalscore : 0), |
| | 551 | (defined($minsize) ? $minsize : 0), |
| | 552 | (defined($maxsize) ? $maxsize : 0), |
| | 553 | (defined($totalsize) ? $totalsize : 0), |
| | 554 | (defined($items) ? $items : 0), |
| | 555 | $user_id) |
| | 556 | or die (sprintf("Maia: [update_mail_stats] Couldn't execute query: %s", $dbh->errstr)); |
| | 557 | $sth4->finish; |
| | 558 | } |
| | 559 | $sth2->finish; |
| | 560 | } |
| | 561 | $sth->finish; |
| | 562 | } |
| | 563 | |
| | 564 | |
| | 565 | |