3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
21 namespace MediaWiki\Parser
;
23 use MediaWiki\HookContainer\HookContainer
;
24 use MediaWiki\HookContainer\HookRunner
;
25 use MediaWiki\Language\Language
;
28 * Store information about magic words, and create/cache MagicWord objects.
30 * See docs/magicword.md.
32 * Possible future improvements:
33 * * Simultaneous searching for a number of magic words
34 * * $mObjects in shared memory
39 class MagicWordFactory
{
41 private bool $mVariableIDsInitialised = false;
44 private array $mVariableIDs = [
50 'currentmonthnamegen',
110 'numberofactiveusers',
131 private array $mDoubleUnderscoreIDs = [
140 'expectunusedcategory',
141 'expectunusedtemplate',
149 /** @var array<string,MagicWord> */
150 private array $mObjects = [];
151 private ?MagicWordArray
$mDoubleUnderscoreArray = null;
153 private Language
$contLang;
154 private HookRunner
$hookRunner;
157 * @internal For ServiceWiring only
159 public function __construct( Language
$contentLanguage, HookContainer
$hookContainer ) {
160 $this->contLang
= $contentLanguage;
161 $this->hookRunner
= new HookRunner( $hookContainer );
164 public function getContentLanguage(): Language
{
165 return $this->contLang
;
169 * Get a MagicWord object for a given internal ID
171 * @param string $id The internal name of the magic word
174 public function get( $id ): MagicWord
{
175 if ( !isset( $this->mObjects
[$id] ) ) {
176 $mw = new MagicWord( null, [], false, $this->contLang
);
178 $this->mObjects
[$id] = $mw;
180 return $this->mObjects
[$id];
184 * Get an array of parser variable IDs
188 public function getVariableIDs(): array {
189 if ( !$this->mVariableIDsInitialised
) {
191 $this->hookRunner
->onMagicWordwgVariableIDs( $this->mVariableIDs
);
192 $this->hookRunner
->onGetMagicVariableIDs( $this->mVariableIDs
);
193 $this->mVariableIDsInitialised
= true;
195 return $this->mVariableIDs
;
199 * Get an array of parser substitution modifier IDs
202 * @deprecated since 1.42, use {@see getSubstArray} instead
204 public function getSubstIDs(): array {
205 wfDeprecated( __METHOD__
, '1.42' );
206 return [ 'subst', 'safesubst' ];
210 * @internal for use in {@see Parser::braceSubstitution} only
212 public function getSubstArray(): MagicWordArray
{
213 return $this->newArray( [ 'subst', 'safesubst' ] );
217 * Allow external reads of TTL array
221 * @deprecated Since 1.40
223 public function getCacheTTL( $id ) {
228 * Get a MagicWordArray of double-underscore entities
230 public function getDoubleUnderscoreArray(): MagicWordArray
{
231 if ( $this->mDoubleUnderscoreArray
=== null ) {
232 $this->hookRunner
->onGetDoubleUnderscoreIDs( $this->mDoubleUnderscoreIDs
);
233 $this->mDoubleUnderscoreArray
= $this->newArray( $this->mDoubleUnderscoreIDs
);
235 return $this->mDoubleUnderscoreArray
;
239 * Get a new MagicWordArray with provided $names
241 * @param string[] $names
242 * @return MagicWordArray
244 public function newArray( array $names = [] ): MagicWordArray
{
245 return new MagicWordArray( $names, $this );
249 /** @deprecated class alias since 1.40 */
250 class_alias( MagicWordFactory
::class, 'MagicWordFactory' );