summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/ParserFunctions/DocumentationParserFunction.php
blob: fa739b8a8c4adf6f937f4d6dce5ca62f7540f1d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php

namespace SMW\ParserFunctions;

use ParamProcessor\ParamDefinition;
use ParamProcessor\ProcessedParam;
use ParamProcessor\ProcessingError;
use ParamProcessor\ProcessingResult;
use Parser;
use ParserHooks\HookDefinition;
use ParserHooks\HookHandler;
use SMW\ParameterListDocBuilder;
use SMWQueryProcessor as QueryProcessor;

/**
 * Class that provides the {{#smwdoc}} parser function, which displays parameter
 * documentation for a specified result format.
 *
 * @ingroup ParserFunction
 *
 * @license GNU GPL v2+
 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
 */
class DocumentationParserFunction implements HookHandler {

	/**
	 * @var string
	 */
	private $language = 'en';

	/**
	 * @param Parser $parser
	 * @param ProcessingResult $result
	 *
	 * @return mixed
	 */
	public function handle( Parser $parser, ProcessingResult $result ) {

		if ( $result->hasFatal() ) {
			return $this->getOutputForErrors( $result->getErrors() );
		}

		$parameters = $result->getParameters();
		$format = $parameters['format']->getValue();

		$formatParameters = QueryProcessor::getFormatParameters(
			$format
		);

		$this->language = $parameters['language']->getValue();

		if ( $formatParameters === [] ) {
			return $this->msg( 'smw-smwdoc-default-no-parameter-list', $format );
		}

		return $this->buildParameterListDocumentation( $parameters, $formatParameters );
	}

	/**
	 * @return HookDefinition
	 */
	public static function getHookDefinition() {
		return new HookDefinition(
			'smwdoc',
			[
				[
					'name' => 'format',
					'message' => 'smw-smwdoc-par-format',
					'values' => array_keys( $GLOBALS['smwgResultFormats'] ),
				],
				[
					'name' => 'language',
					'message' => 'smw-smwdoc-par-language',
					'default' => $GLOBALS['wgLanguageCode'],
				],
				[
					'name' => 'parameters',
					'message' => 'smw-smwdoc-par-parameters',
					'values' => [ 'all', 'specific', 'base' ],
					'default' => 'specific',
				],
			],
			[ 'format', 'language', 'parameters' ]
		);
	}

	/**
	 * @param ProcessedParam[] $parameters
	 *
	 * @return string
	 */
	private function buildParameterListDocumentation( array $parameters, $formatParameters ) {

		if ( $parameters['parameters']->getValue() === 'specific' ) {
			foreach ( array_keys( QueryProcessor::getParameters() ) as $name ) {
				unset( $formatParameters[$name] );
			}
		} elseif ( $parameters['parameters']->getValue() === 'base' ) {
			foreach ( array_diff_key( $formatParameters, QueryProcessor::getParameters() ) as $param ) {
				unset( $formatParameters[$param->getName()] );
			}
		}

		$docBuilder = new ParameterListDocBuilder(
			[ $this, 'msg' ]
		);

		if ( ( $parameterTable = $docBuilder->getParameterTable( $formatParameters ) ) !== ''  ) {
			return $parameterTable;
		}

		return $this->msg( 'smw-smwdoc-default-no-parameter-list', $parameters['format']->getValue() );
	}

	/**
	 * @since 3.0
	 *
	 * @param ...$args
	 *
	 * @return string
	 */
	public function msg( ...$args ) {
		return wfMessage( array_shift( $args ) )->params( $args )->useDatabase( true )->inLanguage( $this->language )->text();
	}

	/**
	 * @param ProcessingError[] $errors
	 * @return string
	 */
	private function getOutputForErrors( $errors ) {
		// TODO: see https://github.com/SemanticMediaWiki/SemanticMediaWiki/issues/1485
		return 'A fatal error occurred in the #smwdoc parser function';
	}

}