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
|
|
|
* ParameterProvider constructor. |
46
|
|
|
* @param string|null $templateName |
47
|
|
|
*/ |
48
|
|
|
public function __construct( $templateName ) { |
49
|
|
|
$this->templateName = $templateName ? $templateName : ''; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
|
|
public function getEscapedUploadPageText() { |
56
|
|
|
|
57
|
|
|
if ( $this->templateName === '' ) { |
58
|
|
|
return ''; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return '{{' . $this->getEscapedParameter( self::IDX_TEMPLATENAME ) . $this->getEscapedParameter( self::IDX_TEMPLATEPARAMETERS ) . '}}'; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param int $key |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
|
|
private function getEscapedParameter( $key ) { |
69
|
|
|
return $this->escape( $this->getParameter( $key ) ); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param string $text |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
|
|
private function escape( $text ) { |
77
|
|
|
return htmlspecialchars( $text, ENT_QUOTES, 'UTF-8', false ); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param int $key |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
|
|
private function getParameter( $key ) { |
85
|
|
|
if ( $this->parameters === null ) { |
86
|
|
|
$this->populateParameters(); |
87
|
|
|
} |
88
|
|
|
return $this->parameters[ $key ]; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
private function populateParameters() { |
92
|
|
|
|
93
|
|
|
if ( $this->templateName === '' || $this->populateParametersFromKey() === false ) { |
94
|
|
|
$this->populateParametersFromDefaults(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
*/ |
101
|
|
|
private function populateParametersFromKey() { |
102
|
|
|
$paramMsg = Message::newFromKey( 'simplebatchupload-parameters' ); |
103
|
|
|
|
104
|
|
|
if ( $paramMsg->exists() ) { |
105
|
|
|
|
106
|
|
|
$paramLines = explode( "\n", $paramMsg->plain() ); |
107
|
|
|
$paramSet = array_map( [ $this, 'parseParamLine' ], $paramLines ); |
108
|
|
|
$paramMap = array_combine( array_column( $paramSet, 0 ), $paramSet ); |
109
|
|
|
|
110
|
|
|
if ( array_key_exists( $this->templateName, $paramMap ) ) { |
|
|
|
|
111
|
|
|
$this->setParameters( $this->templateName, '', $paramMap[ $this->templateName ][ 1 ], $paramMap[ $this->templateName ][ 2 ] ); |
112
|
|
|
return true; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
return false; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
private function populateParametersFromDefaults() { |
119
|
|
|
$this->setParameters( $this->templateName, '', Message::newFromKey( 'simplebatchupload-comment' )->text(), Message::newFromKey( 'batchupload' )->text() ); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param string $templateName |
124
|
|
|
* @param string $templateParameters |
125
|
|
|
* @param string $uploadComment |
126
|
|
|
* @param string $specialPageTitle |
127
|
|
|
*/ |
128
|
|
|
private function setParameters( $templateName, $templateParameters, $uploadComment, $specialPageTitle ) { |
129
|
|
|
$this->parameters = [ |
130
|
|
|
self::IDX_TEMPLATENAME => $templateName, |
131
|
|
|
self::IDX_TEMPLATEPARAMETERS => $templateParameters, |
132
|
|
|
self::IDX_COMMENT => $uploadComment, |
133
|
|
|
self::IDX_SPECIALPAGETITLE => $specialPageTitle, |
134
|
|
|
]; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @return string |
139
|
|
|
*/ |
140
|
|
|
public function getEscapedUploadComment() { |
141
|
|
|
return $this->getEscapedParameter( self::IDX_COMMENT ); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @return string |
146
|
|
|
*/ |
147
|
|
|
public function getSpecialPageTitle() { |
148
|
|
|
return $this->getParameter( self::IDX_SPECIALPAGETITLE ); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @param string $parameter |
153
|
|
|
*/ |
154
|
|
|
public function addTemplateParameter( $parameter ) { |
155
|
|
|
|
156
|
|
|
if ( $this->parameters === null ) { |
157
|
|
|
$this->populateParameters(); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
$this->parameters[ self::IDX_TEMPLATEPARAMETERS ] .= '|' . $parameter; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @param string $paramLine |
165
|
|
|
* @return string[] |
166
|
|
|
*/ |
167
|
|
|
private function parseParamLine( $paramLine ) { |
168
|
|
|
return array_replace( [ '', '', '' ], array_map( 'trim', explode( '|', $paramLine, 3 ) ) ); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
} |
172
|
|
|
|