Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 18 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| Controller | |
0.00% |
0 / 18 |
|
0.00% |
0 / 2 |
72 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
| validate | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
30 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Flow\SpamFilter; |
| 4 | |
| 5 | use Flow\Exception\FlowException; |
| 6 | use Flow\Model\AbstractRevision; |
| 7 | use MediaWiki\Context\IContextSource; |
| 8 | use MediaWiki\Status\Status; |
| 9 | use MediaWiki\Title\Title; |
| 10 | |
| 11 | class Controller { |
| 12 | /** |
| 13 | * @var SpamFilter[] |
| 14 | */ |
| 15 | protected $spamfilters = []; |
| 16 | |
| 17 | /** |
| 18 | * Accepts multiple spamfilters. |
| 19 | * |
| 20 | * @param SpamFilter $spamfilter,... |
| 21 | * @throws FlowException When provided arguments are not an instance of SpamFilter |
| 22 | */ |
| 23 | public function __construct( SpamFilter $spamfilter /* [, SpamFilter $spamfilter2 [, ...]] */ ) { |
| 24 | $this->spamfilters = array_filter( func_get_args() ); |
| 25 | |
| 26 | // validate data |
| 27 | foreach ( $this->spamfilters as $spamfilter ) { |
| 28 | if ( !$spamfilter instanceof SpamFilter ) { |
| 29 | throw new FlowException( 'Invalid spamfilter', 'default' ); |
| 30 | } |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * @param IContextSource $context |
| 36 | * @param AbstractRevision $newRevision |
| 37 | * @param AbstractRevision|null $oldRevision |
| 38 | * @param Title $title Title that is most specific to the action, e.g. topic for |
| 39 | * replies and board for header edits. |
| 40 | * @param Title $ownerTitle Board title |
| 41 | * @return Status |
| 42 | */ |
| 43 | public function validate( |
| 44 | IContextSource $context, |
| 45 | AbstractRevision $newRevision, |
| 46 | ?AbstractRevision $oldRevision, |
| 47 | Title $title, |
| 48 | Title $ownerTitle |
| 49 | ) { |
| 50 | foreach ( $this->spamfilters as $spamfilter ) { |
| 51 | if ( !$spamfilter->enabled() ) { |
| 52 | continue; |
| 53 | } |
| 54 | |
| 55 | $status = $spamfilter->validate( $context, $newRevision, $oldRevision, $title, $ownerTitle ); |
| 56 | |
| 57 | // no need to go through other filters when invalid data is discovered |
| 58 | if ( !$status->isOK() ) { |
| 59 | $titleString = $title->getPrefixedDBkey(); |
| 60 | $oldRevid = ( $oldRevision !== null ) |
| 61 | ? $oldRevision->getRevisionId()->getAlphadecimal() : 'None'; |
| 62 | $newRevid = $newRevision->getRevisionId()->getAlphadecimal(); |
| 63 | $klass = get_class( $spamfilter ); |
| 64 | wfDebugLog( 'Flow', __METHOD__ . ": Spam filter failed on '" . $titleString . "'. |
| 65 | Old revid: $oldRevid. New revid: $newRevid. Filter: $klass" ); |
| 66 | return $status; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | return Status::newGood(); |
| 71 | } |
| 72 | } |