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