Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
56.25% covered (warning)
56.25%
18 / 32
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
Hooks
56.25% covered (warning)
56.25%
18 / 32
0.00% covered (danger)
0.00%
0 / 3
11.10
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
2
 onRevisionFromEditComplete
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 onHistoryTools
94.74% covered (success)
94.74%
18 / 19
0.00% covered (danger)
0.00%
0 / 1
5.00
1<?php
2/**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 *
17 * @file
18 */
19
20namespace AutoModerator;
21
22use AutoModerator\Config\AutoModeratorConfigLoaderStaticTrait;
23use AutoModerator\Hooks\RevisionFromEditCompleteHookHandler;
24use JobQueueGroup;
25use MediaWiki\Config\Config;
26use MediaWiki\Content\ContentHandlerFactory;
27use MediaWiki\Hook\HistoryToolsHook;
28use MediaWiki\Html\Html;
29use MediaWiki\Page\Hook\RevisionFromEditCompleteHook;
30use MediaWiki\Page\WikiPageFactory;
31use MediaWiki\Permissions\RestrictionStore;
32use MediaWiki\Revision\RevisionStore;
33use MediaWiki\Title\TitleFactory;
34use MediaWiki\User\UserGroupManager;
35
36class Hooks implements
37    RevisionFromEditCompleteHook,
38    HistoryToolsHook
39{
40    use AutoModeratorConfigLoaderStaticTrait;
41
42    private Config $wikiConfig;
43
44    private UserGroupManager $userGroupManager;
45
46    private Config $config;
47
48    private WikiPageFactory $wikiPageFactory;
49
50    private RevisionStore $revisionStore;
51
52    private ContentHandlerFactory $contentHandlerFactory;
53
54    private RestrictionStore $restrictionStore;
55
56    private JobQueueGroup $jobQueueGroup;
57
58    private TitleFactory $titleFactory;
59
60    /**
61     * @param Config $wikiConfig
62     * @param UserGroupManager $userGroupManager
63     * @param Config $config
64     * @param WikiPageFactory $wikiPageFactory
65     * @param RevisionStore $revisionStore
66     * @param ContentHandlerFactory $contentHandlerFactory
67     * @param RestrictionStore $restrictionStore
68     * @param JobQueueGroup $jobQueueGroup
69     * @param TitleFactory $titleFactory
70     */
71    public function __construct( Config $wikiConfig, UserGroupManager $userGroupManager, Config $config,
72        WikiPageFactory $wikiPageFactory, RevisionStore $revisionStore, ContentHandlerFactory $contentHandlerFactory,
73        RestrictionStore $restrictionStore, JobQueueGroup $jobQueueGroup, TitleFactory $titleFactory ) {
74            $this->wikiConfig = $wikiConfig;
75            $this->userGroupManager = $userGroupManager;
76            $this->config = $config;
77            $this->wikiPageFactory = $wikiPageFactory;
78            $this->revisionStore = $revisionStore;
79            $this->contentHandlerFactory = $contentHandlerFactory;
80            $this->restrictionStore = $restrictionStore;
81            $this->jobQueueGroup = $jobQueueGroup;
82            $this->titleFactory = $titleFactory;
83    }
84
85    /**
86     * @inheritDoc
87     */
88    public function onRevisionFromEditComplete( $wikiPage, $rev, $originalRevId, $user, &$tags ) {
89        $handler = new RevisionFromEditCompleteHookHandler( $this->wikiConfig,
90            $this->userGroupManager, $this->config, $this->wikiPageFactory, $this->revisionStore,
91            $this->contentHandlerFactory, $this->restrictionStore, $this->jobQueueGroup, $this->titleFactory, );
92        $handler->handle( $wikiPage, $rev, $originalRevId, $user, $tags );
93    }
94
95    /**
96     * @inheritDoc
97     */
98    public function onHistoryTools( $revRecord, &$links, $prevRevRecord, $userIdentity ) {
99        $revUser = $revRecord->getUser();
100        $falsePositivePageText = $this->wikiConfig->get( 'AutoModeratorFalsePositivePageTitle' );
101        if ( $revUser === null || $falsePositivePageText === null ) {
102            // Cannot see the user or the false positive page isn't configured
103            return;
104        }
105        $autoModeratorUser = Util::getAutoModeratorUser( $this->config, $this->userGroupManager );
106        $falsePositivePageTitle = $this->titleFactory->newFromText( $falsePositivePageText );
107        if ( $falsePositivePageTitle === null ) {
108            // The false positive page title has been configured, but the page has not been created
109            return;
110        }
111        $falsePositivePageUrl = $falsePositivePageTitle->getFullURL();
112        // Only add the report link if it's an AutoModerator revert
113        if ( $autoModeratorUser->getId() === $revUser->getId() ) {
114            $links[] = Html::element(
115                'a',
116                [
117                    'class' => 'mw-automoderator-report-link',
118                    'href' => $falsePositivePageUrl,
119                    'title' => wfMessage( 'automoderator-wiki-report-false-positive' )->text(),
120                ],
121                wfMessage( 'automoderator-wiki-report-false-positive' )->text()
122            );
123        }
124    }
125}