Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
81.48% covered (warning)
81.48%
22 / 27
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
Hooks
81.48% covered (warning)
81.48%
22 / 27
0.00% covered (danger)
0.00%
0 / 2
6.23
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 onHistoryTools
95.65% covered (success)
95.65%
22 / 23
0.00% covered (danger)
0.00%
0 / 1
5
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 MediaWiki\Config\Config;
23use MediaWiki\Hook\HistoryToolsHook;
24use MediaWiki\Html\Html;
25use MediaWiki\Title\TitleFactory;
26use MediaWiki\User\UserGroupManager;
27
28class Hooks implements
29    HistoryToolsHook
30{
31
32    private Config $wikiConfig;
33
34    private UserGroupManager $userGroupManager;
35
36    private Config $config;
37
38    private TitleFactory $titleFactory;
39
40    /**
41     * @param Config $wikiConfig
42     * @param UserGroupManager $userGroupManager
43     * @param Config $config
44     * @param TitleFactory $titleFactory
45     */
46    public function __construct(
47        Config $wikiConfig,
48        UserGroupManager $userGroupManager,
49        Config $config,
50        TitleFactory $titleFactory
51    ) {
52        $this->wikiConfig = $wikiConfig;
53        $this->userGroupManager = $userGroupManager;
54        $this->config = $config;
55        $this->titleFactory = $titleFactory;
56    }
57
58    /**
59     * @inheritDoc
60     */
61    public function onHistoryTools( $revRecord, &$links, $prevRevRecord, $userIdentity ) {
62        $revUser = $revRecord->getUser();
63        $falsePositivePageText = $this->wikiConfig->get( 'AutoModeratorFalsePositivePageTitle' );
64        if ( $revUser === null || $falsePositivePageText === null ) {
65            // Cannot see the user or the false positive page isn't configured
66            return;
67        }
68        $autoModeratorUser = Util::getAutoModeratorUser( $this->config, $this->userGroupManager );
69        $falsePositivePageTitle = $this->titleFactory->newFromText( $falsePositivePageText );
70        if ( $falsePositivePageTitle === null ) {
71            // The false positive page title has been configured, but the page has not been created
72            return;
73        }
74        $falsePositivePageUrl = $falsePositivePageTitle->getFullURL();
75        // Add parameters to false positive page
76        $falsePositivePreloadTemplate = $falsePositivePageTitle . '/Preload';
77        $pageTitle = $this->titleFactory->newFromPageIdentity( $revRecord->getPage() );
78        $falsePositiveParams = '?action=edit&section=new&nosummary=true&preload=' . $falsePositivePreloadTemplate .
79            '&preloadparams[]=' . $revRecord->getId() . '&preloadparams[]=' . $pageTitle;
80        // Only add the report link if it's an AutoModerator revert
81        if ( $autoModeratorUser->getId() === $revUser->getId() ) {
82            $links[] = Html::element(
83                'a',
84                [
85                    'class' => 'mw-automoderator-report-link',
86                    'href' => $falsePositivePageUrl . $falsePositiveParams,
87                    'title' => wfMessage( 'automoderator-wiki-report-false-positive' )->text(),
88                ],
89                wfMessage( 'automoderator-wiki-report-false-positive' )->text()
90            );
91        }
92    }
93}