1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SMW\ParserFunctions; |
4
|
|
|
|
5
|
|
|
use ParamProcessor\ParamDefinition; |
6
|
|
|
use ParamProcessor\ProcessingError; |
7
|
|
|
use ParamProcessor\ProcessingResult; |
8
|
|
|
use Parser; |
9
|
|
|
use ParserHooks\HookDefinition; |
10
|
|
|
use ParserHooks\HookHandler; |
11
|
|
|
use SMW\ParameterListDocBuilder; |
12
|
|
|
use SMWQueryProcessor; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class that provides the {{#smwdoc}} parser function, which displays parameter |
16
|
|
|
* documentation for a specified result format. |
17
|
|
|
* |
18
|
|
|
* @ingroup ParserFunction |
19
|
|
|
* |
20
|
|
|
* @license GNU GPL v2+ |
21
|
|
|
* @author Jeroen De Dauw < [email protected] > |
22
|
|
|
*/ |
23
|
|
|
class DocumentationParserFunction implements HookHandler { |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private $language; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param Parser $parser |
32
|
|
|
* @param ProcessingResult $result |
33
|
|
|
* |
34
|
|
|
* @return mixed |
35
|
|
|
*/ |
36
|
|
|
public function handle( Parser $parser, ProcessingResult $result ) { |
37
|
2 |
|
if ( $result->hasFatal() ) { |
38
|
2 |
|
return $this->getOutputForErrors( $result->getErrors() ); |
39
|
|
|
} |
40
|
2 |
|
|
41
|
|
|
$parameters = $result->getParameters(); |
42
|
2 |
|
|
43
|
|
|
$this->language = $parameters['language']->getValue(); |
44
|
2 |
|
|
45
|
1 |
|
$params = $this->getFormatParameters( $parameters['format']->getValue() ); |
46
|
1 |
|
|
47
|
|
|
if ( $parameters['parameters']->getValue() === 'specific' ) { |
48
|
|
|
foreach ( array_keys( SMWQueryProcessor::getParameters() ) as $name ) { |
49
|
1 |
|
unset( $params[$name] ); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
elseif ( $parameters['parameters']->getValue() === 'base' ) { |
53
|
|
|
foreach ( array_diff_key( $params, SMWQueryProcessor::getParameters() ) as $param ) { |
54
|
|
|
unset( $params[$param->getName()] ); |
55
|
2 |
|
} |
56
|
|
|
} |
57
|
2 |
|
|
58
|
|
|
$docBuilder = new ParameterListDocBuilder( $this->newMessageFunction() ); |
59
|
|
|
|
60
|
2 |
|
return $docBuilder->getParameterTable( $params ); |
61
|
2 |
|
} |
62
|
|
|
|
63
|
2 |
|
private function newMessageFunction() { |
64
|
1 |
|
$language = $this->language; |
65
|
1 |
|
|
66
|
1 |
|
return function() use ( $language ) { |
67
|
2 |
|
$args = func_get_args(); |
68
|
|
|
$key = array_shift( $args ); |
69
|
|
|
return wfMessage( $key )->params( $args )->useDatabase( true )->inLanguage( $language )->text(); |
70
|
|
|
}; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param string $format |
75
|
2 |
|
* |
76
|
2 |
|
* @return array of IParamDefinition |
77
|
1 |
|
*/ |
78
|
|
|
private function getFormatParameters( $format ) { |
|
|
|
|
79
|
|
|
if ( !array_key_exists( $format, $GLOBALS['smwgResultFormats'] ) ) { |
80
|
1 |
|
return array(); |
81
|
1 |
|
} |
82
|
|
|
|
83
|
|
|
return ParamDefinition::getCleanDefinitions( |
84
|
|
|
SMWQueryProcessor::getResultPrinter( $format )->getParamDefinitions( SMWQueryProcessor::getParameters() ) |
85
|
14 |
|
); |
86
|
14 |
|
} |
87
|
14 |
|
|
88
|
|
|
/** |
89
|
|
|
* @param ProcessingError[] $errors |
90
|
14 |
|
* @return string |
91
|
14 |
|
*/ |
92
|
14 |
|
private function getOutputForErrors( $errors ) { |
|
|
|
|
93
|
14 |
|
// TODO: see https://github.com/SemanticMediaWiki/SemanticMediaWiki/issues/1485 |
94
|
|
|
return 'A fatal error occurred in the #smwdoc parser function'; |
95
|
14 |
|
} |
96
|
14 |
|
|
97
|
14 |
|
public static function getHookDefinition() { |
|
|
|
|
98
|
|
|
return new HookDefinition( |
99
|
|
|
'smwdoc', |
100
|
|
|
array( |
|
|
|
|
101
|
|
|
array( |
102
|
|
|
'name' => 'format', |
103
|
|
|
'message' => 'smw-smwdoc-par-format', |
104
|
|
|
'values' => array_keys( $GLOBALS['smwgResultFormats'] ), |
105
|
|
|
), |
106
|
14 |
|
array( |
107
|
|
|
'name' => 'language', |
108
|
|
|
'message' => 'smw-smwdoc-par-language', |
109
|
|
|
'default' => $GLOBALS['wgLanguageCode'], |
110
|
|
|
), |
111
|
|
|
array( |
112
|
|
|
'name' => 'parameters', |
113
|
|
|
'message' => 'smw-smwdoc-par-parameters', |
114
|
|
|
'values' => array( 'all', 'specific', 'base' ), |
115
|
|
|
'default' => 'specific', |
116
|
|
|
), |
117
|
|
|
), |
118
|
|
|
array( 'format', 'language', 'parameters' ) |
119
|
|
|
); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
} |
123
|
|
|
|
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: