Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
SubpageListBuilder.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\MessageGroupProcessing;
5
7use MediaWiki\Page\LinkBatchFactory;
8use MediaWiki\Title\Title;
9
18
19 public function __construct(
20 private readonly TranslatableBundleFactory $bundleFactory,
21 private readonly LinkBatchFactory $linkBatchFactory,
22 ) {
23 }
24
25 public function getSubpagesPerType( TranslatableBundle $bundle, bool $fetchTalkPages ): array {
26 $classifiedSubPages = $this->getEmptyResultSet();
27
28 $classifiedSubPages['translationPages'] = $bundle->getTranslationPages();
29 $classifiedSubPages['translationUnitPages'] = $bundle->getTranslationUnitPages();
30
31 // It's possible that subpages may not be allowed and getSubpages will return an
32 // empty array but that's not a problem.
33 $allSubpages = $bundle->getTitle()->getSubpages();
34
35 // Index the subpages
36 $allSubpagesIndexed = [];
37 foreach ( $allSubpages as $page ) {
38 $allSubpagesIndexed[ $page->getPrefixedDBkey() ] = $page;
39 }
40
41 // Remove translation pages from subpages
42 foreach ( $classifiedSubPages[ 'translationPages' ] as $translationPage ) {
43 if ( isset( $allSubpagesIndexed[ $translationPage->getPrefixedDBkey() ] ) ) {
44 unset( $allSubpagesIndexed[ $translationPage->getPrefixedDBkey() ] );
45 }
46 }
47
48 // Remove subpages that are translatable bundles
49 foreach ( $allSubpagesIndexed as $index => $subpage ) {
50 if ( $this->bundleFactory->getBundle( $subpage ) ) {
51 $classifiedSubPages['translatableSubpages'][] = $subpage;
52 unset( $allSubpagesIndexed[$index] );
53 }
54 }
55
56 // Remove translation pages for translatable pages found
57 $allSubpagesIndexed = $this->filterOtherTranslationPages(
58 $allSubpagesIndexed, $classifiedSubPages['translatableSubpages']
59 );
60
61 $classifiedSubPages['normalSubpages'] = $allSubpagesIndexed;
62
63 if ( $fetchTalkPages && !$bundle->getTitle()->isTalkPage() ) {
64 // We don't fetch talk pages for translatable subpages
65 $talkPages = $this->getTalkPages(
66 array_merge(
67 [ $bundle->getTitle() ],
68 $classifiedSubPages['translationPages'],
69 $classifiedSubPages['translationUnitPages'],
70 $classifiedSubPages['normalSubpages']
71 )
72 );
73
74 $translatableTalkPages = [];
75 foreach ( $talkPages as $key => $talkPage ) {
76 if ( $talkPage === null ) {
77 continue;
78 }
79
80 if ( $this->bundleFactory->getBundle( $talkPage ) ) {
81 $translatableTalkPages[] = $talkPage;
82 unset( $talkPages[$key] );
83 }
84 }
85
86 $classifiedSubPages['talkPages'] = $talkPages;
87 $classifiedSubPages['translatableTalkPages'] = $translatableTalkPages;
88 }
89
90 return $classifiedSubPages;
91 }
92
93 public function getEmptyResultSet(): array {
94 return [
95 'translationPages' => [],
96 'translatableSubpages' => [],
97 'translationUnitPages' => [],
98 'normalSubpages' => [],
99 'talkPages' => [],
100 'translatableTalkPages' => []
101 ];
102 }
103
109 private function filterOtherTranslationPages( array $allPages, array $translatablePages ): array {
110 $mappedTranslatablePages = [];
111 foreach ( $translatablePages as $index => $page ) {
112 $mappedTranslatablePages[ $page->getText() ] = $index;
113 }
114
115 foreach ( $allPages as $prefixedDbKeyTitle => $subpage ) {
116 [ $key, ] = Utilities::figureMessage( $subpage->getText() );
117 if ( isset( $mappedTranslatablePages[ $key ] ) ) {
118 unset( $allPages[ $prefixedDbKeyTitle ] );
119 }
120 }
121
122 return $allPages;
123 }
124
132 private function getTalkPages( array $pages ): array {
133 $lb = $this->linkBatchFactory->newLinkBatch();
134 $talkPageList = [];
135
136 foreach ( $pages as $page ) {
137 $talkPage = $page->getTalkPageIfDefined();
138 $talkPageList[ $page->getPrefixedDBkey() ] = $talkPage;
139 if ( $talkPage ) {
140 $lb->addObj( $talkPage );
141 }
142 }
143
144 $lb->setCaller( __METHOD__ )->execute();
145 foreach ( $talkPageList as $index => $talkPage ) {
146 if ( !$talkPage || !$talkPage->exists() ) {
147 $talkPageList[$index] = null;
148 }
149 }
150
151 return $talkPageList;
152 }
153}
Generates list of subpages for the translatable bundle that can be moved or deleted.
Create instances of various classes based on the type of TranslatableBundle.
Translatable bundle represents a message group where its translatable content is defined on a wiki pa...
getTitle()
Return the title of the page where the translatable bundle is defined.
getTranslationPages()
Return the available translation pages for the bundle.
getTranslationUnitPages(?string $code=null)
Return the available translation units for the bundle.
figureMessage()
Recommended to use getCode and getKey instead.
Essentially random collection of helper functions, similar to GlobalFunctions.php.
Definition Utilities.php:29