1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SubPageList\Lister\UI; |
4
|
|
|
|
5
|
|
|
use Html; |
6
|
|
|
use RuntimeException; |
7
|
|
|
use SubPageList\Lister\Page; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @since 1.2 |
11
|
|
|
* |
12
|
|
|
* @licence GNU GPL v2+ |
13
|
|
|
* @author Jeroen De Dauw < [email protected] > |
14
|
|
|
*/ |
15
|
|
|
class WikitextSubPageListRenderer implements SubPageListRenderer { |
16
|
|
|
|
17
|
|
|
private $options; |
18
|
|
|
private $text; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var HierarchyRendererFactory |
22
|
|
|
*/ |
23
|
|
|
private $hierarchyRendererFactory; |
24
|
|
|
|
25
|
39 |
|
public function __construct() { |
26
|
39 |
|
$this->hierarchyRendererFactory = new HierarchyRendererFactory(); |
27
|
39 |
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @see SubPageListRenderer::render |
31
|
|
|
* |
32
|
|
|
* @param Page $page |
33
|
|
|
* @param array $options |
34
|
|
|
* |
35
|
|
|
* @return string |
36
|
|
|
*/ |
37
|
31 |
|
public function render( Page $page, array $options ) { |
38
|
31 |
|
$this->options = $options; |
39
|
31 |
|
$this->text = ''; |
40
|
|
|
|
41
|
31 |
|
$this->addHeader(); |
42
|
31 |
|
$this->addPageHierarchy( $page ); |
43
|
31 |
|
$this->addFooter(); |
44
|
|
|
|
45
|
31 |
|
return $this->wrapInElement( $this->text ); |
46
|
|
|
} |
47
|
|
|
|
48
|
31 |
|
private function addHeader() { |
49
|
31 |
|
if ( $this->options['intro'] !== '' ) { |
50
|
5 |
|
$this->text .= $this->options['intro'] . "\n"; |
51
|
|
|
} |
52
|
31 |
|
} |
53
|
|
|
|
54
|
31 |
|
private function addFooter() { |
55
|
31 |
|
if ( $this->options['outro'] !== '' ) { |
56
|
4 |
|
$this->text .= "\n". $this->options['outro']; |
57
|
|
|
} |
58
|
31 |
|
} |
59
|
|
|
|
60
|
31 |
|
private function addPageHierarchy( Page $page ) { |
61
|
31 |
|
$this->text .= $this->hierarchyRendererFactory->newTreeListRenderer( $this->options )->renderHierarchy( $page ); |
62
|
31 |
|
} |
63
|
|
|
|
64
|
31 |
|
private function wrapInElement( $text ) { |
65
|
31 |
|
if ( $this->options['element'] === 'none' ) { |
66
|
1 |
|
return $text; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
// This whitelist checking is done because the first parameter of Html::element is not escaped. |
70
|
30 |
|
$this->assertElementIsAllowed(); |
71
|
|
|
|
72
|
29 |
|
return Html::element( |
73
|
29 |
|
$this->options['element'], |
74
|
|
|
[ |
75
|
29 |
|
'class' => $this->options['class'] |
76
|
|
|
], |
77
|
29 |
|
"\n" . $text . "\n" |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
30 |
|
private function assertElementIsAllowed() { |
82
|
|
|
$allowedElements = [ |
83
|
30 |
|
'p', |
84
|
|
|
'div', |
85
|
|
|
'span' |
86
|
|
|
]; |
87
|
|
|
|
88
|
30 |
|
if ( !in_array( $this->options['element'], $allowedElements ) ) { |
89
|
1 |
|
throw new RuntimeException( |
90
|
1 |
|
'Got an unsupported value for the element option: ' . $this->options['element'] |
91
|
|
|
); |
92
|
|
|
} |
93
|
29 |
|
} |
94
|
|
|
|
95
|
|
|
} |
96
|
|
|
|