MediaWiki  master
PoolWorkArticleView.php
Go to the documentation of this file.
1 <?php
21 use MediaWiki\Logger\Spi as LoggerSpi;
24 use Psr\Log\LoggerInterface;
25 
35 
37  protected $parserOptions;
38 
40  protected $revision = null;
41 
43  protected $renderer = null;
44 
46  protected $parserOutput = false;
47 
49  protected $isDirty = false;
50 
52  protected $isFast = false;
53 
55  protected $error = false;
56 
58  private $loggerSpi;
59 
67  public function __construct(
68  string $workKey,
71  RevisionRenderer $revisionRenderer,
72  LoggerSpi $loggerSpi
73  ) {
74  parent::__construct( 'ArticleView', $workKey );
75  $this->revision = $revision;
76  $this->parserOptions = $parserOptions;
77  $this->renderer = $revisionRenderer;
78  $this->loggerSpi = $loggerSpi;
79  }
80 
86  public function getParserOutput() {
87  return $this->parserOutput;
88  }
89 
95  public function getIsDirty() {
96  return $this->isDirty;
97  }
98 
104  public function getIsFastStale() {
105  return $this->isFast;
106  }
107 
113  public function getError() {
114  return $this->error;
115  }
116 
120  public function doWork() {
121  $renderedRevision = $this->renderer->getRenderedRevision(
122  $this->revision,
123  $this->parserOptions,
124  null,
125  [ 'audience' => RevisionRecord::RAW ]
126  );
127 
128  if ( !$renderedRevision ) {
129  // audience check failed
130  return false;
131  }
132 
133  // Reduce effects of race conditions for slow parses (T48014)
134  $cacheTime = wfTimestampNow();
135 
136  $time = -microtime( true );
137  $this->parserOutput = $renderedRevision->getRevisionParserOutput();
138  $time += microtime( true );
139 
140  // Timing hack
141  if ( $time > 3 ) {
142  // TODO: Use Parser's logger (once it has one)
143  $logger = $this->getLogger( 'slow-parse' );
144  $logger->info( 'Parsing {title} was slow, took {time} seconds', [
145  'time' => number_format( $time, 2 ),
146  'title' => (string)$this->revision->getPageAsLinkTarget(),
147  'trigger' => 'view',
148  ] );
149  }
150 
151  if ( $this->cacheable && $this->parserOutput->isCacheable() ) {
152  $this->saveInCache( $this->parserOutput, $cacheTime );
153  }
154 
155  $this->afterWork( $this->parserOutput );
156 
157  return true;
158  }
159 
167  protected function saveInCache( ParserOutput $output, string $cacheTime ) {
168  // noop
169  }
170 
177  protected function afterWork( ParserOutput $output ) {
178  // noop
179  }
180 
185  public function error( $status ) {
186  $this->error = $status;
187  return false;
188  }
189 
195  protected function getLogger( $name = 'PoolWorkArticleView' ): LoggerInterface {
196  return $this->loggerSpi->getLogger( $name );
197  }
198 }
ParserOptions
Set options of the Parser.
Definition: ParserOptions.php:45
MediaWiki\Revision\RevisionRecord
Page revision base class.
Definition: RevisionRecord.php:47
ParserOutput
Definition: ParserOutput.php:36
PoolWorkArticleView\doWork
doWork()
Definition: PoolWorkArticleView.php:120
PoolWorkArticleView\$renderer
RevisionRenderer $renderer
Definition: PoolWorkArticleView.php:43
PoolWorkArticleView
PoolCounter protected work wrapping RenderedRevision->getRevisionParserOutput.
Definition: PoolWorkArticleView.php:34
Status
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition: Status.php:44
PoolWorkArticleView\$parserOptions
ParserOptions $parserOptions
Definition: PoolWorkArticleView.php:37
PoolWorkArticleView\$parserOutput
ParserOutput bool $parserOutput
Definition: PoolWorkArticleView.php:46
PoolWorkArticleView\$loggerSpi
LoggerSpi $loggerSpi
Definition: PoolWorkArticleView.php:58
PoolWorkArticleView\__construct
__construct(string $workKey, RevisionRecord $revision, ParserOptions $parserOptions, RevisionRenderer $revisionRenderer, LoggerSpi $loggerSpi)
Definition: PoolWorkArticleView.php:67
PoolWorkArticleView\$error
Status bool $error
Definition: PoolWorkArticleView.php:55
wfTimestampNow
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
Definition: GlobalFunctions.php:1686
PoolWorkArticleView\getIsFastStale
getIsFastStale()
Get whether the ParserOutput was retrieved in fast stale mode.
Definition: PoolWorkArticleView.php:104
PoolWorkArticleView\getError
getError()
Get a Status object in case of error or false otherwise.
Definition: PoolWorkArticleView.php:113
MediaWiki\Revision\RevisionRenderer
The RevisionRenderer service provides access to rendered output for revisions.
Definition: RevisionRenderer.php:45
PoolWorkArticleView\getLogger
getLogger( $name='PoolWorkArticleView')
Definition: PoolWorkArticleView.php:195
PoolWorkArticleView\saveInCache
saveInCache(ParserOutput $output, string $cacheTime)
Place the output in the cache from which getCachedWork() will retrieve it.
Definition: PoolWorkArticleView.php:167
PoolWorkArticleView\$isFast
bool $isFast
Definition: PoolWorkArticleView.php:52
PoolCounterWork
Class for dealing with PoolCounters using class members.
Definition: PoolCounterWork.php:27
PoolWorkArticleView\getParserOutput
getParserOutput()
Get the ParserOutput from this object, or false in case of failure.
Definition: PoolWorkArticleView.php:86
PoolWorkArticleView\error
error( $status)
Definition: PoolWorkArticleView.php:185
PoolWorkArticleView\$isDirty
bool $isDirty
Definition: PoolWorkArticleView.php:49
MediaWiki\Logger\Spi
Service provider interface for \Psr\Log\LoggerInterface implementation libraries.
Definition: Spi.php:38
PoolWorkArticleView\$revision
RevisionRecord null $revision
Definition: PoolWorkArticleView.php:40
PoolWorkArticleView\getIsDirty
getIsDirty()
Get whether the ParserOutput is a dirty one (i.e.
Definition: PoolWorkArticleView.php:95
PoolWorkArticleView\afterWork
afterWork(ParserOutput $output)
Subclasses may implement this to perform some action after the work of rendering is done.
Definition: PoolWorkArticleView.php:177