Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
64.29% |
18 / 28 |
|
50.00% |
2 / 4 |
CRAP | |
0.00% |
0 / 1 |
CampaignsPageFactory | |
64.29% |
18 / 28 |
|
50.00% |
2 / 4 |
18.56 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
newPageFromDB | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
newLocalExistingPageFromString | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
6 | |||
newFromLocalMediaWikiPage | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | |
3 | declare( strict_types=1 ); |
4 | |
5 | namespace MediaWiki\Extension\CampaignEvents\MWEntity; |
6 | |
7 | use MediaWiki\DAO\WikiAwareEntity; |
8 | use MediaWiki\Linker\LinkTarget; |
9 | use MediaWiki\Page\PageIdentity; |
10 | use MediaWiki\Page\PageIdentityValue; |
11 | use MediaWiki\Page\PageStoreFactory; |
12 | use MediaWiki\Title\MalformedTitleException; |
13 | use MediaWiki\Title\TitleFormatter; |
14 | use MediaWiki\Title\TitleParser; |
15 | use MediaWiki\WikiMap\WikiMap; |
16 | |
17 | class CampaignsPageFactory { |
18 | public const SERVICE_NAME = 'CampaignEventsPageFactory'; |
19 | |
20 | private PageStoreFactory $pageStoreFactory; |
21 | private TitleParser $titleParser; |
22 | private TitleFormatter $titleFormatter; |
23 | |
24 | /** |
25 | * @param PageStoreFactory $pageStoreFactory |
26 | * @param TitleParser $titleParser |
27 | * @param TitleFormatter $titleFormatter |
28 | */ |
29 | public function __construct( |
30 | PageStoreFactory $pageStoreFactory, |
31 | TitleParser $titleParser, |
32 | TitleFormatter $titleFormatter |
33 | ) { |
34 | $this->pageStoreFactory = $pageStoreFactory; |
35 | $this->titleParser = $titleParser; |
36 | $this->titleFormatter = $titleFormatter; |
37 | } |
38 | |
39 | /** |
40 | * Creates a page object from a DB record. This does NOT require that the page exists. |
41 | * |
42 | * @param int $namespace |
43 | * @param string $dbKey |
44 | * @param string $prefixedText |
45 | * @param string|false $wikiID |
46 | * @return ICampaignsPage |
47 | */ |
48 | public function newPageFromDB( int $namespace, string $dbKey, string $prefixedText, $wikiID ): ICampaignsPage { |
49 | // Event pages stored in the database always have a string wiki ID, so we need to check if they're |
50 | // actually local. |
51 | $adjustedWikiID = WikiMap::isCurrentWikiId( $wikiID ) ? WikiAwareEntity::LOCAL : $wikiID; |
52 | $pageStore = $this->pageStoreFactory->getPageStore( $adjustedWikiID ); |
53 | $page = $pageStore->getPageByName( $namespace, $dbKey ); |
54 | if ( !$page ) { |
55 | // The page does not exist; this can happen e.g. if the event page was deleted. |
56 | $page = new PageIdentityValue( 0, $namespace, $dbKey, $adjustedWikiID ); |
57 | } |
58 | return new MWPageProxy( $page, $prefixedText ); |
59 | } |
60 | |
61 | /** |
62 | * @param string $titleStr |
63 | * @return ICampaignsPage |
64 | * @throws InvalidTitleStringException |
65 | * @throws UnexpectedInterwikiException If the page title has an interwiki prefix |
66 | * @throws UnexpectedVirtualNamespaceException |
67 | * @throws UnexpectedSectionAnchorException |
68 | * @throws PageNotFoundException If the page does not exist |
69 | */ |
70 | public function newLocalExistingPageFromString( string $titleStr ): ICampaignsPage { |
71 | // This is similar to PageStore::getPageByText, but with better error handling |
72 | // and it also requires that the page exists. |
73 | try { |
74 | $pageTitle = $this->titleParser->parseTitle( $titleStr ); |
75 | } catch ( MalformedTitleException $e ) { |
76 | throw new InvalidTitleStringException( $titleStr, $e->getErrorMessage(), $e->getErrorMessageParameters() ); |
77 | } |
78 | |
79 | if ( $pageTitle->isExternal() ) { |
80 | throw new UnexpectedInterwikiException( $pageTitle->getInterwiki() ); |
81 | } |
82 | |
83 | $namespace = $pageTitle->getNamespace(); |
84 | if ( $namespace < 0 ) { |
85 | throw new UnexpectedVirtualNamespaceException( $namespace ); |
86 | } |
87 | |
88 | if ( $pageTitle->hasFragment() ) { |
89 | throw new UnexpectedSectionAnchorException( $pageTitle->getFragment() ); |
90 | } |
91 | |
92 | $dbKey = $pageTitle->getDBkey(); |
93 | $page = $this->pageStoreFactory->getPageStore()->getPageByName( $namespace, $dbKey ); |
94 | if ( !$page ) { |
95 | throw new PageNotFoundException( $namespace, $dbKey, WikiAwareEntity::LOCAL ); |
96 | } |
97 | return new MWPageProxy( $page, $this->titleFormatter->getPrefixedText( $pageTitle ) ); |
98 | } |
99 | |
100 | /** |
101 | * Convert a MW page interface (LinkTarget or ProperPageIdentity) into an ICampaignsPage, without |
102 | * further checks (e.g. existence). |
103 | * |
104 | * @param PageIdentity|LinkTarget $page Must be a page in the local wiki |
105 | * @return ICampaignsPage |
106 | */ |
107 | public function newFromLocalMediaWikiPage( $page ): ICampaignsPage { |
108 | $page->assertWiki( WikiAwareEntity::LOCAL ); |
109 | if ( $page instanceof LinkTarget ) { |
110 | $page = $this->pageStoreFactory->getPageStore()->getPageForLink( $page ); |
111 | } |
112 | return new MWPageProxy( $page, $this->titleFormatter->getPrefixedText( $page ) ); |
113 | } |
114 | } |