Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
89.47% covered (warning)
89.47%
17 / 19
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
ConfirmEditHandler
89.47% covered (warning)
89.47%
17 / 19
0.00% covered (danger)
0.00%
0 / 1
8.07
0.00% covered (danger)
0.00%
0 / 1
 onEditFilterMergedContent
89.47% covered (warning)
89.47%
17 / 19
0.00% covered (danger)
0.00%
0 / 1
8.07
1<?php
2
3namespace MediaWiki\Extension\AbuseFilter\Hooks\Handlers;
4
5use Content;
6use ExtensionRegistry;
7use MediaWiki\Context\IContextSource;
8use MediaWiki\Extension\ConfirmEdit\Hooks;
9use MediaWiki\Hook\EditFilterMergedContentHook;
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        // FIXME: Remove method_exists checks after I3484d66298bc9f49dfbe003a0605e2ac1a092e10 is merged
27        if ( !method_exists( $simpleCaptcha, 'shouldForceShowCaptcha' ) ||
28            !method_exists( $simpleCaptcha, 'isCaptchaSolved' ) ||
29            !method_exists( $simpleCaptcha, 'editFilterMergedContentHandlerAlreadyInvoked' ) ) {
30            return true;
31        }
32        // In WMF production, AbuseFilter is loaded after ConfirmEdit. That means,
33        // Extension:ConfirmEdit's EditFilterMergedContent hook has already run, and that hook
34        // is responsible for deciding whether to show a CAPTCHA via the SimpleCaptcha::confirmEditMerged
35        // method.
36        // Here, we look to see if:
37        // 1. CaptchaConsequence in AbuseFilter modified the global SimpleCaptcha instance to say that
38        //    we should force showing a Captcha
39        // 2. that the Captcha hasn't yet been solved
40        // 3. ConfirmEdit's EditFilterMergedContent handler has already run (ConfirmEdit was loaded
41        //    ahead of AbuseFilter via wfLoadExtension())
42        // If all conditions are true, we invoke SimpleCaptcha's ConfirmEditMerged method, which
43        // will run in a narrower scope (not invoking ConfirmEdit's onConfirmEditTriggersCaptcha hook,
44        // for example), and will just make sure that the status is modified to present a CAPTCHA to
45        // the user.
46        if ( $simpleCaptcha->shouldForceShowCaptcha() &&
47            !$simpleCaptcha->isCaptchaSolved() &&
48            $simpleCaptcha->editFilterMergedContentHandlerAlreadyInvoked() ) {
49            return $simpleCaptcha->confirmEditMerged(
50                $context,
51                $content,
52                $status,
53                $summary,
54                $user,
55                $minoredit
56            );
57        }
58        return true;
59    }
60
61}