Completed
Push — master ( 72be7d...f249b7 )
by Jeroen De
13s
created

includes/parserhooks/Maps_MapsDoc.php (1 issue)

Severity

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 1
	protected function getName() {
34 1
		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 3
	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 3
		$params = [];
47
48 3
		$params['service'] = [
49 3
			'values' => $GLOBALS['egMapsAvailableServices'],
50
			'tolower' => true,
51
		];
52
53 3
		$params['language'] = [
54 3
			'default' => $GLOBALS['wgLanguageCode'],
55
		];
56
57
		// Give grep a chance to find the usages:
58
		// maps-geocode-par-service, maps-geocode-par-language
59 3
		foreach ( $params as $name => &$param ) {
60 3
			$param['message'] = 'maps-geocode-par-' . $name;
61
		}
62
63 3
		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 1
	protected function getDefaultParameters( $type ) {
75 1
		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
	private 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
	private 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
	 * @param ParamDefinition $parameter
156
	 *
157
	 * @return string
158
	 */
159
	private function getDescriptionRow( ParamDefinition $parameter ) {
160
		$description = $this->msg( $parameter->getMessage() );
161
162
		$type = $this->msg( $parameter->getTypeMessage() );
163
164
		$default = $parameter->isRequired() ? "''" . $this->msg( 'validator-describe-required' ) . "''" : $parameter->getDefault();
165
		if ( is_array( $default ) ) {
166
			$default = implode( ', ', $default );
167
		}
168
		elseif ( is_bool( $default ) ) {
169
			$default = $default ? 'yes' : 'no';
170
		}
171
172
		if ( $default === '' ) $default = "''" . $this->msg( 'validator-describe-empty' ) . "''";
173
174
		return <<<EOT
175
| {$parameter->getName()}
176
| {$type}
177
| {$default}
178
| {$description}
179
EOT;
180
	}
181
182
	private function getServiceParameters( $service ) {
183
		$service = MapsMappingServices::getServiceInstance( $service );
184
185
		$params = [];
186
187
		$params['zoom'] = [
188
			'type' => 'integer',
189
			'message' => 'maps-par-zoom',
190
		];
191
192
		$service->addParameterInfo( $params );
193
194
		return $params;
195
	}
196
197
	/**
198
	 * @see ParserHook::getDescription()
199
	 *
200
	 * @since 1.0
201
	 */
202
	public function getMessage() {
203
		return 'maps-mapsdoc-description';
204
	}
205
206
}
207