Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
88.89% covered (warning)
88.89%
56 / 63
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
AutoModeratorSendRevertTalkPageMsgJob
88.89% covered (warning)
88.89%
56 / 63
66.67% covered (warning)
66.67%
2 / 3
13.23
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
1
 run
86.79% covered (warning)
86.79%
46 / 53
0.00% covered (danger)
0.00%
0 / 1
9.19
 setAllowRetries
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 allowRetries
n/a
0 / 0
n/a
0 / 0
1
 ignoreDuplicates
n/a
0 / 0
n/a
0 / 0
1
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 3 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
14 * along with this program.  If not, see <http:www.gnu.org/licenses/>.
15 */
16
17namespace AutoModerator\Services;
18
19use AutoModerator\Util;
20use Job;
21use MediaWiki\Logger\LoggerFactory;
22use MediaWiki\MediaWikiServices;
23use MediaWiki\Title\Title;
24use RuntimeException;
25
26class AutoModeratorSendRevertTalkPageMsgJob extends Job {
27
28    /**
29     * @var bool
30     */
31    private bool $isRetryable = true;
32
33    /**
34     * @var int
35     */
36    private $revId;
37
38    /**
39     * @var int
40     */
41    private int $parentRevId;
42
43    /**
44     * @var ?string
45     */
46    private ?string $pageTitle;
47
48    /**
49     * @var int
50     */
51    private int $autoModeratorUserId;
52
53    /**
54     * @var string
55     */
56    private string $autoModeratorUserName;
57
58    /**
59     * @var string
60     */
61    private string $talkPageMessageHeader;
62
63    /**
64     * @var string
65     */
66    private string $talkPageMessageEditSummary;
67
68    /**
69     * @var string
70     */
71    private string $falsePositiveReportPageTitle;
72
73    private const NO_USER_TALK_PAGE_ERROR_MESSAGE = 'Failed to retrieve user talk page title '
74        . 'for sending AutoModerator revert talk page message.';
75
76    private const NO_PARENT_REVISION_FOUND = 'Failed to retrieve reverted revision from revision store.';
77
78    /**
79     * @param Title $title
80     * @param array $params
81     *    - 'revId': (int)
82     *    - 'parentRevId': (int)
83     *    - 'autoModeratorUserId': (int)
84     *    - 'autoModeratorUserName': (string)
85     *    - 'talkPageMessageHeader': (string)
86     *    - 'talkPageMessageEditSummary': (string)
87     *    - 'falsePositiveReportPageTitle': (string)
88     */
89    public function __construct( Title $title, array $params ) {
90        parent::__construct( 'AutoModeratorSendRevertTalkPageMsgJob', $title, $params );
91        $this->pageTitle = $title;
92        $this->revId = $params['revId'];
93        $this->parentRevId = $params['parentRevId'];
94        $this->autoModeratorUserId = $params['autoModeratorUserId'];
95        $this->autoModeratorUserName = $params['autoModeratorUserName'];
96        $this->talkPageMessageHeader = $params['talkPageMessageHeader'];
97        $this->talkPageMessageEditSummary = $params['talkPageMessageEditSummary'];
98        $this->falsePositiveReportPageTitle = $params['falsePositiveReportPageTitle'];
99    }
100
101    public function run(): bool {
102        $logger = LoggerFactory::getInstance( 'AutoModerator' );
103        try {
104            $services = MediaWikiServices::getInstance();
105            $userFactory = $services->getUserFactory();
106            $revisionStore = $services->getRevisionStore();
107            $parentRevision = $revisionStore->getRevisionById( $this->parentRevId );
108            if ( !$parentRevision ) {
109                $this->setLastError( self::NO_PARENT_REVISION_FOUND );
110                $this->setAllowRetries( false );
111                return false;
112            }
113            $userTalkPageTitle = $services->getTitleFactory()->makeTitleSafe(
114                NS_USER_TALK,
115                $parentRevision->getUser()->getName()
116            );
117            if ( !$userTalkPageTitle ) {
118                $this->setLastError( self::NO_USER_TALK_PAGE_ERROR_MESSAGE );
119                $this->setAllowRetries( false );
120                return false;
121            }
122
123            $userTalkPage = $services->getWikiPageFactory()->newFromTitle( $userTalkPageTitle );
124
125            $autoModeratorUser = $userFactory->newFromAnyId(
126                $this->autoModeratorUserId,
127                $this->autoModeratorUserName
128            );
129
130            $apiClient = Util::initializeApiClient();
131            // Find if the User Talk page exists before we search for it
132            $findApiResponse = [];
133            if ( $userTalkPage->exists() ) {
134                $findApiResponse = $apiClient->findComment( $this->talkPageMessageHeader, $userTalkPageTitle );
135            }
136
137            if ( array_key_exists( "couldredirect", $findApiResponse ) &&
138                $findApiResponse[ "couldredirect" ] ) {
139                // AutoModerator has already posted on this User Talk page this month
140                // and the topic has not been deleted, adding a follow-up comment instead
141                $pageInfoResponse = $apiClient->getUserTalkPageInfo( $userTalkPageTitle );
142                // Getting the pageInformation to get the comment's id
143                $headerId = $findApiResponse[ "id" ];
144                $threadItems = $pageInfoResponse[ "discussiontoolspageinfo" ][ "threaditemshtml" ];
145                foreach ( $threadItems as $threadItem ) {
146                    if ( $threadItem[ "id" ] === $headerId ) {
147                        // Getting the first reply id from this thread item
148                        $commentId = $threadItem[ "replies" ][ 0 ][ "id" ];
149                        $followUpComment = wfMessage( 'automoderator-wiki-revert-message-follow-up' )->params(
150                            $this->revId,
151                            $this->pageTitle
152                        )->plain();
153                        $apiClient->addFollowUpComment( $commentId, $userTalkPageTitle, $followUpComment,
154                            $autoModeratorUser );
155                        break;
156                    }
157                }
158            } else {
159                // AutoModerator hasn't added a User Talk page message this month,
160                // adding a new topic message
161                $talkPageMessage = wfMessage( 'automoderator-wiki-revert-message' )->params(
162                    $this->autoModeratorUserName,
163                    $this->revId,
164                    $this->pageTitle,
165                    $this->falsePositiveReportPageTitle )->plain();
166                $apiClient->addTopic( $this->talkPageMessageHeader, $userTalkPageTitle, $talkPageMessage,
167                    $this->talkPageMessageEditSummary, $autoModeratorUser );
168            }
169
170        } catch ( RuntimeException $e ) {
171            $this->setLastError( $e );
172            $logger->error( $e->getMessage() );
173            return false;
174        }
175        return true;
176    }
177
178    private function setAllowRetries( bool $isRetryable ) {
179        $this->isRetryable = $isRetryable;
180    }
181
182    /**
183     * @inheritDoc
184     * @codeCoverageIgnore
185     */
186    public function allowRetries(): bool {
187        return $this->isRetryable;
188    }
189
190    /**
191     * @inheritDoc
192     * @codeCoverageIgnore
193     */
194    public function ignoreDuplicates(): bool {
195        return true;
196    }
197}