Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 70 |
|
0.00% |
0 / 13 |
CRAP | |
0.00% |
0 / 1 |
| PPTemplateFrame_Hash | |
0.00% |
0 / 69 |
|
0.00% |
0 / 13 |
812 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
12 | |||
| __toString | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
12 | |||
| cachedExpand | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| isEmpty | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 | |||
| getArguments | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| getNumberedArguments | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| getNamedArguments | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| getNumberedArgument | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
12 | |||
| getNamedArgument | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| getArgument | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| isTemplate | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setVolatile | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| setTTL | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * @license GPL-2.0-or-later |
| 4 | * @file |
| 5 | * @ingroup Parser |
| 6 | */ |
| 7 | |
| 8 | namespace MediaWiki\Parser; |
| 9 | |
| 10 | use MediaWiki\Title\Title; |
| 11 | |
| 12 | /** |
| 13 | * Expansion frame with template arguments |
| 14 | * @ingroup Parser |
| 15 | */ |
| 16 | // phpcs:ignore Squiz.Classes.ValidClassName.NotCamelCaps |
| 17 | class PPTemplateFrame_Hash extends PPFrame_Hash { |
| 18 | |
| 19 | /** @var array */ |
| 20 | public $numberedArgs; |
| 21 | /** @var array */ |
| 22 | public $namedArgs; |
| 23 | /** @var PPFrame_Hash */ |
| 24 | public $parent; |
| 25 | /** @var array */ |
| 26 | public $numberedExpansionCache; |
| 27 | /** @var array */ |
| 28 | public $namedExpansionCache; |
| 29 | |
| 30 | /** |
| 31 | * @param Preprocessor $preprocessor |
| 32 | * @param false|PPFrame $parent |
| 33 | * @param array $numberedArgs |
| 34 | * @param array $namedArgs |
| 35 | * @param false|Title $title |
| 36 | */ |
| 37 | public function __construct( $preprocessor, $parent = false, $numberedArgs = [], |
| 38 | $namedArgs = [], $title = false |
| 39 | ) { |
| 40 | parent::__construct( $preprocessor ); |
| 41 | /** @var PPFrame_Hash $parent */ |
| 42 | '@phan-var PPFrame_Hash $parent'; |
| 43 | |
| 44 | $this->parent = $parent; |
| 45 | $this->numberedArgs = $numberedArgs; |
| 46 | $this->namedArgs = $namedArgs; |
| 47 | $this->title = $title; |
| 48 | $pdbk = $title ? $title->getPrefixedDBkey() : false; |
| 49 | $this->titleCache = $parent->titleCache; |
| 50 | $this->titleCache[] = $pdbk; |
| 51 | $this->loopCheckHash = /*clone*/ $parent->loopCheckHash; |
| 52 | if ( $pdbk !== false ) { |
| 53 | $this->loopCheckHash[$pdbk] = true; |
| 54 | } |
| 55 | $this->depth = $parent->depth + 1; |
| 56 | $this->numberedExpansionCache = $this->namedExpansionCache = []; |
| 57 | } |
| 58 | |
| 59 | public function __toString() { |
| 60 | $s = 'tplframe{'; |
| 61 | $first = true; |
| 62 | $args = $this->numberedArgs + $this->namedArgs; |
| 63 | foreach ( $args as $name => $value ) { |
| 64 | if ( $first ) { |
| 65 | $first = false; |
| 66 | } else { |
| 67 | $s .= ', '; |
| 68 | } |
| 69 | $s .= "\"$name\":\"" . |
| 70 | str_replace( '"', '\\"', $value->__toString() ) . '"'; |
| 71 | } |
| 72 | $s .= '}'; |
| 73 | return $s; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * @param string|int $key |
| 78 | * @param string|PPNode $root |
| 79 | * @param int $flags |
| 80 | * @return string |
| 81 | */ |
| 82 | public function cachedExpand( $key, $root, $flags = 0 ) { |
| 83 | if ( isset( $this->parent->childExpansionCache[$key] ) ) { |
| 84 | return $this->parent->childExpansionCache[$key]; |
| 85 | } |
| 86 | $retval = $this->expand( $root, $flags ); |
| 87 | if ( !$this->isVolatile() ) { |
| 88 | $this->parent->childExpansionCache[$key] = $retval; |
| 89 | } |
| 90 | return $retval; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Returns true if there are no arguments in this frame |
| 95 | * |
| 96 | * @return bool |
| 97 | */ |
| 98 | public function isEmpty() { |
| 99 | return !count( $this->numberedArgs ) && !count( $this->namedArgs ); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * @return array |
| 104 | */ |
| 105 | public function getArguments() { |
| 106 | $arguments = []; |
| 107 | foreach ( array_merge( |
| 108 | array_keys( $this->numberedArgs ), |
| 109 | array_keys( $this->namedArgs ) ) as $key ) { |
| 110 | $arguments[$key] = $this->getArgument( $key ); |
| 111 | } |
| 112 | return $arguments; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * @return array |
| 117 | */ |
| 118 | public function getNumberedArguments() { |
| 119 | $arguments = []; |
| 120 | foreach ( $this->numberedArgs as $key => $_ ) { |
| 121 | $arguments[$key] = $this->getArgument( $key ); |
| 122 | } |
| 123 | return $arguments; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * @return array |
| 128 | */ |
| 129 | public function getNamedArguments() { |
| 130 | $arguments = []; |
| 131 | foreach ( $this->namedArgs as $key => $_ ) { |
| 132 | $arguments[$key] = $this->getArgument( $key ); |
| 133 | } |
| 134 | return $arguments; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * @param int $index |
| 139 | * @return string|false |
| 140 | */ |
| 141 | public function getNumberedArgument( $index ) { |
| 142 | if ( !isset( $this->numberedArgs[$index] ) ) { |
| 143 | return false; |
| 144 | } |
| 145 | if ( !isset( $this->numberedExpansionCache[$index] ) ) { |
| 146 | # No trimming for unnamed arguments |
| 147 | $this->numberedExpansionCache[$index] = $this->parent->expand( |
| 148 | $this->numberedArgs[$index], |
| 149 | PPFrame::STRIP_COMMENTS |
| 150 | ); |
| 151 | } |
| 152 | return $this->numberedExpansionCache[$index]; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * @param string $name |
| 157 | * @return string|false |
| 158 | */ |
| 159 | public function getNamedArgument( $name ) { |
| 160 | if ( !isset( $this->namedArgs[$name] ) ) { |
| 161 | return false; |
| 162 | } |
| 163 | if ( !isset( $this->namedExpansionCache[$name] ) ) { |
| 164 | # Trim named arguments post-expand, for backwards compatibility |
| 165 | $this->namedExpansionCache[$name] = trim( |
| 166 | $this->parent->expand( $this->namedArgs[$name], PPFrame::STRIP_COMMENTS ) ); |
| 167 | } |
| 168 | return $this->namedExpansionCache[$name]; |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * @param int|string $name |
| 173 | * @return string|false |
| 174 | */ |
| 175 | public function getArgument( $name ) { |
| 176 | $text = $this->getNumberedArgument( $name ); |
| 177 | if ( $text === false ) { |
| 178 | $text = $this->getNamedArgument( $name ); |
| 179 | } |
| 180 | return $text; |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Return true if the frame is a template frame |
| 185 | * |
| 186 | * @return bool |
| 187 | */ |
| 188 | public function isTemplate() { |
| 189 | return true; |
| 190 | } |
| 191 | |
| 192 | /** @inheritDoc */ |
| 193 | public function setVolatile( $flag = true ) { |
| 194 | parent::setVolatile( $flag ); |
| 195 | $this->parent->setVolatile( $flag ); |
| 196 | } |
| 197 | |
| 198 | /** @inheritDoc */ |
| 199 | public function setTTL( $ttl ) { |
| 200 | wfDeprecated( __METHOD__, '1.46' ); |
| 201 | parent::setTTL( $ttl ); |
| 202 | $this->parent->setTTL( $ttl ); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | /** @deprecated class alias since 1.43 */ |
| 207 | class_alias( PPTemplateFrame_Hash::class, 'PPTemplateFrame_Hash' ); |