Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
54.76% |
23 / 42 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
LocalMessageContentFetcher | |
54.76% |
23 / 42 |
|
0.00% |
0 / 3 |
16.50 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
getContent | |
82.14% |
23 / 28 |
|
0.00% |
0 / 1 |
5.14 | |||
getTitle | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
12 |
1 | <?php |
2 | declare( strict_types = 1 ); |
3 | |
4 | namespace MediaWiki\MassMessage\MessageContentFetcher; |
5 | |
6 | use MediaWiki\MassMessage\LanguageAwareText; |
7 | use MediaWiki\Revision\RevisionStore; |
8 | use MediaWiki\Revision\SlotRecord; |
9 | use MediaWiki\Status\Status; |
10 | use MediaWiki\Title\Title; |
11 | use MediaWiki\WikiMap\WikiMap; |
12 | use TextContent; |
13 | |
14 | /** |
15 | * Fetches content from the local wiki |
16 | * @author Abijeet Patro |
17 | * @since 2022.01 |
18 | * @license GPL-2.0-or-later |
19 | */ |
20 | class LocalMessageContentFetcher { |
21 | /** @var RevisionStore */ |
22 | private $revisionStore; |
23 | /** @var string */ |
24 | private $currentWikiId; |
25 | |
26 | /** |
27 | * @param RevisionStore $revisionStore |
28 | */ |
29 | public function __construct( RevisionStore $revisionStore ) { |
30 | $this->revisionStore = $revisionStore; |
31 | $this->currentWikiId = WikiMap::getCurrentWikiId(); |
32 | } |
33 | |
34 | /** |
35 | * Fetch the page content with the given title from the same wiki. |
36 | * |
37 | * @param Title $pageTitle |
38 | * @return Status Value is LanguageAwareText or null on failure |
39 | */ |
40 | public function getContent( Title $pageTitle ): Status { |
41 | if ( !$pageTitle->exists() ) { |
42 | return Status::newFatal( |
43 | 'massmessage-page-message-not-found', |
44 | $pageTitle->getPrefixedText(), |
45 | $this->currentWikiId |
46 | ); |
47 | } |
48 | |
49 | $revision = $this->revisionStore->getRevisionByTitle( $pageTitle ); |
50 | |
51 | if ( $revision === null ) { |
52 | return Status::newFatal( |
53 | 'massmessage-page-message-no-revision', |
54 | $pageTitle->getPrefixedText() |
55 | ); |
56 | } |
57 | |
58 | $content = $revision->getContent( SlotRecord::MAIN ); |
59 | $wikitext = null; |
60 | if ( $content instanceof TextContent ) { |
61 | $wikitext = $content->getText(); |
62 | } |
63 | |
64 | if ( $wikitext === null ) { |
65 | return Status::newFatal( |
66 | 'massmessage-page-message-no-revision-content', |
67 | $pageTitle->getPrefixedText(), |
68 | $revision->getId() |
69 | ); |
70 | } |
71 | |
72 | $content = new LanguageAwareText( |
73 | $wikitext, |
74 | $pageTitle->getPageLanguage()->getCode(), |
75 | $pageTitle->getPageLanguage()->getDir() |
76 | ); |
77 | |
78 | return Status::newGood( $content ); |
79 | } |
80 | |
81 | /** |
82 | * Fetch the page title given the title string |
83 | * |
84 | * @param string $title |
85 | * @return Status |
86 | */ |
87 | public function getTitle( string $title ): Status { |
88 | $pageTitle = Title::newFromText( $title ); |
89 | |
90 | if ( $pageTitle === null ) { |
91 | return Status::newFatal( |
92 | 'massmessage-page-message-invalid', $title |
93 | ); |
94 | } elseif ( !$pageTitle->exists() ) { |
95 | return Status::newFatal( |
96 | 'massmessage-page-message-not-found', |
97 | $pageTitle->getPrefixedText(), |
98 | $this->currentWikiId |
99 | ); |
100 | } |
101 | |
102 | return Status::newGood( $pageTitle ); |
103 | } |
104 | } |