MagicWordFactory.php 6.2 KB

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