PoolWorkArticleView.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. /**
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along
  14. * with this program; if not, write to the Free Software Foundation, Inc.,
  15. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. * http://www.gnu.org/copyleft/gpl.html
  17. *
  18. * @file
  19. */
  20. use MediaWiki\MediaWikiServices;
  21. class PoolWorkArticleView extends PoolCounterWork {
  22. /** @var WikiPage */
  23. private $page;
  24. /** @var string */
  25. private $cacheKey;
  26. /** @var int */
  27. private $revid;
  28. /** @var ParserCache */
  29. private $parserCache;
  30. /** @var ParserOptions */
  31. private $parserOptions;
  32. /** @var Content|null */
  33. private $content = null;
  34. /** @var ParserOutput|bool */
  35. private $parserOutput = false;
  36. /** @var bool */
  37. private $isDirty = false;
  38. /** @var Status|bool */
  39. private $error = false;
  40. /**
  41. * @param WikiPage $page
  42. * @param ParserOptions $parserOptions ParserOptions to use for the parse
  43. * @param int $revid ID of the revision being parsed.
  44. * @param bool $useParserCache Whether to use the parser cache.
  45. * operation.
  46. * @param Content|string $content Content to parse or null to load it; may
  47. * also be given as a wikitext string, for BC.
  48. */
  49. public function __construct( WikiPage $page, ParserOptions $parserOptions,
  50. $revid, $useParserCache, $content = null
  51. ) {
  52. if ( is_string( $content ) ) { // BC: old style call
  53. $modelId = $page->getRevision()->getContentModel();
  54. $format = $page->getRevision()->getContentFormat();
  55. $content = ContentHandler::makeContent( $content, $page->getTitle(), $modelId, $format );
  56. }
  57. $this->page = $page;
  58. $this->revid = $revid;
  59. $this->cacheable = $useParserCache;
  60. $this->parserOptions = $parserOptions;
  61. $this->content = $content;
  62. $this->parserCache = MediaWikiServices::getInstance()->getParserCache();
  63. $this->cacheKey = $this->parserCache->getKey( $page, $parserOptions );
  64. $keyPrefix = $this->cacheKey ?: wfMemcKey( 'articleview', 'missingcachekey' );
  65. parent::__construct( 'ArticleView', $keyPrefix . ':revid:' . $revid );
  66. }
  67. /**
  68. * Get the ParserOutput from this object, or false in case of failure
  69. *
  70. * @return ParserOutput|bool
  71. */
  72. public function getParserOutput() {
  73. return $this->parserOutput;
  74. }
  75. /**
  76. * Get whether the ParserOutput is a dirty one (i.e. expired)
  77. *
  78. * @return bool
  79. */
  80. public function getIsDirty() {
  81. return $this->isDirty;
  82. }
  83. /**
  84. * Get a Status object in case of error or false otherwise
  85. *
  86. * @return Status|bool
  87. */
  88. public function getError() {
  89. return $this->error;
  90. }
  91. /**
  92. * @return bool
  93. */
  94. public function doWork() {
  95. global $wgUseFileCache;
  96. // @todo several of the methods called on $this->page are not declared in Page, but present
  97. // in WikiPage and delegated by Article.
  98. $isCurrent = $this->revid === $this->page->getLatest();
  99. if ( $this->content !== null ) {
  100. $content = $this->content;
  101. } elseif ( $isCurrent ) {
  102. // XXX: why use RAW audience here, and PUBLIC (default) below?
  103. $content = $this->page->getContent( Revision::RAW );
  104. } else {
  105. $rev = Revision::newFromTitle( $this->page->getTitle(), $this->revid );
  106. if ( $rev === null ) {
  107. $content = null;
  108. } else {
  109. // XXX: why use PUBLIC audience here (default), and RAW above?
  110. $content = $rev->getContent();
  111. }
  112. }
  113. if ( $content === null ) {
  114. return false;
  115. }
  116. // Reduce effects of race conditions for slow parses (T48014)
  117. $cacheTime = wfTimestampNow();
  118. $time = - microtime( true );
  119. $this->parserOutput = $content->getParserOutput(
  120. $this->page->getTitle(),
  121. $this->revid,
  122. $this->parserOptions
  123. );
  124. $time += microtime( true );
  125. // Timing hack
  126. if ( $time > 3 ) {
  127. // TODO: Use Parser's logger (once it has one)
  128. $logger = MediaWiki\Logger\LoggerFactory::getInstance( 'slow-parse' );
  129. $logger->info( '{time} {title}', [
  130. 'time' => number_format( $time, 2 ),
  131. 'title' => $this->page->getTitle()->getPrefixedDBkey(),
  132. 'ns' => $this->page->getTitle()->getNamespace(),
  133. 'trigger' => 'view',
  134. ] );
  135. }
  136. if ( $this->cacheable && $this->parserOutput->isCacheable() && $isCurrent ) {
  137. $this->parserCache->save(
  138. $this->parserOutput, $this->page, $this->parserOptions, $cacheTime, $this->revid );
  139. }
  140. // Make sure file cache is not used on uncacheable content.
  141. // Output that has magic words in it can still use the parser cache
  142. // (if enabled), though it will generally expire sooner.
  143. if ( !$this->parserOutput->isCacheable() ) {
  144. $wgUseFileCache = false;
  145. }
  146. if ( $isCurrent ) {
  147. $this->page->triggerOpportunisticLinksUpdate( $this->parserOutput );
  148. }
  149. return true;
  150. }
  151. /**
  152. * @return bool
  153. */
  154. public function getCachedWork() {
  155. $this->parserOutput = $this->parserCache->get( $this->page, $this->parserOptions );
  156. if ( $this->parserOutput === false ) {
  157. wfDebug( __METHOD__ . ": parser cache miss\n" );
  158. return false;
  159. } else {
  160. wfDebug( __METHOD__ . ": parser cache hit\n" );
  161. return true;
  162. }
  163. }
  164. /**
  165. * @return bool
  166. */
  167. public function fallback() {
  168. $this->parserOutput = $this->parserCache->getDirty( $this->page, $this->parserOptions );
  169. if ( $this->parserOutput === false ) {
  170. wfDebugLog( 'dirty', 'dirty missing' );
  171. wfDebug( __METHOD__ . ": no dirty cache\n" );
  172. return false;
  173. } else {
  174. wfDebug( __METHOD__ . ": sending dirty output\n" );
  175. wfDebugLog( 'dirty', "dirty output {$this->cacheKey}" );
  176. $this->isDirty = true;
  177. return true;
  178. }
  179. }
  180. /**
  181. * @param Status $status
  182. * @return bool
  183. */
  184. public function error( $status ) {
  185. $this->error = $status;
  186. return false;
  187. }
  188. }