Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
83.67% covered (warning)
83.67%
41 / 49
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
TalkPageMessageSender
83.67% covered (warning)
83.67%
41 / 49
50.00% covered (danger)
50.00%
1 / 2
6.16
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 insertAutoModeratorSendRevertTalkPageMsgJob
81.82% covered (warning)
81.82%
36 / 44
0.00% covered (danger)
0.00%
0 / 1
5.15
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\Services\AutoModeratorSendRevertTalkPageMsgJob;
23use Exception;
24use MediaWiki\Config\Config;
25use MediaWiki\JobQueue\JobQueueGroup;
26use MediaWiki\MainConfigNames;
27use MediaWiki\MediaWikiServices;
28use MediaWiki\Registration\ExtensionRegistry;
29use MediaWiki\Revision\RevisionStore;
30use MediaWiki\Title\Title;
31use MediaWiki\Title\TitleFactory;
32use MediaWiki\User\User;
33use Psr\Log\LoggerInterface;
34use Wikimedia\Timestamp\ConvertibleTimestamp;
35
36class TalkPageMessageSender {
37
38    private RevisionStore $revisionStore;
39
40    private Config $config;
41
42    private Config $wikiConfig;
43
44    private JobQueueGroup $jobQueueGroup;
45
46    private TitleFactory $titleFactory;
47
48    public function __construct( RevisionStore $revisionStore, Config $config, Config $wikiConfig,
49        JobQueueGroup $jobQueueGroup, TitleFactory $titleFactory ) {
50        $this->revisionStore = $revisionStore;
51        $this->config = $config;
52        $this->wikiConfig = $wikiConfig;
53        $this->jobQueueGroup = $jobQueueGroup;
54        $this->titleFactory = $titleFactory;
55    }
56
57    /**
58     * @param Title $title
59     * @param int $revId
60     * @param int $rollbackRevId
61     * @param User $autoModeratorUser
62     * @param LoggerInterface $logger
63     * @return void
64     */
65    public function insertAutoModeratorSendRevertTalkPageMsgJob(
66        Title $title,
67        int $revId,
68        int $rollbackRevId,
69        User $autoModeratorUser,
70        LoggerInterface $logger
71    ): void {
72        if ( !ExtensionRegistry::getInstance()->isLoaded( 'DiscussionTools' ) ) {
73            // Discussion Tools is not loaded, we will not push a new job to the queue
74            return;
75        }
76        try {
77            $rev = $this->revisionStore->getRevisionById( $revId );
78            if ( $rev === null ) {
79                $logger->debug( __METHOD__ . ': AutoModerator skip rev - new page creation' );
80                return;
81            }
82            $language = MediaWikiServices::getInstance()->getContentLanguage();
83            $timestamp = new ConvertibleTimestamp();
84            if ( $this->config->get( MainConfigNames::TranslateNumerals ) ) {
85                $year = $language->formatNumNoSeparators( $timestamp->format( 'Y' ) );
86            } else {
87                $year = $timestamp->format( 'Y' );
88            }
89
90            $falsePositivePageTitleText = $this->wikiConfig->get( "AutoModeratorFalsePositivePageTitle" );
91            $falsePositivePageTitle = $this->titleFactory->newFromText( $falsePositivePageTitleText )->getFullURL();
92            $falsePositivePreloadTemplate = $falsePositivePageTitleText . '/Preload';
93            $pageTitle = $this->titleFactory->newFromPageIdentity( $rev->getPage() );
94            $falsePositiveParams = '?action=edit&section=new&nosummary=true&preload=' . $falsePositivePreloadTemplate .
95            '&preloadparams%5B%5D=' . $revId . '&preloadparams%5B%5D=' . $pageTitle;
96
97            $userTalkPageJob = new AutoModeratorSendRevertTalkPageMsgJob(
98                $title,
99                [
100                    'revId' => $revId,
101                    'rollbackRevId' => $rollbackRevId,
102                    // The test/production environments do not work when you pass the entire User object.
103                    // To get around this, we have split the required parameters from the User object
104                    // into individual parameters so that the test/production Job constructor will accept them.
105                    'autoModeratorUserId' => $autoModeratorUser->getId(),
106                    'autoModeratorUserName' => $autoModeratorUser->getName(),
107                    'talkPageMessageHeader' => wfMessage( 'automoderator-wiki-revert-message-header' )
108                        ->params(
109                            $language->getMonthName( (int)$timestamp->format( 'n' ) ),
110                            $year,
111                            $autoModeratorUser->getName() )->plain(),
112                    'talkPageMessageEditSummary' => wfMessage( 'automoderator-wiki-revert-edit-summary' )
113                        ->params( $title )->plain(),
114                    'falsePositiveReportPageTitle' => $falsePositivePageTitle . $falsePositiveParams
115                ]
116            );
117            $this->jobQueueGroup->push( $userTalkPageJob );
118            $logger->debug( 'AutoModeratorSendRevertTalkPageMsgJob pushed for {rev}', [
119                'rev' => $revId,
120            ] );
121        } catch ( Exception $e ) {
122            $msg = $e->getMessage();
123            $logger->error( 'AutoModeratorSendRevertTalkPageMsgJob push failed for {rev}: {msg}', [
124                'rev' => $revId,
125                'msg' => $msg
126            ] );
127        }
128    }
129
130}