train.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. /**
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2012, StatusNet, Inc.
  5. *
  6. * Train a notice as spam
  7. *
  8. * PHP version 5
  9. *
  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.
  14. *
  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.
  19. *
  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/>.
  22. *
  23. * @category Spam
  24. * @package StatusNet
  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/
  29. */
  30. if (!defined('STATUSNET')) {
  31. // This check helps protect against security problems;
  32. // your code file can't be executed directly from the web.
  33. exit(1);
  34. }
  35. /**
  36. * Train a notice as spam
  37. *
  38. * @category Spam
  39. * @package StatusNet
  40. * @author Evan Prodromou <evan@status.net>
  41. * @copyright 2012 StatusNet, Inc.
  42. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  43. * @link http://status.net/
  44. */
  45. class TrainAction extends Action
  46. {
  47. protected $notice = null;
  48. protected $filter = null;
  49. protected $category = null;
  50. /**
  51. * For initializing members of the class.
  52. *
  53. * @param array $args misc. arguments
  54. *
  55. * @return boolean true
  56. * @throws ClientException
  57. * @throws ServerException
  58. */
  59. function prepare(array $args = [])
  60. {
  61. parent::prepare($args);
  62. // User must be logged in.
  63. $user = common_current_user();
  64. if (empty($user)) {
  65. throw new ClientException(_("You must be logged in to train spam."), 403);
  66. }
  67. // User must have the right to review spam
  68. if (!$user->hasRight(ActivitySpamPlugin::TRAINSPAM)) {
  69. throw new ClientException(_('You cannot review spam on this site.'), 403);
  70. }
  71. $id = $this->trimmed('notice');
  72. $this->notice = Notice::getKV('id', $id);
  73. if (empty($this->notice)) {
  74. throw new ClientException(_("No such notice."));
  75. }
  76. $this->checkSessionToken();
  77. $filter = null;
  78. Event::handle('GetSpamFilter', array(&$filter));
  79. if (empty($filter)) {
  80. throw new ServerException(_("No spam filter configured."));
  81. }
  82. $this->filter = $filter;
  83. $this->category = $this->trimmed('category');
  84. if ($this->category !== SpamFilter::SPAM &&
  85. $this->category !== SpamFilter::HAM)
  86. {
  87. throw new ClientException(_("No such category."));
  88. }
  89. return true;
  90. }
  91. /**
  92. * Handler method
  93. *
  94. * @return void
  95. * @throws ClientException
  96. */
  97. function handle()
  98. {
  99. // Train
  100. $this->filter->trainOnError($this->notice, $this->category);
  101. // Re-test
  102. $result = $this->filter->test($this->notice);
  103. // Update or insert
  104. $score = Spam_score::save($this->notice, $result);
  105. // Show new toggle form
  106. if ($this->category === SpamFilter::SPAM) {
  107. $form = new TrainHamForm($this, $this->notice);
  108. } else {
  109. $form = new TrainSpamForm($this, $this->notice);
  110. }
  111. if ($this->boolean('ajax')) {
  112. $this->startHTML('text/xml;charset=utf-8');
  113. $this->elementStart('head');
  114. // TRANS: Page title for page on which we train the spam filter for ham or spam
  115. $this->element('title', null, _('Train spam filter'));
  116. $this->elementEnd('head');
  117. $this->elementStart('body');
  118. $form->show();
  119. $this->elementEnd('body');
  120. $this->endHTML();
  121. } else {
  122. common_redirect(common_local_url('spam'), 303);
  123. }
  124. }
  125. }