|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ParserHooks; |
|
4
|
|
|
|
|
5
|
|
|
use ParamProcessor\Processor; |
|
6
|
|
|
use Parser; |
|
7
|
|
|
use ParserHooks\Internal\Runner; |
|
8
|
|
|
use PPFrame; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class that handles a parser function hook call coming from MediaWiki |
|
12
|
|
|
* by processing the parameters as declared in the hook definition and |
|
13
|
|
|
* then passes the result to the hook handler. |
|
14
|
|
|
* |
|
15
|
|
|
* @since 1.0 |
|
16
|
|
|
* |
|
17
|
|
|
* @licence GNU GPL v2+ |
|
18
|
|
|
* @author Jeroen De Dauw < [email protected] > |
|
19
|
|
|
*/ |
|
20
|
|
|
class FunctionRunner extends Runner { |
|
21
|
|
|
|
|
22
|
|
|
const OPT_DO_PARSE = 'parse'; // Boolean, since 1.1 |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @since 1.1 |
|
26
|
|
|
* |
|
27
|
|
|
* @param HookDefinition $definition |
|
28
|
|
|
* @param HookHandler $handler |
|
29
|
|
|
* @param array $options |
|
30
|
|
|
* @param Processor|null $paramProcessor |
|
31
|
|
|
*/ |
|
32
|
2 |
|
public function __construct( HookDefinition $definition, HookHandler $handler, array $options = [], Processor $paramProcessor = null ) { |
|
33
|
2 |
|
parent::__construct( $definition, $handler, $options, $paramProcessor ); |
|
34
|
2 |
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @since 1.0 |
|
38
|
|
|
* |
|
39
|
|
|
* @param Parser $parser |
|
40
|
|
|
* @param array $arguments |
|
41
|
|
|
* @param PPFrame $frame |
|
42
|
|
|
* |
|
43
|
|
|
* @return array |
|
44
|
|
|
*/ |
|
45
|
2 |
|
public function run( Parser &$parser, array $arguments, PPFrame $frame ) { |
|
46
|
2 |
|
$resultText = $this->handler->handle( |
|
47
|
2 |
|
$parser, |
|
48
|
2 |
|
$this->getProcessedParams( $this->getExpandedParams( $arguments, $frame ) ) |
|
49
|
|
|
); |
|
50
|
|
|
|
|
51
|
2 |
|
return $this->getResultStructure( $resultText ); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
2 |
|
protected function getExpandedParams( array $rawArguments, PPFrame $frame ) { |
|
55
|
2 |
|
$rawArgList = []; |
|
56
|
|
|
|
|
57
|
2 |
|
foreach( $rawArguments as $arg ) { |
|
58
|
2 |
|
$rawArgList[] = $frame->expand( $arg ); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
2 |
|
return $rawArgList; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
2 |
|
protected function getProcessedParams( array $expendedArgs ) { |
|
65
|
2 |
|
$this->paramProcessor->setFunctionParams( |
|
66
|
2 |
|
$expendedArgs, |
|
67
|
2 |
|
$this->definition->getParameters(), |
|
68
|
2 |
|
$this->definition->getDefaultParameters() |
|
69
|
|
|
); |
|
70
|
|
|
|
|
71
|
2 |
|
return $this->paramProcessor->processParameters(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
2 |
|
protected function getResultStructure( $resultText ) { |
|
75
|
2 |
|
$result = [ $resultText ]; |
|
76
|
|
|
|
|
77
|
2 |
|
if ( !$this->getOption( self::OPT_DO_PARSE ) ) { |
|
78
|
1 |
|
$result['noparse'] = true; |
|
79
|
1 |
|
$result['isHTML'] = true; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
2 |
|
return $result; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @see Runner::getDefaultOptions |
|
87
|
|
|
* |
|
88
|
|
|
* @since 1.1 |
|
89
|
|
|
* |
|
90
|
|
|
* @return array |
|
91
|
|
|
*/ |
|
92
|
2 |
|
protected function getDefaultOptions() { |
|
93
|
|
|
return [ |
|
94
|
2 |
|
self::OPT_DO_PARSE => true, |
|
95
|
|
|
]; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
} |
|
99
|
|
|
|