1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SubPageList\Lister; |
4
|
|
|
|
5
|
|
|
use LogicException; |
6
|
|
|
use ParamProcessor\ProcessedParam; |
7
|
|
|
use ParamProcessor\ProcessingResult; |
8
|
|
|
use Parser; |
9
|
|
|
use ParserHooks\HookHandler; |
10
|
|
|
use SubPageList\TitleFactory; |
11
|
|
|
use SubPageList\Lister\UI\SubPageListRenderer; |
12
|
|
|
use Title; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Handler for the subpagelist parser hook. |
16
|
|
|
* |
17
|
|
|
* @since 1.2 |
18
|
|
|
* |
19
|
|
|
* @licence GNU GPL v2+ |
20
|
|
|
* @author Jeroen De Dauw < [email protected] > |
21
|
|
|
*/ |
22
|
|
|
class SubPageList implements HookHandler { |
23
|
|
|
|
24
|
|
|
private $subPageFinder; |
25
|
|
|
private $pageHierarchyCreator; |
26
|
|
|
private $subPageListRenderer; |
27
|
|
|
private $titleFactory; |
28
|
|
|
|
29
|
19 |
|
public function __construct( SubPageFinder $finder, PageHierarchyCreator $hierarchyCreator, |
30
|
|
|
SubPageListRenderer $renderer, TitleFactory $titleFactory ) { |
31
|
|
|
|
32
|
19 |
|
$this->subPageFinder = $finder; |
33
|
19 |
|
$this->pageHierarchyCreator = $hierarchyCreator; |
34
|
19 |
|
$this->subPageListRenderer = $renderer; |
35
|
19 |
|
$this->titleFactory = $titleFactory; |
36
|
19 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @see HookHandler::handle |
40
|
|
|
* |
41
|
|
|
* @since 1.2 |
42
|
|
|
* |
43
|
|
|
* @param Parser $parser |
44
|
|
|
* @param ProcessingResult $result |
45
|
|
|
* |
46
|
|
|
* @return string |
47
|
|
|
*/ |
48
|
19 |
|
public function handle( Parser $parser, ProcessingResult $result ) { |
49
|
19 |
|
if ( $result->hasFatal() ) { |
50
|
|
|
// This should not occur given the current parameter definitions. |
51
|
|
|
return 'Error: invalid input into subPageList function'; |
52
|
|
|
} |
53
|
|
|
|
54
|
19 |
|
$parameters = $this->paramsToOptions( $result->getParameters() ); |
55
|
|
|
|
56
|
19 |
|
$title = $this->getTitle( $parser, $parameters['page'] ); |
57
|
|
|
|
58
|
19 |
|
if ( $title !== null ) { |
59
|
18 |
|
return $this->renderForTitle( $title, $parameters ); |
60
|
|
|
} |
61
|
|
|
|
62
|
1 |
|
return 'Error: invalid title provided'; // TODO (might want to use a title param...) |
63
|
|
|
} |
64
|
|
|
|
65
|
19 |
|
private function getTitle( Parser $parser, $pageName ) { |
66
|
19 |
|
if ( $pageName === '' ) { |
67
|
1 |
|
return $parser->getTitle(); |
68
|
|
|
} |
69
|
|
|
else { |
70
|
18 |
|
return $this->titleFactory->newFromText( $pageName ); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param Title $title |
76
|
|
|
* @param array $parameters |
77
|
|
|
* |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
18 |
|
private function renderForTitle( Title $title, array $parameters ) { |
81
|
18 |
|
$topLevelPage = $this->getPageHierarchy( $title, $parameters['limit'], $parameters['redirects'] ); |
82
|
|
|
|
83
|
18 |
|
if ( $this->shouldUseDefault( $topLevelPage, $parameters['showpage'] ) ) { |
84
|
3 |
|
return $this->getDefault( $parameters['page'], $parameters['default'] ); |
85
|
|
|
} |
86
|
|
|
else { |
87
|
15 |
|
return $this->getRenderedList( $topLevelPage, $parameters ); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param Title $title |
93
|
|
|
* @param int $limit |
94
|
|
|
* @param bool $includeRedirects |
95
|
|
|
* |
96
|
|
|
* @return Page |
97
|
|
|
* @throws LogicException |
98
|
|
|
*/ |
99
|
18 |
|
private function getPageHierarchy( Title $title, $limit, $includeRedirects ) { |
100
|
18 |
|
$this->subPageFinder->setLimit( $limit ); |
101
|
18 |
|
$this->subPageFinder->setIncludeRedirects( $includeRedirects ); |
102
|
|
|
|
103
|
18 |
|
$subPageTitles = $this->subPageFinder->getSubPagesFor( $title ); |
104
|
18 |
|
$subPageTitles[] = $title; |
105
|
|
|
|
106
|
18 |
|
$pageHierarchy = $this->pageHierarchyCreator->createHierarchy( $subPageTitles ); |
107
|
|
|
|
108
|
18 |
|
if ( count( $pageHierarchy ) !== 1 ) { |
109
|
|
|
throw new LogicException( 'Expected exactly one top level page' ); |
110
|
|
|
} |
111
|
|
|
|
112
|
18 |
|
$topLevelPage = reset( $pageHierarchy ); |
|
|
|
|
113
|
18 |
|
return $topLevelPage; |
114
|
|
|
} |
115
|
|
|
|
116
|
18 |
|
private function shouldUseDefault( Page $topLevelPage, $showTopLevelPage ) { |
117
|
|
|
// Note: this behaviour is not fully correct. |
118
|
|
|
// Other parameters that omit results need to be held into account as well. |
119
|
18 |
|
return !$showTopLevelPage && $topLevelPage->getSubPages() === []; |
120
|
|
|
} |
121
|
|
|
|
122
|
15 |
|
private function getRenderedList( Page $topLevelPage, $parameters ) { |
123
|
15 |
|
return $this->subPageListRenderer->render( |
124
|
15 |
|
$topLevelPage, |
125
|
|
|
$parameters |
126
|
15 |
|
); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param string $titleText |
131
|
|
|
* @param string $default |
132
|
|
|
* |
133
|
|
|
* @return string |
134
|
|
|
*/ |
135
|
3 |
|
private function getDefault( $titleText, $default ) { |
136
|
3 |
|
if ( $default === '' ) { |
137
|
1 |
|
return "\"$titleText\" has no sub pages."; // TODO |
138
|
|
|
} |
139
|
|
|
|
140
|
2 |
|
if ( $default === '-' ) { |
141
|
1 |
|
return ''; |
142
|
|
|
} |
143
|
|
|
|
144
|
1 |
|
return $default; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param ProcessedParam[] $parameters |
149
|
|
|
* |
150
|
|
|
* @return array |
151
|
|
|
*/ |
152
|
19 |
|
private function paramsToOptions( array $parameters ) { |
153
|
19 |
|
$options = []; |
154
|
|
|
|
155
|
19 |
|
foreach ( $parameters as $parameter ) { |
156
|
19 |
|
$options[$parameter->getName()] = $parameter->getValue(); |
157
|
19 |
|
} |
158
|
|
|
|
159
|
19 |
|
return $options; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
} |
163
|
|
|
|