Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 61
0.00% covered (danger)
0.00%
0 / 16
CRAP
0.00% covered (danger)
0.00%
0 / 1
PageMoveCollection
0.00% covered (danger)
0.00%
0 / 61
0.00% covered (danger)
0.00%
0 / 16
650
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
2
 getTranslatablePage
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getTranslationPagesPair
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getUnitPagesPair
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getSubpagesPair
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getTranslatableSubpages
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getTranslationPages
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getUnitPages
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getSubpages
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getListOfPages
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
6
 getListOfPagesToRedirect
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
2
 getNonMovableSubpages
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getOldPagesFromList
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 getPagePairFromList
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
 getLeaveRedirectPairFromList
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
20
 getTalkpages
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\PageTranslation;
5
6use MediaWiki\Status\Status;
7use MediaWiki\Title\Title;
8
9/**
10 * Collection of pages potentially affected by a page move operation.
11 * @author Abijeet Patro
12 * @license GPL-2.0-or-later
13 * @since 2021.09
14 */
15class PageMoveCollection {
16    private PageMoveOperation $translatablePage;
17    /** @var PageMoveOperation[] */
18    private array $translationPagePairs;
19    /** @var PageMoveOperation[] */
20    private array $unitPagesPairs;
21    /** @var PageMoveOperation[] */
22    private array $subpagesPairs;
23    /** @var PageMoveOperation[] */
24    private array $talkpagesPairs;
25    /** @var Title[] */
26    private array $translatableSubpages;
27    /** @var array<string,Status> */
28    private array $nonMovableSubpages;
29
30    /**
31     * @param PageMoveOperation $translatablePage
32     * @param PageMoveOperation[] $translationPagePairs Translation pages
33     * @param PageMoveOperation[] $unitPagesPairs Translation unit pages
34     * @param PageMoveOperation[] $subpagesPairs Non-translatable subpages
35     * @param array<string,Status> $nonMovableSubpages Subpages that are not movable
36     * @param Title[] $translatableSubpages
37     */
38    public function __construct(
39        PageMoveOperation $translatablePage,
40        array $translationPagePairs,
41        array $unitPagesPairs,
42        array $subpagesPairs,
43        array $nonMovableSubpages,
44        array $translatableSubpages
45    ) {
46        $this->translatablePage = $translatablePage;
47        $this->translationPagePairs = $translationPagePairs;
48        $this->unitPagesPairs = $unitPagesPairs;
49        $this->subpagesPairs = $subpagesPairs;
50        $this->translatableSubpages = $translatableSubpages;
51
52        // Populate the talk pages from the various inputs.
53        $this->talkpagesPairs = $this->getTalkpages(
54            $this->translatablePage, ...$translationPagePairs, ...$unitPagesPairs, ...$subpagesPairs
55        );
56
57        $this->nonMovableSubpages = $nonMovableSubpages;
58    }
59
60    public function getTranslatablePage(): PageMoveOperation {
61        return $this->translatablePage;
62    }
63
64    /** @return PageMoveOperation[] */
65    public function getTranslationPagesPair(): array {
66        return $this->translationPagePairs;
67    }
68
69    /** @return PageMoveOperation[] */
70    public function getUnitPagesPair(): array {
71        return $this->unitPagesPairs;
72    }
73
74    /** @return PageMoveOperation[] */
75    public function getSubpagesPair(): array {
76        return $this->subpagesPairs;
77    }
78
79    /** @return Title[] */
80    public function getTranslatableSubpages(): array {
81        return $this->translatableSubpages;
82    }
83
84    /** @return Title[] */
85    public function getTranslationPages(): array {
86        return $this->getOldPagesFromList( $this->translationPagePairs );
87    }
88
89    /** @return Title[] */
90    public function getUnitPages(): array {
91        return $this->getOldPagesFromList( $this->unitPagesPairs );
92    }
93
94    /** @return Title[] */
95    public function getSubpages(): array {
96        return $this->getOldPagesFromList( $this->subpagesPairs );
97    }
98
99    /** @return string[] */
100    public function getListOfPages(): array {
101        $pageList = [
102            $this->translatablePage->getOldTitle()->getPrefixedText() =>
103                $this->translatablePage->getNewTitle() ?
104                    $this->translatablePage->getNewTitle()->getPrefixedText() : null
105        ];
106        $pageList = array_merge( $pageList, $this->getPagePairFromList( $this->translationPagePairs ) );
107        $pageList = array_merge( $pageList, $this->getPagePairFromList( $this->unitPagesPairs ) );
108        $pageList = array_merge( $pageList, $this->getPagePairFromList( $this->subpagesPairs ) );
109        $pageList = array_merge( $pageList, $this->getPagePairFromList( $this->talkpagesPairs ) );
110
111        return $pageList;
112    }
113
114    /** @return array<string,bool> */
115    public function getListOfPagesToRedirect(): array {
116        $redirects = [
117            $this->translatablePage->getOldTitle()->getPrefixedText() =>
118                    $this->translatablePage->shouldLeaveRedirect()
119        ];
120
121        $redirects = array_merge( $redirects, $this->getLeaveRedirectPairFromList( $this->translationPagePairs ) );
122        $redirects = array_merge( $redirects, $this->getLeaveRedirectPairFromList( $this->unitPagesPairs ) );
123        $redirects = array_merge( $redirects, $this->getLeaveRedirectPairFromList( $this->subpagesPairs ) );
124        $redirects = array_merge( $redirects, $this->getLeaveRedirectPairFromList( $this->talkpagesPairs ) );
125
126        return $redirects;
127    }
128
129    /**
130     * Get list of subpages which cannot be moved for various reasons
131     * (e.g. the target page already exists). Those do not include translatable
132     * subpages which cannot be moved because of current limitation.
133     */
134    public function getNonMovableSubpages(): array {
135        return $this->nonMovableSubpages;
136    }
137
138    /**
139     * @param PageMoveOperation[] $pagePairs
140     * @return Title[]
141     */
142    private function getOldPagesFromList( array $pagePairs ): array {
143        $oldTitles = [];
144        foreach ( $pagePairs as $pair ) {
145            $oldTitles[] = $pair->getOldTitle();
146        }
147
148        return $oldTitles;
149    }
150
151    /** @return string[] */
152    private function getPagePairFromList( array $pagePairs ): array {
153        $pairs = [];
154        foreach ( $pagePairs as $pair ) {
155            $pairs[ $pair->getOldTitle()->getPrefixedText() ] =
156                $pair->getNewTitle() ? $pair->getNewTitle()->getPrefixedText() : null;
157        }
158
159        return $pairs;
160    }
161
162    /** @return array<string,bool> */
163    private function getLeaveRedirectPairFromList( array $pagePairs ): array {
164        $pairs = [];
165        foreach ( $pagePairs as $pair ) {
166            if ( $pair->shouldLeaveRedirect() ) {
167                $pairs[ $pair->getOldTitle()->getPrefixedText() ] = true;
168                $talkpage = $pair->getOldTalkPage();
169                if ( $talkpage ) {
170                    $pairs[ $talkpage->getPrefixedText() ] = true;
171                }
172            }
173        }
174
175        return $pairs;
176    }
177
178    /** @return PageMoveOperation[] */
179    private function getTalkpages( PageMoveOperation ...$allMoveOperations ): array {
180        $talkpagesPairs = [];
181        foreach ( $allMoveOperations as $moveOperation ) {
182            if ( $moveOperation->hasTalkpage() ) {
183                $talkpagesPairs[] = new PageMoveOperation(
184                    $moveOperation->getOldTalkpage(), $moveOperation->getNewTalkpage()
185                );
186            }
187        }
188        return $talkpagesPairs;
189    }
190}