We are no longer offering accounts on this server. Consider https://gitlab.freedesktop.org/ as a place to host projects.

testuser.php 2.93 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
<?php
/*
 * StatusNet - the distributed open-source microblogging tool
 * Copyright (C) 2012 StatusNet, Inc.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

20
define('INSTALLDIR', realpath(dirname(__FILE__) . '/../../..'));
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35

$shortoptions = 'i:n:a';
$longoptions = array('id=', 'nickname=', 'all');

$helptext = <<<END_OF_TESTUSER_HELP
testuser.php [options]
Test user activities against the spam filter

  -i --id       ID of user to export
  -n --nickname nickname of the user to export
  -a --all      All users
END_OF_TESTUSER_HELP;

require_once INSTALLDIR.'/scripts/commandline.inc';

Evan Prodromou's avatar
Evan Prodromou committed
36
function testAllUsers($filter) {
37 38 39 40 41 42 43 44 45 46 47 48 49 50
    $found = false;
    $offset = 0;
    $limit  = 1000;

    do {

        $user = new User();
        $user->orderBy('created');
        $user->limit($offset, $limit);

        $found = $user->find();

        if ($found) {
            while ($user->fetch()) {
Evan Prodromou's avatar
Evan Prodromou committed
51 52 53 54 55
                try {
                    testUser($filter, $user);
                } catch (Exception $e) {
                    printfnq("ERROR testing user %s\n: %s", $user->nickname, $e->getMessage());
                }
56 57 58 59 60 61 62 63 64
            }
            $offset += $found;
        }

    } while ($found > 0);
}

function testUser($filter, $user) {

Evan Prodromou's avatar
Evan Prodromou committed
65 66
    printfnq("Testing user %s\n", $user->nickname);

67
    $profile = Profile::staticGet('id', $user->id);
68 69 70 71 72 73 74 75 76

    $str = new ProfileNoticeStream($profile, $profile);

    $offset = 0;
    $limit  = 100;

    do {
        $notice = $str->getNotices($offset, $limit);
        while ($notice->fetch()) {
Evan Prodromou's avatar
Evan Prodromou committed
77 78 79 80 81 82
            try {
                printfv("Testing notice %d...", $notice->id);
                $result = $filter->test($notice);
                Spam_score::save($notice, $result);
                printfv("%s\n", ($result->isSpam) ? "SPAM" : "HAM");
            } catch (Exception $e) {
83
                printfnq("ERROR testing notice %d: %s\n", $notice->id, $e->getMessage());
Evan Prodromou's avatar
Evan Prodromou committed
84
            }
85 86 87 88 89 90 91 92 93 94 95
        }
        $offset += $notice->N;
    } while ($notice->N > 0);
}

try {
    $filter = null;
    Event::handle('GetSpamFilter', array(&$filter));
    if (empty($filter)) {
        throw new Exception(_("No spam filter."));
    }
Evan Prodromou's avatar
Evan Prodromou committed
96
    if (have_option('a', 'all')) {
97 98 99 100 101 102 103 104 105
        testAllUsers($filter);
    } else {
        $user = getUser();
        testUser($filter, $user);
    }
} catch (Exception $e) {
    print $e->getMessage()."\n";
    exit(1);
}