MagicWordFactory.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. 'index',
  172. 'noindex',
  173. 'staticredirect',
  174. 'notitleconvert',
  175. 'nocontentconvert',
  176. ];
  177. /** @var string[] */
  178. private $mSubstIDs = [
  179. 'subst',
  180. 'safesubst',
  181. ];
  182. /** @var array [ string => MagicWord ] */
  183. private $mObjects = [];
  184. /** @var MagicWordArray */
  185. private $mDoubleUnderscoreArray = null;
  186. /** @var Language */
  187. private $contLang;
  188. /**#@-*/
  189. /**
  190. * @param Language $contLang Content language
  191. */
  192. public function __construct( Language $contLang ) {
  193. $this->contLang = $contLang;
  194. }
  195. public function getContentLanguage() {
  196. return $this->contLang;
  197. }
  198. /**
  199. * Factory: creates an object representing an ID
  200. *
  201. * @param string $id The internal name of the magic word
  202. *
  203. * @return MagicWord
  204. */
  205. public function get( $id ) {
  206. if ( !isset( $this->mObjects[$id] ) ) {
  207. $mw = new MagicWord( null, [], false, $this->contLang );
  208. $mw->load( $id );
  209. $this->mObjects[$id] = $mw;
  210. }
  211. return $this->mObjects[$id];
  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. * Get an array of parser substitution modifier IDs
  228. * @return string[]
  229. */
  230. public function getSubstIDs() {
  231. return $this->mSubstIDs;
  232. }
  233. /**
  234. * Allow external reads of TTL array
  235. *
  236. * @param string $id
  237. * @return int
  238. */
  239. public function getCacheTTL( $id ) {
  240. if ( array_key_exists( $id, $this->mCacheTTLs ) ) {
  241. return $this->mCacheTTLs[$id];
  242. } else {
  243. return -1;
  244. }
  245. }
  246. /**
  247. * Get a MagicWordArray of double-underscore entities
  248. *
  249. * @return MagicWordArray
  250. */
  251. public function getDoubleUnderscoreArray() {
  252. if ( is_null( $this->mDoubleUnderscoreArray ) ) {
  253. Hooks::run( 'GetDoubleUnderscoreIDs', [ &$this->mDoubleUnderscoreIDs ] );
  254. $this->mDoubleUnderscoreArray = $this->newArray( $this->mDoubleUnderscoreIDs );
  255. }
  256. return $this->mDoubleUnderscoreArray;
  257. }
  258. /**
  259. * Get a new MagicWordArray with provided $names
  260. *
  261. * @param array $names
  262. * @return MagicWordArray
  263. */
  264. public function newArray( array $names = [] ) : MagicWordArray {
  265. return new MagicWordArray( $names, $this );
  266. }
  267. }