Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
50.00% covered (danger)
50.00%
8 / 16
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
ConfirmEdit
50.00% covered (danger)
50.00%
8 / 16
50.00% covered (danger)
50.00%
1 / 2
8.12
0.00% covered (danger)
0.00%
0 / 1
 validate
46.67% covered (danger)
46.67%
7 / 15
0.00% covered (danger)
0.00%
0 / 1
6.43
 enabled
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace Flow\SpamFilter;
4
5use ExtensionRegistry;
6use Flow\Model\AbstractRevision;
7use Flow\Model\HtmlRenderingInformation;
8use IContextSource;
9use MediaWiki\Extension\ConfirmEdit\Hooks as ConfirmEditHooks;
10use MediaWiki\Extension\ConfirmEdit\SimpleCaptcha\SimpleCaptcha;
11use MediaWiki\MediaWikiServices;
12use MediaWiki\Status\Status;
13use MediaWiki\Title\Title;
14
15class ConfirmEdit implements SpamFilter {
16    /**
17     * @param IContextSource $context
18     * @param AbstractRevision $newRevision
19     * @param AbstractRevision|null $oldRevision
20     * @param Title $title
21     * @param Title $ownerTitle
22     * @return Status
23     */
24    public function validate(
25        IContextSource $context,
26        AbstractRevision $newRevision,
27        ?AbstractRevision $oldRevision,
28        Title $title,
29        Title $ownerTitle
30    ) {
31        $newContent = $newRevision->getContentInWikitext();
32        $oldContent = ( $oldRevision !== null ) ? $oldRevision->getContentInWikitext() : '';
33
34        /** @var SimpleCaptcha $captcha */
35        $captcha = ConfirmEditHooks::getInstance();
36        $wikiPage = MediaWikiServices::getInstance()->getWikiPageFactory()->newFromTitle( $title );
37
38        // first check if the submitted content is offensive (as flagged by
39        // ConfirmEdit), next check for a (valid) captcha to have been entered
40        if (
41            $captcha->shouldCheck( $wikiPage, $newContent, '', $context, $oldContent ) &&
42            !$captcha->passCaptchaLimitedFromRequest( $context->getRequest(), $context->getUser() )
43        ) {
44            // getting here means we submitted bad content without good captcha
45            // result (or any captcha result at all) - let's get the captcha
46            // information (HTML, modules, etc.) to display as error message!
47            $captchaInfo = $captcha->getFormInformation();
48            $captchaRenderingInfo = HtmlRenderingInformation::fromArray(
49                $captchaInfo
50            );
51
52            $msg = wfMessage( 'flow-spam-confirmedit-form' )->rawParams( $captchaInfo['html'] );
53            $status = Status::newFatal( $msg );
54            $status->setResult( false, $captchaRenderingInfo );
55            return $status;
56        }
57
58        return Status::newGood();
59    }
60
61    /**
62     * Checks if ConfirmEdit is installed.
63     *
64     * @return bool
65     */
66    public function enabled() {
67        return ExtensionRegistry::getInstance()->isLoaded( 'ConfirmEdit' );
68    }
69}