Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
SubpageListBuilder.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\MessageGroupProcessing;
5
6use MediaWiki\Cache\LinkBatchFactory;
7use Title;
9
19 private $bundleFactory;
21 private $linkBatchFactory;
22
23 public function __construct(
24 TranslatableBundleFactory $bundleFactory,
25 LinkBatchFactory $linkBatchFactory
26 ) {
27 $this->bundleFactory = $bundleFactory;
28 $this->linkBatchFactory = $linkBatchFactory;
29 }
30
31 public function getSubpagesPerType( TranslatableBundle $bundle, bool $fetchTalkPages ): array {
32 $classifiedSubPages = $this->getEmptyResultSet();
33
34 $classifiedSubPages['translationPages'] = $bundle->getTranslationPages();
35 $classifiedSubPages['translationUnitPages'] = $bundle->getTranslationUnitPages();
36
37 // It's possible that subpages may not be allowed and getSubpages will return an
38 // empty array but that's not a problem.
39 $allSubpages = $bundle->getTitle()->getSubpages();
40
41 // Index the subpages
42 $allSubpagesIndexed = [];
43 foreach ( $allSubpages as $page ) {
44 $allSubpagesIndexed[ $page->getPrefixedDBkey() ] = $page;
45 }
46
47 // Remove translation pages from subpages
48 foreach ( $classifiedSubPages[ 'translationPages' ] as $translationPage ) {
49 if ( isset( $allSubpagesIndexed[ $translationPage->getPrefixedDBkey() ] ) ) {
50 unset( $allSubpagesIndexed[ $translationPage->getPrefixedDBkey() ] );
51 }
52 }
53
54 // Remove subpages that are translatable bundles
55 foreach ( $allSubpagesIndexed as $index => $subpage ) {
56 if ( $this->bundleFactory->getBundle( $subpage ) ) {
57 $classifiedSubPages['translatableSubpages'][] = $subpage;
58 unset( $allSubpagesIndexed[$index] );
59 }
60 }
61
62 // Remove translation pages for translatable pages found
63 $allSubpagesIndexed = $this->filterOtherTranslationPages(
64 $allSubpagesIndexed, $classifiedSubPages['translatableSubpages']
65 );
66
67 $classifiedSubPages['normalSubpages'] = $allSubpagesIndexed;
68
69 if ( $fetchTalkPages && !$bundle->getTitle()->isTalkPage() ) {
70 // We don't fetch talk pages for translatable subpages
71 $talkPages = $this->getTalkPages(
72 array_merge(
73 [ $bundle->getTitle() ],
74 $classifiedSubPages['translationPages'],
75 $classifiedSubPages['translationUnitPages'],
76 $classifiedSubPages['normalSubpages']
77 )
78 );
79
80 $translatableTalkPages = [];
81 foreach ( $talkPages as $key => $talkPage ) {
82 if ( $talkPage === null ) {
83 continue;
84 }
85
86 if ( $this->bundleFactory->getBundle( $talkPage ) ) {
87 $translatableTalkPages[] = $talkPage;
88 unset( $talkPages[$key] );
89 }
90 }
91
92 $classifiedSubPages['talkPages'] = $talkPages;
93 $classifiedSubPages['translatableTalkPages'] = $translatableTalkPages;
94 }
95
96 return $classifiedSubPages;
97 }
98
99 public function getEmptyResultSet(): array {
100 return [
101 'translationPages' => [],
102 'translatableSubpages' => [],
103 'translationUnitPages' => [],
104 'normalSubpages' => [],
105 'talkPages' => [],
106 'translatableTalkPages' => []
107 ];
108 }
109
115 private function filterOtherTranslationPages( array $allPages, array $translatablePages ): array {
116 $mappedTranslatablePages = [];
117 foreach ( $translatablePages as $index => $page ) {
118 $mappedTranslatablePages[ $page->getText() ] = $index;
119 }
120
121 foreach ( $allPages as $prefixedDbKeyTitle => $subpage ) {
122 [ $key, ] = TranslateUtils::figureMessage( $subpage->getText() );
123 if ( isset( $mappedTranslatablePages[ $key ] ) ) {
124 unset( $allPages[ $prefixedDbKeyTitle ] );
125 }
126 }
127
128 return $allPages;
129 }
130
138 private function getTalkPages( array $pages ): array {
139 $lb = $this->linkBatchFactory->newLinkBatch();
140 $talkPageList = [];
141
142 foreach ( $pages as $page ) {
143 $talkPage = $page->getTalkPageIfDefined();
144 $talkPageList[ $page->getPrefixedDBkey() ] = $talkPage;
145 if ( $talkPage ) {
146 $lb->addObj( $talkPage );
147 }
148 }
149
150 $lb->setCaller( __METHOD__ )->execute();
151 foreach ( $talkPageList as $index => $talkPage ) {
152 if ( !$talkPage || !$talkPage->exists() ) {
153 $talkPageList[$index] = null;
154 }
155 }
156
157 return $talkPageList;
158 }
159}
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.
Essentially random collection of helper functions, similar to GlobalFunctions.php.