Merge "Use camel case for variable names in Article.php"
[lhc/web/wiklou.git] / includes / parser / Preprocessor.php
1 <?php
2 /**
3 * Interfaces for preprocessors
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Parser
22 */
23
24 /**
25 * @ingroup Parser
26 */
27 interface Preprocessor {
28 /**
29 * Create a new preprocessor object based on an initialised Parser object
30 *
31 * @param Parser $parser
32 */
33 public function __construct( $parser );
34
35 /**
36 * Create a new top-level frame for expansion of a page
37 *
38 * @return PPFrame
39 */
40 public function newFrame();
41
42 /**
43 * Create a new custom frame for programmatic use of parameter replacement
44 * as used in some extensions.
45 *
46 * @param array $args
47 *
48 * @return PPFrame
49 */
50 public function newCustomFrame( $args );
51
52 /**
53 * Create a new custom node for programmatic use of parameter replacement
54 * as used in some extensions.
55 *
56 * @param array $values
57 */
58 public function newPartNodeArray( $values );
59
60 /**
61 * Preprocess text to a PPNode
62 *
63 * @param string $text
64 * @param int $flags
65 *
66 * @return PPNode
67 */
68 public function preprocessToObj( $text, $flags = 0 );
69 }
70
71 /**
72 * @ingroup Parser
73 */
74 interface PPFrame {
75 const NO_ARGS = 1;
76 const NO_TEMPLATES = 2;
77 const STRIP_COMMENTS = 4;
78 const NO_IGNORE = 8;
79 const RECOVER_COMMENTS = 16;
80 const NO_TAGS = 32;
81
82 const RECOVER_ORIG = 59; // = 1|2|8|16|32 no constant expression support in PHP yet
83
84 /** This constant exists when $indexOffset is supported in newChild() */
85 const SUPPORTS_INDEX_OFFSET = 1;
86
87 /**
88 * Create a child frame
89 *
90 * @param array|bool $args
91 * @param bool|Title $title
92 * @param int $indexOffset A number subtracted from the index attributes of the arguments
93 *
94 * @return PPFrame
95 */
96 public function newChild( $args = false, $title = false, $indexOffset = 0 );
97
98 /**
99 * Expand a document tree node, caching the result on its parent with the given key
100 */
101 public function cachedExpand( $key, $root, $flags = 0 );
102
103 /**
104 * Expand a document tree node
105 */
106 public function expand( $root, $flags = 0 );
107
108 /**
109 * Implode with flags for expand()
110 */
111 public function implodeWithFlags( $sep, $flags /*, ... */ );
112
113 /**
114 * Implode with no flags specified
115 */
116 public function implode( $sep /*, ... */ );
117
118 /**
119 * Makes an object that, when expand()ed, will be the same as one obtained
120 * with implode()
121 */
122 public function virtualImplode( $sep /*, ... */ );
123
124 /**
125 * Virtual implode with brackets
126 */
127 public function virtualBracketedImplode( $start, $sep, $end /*, ... */ );
128
129 /**
130 * Returns true if there are no arguments in this frame
131 *
132 * @return bool
133 */
134 public function isEmpty();
135
136 /**
137 * Returns all arguments of this frame
138 */
139 public function getArguments();
140
141 /**
142 * Returns all numbered arguments of this frame
143 */
144 public function getNumberedArguments();
145
146 /**
147 * Returns all named arguments of this frame
148 */
149 public function getNamedArguments();
150
151 /**
152 * Get an argument to this frame by name
153 */
154 public function getArgument( $name );
155
156 /**
157 * Returns true if the infinite loop check is OK, false if a loop is detected
158 *
159 * @param Title $title
160 * @return bool
161 */
162 public function loopCheck( $title );
163
164 /**
165 * Return true if the frame is a template frame
166 */
167 public function isTemplate();
168
169 /**
170 * Set the "volatile" flag.
171 *
172 * Note that this is somewhat of a "hack" in order to make extensions
173 * with side effects (such as Cite) work with the PHP parser. New
174 * extensions should be written in a way that they do not need this
175 * function, because other parsers (such as Parsoid) are not guaranteed
176 * to respect it, and it may be removed in the future.
177 *
178 * @param bool $flag
179 */
180 public function setVolatile( $flag = true );
181
182 /**
183 * Get the "volatile" flag.
184 *
185 * Callers should avoid caching the result of an expansion if it has the
186 * volatile flag set.
187 *
188 * @see self::setVolatile()
189 * @return bool
190 */
191 public function isVolatile();
192
193 /**
194 * Get the TTL of the frame's output.
195 *
196 * This is the maximum amount of time, in seconds, that this frame's
197 * output should be cached for. A value of null indicates that no
198 * maximum has been specified.
199 *
200 * Note that this TTL only applies to caching frames as parts of pages.
201 * It is not relevant to caching the entire rendered output of a page.
202 *
203 * @return int|null
204 */
205 public function getTTL();
206
207 /**
208 * Set the TTL of the output of this frame and all of its ancestors.
209 * Has no effect if the new TTL is greater than the one already set.
210 * Note that it is the caller's responsibility to change the cache
211 * expiry of the page as a whole, if such behavior is desired.
212 *
213 * @see self::getTTL()
214 * @param int $ttl
215 */
216 public function setTTL( $ttl );
217
218 /**
219 * Get a title of frame
220 *
221 * @return Title
222 */
223 public function getTitle();
224 }
225
226 /**
227 * There are three types of nodes:
228 * * Tree nodes, which have a name and contain other nodes as children
229 * * Array nodes, which also contain other nodes but aren't considered part of a tree
230 * * Leaf nodes, which contain the actual data
231 *
232 * This interface provides access to the tree structure and to the contents of array nodes,
233 * but it does not provide access to the internal structure of leaf nodes. Access to leaf
234 * data is provided via two means:
235 * * PPFrame::expand(), which provides expanded text
236 * * The PPNode::split*() functions, which provide metadata about certain types of tree node
237 * @ingroup Parser
238 */
239 interface PPNode {
240 /**
241 * Get an array-type node containing the children of this node.
242 * Returns false if this is not a tree node.
243 */
244 public function getChildren();
245
246 /**
247 * Get the first child of a tree node. False if there isn't one.
248 *
249 * @return PPNode
250 */
251 public function getFirstChild();
252
253 /**
254 * Get the next sibling of any node. False if there isn't one
255 */
256 public function getNextSibling();
257
258 /**
259 * Get all children of this tree node which have a given name.
260 * Returns an array-type node, or false if this is not a tree node.
261 */
262 public function getChildrenOfType( $type );
263
264 /**
265 * Returns the length of the array, or false if this is not an array-type node
266 */
267 public function getLength();
268
269 /**
270 * Returns an item of an array-type node
271 */
272 public function item( $i );
273
274 /**
275 * Get the name of this node. The following names are defined here:
276 *
277 * h A heading node.
278 * template A double-brace node.
279 * tplarg A triple-brace node.
280 * title The first argument to a template or tplarg node.
281 * part Subsequent arguments to a template or tplarg node.
282 * #nodelist An array-type node
283 *
284 * The subclass may define various other names for tree and leaf nodes.
285 */
286 public function getName();
287
288 /**
289 * Split a "<part>" node into an associative array containing:
290 * name PPNode name
291 * index String index
292 * value PPNode value
293 */
294 public function splitArg();
295
296 /**
297 * Split an "<ext>" node into an associative array containing name, attr, inner and close
298 * All values in the resulting array are PPNodes. Inner and close are optional.
299 */
300 public function splitExt();
301
302 /**
303 * Split an "<h>" node
304 */
305 public function splitHeading();
306 }