Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 36 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
MassMessageServerSideJob | |
0.00% |
0 / 36 |
|
0.00% |
0 / 4 |
110 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
isOptedOut | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
makeText | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
editPage | |
0.00% |
0 / 32 |
|
0.00% |
0 / 1 |
56 |
1 | <?php |
2 | |
3 | namespace MediaWiki\MassMessage\Job; |
4 | |
5 | use Job; |
6 | use MediaWiki\Content\TextContent; |
7 | use MediaWiki\Content\WikitextContent; |
8 | use MediaWiki\MassMessage\MassMessage; |
9 | use MediaWiki\MediaWikiServices; |
10 | use MediaWiki\Revision\RevisionRecord; |
11 | use MediaWiki\Status\Status; |
12 | use MediaWiki\Title\Title; |
13 | use RuntimeException; |
14 | |
15 | /** |
16 | * JobQueue class for jobs queued server side. |
17 | * |
18 | * @file |
19 | * @ingroup JobQueue |
20 | * @author Kunal Mehta |
21 | * @license GPL-2.0-or-later |
22 | */ |
23 | |
24 | class MassMessageServerSideJob extends MassMessageJob { |
25 | /** |
26 | * @param Title $title |
27 | * @param array $params |
28 | */ |
29 | public function __construct( Title $title, array $params ) { |
30 | Job::__construct( 'MassMessageServerSideJob', $title, $params ); |
31 | $this->removeDuplicates = true; |
32 | } |
33 | |
34 | /** |
35 | * Can't opt-out of these messages! |
36 | * |
37 | * @param Title $title |
38 | * @return bool |
39 | */ |
40 | public function isOptedOut( Title $title ) { |
41 | return false; |
42 | } |
43 | |
44 | /** |
45 | * Don't add any hidden comments. |
46 | * |
47 | * @param bool $stripTildes ignored |
48 | * @return Status |
49 | */ |
50 | protected function makeText( bool $stripTildes = false ): Status { |
51 | return Status::newGood( $this->params['message'] ); |
52 | } |
53 | |
54 | /** |
55 | * @param string $text |
56 | * @param string $subject |
57 | * @param string $dedupeHash |
58 | * @return bool |
59 | */ |
60 | protected function editPage( string $text, string $subject, string $dedupeHash ): bool { |
61 | $tries = 1; |
62 | $titleText = $this->title->getPrefixedText(); |
63 | $user = MassMessage::getMessengerUser(); |
64 | $wikiPageFactory = MediaWikiServices::getInstance()->getWikiPageFactory(); |
65 | $text = "== $subject ==\n\n$text"; |
66 | while ( true ) { |
67 | $page = $wikiPageFactory->newFromTitle( $this->title ); |
68 | $flags = 0; |
69 | if ( $page->exists() ) { |
70 | $oldContent = $page->getContent( RevisionRecord::RAW ); |
71 | /** @var TextContent $oldContent */ |
72 | '@phan-var TextContent $oldContent'; |
73 | $text = $oldContent->getText() . "\n\n" . $text; |
74 | $flags |= EDIT_UPDATE; |
75 | } else { |
76 | $flags |= EDIT_NEW; |
77 | } |
78 | if ( $this->title->inNamespace( NS_USER_TALK ) ) { |
79 | $flags |= EDIT_FORCE_BOT; |
80 | } |
81 | $status = $page->doUserEditContent( |
82 | new WikitextContent( $text ), |
83 | $user, |
84 | $subject, |
85 | $flags |
86 | ); |
87 | if ( $status->isOK() ) { |
88 | break; |
89 | } |
90 | |
91 | if ( !$status->hasMessage( 'edit-conflict' ) ) { |
92 | throw new RuntimeException( "Error editing $titleText: {$status->getWikiText()}" ); |
93 | } |
94 | |
95 | if ( $tries > 5 ) { |
96 | throw new RuntimeException( |
97 | "Got 5 edit conflicts when trying to edit $titleText" |
98 | ); |
99 | } |
100 | |
101 | $tries++; |
102 | } |
103 | |
104 | return true; |
105 | } |
106 | } |