Merge "Update categoriespagetext message"
[lhc/web/wiklou.git] / includes / MagicWordFactory.php
1 <?php
2 /**
3 * See docs/magicword.txt.
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 * A factory that stores information about MagicWords, and creates them on demand with caching.
26 *
27 * Possible future improvements:
28 * * Simultaneous searching for a number of magic words
29 * * $mObjects in shared memory
30 *
31 * @since 1.32
32 * @ingroup Parser
33 */
34 class MagicWordFactory {
35 /**#@-*/
36
37 /** @var bool */
38 private $mVariableIDsInitialised = false;
39
40 /** @var string[] */
41 private $mVariableIDs = [
42 '!',
43 'currentmonth',
44 'currentmonth1',
45 'currentmonthname',
46 'currentmonthnamegen',
47 'currentmonthabbrev',
48 'currentday',
49 'currentday2',
50 'currentdayname',
51 'currentyear',
52 'currenttime',
53 'currenthour',
54 'localmonth',
55 'localmonth1',
56 'localmonthname',
57 'localmonthnamegen',
58 'localmonthabbrev',
59 'localday',
60 'localday2',
61 'localdayname',
62 'localyear',
63 'localtime',
64 'localhour',
65 'numberofarticles',
66 'numberoffiles',
67 'numberofedits',
68 'articlepath',
69 'pageid',
70 'sitename',
71 'server',
72 'servername',
73 'scriptpath',
74 'stylepath',
75 'pagename',
76 'pagenamee',
77 'fullpagename',
78 'fullpagenamee',
79 'namespace',
80 'namespacee',
81 'namespacenumber',
82 'currentweek',
83 'currentdow',
84 'localweek',
85 'localdow',
86 'revisionid',
87 'revisionday',
88 'revisionday2',
89 'revisionmonth',
90 'revisionmonth1',
91 'revisionyear',
92 'revisiontimestamp',
93 'revisionuser',
94 'revisionsize',
95 'subpagename',
96 'subpagenamee',
97 'talkspace',
98 'talkspacee',
99 'subjectspace',
100 'subjectspacee',
101 'talkpagename',
102 'talkpagenamee',
103 'subjectpagename',
104 'subjectpagenamee',
105 'numberofusers',
106 'numberofactiveusers',
107 'numberofpages',
108 'currentversion',
109 'rootpagename',
110 'rootpagenamee',
111 'basepagename',
112 'basepagenamee',
113 'currenttimestamp',
114 'localtimestamp',
115 'directionmark',
116 'contentlanguage',
117 'pagelanguage',
118 'numberofadmins',
119 'cascadingsources',
120 ];
121
122 /** Array of caching hints for ParserCache
123 * @var array [ string => int ]
124 */
125 private $mCacheTTLs = [
126 'currentmonth' => 86400,
127 'currentmonth1' => 86400,
128 'currentmonthname' => 86400,
129 'currentmonthnamegen' => 86400,
130 'currentmonthabbrev' => 86400,
131 'currentday' => 3600,
132 'currentday2' => 3600,
133 'currentdayname' => 3600,
134 'currentyear' => 86400,
135 'currenttime' => 3600,
136 'currenthour' => 3600,
137 'localmonth' => 86400,
138 'localmonth1' => 86400,
139 'localmonthname' => 86400,
140 'localmonthnamegen' => 86400,
141 'localmonthabbrev' => 86400,
142 'localday' => 3600,
143 'localday2' => 3600,
144 'localdayname' => 3600,
145 'localyear' => 86400,
146 'localtime' => 3600,
147 'localhour' => 3600,
148 'numberofarticles' => 3600,
149 'numberoffiles' => 3600,
150 'numberofedits' => 3600,
151 'currentweek' => 3600,
152 'currentdow' => 3600,
153 'localweek' => 3600,
154 'localdow' => 3600,
155 'numberofusers' => 3600,
156 'numberofactiveusers' => 3600,
157 'numberofpages' => 3600,
158 'currentversion' => 86400,
159 'currenttimestamp' => 3600,
160 'localtimestamp' => 3600,
161 'pagesinnamespace' => 3600,
162 'numberofadmins' => 3600,
163 'numberingroup' => 3600,
164 ];
165
166 /** @var string[] */
167 private $mDoubleUnderscoreIDs = [
168 'notoc',
169 'nogallery',
170 'forcetoc',
171 'toc',
172 'noeditsection',
173 'newsectionlink',
174 'nonewsectionlink',
175 'hiddencat',
176 'index',
177 'noindex',
178 'staticredirect',
179 'notitleconvert',
180 'nocontentconvert',
181 ];
182
183 /** @var string[] */
184 private $mSubstIDs = [
185 'subst',
186 'safesubst',
187 ];
188
189 /** @var array [ string => MagicWord ] */
190 private $mObjects = [];
191
192 /** @var MagicWordArray */
193 private $mDoubleUnderscoreArray = null;
194
195 /**#@-*/
196
197 /**
198 * Factory: creates an object representing an ID
199 *
200 * @param string $id The internal name of the magic word
201 *
202 * @return MagicWord
203 */
204 public function get( $id ) {
205 if ( !isset( $this->mObjects[$id] ) ) {
206 $mw = new MagicWord();
207 $mw->load( $id );
208 $this->mObjects[$id] = $mw;
209 }
210 return $this->mObjects[$id];
211 }
212
213 /**
214 * Get an array of parser variable IDs
215 *
216 * @return string[]
217 */
218 public function getVariableIDs() {
219 if ( !$this->mVariableIDsInitialised ) {
220 # Get variable IDs
221 Hooks::run( 'MagicWordwgVariableIDs', [ &$this->mVariableIDs ] );
222 $this->mVariableIDsInitialised = true;
223 }
224 return $this->mVariableIDs;
225 }
226
227 /**
228 * Get an array of parser substitution modifier IDs
229 * @return string[]
230 */
231 public function getSubstIDs() {
232 return $this->mSubstIDs;
233 }
234
235 /**
236 * Allow external reads of TTL array
237 *
238 * @param string $id
239 * @return int
240 */
241 public function getCacheTTL( $id ) {
242 if ( array_key_exists( $id, $this->mCacheTTLs ) ) {
243 return $this->mCacheTTLs[$id];
244 } else {
245 return -1;
246 }
247 }
248
249 /**
250 * Get a MagicWordArray of double-underscore entities
251 *
252 * @return MagicWordArray
253 */
254 public function getDoubleUnderscoreArray() {
255 if ( is_null( $this->mDoubleUnderscoreArray ) ) {
256 Hooks::run( 'GetDoubleUnderscoreIDs', [ &$this->mDoubleUnderscoreIDs ] );
257 $this->mDoubleUnderscoreArray = $this->newArray( $this->mDoubleUnderscoreIDs );
258 }
259 return $this->mDoubleUnderscoreArray;
260 }
261
262 /**
263 * Get a new MagicWordArray with provided $names
264 *
265 * @param array $names
266 * @return MagicWordArray
267 */
268 public function newArray( array $names = [] ) : MagicWordArray {
269 return new MagicWordArray( $names, $this );
270 }
271 }