Completed
Push — master ( 684184...9a35aa )
by Jeroen De
08:07
created

includes/parserhooks/Maps_MapsDoc.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
use ParamProcessor\ParamDefinition;
4
5
/**
6
 * Class for the 'mapsdoc' parser hooks,
7
 * which displays documentation for a specified mapping service.
8
 *
9
 * @since 1.0
10
 *
11
 * @licence GNU GPL v2+
12
 * @author Jeroen De Dauw < [email protected] >
13
 */
14
class MapsMapsDoc extends ParserHook {
15
16
	/**
17
	 * Field to store the value of the language parameter.
18
	 *
19
	 * @since 1.0.1
20
	 *
21
	 * @var string
22
	 */
23
	protected $language;
24
25
	/**
26
	 * Gets the name of the parser hook.
27
	 * @see ParserHook::getName
28
	 *
29
	 * @since 1.0
30
	 *
31
	 * @return string
32
	 */
33
	protected function getName() {
34
		return 'mapsdoc';
35
	}
36
37
	/**
38
	 * Returns an array containing the parameter info.
39
	 * @see ParserHook::getParameterInfo
40
	 *
41
	 * @since 1.0
42
	 *
43
	 * @return array
44
	 */
45
	protected function getParameterInfo( $type ) {
0 ignored issues
show
getParameterInfo uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
46
		$params = [];
47
48
		$params['service'] = [
49
			'values' => $GLOBALS['egMapsAvailableServices'],
50
			'tolower' => true,
51
		];
52
53
		$params['language'] = [
54
			'default' => $GLOBALS['wgLanguageCode'],
55
		];
56
57
		// Give grep a chance to find the usages:
58
		// maps-geocode-par-service, maps-geocode-par-language
59
		foreach ( $params as $name => &$param ) {
60
			$param['message'] = 'maps-geocode-par-' . $name;
61
		}
62
63
		return $params;
64
	}
65
66
	/**
67
	 * Returns the list of default parameters.
68
	 * @see ParserHook::getDefaultParameters
69
	 *
70
	 * @since 1.0
71
	 *
72
	 * @return array
73
	 */
74
	protected function getDefaultParameters( $type ) {
75
		return [ 'service', 'language' ];
76
	}
77
78
	/**
79
	 * Renders and returns the output.
80
	 * @see ParserHook::render
81
	 *
82
	 * @since 1.0
83
	 *
84
	 * @param array $parameters
85
	 *
86
	 * @return string
87
	 */
88
	public function render( array $parameters ) {
89
		$this->language = $parameters['language'];
90
91
		$params = $this->getServiceParameters( $parameters['service'] );
92
93
		return $this->getParameterTable( $params );
94
	}
95
96
	/**
97
	 * Message function that takes into account the language parameter.
98
	 *
99
	 * @since 1.0.1
100
	 *
101
	 * @param string $key
102
	 * @param ... $args
103
	 *
104
	 * @return string
105
	 */
106
	protected function msg() {
107
		$args = func_get_args();
108
		$key = array_shift( $args );
109
		return wfMessage( $key, $args )->inLanguage( $this->language )->text();
110
	}
111
112
	/**
113
	 * Returns the wikitext for a table listing the provided parameters.
114
	 *
115
	 * @since 1.0
116
	 *
117
	 * @param array $parameters
118
	 *
119
	 * @return string
120
	 */
121
	protected function getParameterTable( array $parameters ) {
122
		$tableRows = [];
123
124
		$parameters = ParamDefinition::getCleanDefinitions( $parameters );
125
126
		foreach ( $parameters as $parameter ) {
127
			$tableRows[] = $this->getDescriptionRow( $parameter );
128
		}
129
130
		$table = '';
131
132
		if ( count( $tableRows ) > 0 ) {
133
			$tableRows = array_merge( [
134
			'!' . $this->msg( 'validator-describe-header-parameter' ) ."\n" .
135
			//'!' . $this->msg( 'validator-describe-header-aliases' ) ."\n" .
136
			'!' . $this->msg( 'validator-describe-header-type' ) ."\n" .
137
			'!' . $this->msg( 'validator-describe-header-default' ) ."\n" .
138
			'!' . $this->msg( 'validator-describe-header-description' )
139
			], $tableRows );
140
141
			$table = implode( "\n|-\n", $tableRows );
142
143
			$table =
144
					'{| class="wikitable sortable"' . "\n" .
145
					$table .
146
					"\n|}";
147
		}
148
149
		return $table;
150
	}
151
152
	/**
153
	 * Returns the wikitext for a table row describing a single parameter.
154
	 *
155
	 * @since 1.0
156
	 *
157
	 * @param ParamDefinition $parameter
158
	 *
159
	 * @return string
160
	 */
161
	protected function getDescriptionRow( ParamDefinition $parameter ) {
162
		$description = $this->msg( $parameter->getMessage() );
163
164
		$type = $parameter->getTypeMessage();
165
166
		$default = $parameter->isRequired() ? "''" . $this->msg( 'validator-describe-required' ) . "''" : $parameter->getDefault();
167
		if ( is_array( $default ) ) {
168
			$default = implode( ', ', $default );
169
		}
170
		elseif ( is_bool( $default ) ) {
171
			$default = $default ? 'yes' : 'no';
172
		}
173
174
		if ( $default === '' ) $default = "''" . $this->msg( 'validator-describe-empty' ) . "''";
175
176
		return <<<EOT
177
| {$parameter->getName()}
178
| {$type}
179
| {$default}
180
| {$description}
181
EOT;
182
	}
183
184
	protected function getServiceParameters( $service ) {
185
		$service = MapsMappingServices::getServiceInstance( $service );
186
187
		$params = [];
188
189
		$params['zoom'] = [
190
			'type' => 'integer',
191
			'message' => 'maps-par-zoom',
192
		];
193
194
		$service->addParameterInfo( $params );
195
196
		return $params;
197
	}
198
199
	/**
200
	 * @see ParserHook::getDescription()
201
	 *
202
	 * @since 1.0
203
	 */
204
	public function getMessage() {
205
		return 'maps-mapsdoc-description';
206
	}
207
208
}
209