PageRenderTime 56ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/phase3/includes/parser/ParserOutput.php

https://github.com/ChuguluGames/mediawiki-svn
PHP | 476 lines | 306 code | 52 blank | 118 comment | 38 complexity | 09fffafd1a6aeb07886e7f85e899caee 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. $mDistantTemplates = array(), # 3-D map of WIKIID/NS/DBK to ID for the template references. ID=zero for broken.
  108. $mDistantTemplateIds = array(), # 3-D map of WIKIID/NS/DBK to rev ID for the template references. ID=zero for broken.
  109. $mImages = array(), # DB keys of the images used, in the array key only
  110. $mImageTimeKeys = array(), # DB keys of the images used mapped to sha1 and MW timestamp
  111. $mExternalLinks = array(), # External link URLs, in the key only
  112. $mInterwikiLinks = array(), # 2-D map of prefix/DBK (in keys only) for the inline interwiki links in the document.
  113. $mNewSection = false, # Show a new section link?
  114. $mHideNewSection = false, # Hide the new section link?
  115. $mNoGallery = false, # No gallery on category page? (__NOGALLERY__)
  116. $mHeadItems = array(), # Items to put in the <head> section
  117. $mModules = array(), # Modules to be loaded by the resource loader
  118. $mModuleScripts = array(), # Modules of which only the JS will be loaded by the resource loader
  119. $mModuleStyles = array(), # Modules of which only the CSSS will be loaded by the resource loader
  120. $mModuleMessages = array(), # Modules of which only the messages will be loaded by the resource loader
  121. $mOutputHooks = array(), # Hook tags as per $wgParserOutputHooks
  122. $mWarnings = array(), # Warning text to be returned to the user. Wikitext formatted, in the key only
  123. $mSections = array(), # Table of contents
  124. $mEditSectionTokens = false, # prefix/suffix markers if edit sections were output as tokens
  125. $mProperties = array(), # Name/value pairs to be cached in the DB
  126. $mTOCHTML = ''; # HTML of the TOC
  127. private $mIndexPolicy = ''; # 'index' or 'noindex'? Any other value will result in no change.
  128. private $mAccessedOptions = array(); # List of ParserOptions (stored in the keys)
  129. const EDITSECTION_REGEX = '#<(?:mw:)?editsection page="(.*?)" section="(.*?)"(?:/>|>(.*?)(</(?:mw:)?editsection>))#';
  130. function __construct( $text = '', $languageLinks = array(), $categoryLinks = array(),
  131. $containsOldMagic = false, $titletext = '' )
  132. {
  133. $this->mText = $text;
  134. $this->mLanguageLinks = $languageLinks;
  135. $this->mCategories = $categoryLinks;
  136. $this->mContainsOldMagic = $containsOldMagic;
  137. $this->mTitleText = $titletext;
  138. }
  139. function getText() {
  140. if ( $this->mEditSectionTokens ) {
  141. return preg_replace_callback( ParserOutput::EDITSECTION_REGEX,
  142. array( &$this, 'replaceEditSectionLinksCallback' ), $this->mText );
  143. }
  144. return $this->mText;
  145. }
  146. /**
  147. * callback used by getText to replace editsection tokens
  148. * @private
  149. */
  150. function replaceEditSectionLinksCallback( $m ) {
  151. global $wgOut, $wgLang;
  152. $args = array(
  153. htmlspecialchars_decode($m[1]),
  154. htmlspecialchars_decode($m[2]),
  155. isset($m[4]) ? $m[3] : null,
  156. );
  157. $args[0] = Title::newFromText( $args[0] );
  158. if ( !is_object($args[0]) ) {
  159. throw new MWException("Bad parser output text.");
  160. }
  161. $args[] = $wgLang->getCode();
  162. $skin = $wgOut->getSkin();
  163. return call_user_func_array( array( $skin, 'doEditSectionLink' ), $args );
  164. }
  165. function &getLanguageLinks() { return $this->mLanguageLinks; }
  166. function getInterwikiLinks() { return $this->mInterwikiLinks; }
  167. function getCategoryLinks() { return array_keys( $this->mCategories ); }
  168. function &getCategories() { return $this->mCategories; }
  169. function getTitleText() { return $this->mTitleText; }
  170. function getSections() { return $this->mSections; }
  171. function getEditSectionTokens() { return $this->mEditSectionTokens; }
  172. function &getLinks() { return $this->mLinks; }
  173. function &getTemplates() { return $this->mTemplates; }
  174. function &getDistantTemplates() { return $this->mDistantTemplates; }
  175. function &getDistantTemplateIds() { return $this->mDistantTemplateIds; }
  176. function &getTemplateIds() { return $this->mTemplateIds; }
  177. function &getImages() { return $this->mImages; }
  178. function &getImageTimeKeys() { return $this->mImageTimeKeys; }
  179. function &getExternalLinks() { return $this->mExternalLinks; }
  180. function getNoGallery() { return $this->mNoGallery; }
  181. function getHeadItems() { return $this->mHeadItems; }
  182. function getModules() { return $this->mModules; }
  183. function getModuleScripts() { return $this->mModuleScripts; }
  184. function getModuleStyles() { return $this->mModuleStyles; }
  185. function getModuleMessages() { return $this->mModuleMessages; }
  186. function getOutputHooks() { return (array)$this->mOutputHooks; }
  187. function getWarnings() { return array_keys( $this->mWarnings ); }
  188. function getIndexPolicy() { return $this->mIndexPolicy; }
  189. function getTOCHTML() { return $this->mTOCHTML; }
  190. function setText( $text ) { return wfSetVar( $this->mText, $text ); }
  191. function setLanguageLinks( $ll ) { return wfSetVar( $this->mLanguageLinks, $ll ); }
  192. function setCategoryLinks( $cl ) { return wfSetVar( $this->mCategories, $cl ); }
  193. function setTitleText( $t ) { return wfSetVar( $this->mTitleText, $t ); }
  194. function setSections( $toc ) { return wfSetVar( $this->mSections, $toc ); }
  195. function setEditSectionTokens( $t ) { return wfSetVar( $this->mEditSectionTokens, $t ); }
  196. function setIndexPolicy( $policy ) { return wfSetVar( $this->mIndexPolicy, $policy ); }
  197. function setTOCHTML( $tochtml ) { return wfSetVar( $this->mTOCHTML, $tochtml ); }
  198. function addCategory( $c, $sort ) { $this->mCategories[$c] = $sort; }
  199. function addLanguageLink( $t ) { $this->mLanguageLinks[] = $t; }
  200. function addWarning( $s ) { $this->mWarnings[$s] = 1; }
  201. function addOutputHook( $hook, $data = false ) {
  202. $this->mOutputHooks[] = array( $hook, $data );
  203. }
  204. function setNewSection( $value ) {
  205. $this->mNewSection = (bool)$value;
  206. }
  207. function hideNewSection ( $value ) {
  208. $this->mHideNewSection = (bool)$value;
  209. }
  210. function getHideNewSection () {
  211. return (bool)$this->mHideNewSection;
  212. }
  213. function getNewSection() {
  214. return (bool)$this->mNewSection;
  215. }
  216. function addExternalLink( $url ) {
  217. # We don't register links pointing to our own server, unless... :-)
  218. global $wgServer, $wgRegisterInternalExternals;
  219. if( $wgRegisterInternalExternals or stripos($url,$wgServer.'/')!==0)
  220. $this->mExternalLinks[$url] = 1;
  221. }
  222. /**
  223. * Record a local or interwiki inline link for saving in future link tables.
  224. *
  225. * @param $title Title object
  226. * @param $id Mixed: optional known page_id so we can skip the lookup
  227. */
  228. function addLink( $title, $id = null ) {
  229. if ( $title->isExternal() ) {
  230. // Don't record interwikis in pagelinks
  231. $this->addInterwikiLink( $title );
  232. return;
  233. }
  234. $ns = $title->getNamespace();
  235. $dbk = $title->getDBkey();
  236. if ( $ns == NS_MEDIA ) {
  237. // Normalize this pseudo-alias if it makes it down here...
  238. $ns = NS_FILE;
  239. } elseif( $ns == NS_SPECIAL ) {
  240. // We don't record Special: links currently
  241. // It might actually be wise to, but we'd need to do some normalization.
  242. return;
  243. } elseif( $dbk === '' ) {
  244. // Don't record self links - [[#Foo]]
  245. return;
  246. }
  247. if ( !isset( $this->mLinks[$ns] ) ) {
  248. $this->mLinks[$ns] = array();
  249. }
  250. if ( is_null( $id ) ) {
  251. $id = $title->getArticleID();
  252. }
  253. $this->mLinks[$ns][$dbk] = $id;
  254. }
  255. /**
  256. * Register a file dependency for this output
  257. * @param $name string Title dbKey
  258. * @param $timestamp string MW timestamp of file creation (or false if non-existing)
  259. * @param $sha string base 36 SHA-1 of file (or false if non-existing)
  260. * @return void
  261. */
  262. function addImage( $name, $timestamp = null, $sha1 = null ) {
  263. $this->mImages[$name] = 1;
  264. if ( $timestamp !== null && $sha1 !== null ) {
  265. $this->mImageTimeKeys[$name] = array( 'time' => $timestamp, 'sha1' => $sha1 );
  266. }
  267. }
  268. /**
  269. * Register a template dependency for this output
  270. * @param $title Title
  271. * @param $page_id
  272. * @param $rev_id
  273. * @return void
  274. */
  275. function addTemplate( $title, $page_id, $rev_id ) {
  276. $ns = $title->getNamespace();
  277. $dbk = $title->getDBkey();
  278. if ( !isset( $this->mTemplates[$ns] ) ) {
  279. $this->mTemplates[$ns] = array();
  280. }
  281. $this->mTemplates[$ns][$dbk] = $page_id;
  282. if ( !isset( $this->mTemplateIds[$ns] ) ) {
  283. $this->mTemplateIds[$ns] = array();
  284. }
  285. $this->mTemplateIds[$ns][$dbk] = $rev_id; // For versioning
  286. }
  287. function addDistantTemplate( $title, $page_id, $rev_id ) {
  288. $prefix = $title->getInterwiki();
  289. if ( $prefix !=='' ) {
  290. $ns = $title->getNamespace();
  291. $dbk = $title->getDBkey();
  292. if ( !isset( $this->mDistantTemplates[$prefix] ) ) {
  293. $this->mDistantTemplates[$prefix] = array();
  294. }
  295. if ( !isset( $this->mDistantTemplates[$prefix][$ns] ) ) {
  296. $this->mDistantTemplates[$prefix][$ns] = array();
  297. }
  298. $this->mDistantTemplates[$prefix][$ns][$dbk] = $page_id;
  299. // For versioning
  300. if ( !isset( $this->mDistantTemplateIds[$prefix] ) ) {
  301. $this->mDistantTemplateIds[$prefix] = array();
  302. }
  303. if ( !isset( $this->mDistantTemplateIds[$prefix][$ns] ) ) {
  304. $this->mDistantTemplateIds[$prefix][$ns] = array();
  305. }
  306. $this->mDistantTemplateIds[$prefix][$ns][$dbk] = $rev_id;
  307. }
  308. }
  309. /**
  310. * @param $title Title object, must be an interwiki link
  311. * @throws MWException if given invalid input
  312. */
  313. function addInterwikiLink( $title ) {
  314. $prefix = $title->getInterwiki();
  315. if( $prefix == '' ) {
  316. throw new MWException( 'Non-interwiki link passed, internal parser error.' );
  317. }
  318. if (!isset($this->mInterwikiLinks[$prefix])) {
  319. $this->mInterwikiLinks[$prefix] = array();
  320. }
  321. $this->mInterwikiLinks[$prefix][$title->getDBkey()] = 1;
  322. }
  323. /**
  324. * Add some text to the <head>.
  325. * If $tag is set, the section with that tag will only be included once
  326. * in a given page.
  327. */
  328. function addHeadItem( $section, $tag = false ) {
  329. if ( $tag !== false ) {
  330. $this->mHeadItems[$tag] = $section;
  331. } else {
  332. $this->mHeadItems[] = $section;
  333. }
  334. }
  335. public function addModules( $modules ) {
  336. $this->mModules = array_merge( $this->mModules, (array) $modules );
  337. }
  338. public function addModuleScripts( $modules ) {
  339. $this->mModuleScripts = array_merge( $this->mModuleScripts, (array)$modules );
  340. }
  341. public function addModuleStyles( $modules ) {
  342. $this->mModuleStyles = array_merge( $this->mModuleStyles, (array)$modules );
  343. }
  344. public function addModuleMessages( $modules ) {
  345. $this->mModuleMessages = array_merge( $this->mModuleMessages, (array)$modules );
  346. }
  347. /**
  348. * Copy items from the OutputPage object into this one
  349. *
  350. * @param $out OutputPage object
  351. */
  352. public function addOutputPageMetadata( OutputPage $out ) {
  353. $this->addModules( $out->getModules() );
  354. $this->addModuleScripts( $out->getModuleScripts() );
  355. $this->addModuleStyles( $out->getModuleStyles() );
  356. $this->addModuleMessages( $out->getModuleMessages() );
  357. $this->mHeadItems = array_merge( $this->mHeadItems, $out->getHeadItemsArray() );
  358. }
  359. /**
  360. * Override the title to be used for display
  361. * -- this is assumed to have been validated
  362. * (check equal normalisation, etc.)
  363. *
  364. * @param $text String: desired title text
  365. */
  366. public function setDisplayTitle( $text ) {
  367. $this->setTitleText( $text );
  368. $this->setProperty( 'displaytitle', $text );
  369. }
  370. /**
  371. * Get the title to be used for display
  372. *
  373. * @return String
  374. */
  375. public function getDisplayTitle() {
  376. $t = $this->getTitleText();
  377. if( $t === '' ) {
  378. return false;
  379. }
  380. return $t;
  381. }
  382. /**
  383. * Fairly generic flag setter thingy.
  384. */
  385. public function setFlag( $flag ) {
  386. $this->mFlags[$flag] = true;
  387. }
  388. public function getFlag( $flag ) {
  389. return isset( $this->mFlags[$flag] );
  390. }
  391. /**
  392. * Set a property to be cached in the DB
  393. */
  394. public function setProperty( $name, $value ) {
  395. $this->mProperties[$name] = $value;
  396. }
  397. public function getProperty( $name ){
  398. return isset( $this->mProperties[$name] ) ? $this->mProperties[$name] : false;
  399. }
  400. public function getProperties() {
  401. if ( !isset( $this->mProperties ) ) {
  402. $this->mProperties = array();
  403. }
  404. return $this->mProperties;
  405. }
  406. /**
  407. * Returns the options from its ParserOptions which have been taken
  408. * into account to produce this output or false if not available.
  409. * @return mixed Array
  410. */
  411. public function getUsedOptions() {
  412. if ( !isset( $this->mAccessedOptions ) ) {
  413. return array();
  414. }
  415. return array_keys( $this->mAccessedOptions );
  416. }
  417. /**
  418. * Callback passed by the Parser to the ParserOptions to keep track of which options are used.
  419. * @access private
  420. */
  421. function recordOption( $option ) {
  422. $this->mAccessedOptions[$option] = true;
  423. }
  424. }