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