3 * See docs/magicword.txt.
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.
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.
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
25 * A factory that stores information about MagicWords, and creates them on demand with caching.
27 * Possible future improvements:
28 * * Simultaneous searching for a number of magic words
29 * * $mObjects in shared memory
34 class MagicWordFactory
{
38 private $mVariableIDsInitialised = false;
41 private $mVariableIDs = [
46 'currentmonthnamegen',
106 'numberofactiveusers',
122 /** Array of caching hints for ParserCache
123 * @var array [ string => int ]
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,
144 'localdayname' => 3600,
145 'localyear' => 86400,
148 'numberofarticles' => 3600,
149 'numberoffiles' => 3600,
150 'numberofedits' => 3600,
151 'currentweek' => 3600,
152 'currentdow' => 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,
167 private $mDoubleUnderscoreIDs = [
184 private $mSubstIDs = [
189 /** @var array [ string => MagicWord ] */
190 private $mObjects = [];
192 /** @var MagicWordArray */
193 private $mDoubleUnderscoreArray = null;
198 * Factory: creates an object representing an ID
200 * @param string $id The internal name of the magic word
204 public function get( $id ) {
205 if ( !isset( $this->mObjects
[$id] ) ) {
206 $mw = new MagicWord();
208 $this->mObjects
[$id] = $mw;
210 return $this->mObjects
[$id];
214 * Get an array of parser variable IDs
218 public function getVariableIDs() {
219 if ( !$this->mVariableIDsInitialised
) {
221 Hooks
::run( 'MagicWordwgVariableIDs', [ &$this->mVariableIDs
] );
222 $this->mVariableIDsInitialised
= true;
224 return $this->mVariableIDs
;
228 * Get an array of parser substitution modifier IDs
231 public function getSubstIDs() {
232 return $this->mSubstIDs
;
236 * Allow external reads of TTL array
241 public function getCacheTTL( $id ) {
242 if ( array_key_exists( $id, $this->mCacheTTLs
) ) {
243 return $this->mCacheTTLs
[$id];
250 * Get a MagicWordArray of double-underscore entities
252 * @return MagicWordArray
254 public function getDoubleUnderscoreArray() {
255 if ( is_null( $this->mDoubleUnderscoreArray
) ) {
256 Hooks
::run( 'GetDoubleUnderscoreIDs', [ &$this->mDoubleUnderscoreIDs
] );
257 $this->mDoubleUnderscoreArray
= $this->newArray( $this->mDoubleUnderscoreIDs
);
259 return $this->mDoubleUnderscoreArray
;
263 * Get a new MagicWordArray with provided $names
265 * @param array $names
266 * @return MagicWordArray
268 public function newArray( array $names = [] ) : MagicWordArray
{
269 return new MagicWordArray( $names, $this );