| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
namespace MediaWiki\Parser; |
| 23 |
|
| 24 |
use MediaWiki\Title\Title; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
class PPTemplateFrame_Hash extends PPFrame_Hash { |
| 32 |
|
| 33 |
|
| 34 |
public $numberedArgs; |
| 35 |
|
| 36 |
public $namedArgs; |
| 37 |
|
| 38 |
public $parent; |
| 39 |
|
| 40 |
public $numberedExpansionCache; |
| 41 |
|
| 42 |
public $namedExpansionCache; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public function __construct( $preprocessor, $parent = false, $numberedArgs = [], |
| 52 |
$namedArgs = [], $title = false |
| 53 |
) { |
| 54 |
parent::__construct( $preprocessor ); |
| 55 |
|
| 56 |
'@phan-var PPFrame_Hash $parent'; |
| 57 |
|
| 58 |
$this->parent = $parent; |
| 59 |
$this->numberedArgs = $numberedArgs; |
| 60 |
$this->namedArgs = $namedArgs; |
| 61 |
$this->title = $title; |
| 62 |
$pdbk = $title ? $title->getPrefixedDBkey() : false; |
| 63 |
$this->titleCache = $parent->titleCache; |
| 64 |
$this->titleCache[] = $pdbk; |
| 65 |
$this->loopCheckHash = $parent->loopCheckHash; |
| 66 |
if ( $pdbk !== false ) { |
| 67 |
$this->loopCheckHash[$pdbk] = true; |
| 68 |
} |
| 69 |
$this->depth = $parent->depth + 1; |
| 70 |
$this->numberedExpansionCache = $this->namedExpansionCache = []; |
| 71 |
} |
| 72 |
|
| 73 |
public function __toString() { |
| 74 |
$s = 'tplframe{'; |
| 75 |
$first = true; |
| 76 |
$args = $this->numberedArgs + $this->namedArgs; |
| 77 |
foreach ( $args as $name => $value ) { |
| 78 |
if ( $first ) { |
| 79 |
$first = false; |
| 80 |
} else { |
| 81 |
$s .= ', '; |
| 82 |
} |
| 83 |
$s .= "\"$name\":\"" . |
| 84 |
str_replace( '"', '\\"', $value->__toString() ) . '"'; |
| 85 |
} |
| 86 |
$s .= '}'; |
| 87 |
return $s; |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
public function cachedExpand( $key, $root, $flags = 0 ) { |
| 97 |
if ( isset( $this->parent->childExpansionCache[$key] ) ) { |
| 98 |
return $this->parent->childExpansionCache[$key]; |
| 99 |
} |
| 100 |
$retval = $this->expand( $root, $flags ); |
| 101 |
if ( !$this->isVolatile() ) { |
| 102 |
$this->parent->childExpansionCache[$key] = $retval; |
| 103 |
} |
| 104 |
return $retval; |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
public function isEmpty() { |
| 113 |
return !count( $this->numberedArgs ) && !count( $this->namedArgs ); |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
public function getArguments() { |
| 120 |
$arguments = []; |
| 121 |
foreach ( array_merge( |
| 122 |
array_keys( $this->numberedArgs ), |
| 123 |
array_keys( $this->namedArgs ) ) as $key ) { |
| 124 |
$arguments[$key] = $this->getArgument( $key ); |
| 125 |
} |
| 126 |
return $arguments; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
public function getNumberedArguments() { |
| 133 |
$arguments = []; |
| 134 |
foreach ( $this->numberedArgs as $key => $_ ) { |
| 135 |
$arguments[$key] = $this->getArgument( $key ); |
| 136 |
} |
| 137 |
return $arguments; |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
public function getNamedArguments() { |
| 144 |
$arguments = []; |
| 145 |
foreach ( $this->namedArgs as $key => $_ ) { |
| 146 |
$arguments[$key] = $this->getArgument( $key ); |
| 147 |
} |
| 148 |
return $arguments; |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
public function getNumberedArgument( $index ) { |
| 156 |
if ( !isset( $this->numberedArgs[$index] ) ) { |
| 157 |
return false; |
| 158 |
} |
| 159 |
if ( !isset( $this->numberedExpansionCache[$index] ) ) { |
| 160 |
|
| 161 |
$this->numberedExpansionCache[$index] = $this->parent->expand( |
| 162 |
$this->numberedArgs[$index], |
| 163 |
PPFrame::STRIP_COMMENTS |
| 164 |
); |
| 165 |
} |
| 166 |
return $this->numberedExpansionCache[$index]; |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
public function getNamedArgument( $name ) { |
| 174 |
if ( !isset( $this->namedArgs[$name] ) ) { |
| 175 |
return false; |
| 176 |
} |
| 177 |
if ( !isset( $this->namedExpansionCache[$name] ) ) { |
| 178 |
|
| 179 |
$this->namedExpansionCache[$name] = trim( |
| 180 |
$this->parent->expand( $this->namedArgs[$name], PPFrame::STRIP_COMMENTS ) ); |
| 181 |
} |
| 182 |
return $this->namedExpansionCache[$name]; |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
public function getArgument( $name ) { |
| 190 |
$text = $this->getNumberedArgument( $name ); |
| 191 |
if ( $text === false ) { |
| 192 |
$text = $this->getNamedArgument( $name ); |
| 193 |
} |
| 194 |
return $text; |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
public function isTemplate() { |
| 203 |
return true; |
| 204 |
} |
| 205 |
|
| 206 |
public function setVolatile( $flag = true ) { |
| 207 |
parent::setVolatile( $flag ); |
| 208 |
$this->parent->setVolatile( $flag ); |
| 209 |
} |
| 210 |
|
| 211 |
public function setTTL( $ttl ) { |
| 212 |
parent::setTTL( $ttl ); |
| 213 |
$this->parent->setTTL( $ttl ); |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
class_alias( PPTemplateFrame_Hash::class, 'PPTemplateFrame_Hash' ); |