Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
28 / 28 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
PageEntitySerializer | |
100.00% |
28 / 28 |
|
100.00% |
5 / 5 |
11 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
toArray | |
100.00% |
21 / 21 |
|
100.00% |
1 / 1 |
7 | |||
formatLinkTarget | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
formatWikiPageTitle | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
canonicalPageURL | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
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 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 | * http://www.gnu.org/copyleft/gpl.html |
17 | * |
18 | * @file |
19 | * @author Andrew Otto <otto@wikimedia.org> |
20 | */ |
21 | namespace MediaWiki\Extension\EventBus\Serializers\MediaWiki; |
22 | |
23 | use MediaWiki\Config\Config; |
24 | use MediaWiki\Extension\EventBus\Redirects\RedirectTarget; |
25 | use MediaWiki\Linker\LinkTarget; |
26 | use MediaWiki\Page\PageRecord; |
27 | use MediaWiki\Title\TitleFormatter; |
28 | use WikiPage; |
29 | |
30 | /** |
31 | * Converts a WikiPage to an array matching the fragment/mediawiki/state/entity/page schema |
32 | */ |
33 | class PageEntitySerializer { |
34 | /** |
35 | * @var TitleFormatter |
36 | */ |
37 | private TitleFormatter $titleFormatter; |
38 | /** |
39 | * @var Config |
40 | */ |
41 | private Config $mainConfig; |
42 | |
43 | /** |
44 | * @param Config $mainConfig |
45 | * @param TitleFormatter $titleFormatter |
46 | */ |
47 | public function __construct( |
48 | Config $mainConfig, |
49 | TitleFormatter $titleFormatter |
50 | ) { |
51 | $this->mainConfig = $mainConfig; |
52 | $this->titleFormatter = $titleFormatter; |
53 | } |
54 | |
55 | /** |
56 | * @param WikiPage $wikiPage |
57 | * @param RedirectTarget|null $redirectTarget will only be encoded if not null and pointing to an existing page |
58 | * @return array |
59 | */ |
60 | public function toArray( WikiPage $wikiPage, ?RedirectTarget $redirectTarget = null ): array { |
61 | $serialized = [ |
62 | 'page_id' => $wikiPage->getId(), |
63 | 'page_title' => $this->formatWikiPageTitle( $wikiPage ), |
64 | 'namespace_id' => $wikiPage->getNamespace(), |
65 | 'is_redirect' => $wikiPage->isRedirect(), |
66 | ]; |
67 | if ( $redirectTarget != null ) { |
68 | $redirectLinkTarget = $redirectTarget->getLink(); |
69 | $redirect = [ |
70 | 'page_title' => $this->formatLinkTarget( $redirectLinkTarget ), |
71 | 'namespace_id' => $redirectLinkTarget->getNamespace(), |
72 | ]; |
73 | $redirectPageIdentity = $redirectTarget->getPage(); |
74 | if ( $redirectPageIdentity != null && $redirectPageIdentity->exists() ) { |
75 | $redirect['page_id'] = $redirectPageIdentity->getId(); |
76 | if ( $redirectPageIdentity instanceof PageRecord ) { |
77 | $redirect['is_redirect'] = $redirectPageIdentity->isRedirect(); |
78 | } |
79 | } |
80 | if ( $redirectLinkTarget->getInterwiki() != null && strlen( $redirectLinkTarget->getInterwiki() ) > 0 ) { |
81 | $redirect['interwiki_prefix'] = $redirectLinkTarget->getInterwiki(); |
82 | } |
83 | $serialized['redirect_page_link'] = $redirect; |
84 | } |
85 | return $serialized; |
86 | } |
87 | |
88 | /** |
89 | * Helper function to return a formatted LinkTarget (Title) |
90 | * @param LinkTarget $linkTarget |
91 | * @return string |
92 | */ |
93 | public function formatLinkTarget( LinkTarget $linkTarget ): string { |
94 | return $this->titleFormatter->getPrefixedDBkey( $linkTarget ); |
95 | } |
96 | |
97 | /** |
98 | * Helper function to return a formatted page_title |
99 | * @param WikiPage $wikiPage |
100 | * @return string |
101 | */ |
102 | public function formatWikiPageTitle( WikiPage $wikiPage ): string { |
103 | return $this->formatLinkTarget( $wikiPage->getTitle() ); |
104 | } |
105 | |
106 | /** |
107 | * Helper function that creates a full page path URL |
108 | * for the $wikiPage using CanonicalServer and ArticalPath from mainConfig. |
109 | * |
110 | * @param WikiPage $wikiPage |
111 | * @return string |
112 | */ |
113 | public function canonicalPageURL( WikiPage $wikiPage ): string { |
114 | $titleURL = wfUrlencode( $this->formatWikiPageTitle( $wikiPage ) ); |
115 | // The ArticlePath contains '$1' string where the article title should appear. |
116 | return $this->mainConfig->get( 'CanonicalServer' ) . |
117 | str_replace( '$1', $titleURL, $this->mainConfig->get( 'ArticlePath' ) ); |
118 | } |
119 | } |