MediaWiki master
PPTemplateFrame_Hash.php
Go to the documentation of this file.
1<?php
23
28// phpcs:ignore Squiz.Classes.ValidClassName.NotCamelCaps
30
34 public $namedArgs;
36 public $parent;
41
49 public function __construct( $preprocessor, $parent = false, $numberedArgs = [],
50 $namedArgs = [], $title = false
51 ) {
52 parent::__construct( $preprocessor );
54 '@phan-var PPFrame_Hash $parent';
55
56 $this->parent = $parent;
57 $this->numberedArgs = $numberedArgs;
58 $this->namedArgs = $namedArgs;
59 $this->title = $title;
60 $pdbk = $title ? $title->getPrefixedDBkey() : false;
61 $this->titleCache = $parent->titleCache;
62 $this->titleCache[] = $pdbk;
63 $this->loopCheckHash = /*clone*/ $parent->loopCheckHash;
64 if ( $pdbk !== false ) {
65 $this->loopCheckHash[$pdbk] = true;
66 }
67 $this->depth = $parent->depth + 1;
68 $this->numberedExpansionCache = $this->namedExpansionCache = [];
69 }
70
71 public function __toString() {
72 $s = 'tplframe{';
73 $first = true;
74 $args = $this->numberedArgs + $this->namedArgs;
75 foreach ( $args as $name => $value ) {
76 if ( $first ) {
77 $first = false;
78 } else {
79 $s .= ', ';
80 }
81 $s .= "\"$name\":\"" .
82 str_replace( '"', '\\"', $value->__toString() ) . '"';
83 }
84 $s .= '}';
85 return $s;
86 }
87
94 public function cachedExpand( $key, $root, $flags = 0 ) {
95 if ( isset( $this->parent->childExpansionCache[$key] ) ) {
96 return $this->parent->childExpansionCache[$key];
97 }
98 $retval = $this->expand( $root, $flags );
99 if ( !$this->isVolatile() ) {
100 $this->parent->childExpansionCache[$key] = $retval;
101 }
102 return $retval;
103 }
104
110 public function isEmpty() {
111 return !count( $this->numberedArgs ) && !count( $this->namedArgs );
112 }
113
117 public function getArguments() {
118 $arguments = [];
119 foreach ( array_merge(
120 array_keys( $this->numberedArgs ),
121 array_keys( $this->namedArgs ) ) as $key ) {
122 $arguments[$key] = $this->getArgument( $key );
123 }
124 return $arguments;
125 }
126
130 public function getNumberedArguments() {
131 $arguments = [];
132 foreach ( $this->numberedArgs as $key => $_ ) {
133 $arguments[$key] = $this->getArgument( $key );
134 }
135 return $arguments;
136 }
137
141 public function getNamedArguments() {
142 $arguments = [];
143 foreach ( $this->namedArgs as $key => $_ ) {
144 $arguments[$key] = $this->getArgument( $key );
145 }
146 return $arguments;
147 }
148
153 public function getNumberedArgument( $index ) {
154 if ( !isset( $this->numberedArgs[$index] ) ) {
155 return false;
156 }
157 if ( !isset( $this->numberedExpansionCache[$index] ) ) {
158 # No trimming for unnamed arguments
159 $this->numberedExpansionCache[$index] = $this->parent->expand(
160 $this->numberedArgs[$index],
162 );
163 }
164 return $this->numberedExpansionCache[$index];
165 }
166
171 public function getNamedArgument( $name ) {
172 if ( !isset( $this->namedArgs[$name] ) ) {
173 return false;
174 }
175 if ( !isset( $this->namedExpansionCache[$name] ) ) {
176 # Trim named arguments post-expand, for backwards compatibility
177 $this->namedExpansionCache[$name] = trim(
178 $this->parent->expand( $this->namedArgs[$name], PPFrame::STRIP_COMMENTS ) );
179 }
180 return $this->namedExpansionCache[$name];
181 }
182
187 public function getArgument( $name ) {
188 $text = $this->getNumberedArgument( $name );
189 if ( $text === false ) {
190 $text = $this->getNamedArgument( $name );
191 }
192 return $text;
193 }
194
200 public function isTemplate() {
201 return true;
202 }
203
204 public function setVolatile( $flag = true ) {
205 parent::setVolatile( $flag );
206 $this->parent->setVolatile( $flag );
207 }
208
209 public function setTTL( $ttl ) {
210 parent::setTTL( $ttl );
211 $this->parent->setTTL( $ttl );
212 }
213}
Represents a title within MediaWiki.
Definition Title.php:78
getPrefixedDBkey()
Get the prefixed database key form.
Definition Title.php:1849
An expansion frame, used as a context to expand the result of preprocessToObj()
expand( $root, $flags=0)
Preprocessor $preprocessor
isVolatile()
Get the volatile flag.
Expansion frame with template arguments.
cachedExpand( $key, $root, $flags=0)
isEmpty()
Returns true if there are no arguments in this frame.
isTemplate()
Return true if the frame is a template frame.
setVolatile( $flag=true)
Set the volatile flag.
__construct( $preprocessor, $parent=false, $numberedArgs=[], $namedArgs=[], $title=false)
const STRIP_COMMENTS
Definition PPFrame.php:33