1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* File containing the ParameterProvider class |
4
|
|
|
* |
5
|
|
|
* @copyright (C) 2016 - 2017, Stephan Gambke |
6
|
|
|
* @license GNU General Public License, version 2 (or any later version) |
7
|
|
|
* |
8
|
|
|
* This software is free software; you can redistribute it and/or |
9
|
|
|
* modify it under the terms of the GNU General Public License |
10
|
|
|
* as published by the Free Software Foundation; either version 2 |
11
|
|
|
* of the License, or (at your option) any later version. |
12
|
|
|
* |
13
|
|
|
* This software is distributed in the hope that it will be useful, |
14
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16
|
|
|
* GNU General Public License for more details. |
17
|
|
|
* |
18
|
|
|
* You should have received a copy of the GNU General Public License |
19
|
|
|
* along with this program; if not, see <http://www.gnu.org/licenses/>. |
20
|
|
|
* |
21
|
|
|
* @file |
22
|
|
|
* @ingroup SimpleBatchUpload |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
namespace SimpleBatchUpload; |
26
|
|
|
|
27
|
|
|
use Message; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Class ParameterProvider |
31
|
|
|
* |
32
|
|
|
* @package SimpleBatchUpload |
33
|
|
|
*/ |
34
|
|
|
class ParameterProvider { |
35
|
|
|
|
36
|
|
|
const IDX_TEMPLATENAME = 0; |
37
|
|
|
const IDX_TEMPLATEPARAMETERS = 1; |
38
|
|
|
const IDX_COMMENT = 2; |
39
|
|
|
const IDX_SPECIALPAGETITLE = 3; |
40
|
|
|
|
41
|
|
|
private $templateName; |
42
|
|
|
private $parameters = null; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param string|null $templateName |
46
|
|
|
*/ |
47
|
|
|
public function __construct( $templateName ) { |
48
|
|
|
$this->templateName = $templateName ? $templateName : ''; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function getUploadPageText(): string { |
52
|
|
|
|
53
|
|
|
if ( $this->templateName === '' ) { |
54
|
|
|
return ''; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return '{{' . $this->getParameter( self::IDX_TEMPLATENAME ) . $this->getParameter( self::IDX_TEMPLATEPARAMETERS ) . '}}'; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
private function getEscapedParameter( int $key ): string { |
61
|
|
|
return $this->escape( $this->getParameter( $key ) ); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
private function escape( string $text ): string { |
65
|
|
|
return htmlspecialchars( $text, ENT_QUOTES, 'UTF-8', false ); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
private function getParameter( int $key ): string { |
69
|
|
|
if ( $this->parameters === null ) { |
70
|
|
|
$this->populateParameters(); |
71
|
|
|
} |
72
|
|
|
return $this->parameters[ $key ]; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
private function populateParameters() { |
76
|
|
|
if ( $this->templateName === '' || $this->populateParametersFromKey() === false ) { |
77
|
|
|
$this->populateParametersFromDefaults(); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
private function populateParametersFromKey() { |
82
|
|
|
$paramMsg = Message::newFromKey( 'simplebatchupload-parameters' ); |
83
|
|
|
|
84
|
|
|
if ( $paramMsg->exists() ) { |
85
|
|
|
|
86
|
|
|
$paramLines = explode( "\n", $paramMsg->plain() ); |
87
|
|
|
$paramSet = array_map( [ $this, 'parseParamLine' ], $paramLines ); |
88
|
|
|
$paramMap = array_combine( array_column( $paramSet, 0 ), $paramSet ); |
89
|
|
|
|
90
|
|
|
if ( array_key_exists( $this->templateName, $paramMap ) ) { |
91
|
|
|
$this->setParameters( $this->templateName, '', $paramMap[ $this->templateName ][ 1 ], $paramMap[ $this->templateName ][ 2 ] ); |
92
|
|
|
return true; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
return false; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
private function populateParametersFromDefaults() { |
99
|
|
|
$this->setParameters( $this->templateName, '', Message::newFromKey( 'simplebatchupload-comment' )->text(), Message::newFromKey( 'batchupload' )->text() ); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param string $templateName |
104
|
|
|
* @param string $templateParameters |
105
|
|
|
* @param string $uploadComment |
106
|
|
|
* @param string $specialPageTitle |
107
|
|
|
*/ |
108
|
|
|
private function setParameters( $templateName, $templateParameters, $uploadComment, $specialPageTitle ) { |
109
|
|
|
$this->parameters = [ |
110
|
|
|
self::IDX_TEMPLATENAME => $templateName, |
111
|
|
|
self::IDX_TEMPLATEPARAMETERS => $templateParameters, |
112
|
|
|
self::IDX_COMMENT => $uploadComment, |
113
|
|
|
self::IDX_SPECIALPAGETITLE => $specialPageTitle, |
114
|
|
|
]; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function getEscapedUploadComment(): string { |
118
|
|
|
return $this->getEscapedParameter( self::IDX_COMMENT ); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function getSpecialPageTitle(): string { |
122
|
|
|
return $this->getParameter( self::IDX_SPECIALPAGETITLE ); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function addTemplateParameter( string $parameter ) { |
126
|
|
|
|
127
|
|
|
if ( $this->parameters === null ) { |
128
|
|
|
$this->populateParameters(); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$this->parameters[ self::IDX_TEMPLATEPARAMETERS ] .= '|' . $parameter; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param string $paramLine |
136
|
|
|
* @return string[] |
137
|
|
|
*/ |
138
|
|
|
private function parseParamLine( string $paramLine ): array { |
139
|
|
|
return array_replace( [ '', '', '' ], array_map( 'trim', explode( '|', $paramLine, 3 ) ) ); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
} |
143
|
|
|
|