PageRenderTime 45ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/includes/parser/ParserOutput.php

https://github.com/daevid/MWFork
PHP | 447 lines | 281 code | 49 blank | 117 comment | 33 complexity | 2adb6be266ace8f3cda9892460f33029 MD5 | raw file
  1. <?php
  2. /**
  3. * Output of the PHP parser
  4. *
  5. * @file
  6. * @ingroup Parser
  7. */
  8. /**
  9. * @todo document
  10. * @ingroup Parser
  11. */
  12. class CacheTime {
  13. var $mVersion = Parser::VERSION, # Compatibility check
  14. $mCacheTime = '', # Time when this object was generated, or -1 for uncacheable. Used in ParserCache.
  15. $mCacheExpiry = null, # Seconds after which the object should expire, use 0 for uncachable. Used in ParserCache.
  16. $mContainsOldMagic; # Boolean variable indicating if the input contained variables like {{CURRENTDAY}}
  17. function getCacheTime() { return $this->mCacheTime; }
  18. function containsOldMagic() { return $this->mContainsOldMagic; }
  19. function setContainsOldMagic( $com ) { return wfSetVar( $this->mContainsOldMagic, $com ); }
  20. /**
  21. * setCacheTime() sets the timestamp expressing when the page has been rendered.
  22. * This doesn not control expiry, see updateCacheExpiry() for that!
  23. * @param $t string
  24. * @return string
  25. */
  26. function setCacheTime( $t ) { return wfSetVar( $this->mCacheTime, $t ); }
  27. /**
  28. * Sets the number of seconds after which this object should expire.
  29. * This value is used with the ParserCache.
  30. * If called with a value greater than the value provided at any previous call,
  31. * the new call has no effect. The value returned by getCacheExpiry is smaller
  32. * or equal to the smallest number that was provided as an argument to
  33. * updateCacheExpiry().
  34. *
  35. * @param $seconds number
  36. */
  37. function updateCacheExpiry( $seconds ) {
  38. $seconds = (int)$seconds;
  39. if ( $this->mCacheExpiry === null || $this->mCacheExpiry > $seconds ) {
  40. $this->mCacheExpiry = $seconds;
  41. }
  42. // hack: set old-style marker for uncacheable entries.
  43. if ( $this->mCacheExpiry !== null && $this->mCacheExpiry <= 0 ) {
  44. $this->mCacheTime = -1;
  45. }
  46. }
  47. /**
  48. * Returns the number of seconds after which this object should expire.
  49. * This method is used by ParserCache to determine how long the ParserOutput can be cached.
  50. * The timestamp of expiry can be calculated by adding getCacheExpiry() to getCacheTime().
  51. * The value returned by getCacheExpiry is smaller or equal to the smallest number
  52. * that was provided to a call of updateCacheExpiry(), and smaller or equal to the
  53. * value of $wgParserCacheExpireTime.
  54. */
  55. function getCacheExpiry() {
  56. global $wgParserCacheExpireTime;
  57. if ( $this->mCacheTime < 0 ) {
  58. return 0;
  59. } // old-style marker for "not cachable"
  60. $expire = $this->mCacheExpiry;
  61. if ( $expire === null ) {
  62. $expire = $wgParserCacheExpireTime;
  63. } else {
  64. $expire = min( $expire, $wgParserCacheExpireTime );
  65. }
  66. if( $this->containsOldMagic() ) { //compatibility hack
  67. $expire = min( $expire, 3600 ); # 1 hour
  68. }
  69. if ( $expire <= 0 ) {
  70. return 0; // not cachable
  71. } else {
  72. return $expire;
  73. }
  74. }
  75. /**
  76. * @return bool
  77. */
  78. function isCacheable() {
  79. return $this->getCacheExpiry() > 0;
  80. }
  81. /**
  82. * Return true if this cached output object predates the global or
  83. * per-article cache invalidation timestamps, or if it comes from
  84. * an incompatible older version.
  85. *
  86. * @param $touched String: the affected article's last touched timestamp
  87. * @return Boolean
  88. */
  89. public function expired( $touched ) {
  90. global $wgCacheEpoch;
  91. return !$this->isCacheable() || // parser says it's uncacheable
  92. $this->getCacheTime() < $touched ||
  93. $this->getCacheTime() <= $wgCacheEpoch ||
  94. $this->getCacheTime() < wfTimestamp( TS_MW, time() - $this->getCacheExpiry() ) || // expiry period has passed
  95. !isset( $this->mVersion ) ||
  96. version_compare( $this->mVersion, Parser::VERSION, "lt" );
  97. }
  98. }
  99. class ParserOutput extends CacheTime {
  100. var $mText, # The output text
  101. $mLanguageLinks, # List of the full text of language links, in the order they appear
  102. $mCategories, # Map of category names to sort keys
  103. $mTitleText, # title text of the chosen language variant
  104. $mLinks = array(), # 2-D map of NS/DBK to ID for the links in the document. ID=zero for broken.
  105. $mTemplates = array(), # 2-D map of NS/DBK to ID for the template references. ID=zero for broken.
  106. $mTemplateIds = array(), # 2-D map of NS/DBK to rev ID for the template references. ID=zero for broken.
  107. $mImages = array(), # DB keys of the images used, in the array key only
  108. $mImageTimeKeys = array(), # DB keys of the images used mapped to sha1 and MW timestamp
  109. $mExternalLinks = array(), # External link URLs, in the key only
  110. $mInterwikiLinks = array(), # 2-D map of prefix/DBK (in keys only) for the inline interwiki links in the document.
  111. $mNewSection = false, # Show a new section link?
  112. $mHideNewSection = false, # Hide the new section link?
  113. $mNoGallery = false, # No gallery on category page? (__NOGALLERY__)
  114. $mHeadItems = array(), # Items to put in the <head> section
  115. $mModules = array(), # Modules to be loaded by the resource loader
  116. $mModuleScripts = array(), # Modules of which only the JS will be loaded by the resource loader
  117. $mModuleStyles = array(), # Modules of which only the CSSS will be loaded by the resource loader
  118. $mModuleMessages = array(), # Modules of which only the messages will be loaded by the resource loader
  119. $mOutputHooks = array(), # Hook tags as per $wgParserOutputHooks
  120. $mWarnings = array(), # Warning text to be returned to the user. Wikitext formatted, in the key only
  121. $mSections = array(), # Table of contents
  122. $mEditSectionTokens = false, # prefix/suffix markers if edit sections were output as tokens
  123. $mProperties = array(), # Name/value pairs to be cached in the DB
  124. $mTOCHTML = ''; # HTML of the TOC
  125. private $mIndexPolicy = ''; # 'index' or 'noindex'? Any other value will result in no change.
  126. private $mAccessedOptions = array(); # List of ParserOptions (stored in the keys)
  127. const EDITSECTION_REGEX = '#<(?:mw:)?editsection page="(.*?)" section="(.*?)"(?:/>|>(.*?)(</(?:mw:)?editsection>))#';
  128. function __construct( $text = '', $languageLinks = array(), $categoryLinks = array(),
  129. $containsOldMagic = false, $titletext = '' )
  130. {
  131. $this->mText = $text;
  132. $this->mLanguageLinks = $languageLinks;
  133. $this->mCategories = $categoryLinks;
  134. $this->mContainsOldMagic = $containsOldMagic;
  135. $this->mTitleText = $titletext;
  136. }
  137. function getText() {
  138. if ( $this->mEditSectionTokens ) {
  139. return preg_replace_callback( ParserOutput::EDITSECTION_REGEX,
  140. array( &$this, 'replaceEditSectionLinksCallback' ), $this->mText );
  141. }
  142. return $this->mText;
  143. }
  144. /**
  145. * callback used by getText to replace editsection tokens
  146. * @private
  147. */
  148. function replaceEditSectionLinksCallback( $m ) {
  149. global $wgOut, $wgLang;
  150. $args = array(
  151. htmlspecialchars_decode($m[1]),
  152. htmlspecialchars_decode($m[2]),
  153. isset($m[4]) ? $m[3] : null,
  154. );
  155. $args[0] = Title::newFromText( $args[0] );
  156. if ( !is_object($args[0]) ) {
  157. throw new MWException("Bad parser output text.");
  158. }
  159. $args[] = $wgLang->getCode();
  160. $skin = $wgOut->getSkin();
  161. return call_user_func_array( array( $skin, 'doEditSectionLink' ), $args );
  162. }
  163. function &getLanguageLinks() { return $this->mLanguageLinks; }
  164. function getInterwikiLinks() { return $this->mInterwikiLinks; }
  165. function getCategoryLinks() { return array_keys( $this->mCategories ); }
  166. function &getCategories() { return $this->mCategories; }
  167. function getTitleText() { return $this->mTitleText; }
  168. function getSections() { return $this->mSections; }
  169. function getEditSectionTokens() { return $this->mEditSectionTokens; }
  170. function &getLinks() { return $this->mLinks; }
  171. function &getTemplates() { return $this->mTemplates; }
  172. function &getTemplateIds() { return $this->mTemplateIds; }
  173. function &getImages() { return $this->mImages; }
  174. function &getImageTimeKeys() { return $this->mImageTimeKeys; }
  175. function &getExternalLinks() { return $this->mExternalLinks; }
  176. function getNoGallery() { return $this->mNoGallery; }
  177. function getHeadItems() { return $this->mHeadItems; }
  178. function getModules() { return $this->mModules; }
  179. function getModuleScripts() { return $this->mModuleScripts; }
  180. function getModuleStyles() { return $this->mModuleStyles; }
  181. function getModuleMessages() { return $this->mModuleMessages; }
  182. function getOutputHooks() { return (array)$this->mOutputHooks; }
  183. function getWarnings() { return array_keys( $this->mWarnings ); }
  184. function getIndexPolicy() { return $this->mIndexPolicy; }
  185. function getTOCHTML() { return $this->mTOCHTML; }
  186. function setText( $text ) { return wfSetVar( $this->mText, $text ); }
  187. function setLanguageLinks( $ll ) { return wfSetVar( $this->mLanguageLinks, $ll ); }
  188. function setCategoryLinks( $cl ) { return wfSetVar( $this->mCategories, $cl ); }
  189. function setTitleText( $t ) { return wfSetVar( $this->mTitleText, $t ); }
  190. function setSections( $toc ) { return wfSetVar( $this->mSections, $toc ); }
  191. function setEditSectionTokens( $t ) { return wfSetVar( $this->mEditSectionTokens, $t ); }
  192. function setIndexPolicy( $policy ) { return wfSetVar( $this->mIndexPolicy, $policy ); }
  193. function setTOCHTML( $tochtml ) { return wfSetVar( $this->mTOCHTML, $tochtml ); }
  194. function addCategory( $c, $sort ) { $this->mCategories[$c] = $sort; }
  195. function addLanguageLink( $t ) { $this->mLanguageLinks[] = $t; }
  196. function addWarning( $s ) { $this->mWarnings[$s] = 1; }
  197. function addOutputHook( $hook, $data = false ) {
  198. $this->mOutputHooks[] = array( $hook, $data );
  199. }
  200. function setNewSection( $value ) {
  201. $this->mNewSection = (bool)$value;
  202. }
  203. function hideNewSection ( $value ) {
  204. $this->mHideNewSection = (bool)$value;
  205. }
  206. function getHideNewSection () {
  207. return (bool)$this->mHideNewSection;
  208. }
  209. function getNewSection() {
  210. return (bool)$this->mNewSection;
  211. }
  212. function addExternalLink( $url ) {
  213. # We don't register links pointing to our own server, unless... :-)
  214. global $wgServer, $wgRegisterInternalExternals;
  215. if( $wgRegisterInternalExternals or stripos($url,$wgServer.'/')!==0)
  216. $this->mExternalLinks[$url] = 1;
  217. }
  218. /**
  219. * Record a local or interwiki inline link for saving in future link tables.
  220. *
  221. * @param $title Title object
  222. * @param $id Mixed: optional known page_id so we can skip the lookup
  223. */
  224. function addLink( $title, $id = null ) {
  225. if ( $title->isExternal() ) {
  226. // Don't record interwikis in pagelinks
  227. $this->addInterwikiLink( $title );
  228. return;
  229. }
  230. $ns = $title->getNamespace();
  231. $dbk = $title->getDBkey();
  232. if ( $ns == NS_MEDIA ) {
  233. // Normalize this pseudo-alias if it makes it down here...
  234. $ns = NS_FILE;
  235. } elseif( $ns == NS_SPECIAL ) {
  236. // We don't record Special: links currently
  237. // It might actually be wise to, but we'd need to do some normalization.
  238. return;
  239. } elseif( $dbk === '' ) {
  240. // Don't record self links - [[#Foo]]
  241. return;
  242. }
  243. if ( !isset( $this->mLinks[$ns] ) ) {
  244. $this->mLinks[$ns] = array();
  245. }
  246. if ( is_null( $id ) ) {
  247. $id = $title->getArticleID();
  248. }
  249. $this->mLinks[$ns][$dbk] = $id;
  250. }
  251. /**
  252. * Register a file dependency for this output
  253. * @param $name string Title dbKey
  254. * @param $timestamp string MW timestamp of file creation (or false if non-existing)
  255. * @param $sha string base 36 SHA-1 of file (or false if non-existing)
  256. * @return void
  257. */
  258. function addImage( $name, $timestamp = null, $sha1 = null ) {
  259. $this->mImages[$name] = 1;
  260. if ( $timestamp !== null && $sha1 !== null ) {
  261. $this->mImageTimeKeys[$name] = array( 'time' => $timestamp, 'sha1' => $sha1 );
  262. }
  263. }
  264. /**
  265. * Register a template dependency for this output
  266. * @param $title Title
  267. * @param $page_id
  268. * @param $rev_id
  269. * @return void
  270. */
  271. function addTemplate( $title, $page_id, $rev_id ) {
  272. $ns = $title->getNamespace();
  273. $dbk = $title->getDBkey();
  274. if ( !isset( $this->mTemplates[$ns] ) ) {
  275. $this->mTemplates[$ns] = array();
  276. }
  277. $this->mTemplates[$ns][$dbk] = $page_id;
  278. if ( !isset( $this->mTemplateIds[$ns] ) ) {
  279. $this->mTemplateIds[$ns] = array();
  280. }
  281. $this->mTemplateIds[$ns][$dbk] = $rev_id; // For versioning
  282. }
  283. /**
  284. * @param $title Title object, must be an interwiki link
  285. * @throws MWException if given invalid input
  286. */
  287. function addInterwikiLink( $title ) {
  288. $prefix = $title->getInterwiki();
  289. if( $prefix == '' ) {
  290. throw new MWException( 'Non-interwiki link passed, internal parser error.' );
  291. }
  292. if (!isset($this->mInterwikiLinks[$prefix])) {
  293. $this->mInterwikiLinks[$prefix] = array();
  294. }
  295. $this->mInterwikiLinks[$prefix][$title->getDBkey()] = 1;
  296. }
  297. /**
  298. * Add some text to the <head>.
  299. * If $tag is set, the section with that tag will only be included once
  300. * in a given page.
  301. */
  302. function addHeadItem( $section, $tag = false ) {
  303. if ( $tag !== false ) {
  304. $this->mHeadItems[$tag] = $section;
  305. } else {
  306. $this->mHeadItems[] = $section;
  307. }
  308. }
  309. public function addModules( $modules ) {
  310. $this->mModules = array_merge( $this->mModules, (array) $modules );
  311. }
  312. public function addModuleScripts( $modules ) {
  313. $this->mModuleScripts = array_merge( $this->mModuleScripts, (array)$modules );
  314. }
  315. public function addModuleStyles( $modules ) {
  316. $this->mModuleStyles = array_merge( $this->mModuleStyles, (array)$modules );
  317. }
  318. public function addModuleMessages( $modules ) {
  319. $this->mModuleMessages = array_merge( $this->mModuleMessages, (array)$modules );
  320. }
  321. /**
  322. * Copy items from the OutputPage object into this one
  323. *
  324. * @param $out OutputPage object
  325. */
  326. public function addOutputPageMetadata( OutputPage $out ) {
  327. $this->addModules( $out->getModules() );
  328. $this->addModuleScripts( $out->getModuleScripts() );
  329. $this->addModuleStyles( $out->getModuleStyles() );
  330. $this->addModuleMessages( $out->getModuleMessages() );
  331. $this->mHeadItems = array_merge( $this->mHeadItems, $out->getHeadItemsArray() );
  332. }
  333. /**
  334. * Override the title to be used for display
  335. * -- this is assumed to have been validated
  336. * (check equal normalisation, etc.)
  337. *
  338. * @param $text String: desired title text
  339. */
  340. public function setDisplayTitle( $text ) {
  341. $this->setTitleText( $text );
  342. $this->setProperty( 'displaytitle', $text );
  343. }
  344. /**
  345. * Get the title to be used for display
  346. *
  347. * @return String
  348. */
  349. public function getDisplayTitle() {
  350. $t = $this->getTitleText();
  351. if( $t === '' ) {
  352. return false;
  353. }
  354. return $t;
  355. }
  356. /**
  357. * Fairly generic flag setter thingy.
  358. */
  359. public function setFlag( $flag ) {
  360. $this->mFlags[$flag] = true;
  361. }
  362. public function getFlag( $flag ) {
  363. return isset( $this->mFlags[$flag] );
  364. }
  365. /**
  366. * Set a property to be cached in the DB
  367. */
  368. public function setProperty( $name, $value ) {
  369. $this->mProperties[$name] = $value;
  370. }
  371. public function getProperty( $name ){
  372. return isset( $this->mProperties[$name] ) ? $this->mProperties[$name] : false;
  373. }
  374. public function getProperties() {
  375. if ( !isset( $this->mProperties ) ) {
  376. $this->mProperties = array();
  377. }
  378. return $this->mProperties;
  379. }
  380. /**
  381. * Returns the options from its ParserOptions which have been taken
  382. * into account to produce this output or false if not available.
  383. * @return mixed Array
  384. */
  385. public function getUsedOptions() {
  386. if ( !isset( $this->mAccessedOptions ) ) {
  387. return array();
  388. }
  389. return array_keys( $this->mAccessedOptions );
  390. }
  391. /**
  392. * Callback passed by the Parser to the ParserOptions to keep track of which options are used.
  393. * @access private
  394. */
  395. function recordOption( $option ) {
  396. $this->mAccessedOptions[$option] = true;
  397. }
  398. }