Merge "Clarify fileexists-extension message"
[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 function __construct( $parser );
34
35 /**
36 * Create a new top-level frame for expansion of a page
37 *
38 * @return PPFrame
39 */
40 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 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 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 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
81 const RECOVER_ORIG = 27; // = 1|2|8|16 no constant expression support in PHP yet
82
83 /** This constant exists when $indexOffset is supported in newChild() */
84 const SUPPORTS_INDEX_OFFSET = 1;
85
86 /**
87 * Create a child frame
88 *
89 * @param array|bool $args
90 * @param bool|Title $title
91 * @param int $indexOffset A number subtracted from the index attributes of the arguments
92 *
93 * @return PPFrame
94 */
95 function newChild( $args = false, $title = false, $indexOffset = 0 );
96
97 /**
98 * Expand a document tree node, caching the result on its parent with the given key
99 */
100 function cachedExpand( $key, $root, $flags = 0 );
101
102 /**
103 * Expand a document tree node
104 */
105 function expand( $root, $flags = 0 );
106
107 /**
108 * Implode with flags for expand()
109 */
110 function implodeWithFlags( $sep, $flags /*, ... */ );
111
112 /**
113 * Implode with no flags specified
114 */
115 function implode( $sep /*, ... */ );
116
117 /**
118 * Makes an object that, when expand()ed, will be the same as one obtained
119 * with implode()
120 */
121 function virtualImplode( $sep /*, ... */ );
122
123 /**
124 * Virtual implode with brackets
125 */
126 function virtualBracketedImplode( $start, $sep, $end /*, ... */ );
127
128 /**
129 * Returns true if there are no arguments in this frame
130 *
131 * @return bool
132 */
133 function isEmpty();
134
135 /**
136 * Returns all arguments of this frame
137 */
138 function getArguments();
139
140 /**
141 * Returns all numbered arguments of this frame
142 */
143 function getNumberedArguments();
144
145 /**
146 * Returns all named arguments of this frame
147 */
148 function getNamedArguments();
149
150 /**
151 * Get an argument to this frame by name
152 */
153 function getArgument( $name );
154
155 /**
156 * Returns true if the infinite loop check is OK, false if a loop is detected
157 *
158 * @param Title $title
159 * @return bool
160 */
161 function loopCheck( $title );
162
163 /**
164 * Return true if the frame is a template frame
165 */
166 function isTemplate();
167
168 /**
169 * Set the "volatile" flag.
170 *
171 * Note that this is somewhat of a "hack" in order to make extensions
172 * with side effects (such as Cite) work with the PHP parser. New
173 * extensions should be written in a way that they do not need this
174 * function, because other parsers (such as Parsoid) are not guaranteed
175 * to respect it, and it may be removed in the future.
176 *
177 * @param bool $flag
178 */
179 function setVolatile( $flag = true );
180
181 /**
182 * Get the "volatile" flag.
183 *
184 * Callers should avoid caching the result of an expansion if it has the
185 * volatile flag set.
186 *
187 * @see self::setVolatile()
188 * @return bool
189 */
190 function isVolatile();
191
192 /**
193 * Get the TTL of the frame's output.
194 *
195 * This is the maximum amount of time, in seconds, that this frame's
196 * output should be cached for. A value of null indicates that no
197 * maximum has been specified.
198 *
199 * Note that this TTL only applies to caching frames as parts of pages.
200 * It is not relevant to caching the entire rendered output of a page.
201 *
202 * @return int|null
203 */
204 function getTTL();
205
206 /**
207 * Set the TTL of the output of this frame and all of its ancestors.
208 * Has no effect if the new TTL is greater than the one already set.
209 * Note that it is the caller's responsibility to change the cache
210 * expiry of the page as a whole, if such behavior is desired.
211 *
212 * @see self::getTTL()
213 * @param int $ttl
214 */
215 function setTTL( $ttl );
216
217 /**
218 * Get a title of frame
219 *
220 * @return Title
221 */
222 function getTitle();
223 }
224
225 /**
226 * There are three types of nodes:
227 * * Tree nodes, which have a name and contain other nodes as children
228 * * Array nodes, which also contain other nodes but aren't considered part of a tree
229 * * Leaf nodes, which contain the actual data
230 *
231 * This interface provides access to the tree structure and to the contents of array nodes,
232 * but it does not provide access to the internal structure of leaf nodes. Access to leaf
233 * data is provided via two means:
234 * * PPFrame::expand(), which provides expanded text
235 * * The PPNode::split*() functions, which provide metadata about certain types of tree node
236 * @ingroup Parser
237 */
238 interface PPNode {
239 /**
240 * Get an array-type node containing the children of this node.
241 * Returns false if this is not a tree node.
242 */
243 function getChildren();
244
245 /**
246 * Get the first child of a tree node. False if there isn't one.
247 *
248 * @return PPNode
249 */
250 function getFirstChild();
251
252 /**
253 * Get the next sibling of any node. False if there isn't one
254 */
255 function getNextSibling();
256
257 /**
258 * Get all children of this tree node which have a given name.
259 * Returns an array-type node, or false if this is not a tree node.
260 */
261 function getChildrenOfType( $type );
262
263 /**
264 * Returns the length of the array, or false if this is not an array-type node
265 */
266 function getLength();
267
268 /**
269 * Returns an item of an array-type node
270 */
271 function item( $i );
272
273 /**
274 * Get the name of this node. The following names are defined here:
275 *
276 * h A heading node.
277 * template A double-brace node.
278 * tplarg A triple-brace node.
279 * title The first argument to a template or tplarg node.
280 * part Subsequent arguments to a template or tplarg node.
281 * #nodelist An array-type node
282 *
283 * The subclass may define various other names for tree and leaf nodes.
284 */
285 function getName();
286
287 /**
288 * Split a "<part>" node into an associative array containing:
289 * name PPNode name
290 * index String index
291 * value PPNode value
292 */
293 function splitArg();
294
295 /**
296 * Split an "<ext>" node into an associative array containing name, attr, inner and close
297 * All values in the resulting array are PPNodes. Inner and close are optional.
298 */
299 function splitExt();
300
301 /**
302 * Split an "<h>" node
303 */
304 function splitHeading();
305 }