Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
88.89% covered (warning)
88.89%
16 / 18
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
ConfirmEditHandler
88.89% covered (warning)
88.89%
16 / 18
0.00% covered (danger)
0.00%
0 / 1
6.05
0.00% covered (danger)
0.00%
0 / 1
 onEditFilterMergedContent
88.89% covered (warning)
88.89%
16 / 18
0.00% covered (danger)
0.00%
0 / 1
6.05
1<?php
2
3namespace MediaWiki\Extension\AbuseFilter\Hooks\Handlers;
4
5use MediaWiki\Content\Content;
6use MediaWiki\Context\IContextSource;
7use MediaWiki\Extension\ConfirmEdit\CaptchaTriggers;
8use MediaWiki\Extension\ConfirmEdit\Hooks;
9use MediaWiki\Hook\EditFilterMergedContentHook;
10use MediaWiki\Registration\ExtensionRegistry;
11use MediaWiki\Status\Status;
12use MediaWiki\User\User;
13
14/**
15 * Integration with Extension:ConfirmEdit, if loaded.
16 */
17class ConfirmEditHandler implements EditFilterMergedContentHook {
18
19    /** @inheritDoc */
20    public function onEditFilterMergedContent(
21        IContextSource $context, Content $content, Status $status, $summary, User $user, $minoredit
22    ) {
23        if ( !ExtensionRegistry::getInstance()->isLoaded( 'ConfirmEdit' ) ) {
24            return true;
25        }
26        $action = CaptchaTriggers::EDIT;
27        if ( !$context->getWikiPage()->exists() ) {
28            $action = CaptchaTriggers::CREATE;
29        }
30        $simpleCaptcha = Hooks::getInstance( $action );
31        // In WMF production, AbuseFilter is loaded after ConfirmEdit. That means,
32        // Extension:ConfirmEdit's EditFilterMergedContent hook has already run, and that hook
33        // is responsible for deciding whether to show a CAPTCHA via the SimpleCaptcha::confirmEditMerged
34        // method.
35        // Here, we look to see if:
36        // 1. CaptchaConsequence in AbuseFilter modified the global SimpleCaptcha instance to say that
37        //    we should force showing a Captcha
38        // 2. that the Captcha hasn't yet been solved
39        // 3. ConfirmEdit's EditFilterMergedContent handler has already run (ConfirmEdit was loaded
40        //    ahead of AbuseFilter via wfLoadExtension())
41        // If all conditions are true, we invoke SimpleCaptcha's ConfirmEditMerged method, which
42        // will run in a narrower scope (not invoking ConfirmEdit's onConfirmEditTriggersCaptcha hook,
43        // for example), and will just make sure that the status is modified to present a CAPTCHA to
44        // the user.
45        if ( $simpleCaptcha->shouldForceShowCaptcha() &&
46            !$simpleCaptcha->isCaptchaSolved() &&
47            $simpleCaptcha->editFilterMergedContentHandlerAlreadyInvoked() ) {
48            return $simpleCaptcha->confirmEditMerged(
49                $context,
50                $content,
51                $status,
52                $summary,
53                $user,
54                $minoredit
55            );
56        }
57        return true;
58    }
59
60}