ParserOutput.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. /**
  3. * @todo document
  4. * @ingroup Parser
  5. */
  6. class ParserOutput
  7. {
  8. var $mText, # The output text
  9. $mLanguageLinks, # List of the full text of language links, in the order they appear
  10. $mCategories, # Map of category names to sort keys
  11. $mContainsOldMagic, # Boolean variable indicating if the input contained variables like {{CURRENTDAY}}
  12. $mTitleText, # title text of the chosen language variant
  13. $mCacheTime = '', # Time when this object was generated, or -1 for uncacheable. Used in ParserCache.
  14. $mVersion = Parser::VERSION, # Compatibility check
  15. $mLinks = array(), # 2-D map of NS/DBK to ID for the links in the document. ID=zero for broken.
  16. $mTemplates = array(), # 2-D map of NS/DBK to ID for the template references. ID=zero for broken.
  17. $mTemplateIds = array(), # 2-D map of NS/DBK to rev ID for the template references. ID=zero for broken.
  18. $mImages = array(), # DB keys of the images used, in the array key only
  19. $mExternalLinks = array(), # External link URLs, in the key only
  20. $mNewSection = false, # Show a new section link?
  21. $mHideNewSection = false, # Hide the new section link?
  22. $mNoGallery = false, # No gallery on category page? (__NOGALLERY__)
  23. $mHeadItems = array(), # Items to put in the <head> section
  24. $mOutputHooks = array(), # Hook tags as per $wgParserOutputHooks
  25. $mWarnings = array(), # Warning text to be returned to the user. Wikitext formatted, in the key only
  26. $mSections = array(), # Table of contents
  27. $mProperties = array(); # Name/value pairs to be cached in the DB
  28. private $mIndexPolicy = ''; # 'index' or 'noindex'? Any other value will result in no change.
  29. /**
  30. * Overridden title for display
  31. */
  32. private $displayTitle = false;
  33. function ParserOutput( $text = '', $languageLinks = array(), $categoryLinks = array(),
  34. $containsOldMagic = false, $titletext = '' )
  35. {
  36. $this->mText = $text;
  37. $this->mLanguageLinks = $languageLinks;
  38. $this->mCategories = $categoryLinks;
  39. $this->mContainsOldMagic = $containsOldMagic;
  40. $this->mTitleText = $titletext;
  41. }
  42. function getText() { return $this->mText; }
  43. function &getLanguageLinks() { return $this->mLanguageLinks; }
  44. function getCategoryLinks() { return array_keys( $this->mCategories ); }
  45. function &getCategories() { return $this->mCategories; }
  46. function getCacheTime() { return $this->mCacheTime; }
  47. function getTitleText() { return $this->mTitleText; }
  48. function getSections() { return $this->mSections; }
  49. function &getLinks() { return $this->mLinks; }
  50. function &getTemplates() { return $this->mTemplates; }
  51. function &getImages() { return $this->mImages; }
  52. function &getExternalLinks() { return $this->mExternalLinks; }
  53. function getNoGallery() { return $this->mNoGallery; }
  54. function getSubtitle() { return $this->mSubtitle; }
  55. function getOutputHooks() { return (array)$this->mOutputHooks; }
  56. function getWarnings() { return array_keys( $this->mWarnings ); }
  57. function getIndexPolicy() { return $this->mIndexPolicy; }
  58. function containsOldMagic() { return $this->mContainsOldMagic; }
  59. function setText( $text ) { return wfSetVar( $this->mText, $text ); }
  60. function setLanguageLinks( $ll ) { return wfSetVar( $this->mLanguageLinks, $ll ); }
  61. function setCategoryLinks( $cl ) { return wfSetVar( $this->mCategories, $cl ); }
  62. function setContainsOldMagic( $com ) { return wfSetVar( $this->mContainsOldMagic, $com ); }
  63. function setCacheTime( $t ) { return wfSetVar( $this->mCacheTime, $t ); }
  64. function setTitleText( $t ) { return wfSetVar( $this->mTitleText, $t ); }
  65. function setSections( $toc ) { return wfSetVar( $this->mSections, $toc ); }
  66. function setIndexPolicy( $policy ) { return wfSetVar( $this->mIndexPolicy, $policy ); }
  67. function addCategory( $c, $sort ) { $this->mCategories[$c] = $sort; }
  68. function addLanguageLink( $t ) { $this->mLanguageLinks[] = $t; }
  69. function addExternalLink( $url ) { $this->mExternalLinks[$url] = 1; }
  70. function addWarning( $s ) { $this->mWarnings[$s] = 1; }
  71. function addOutputHook( $hook, $data = false ) {
  72. $this->mOutputHooks[] = array( $hook, $data );
  73. }
  74. function setNewSection( $value ) {
  75. $this->mNewSection = (bool)$value;
  76. }
  77. function hideNewSection ( $value ) {
  78. $this->mHideNewSection = (bool)$value;
  79. }
  80. function getHideNewSection () {
  81. return (bool)$this->mHideNewSection;
  82. }
  83. function getNewSection() {
  84. return (bool)$this->mNewSection;
  85. }
  86. function addLink( $title, $id = null ) {
  87. $ns = $title->getNamespace();
  88. $dbk = $title->getDBkey();
  89. if ( $ns == NS_MEDIA ) {
  90. // Normalize this pseudo-alias if it makes it down here...
  91. $ns = NS_FILE;
  92. } elseif( $ns == NS_SPECIAL ) {
  93. // We don't record Special: links currently
  94. // It might actually be wise to, but we'd need to do some normalization.
  95. return;
  96. } elseif( $dbk === '' ) {
  97. // Don't record self links - [[#Foo]]
  98. return;
  99. }
  100. if ( !isset( $this->mLinks[$ns] ) ) {
  101. $this->mLinks[$ns] = array();
  102. }
  103. if ( is_null( $id ) ) {
  104. $id = $title->getArticleID();
  105. }
  106. $this->mLinks[$ns][$dbk] = $id;
  107. }
  108. function addImage( $name ) {
  109. $this->mImages[$name] = 1;
  110. }
  111. function addTemplate( $title, $page_id, $rev_id ) {
  112. $ns = $title->getNamespace();
  113. $dbk = $title->getDBkey();
  114. if ( !isset( $this->mTemplates[$ns] ) ) {
  115. $this->mTemplates[$ns] = array();
  116. }
  117. $this->mTemplates[$ns][$dbk] = $page_id;
  118. if ( !isset( $this->mTemplateIds[$ns] ) ) {
  119. $this->mTemplateIds[$ns] = array();
  120. }
  121. $this->mTemplateIds[$ns][$dbk] = $rev_id; // For versioning
  122. }
  123. /**
  124. * Return true if this cached output object predates the global or
  125. * per-article cache invalidation timestamps, or if it comes from
  126. * an incompatible older version.
  127. *
  128. * @param string $touched the affected article's last touched timestamp
  129. * @return bool
  130. * @public
  131. */
  132. function expired( $touched ) {
  133. global $wgCacheEpoch;
  134. return $this->getCacheTime() == -1 || // parser says it's uncacheable
  135. $this->getCacheTime() < $touched ||
  136. $this->getCacheTime() <= $wgCacheEpoch ||
  137. !isset( $this->mVersion ) ||
  138. version_compare( $this->mVersion, Parser::VERSION, "lt" );
  139. }
  140. /**
  141. * Add some text to the <head>.
  142. * If $tag is set, the section with that tag will only be included once
  143. * in a given page.
  144. */
  145. function addHeadItem( $section, $tag = false ) {
  146. if ( $tag !== false ) {
  147. $this->mHeadItems[$tag] = $section;
  148. } else {
  149. $this->mHeadItems[] = $section;
  150. }
  151. }
  152. /**
  153. * Override the title to be used for display
  154. * -- this is assumed to have been validated
  155. * (check equal normalisation, etc.)
  156. *
  157. * @param string $text Desired title text
  158. */
  159. public function setDisplayTitle( $text ) {
  160. $this->displayTitle = $text;
  161. }
  162. /**
  163. * Get the title to be used for display
  164. *
  165. * @return string
  166. */
  167. public function getDisplayTitle() {
  168. return $this->displayTitle;
  169. }
  170. /**
  171. * Fairly generic flag setter thingy.
  172. */
  173. public function setFlag( $flag ) {
  174. $this->mFlags[$flag] = true;
  175. }
  176. public function getFlag( $flag ) {
  177. return isset( $this->mFlags[$flag] );
  178. }
  179. /**
  180. * Set a property to be cached in the DB
  181. */
  182. public function setProperty( $name, $value ) {
  183. $this->mProperties[$name] = $value;
  184. }
  185. public function getProperty( $name ){
  186. return isset( $this->mProperties[$name] ) ? $this->mProperties[$name] : false;
  187. }
  188. public function getProperties() {
  189. if ( !isset( $this->mProperties ) ) {
  190. $this->mProperties = array();
  191. }
  192. return $this->mProperties;
  193. }
  194. }