Completed
Push — master ( 530e90...d52c01 )
by Stephan
02:28
created

ParameterProvider::escape()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * File containing the ParameterProvider class
4
 *
5
 * @copyright (C) 2016, 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
 * @ingroup SimpleBatchUpload
34
 */
35
class ParameterProvider {
36
37
	private $parameterIndex;
38
	private $parameters = null;
39
40
	public function __construct( $parameterIndex ) {
41
		$this->parameterIndex = $parameterIndex;
42
	}
43
44
	public function getEscapedUploadPageText() {
45
		return $this->getEscapedParameter( 'pagetext' );
46
	}
47
48
	public function getEscapedUploadComment() {
49
		return $this->getEscapedParameter( 'comment' );
50
	}
51
52
	public function getSpecialPageTitle() {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
53
		return $this->getParameter( 'title' );
54
	}
55
56
	private function getParameter( $key ) {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
57
		if ( $this->parameters === null ) {
58
			$this->populateParameters();
59
		}
60
		return $this->parameters[ $key ];
61
	}
62
63
	private function setParameters( $pagetext, $comment, $title ) {
64
		$this->parameters = [
65
			'pagetext' => $pagetext,
66
			'comment'  => $comment,
67
			'title'    => $title,
68
		];
69
	}
70
71
	private function getEscapedParameter( $key ) {
72
		return $this->escape( $this->getParameter( $key ) );
73
	}
74
75
	private function escape( $text ) {
76
		return htmlspecialchars( $text, ENT_QUOTES, 'UTF-8', false );
77
	}
78
79
	private function populateParameters() {
80
81
		if ( $this->parameterIndex === null || $this->populateParametersFromKey() === false ) {
82
			$this->populateParametersFromDefaults();
83
		}
84
85
	}
86
87
	private function populateParametersFromKey() {
88
		$paramMsg = Message::newFromKey( 'simplebatchupload-parameters' );
89
90
		if ( $paramMsg->exists() ) {
91
92
			$paramSet = explode( "\n", $paramMsg->plain() );
93
			$paramSet = array_map( [ $this, 'parseParamLine' ], $paramSet );
94
			$paramSet = array_combine( array_column( $paramSet, 0 ), $paramSet );
95
96
			if ( array_key_exists( $this->parameterIndex, $paramSet ) ) {
97
				$this->setParameters( '{{' . $this->parameterIndex . '}}', $paramSet[ $this->parameterIndex ][ 1 ], $paramSet[ $this->parameterIndex ][ 2 ] );
98
				return true;
99
			}
100
		}
101
		return false;
102
	}
103
104
	private function populateParametersFromDefaults() {
105
		$this->setParameters( '', Message::newFromKey( 'simplebatchupload-comment' )->text(), Message::newFromKey( 'batchupload' )->text() );
106
	}
107
108
	private function parseParamLine( $paramLine ) {
109
		$paramLine = $paramLine;
0 ignored issues
show
Bug introduced by
Why assign $paramLine to itself?

This checks looks for cases where a variable has been assigned to itself.

This assignement can be removed without consequences.

Loading history...
110
		return array_map( 'trim', explode( '|', $paramLine, 3 ) );
111
	}
112
113
}
114