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