3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2012, StatusNet, Inc.
6 * Train a notice as spam
10 * This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU Affero General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
15 * This program 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 Affero General Public License for more details.
20 * You should have received a copy of the GNU Affero General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 * @author Evan Prodromou <evan@status.net>
26 * @copyright 2012 StatusNet, Inc.
27 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
28 * @link http://status.net/
31 if (!defined('STATUSNET')) {
32 // This check helps protect against security problems;
33 // your code file can't be executed directly from the web.
38 * Train a notice as spam
42 * @author Evan Prodromou <evan@status.net>
43 * @copyright 2012 StatusNet, Inc.
44 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
45 * @link http://status.net/
48 class TrainAction extends Action
50 protected $notice = null;
51 protected $filter = null;
52 protected $category = null;
55 * For initializing members of the class.
57 * @param array $argarray misc. arguments
59 * @return boolean true
62 function prepare($argarray)
64 parent::prepare($argarray);
66 // User must be logged in.
68 $user = common_current_user();
71 throw new ClientException(_("You must be logged in to train spam."), 403);
74 // User must have the right to review spam
76 if (!$user->hasRight(ActivitySpamPlugin::TRAINSPAM)) {
77 throw new ClientException(_('You cannot review spam on this site.'), 403);
80 $id = $this->trimmed('notice');
82 $this->notice = Notice::getKV('id', $id);
84 if (empty($this->notice)) {
85 throw new ClientException(_("No such notice."));
88 $this->checkSessionToken();
92 Event::handle('GetSpamFilter', array(&$filter));
95 throw new ServerException(_("No spam filter configured."));
98 $this->filter = $filter;
100 $this->category = $this->trimmed('category');
102 if ($this->category !== SpamFilter::SPAM &&
103 $this->category !== SpamFilter::HAM)
105 throw new ClientException(_("No such category."));
114 * @param array $argarray is ignored since it's now passed in in prepare()
119 function handle($argarray=null)
123 $this->filter->trainOnError($this->notice, $this->category);
127 $result = $this->filter->test($this->notice);
131 $score = Spam_score::save($this->notice, $result);
133 // Show new toggle form
135 if ($this->category === SpamFilter::SPAM) {
136 $form = new TrainHamForm($this, $this->notice);
138 $form = new TrainSpamForm($this, $this->notice);
141 if ($this->boolean('ajax')) {
142 $this->startHTML('text/xml;charset=utf-8');
143 $this->elementStart('head');
144 // TRANS: Page title for page on which we train the spam filter for ham or spam
145 $this->element('title', null, _('Train spam filter'));
146 $this->elementEnd('head');
147 $this->elementStart('body');
149 $this->elementEnd('body');
152 common_redirect(common_local_url('spam'), 303);