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' => $this->revision->getPageAsLinkTarget()->getDBkey(),
147  'ns' => $this->revision->getPageAsLinkTarget()->getNamespace(),
148  'trigger' => 'view',
149  ] );
150  }
151 
152  if ( $this->cacheable && $this->parserOutput->isCacheable() ) {
153  $this->saveInCache( $this->parserOutput, $cacheTime );
154  }
155 
156  $this->afterWork( $this->parserOutput );
157 
158  return true;
159  }
160 
168  protected function saveInCache( ParserOutput $output, string $cacheTime ) {
169  // noop
170  }
171 
178  protected function afterWork( ParserOutput $output ) {
179  // noop
180  }
181 
186  public function error( $status ) {
187  $this->error = $status;
188  return false;
189  }
190 
196  protected function getLogger( $name = 'PoolWorkArticleView' ): LoggerInterface {
197  return $this->loggerSpi->getLogger( $name );
198  }
199 }
ParserOptions
Set options of the Parser.
Definition: ParserOptions.php:44
Revision\RevisionRecord
Page revision base class.
Definition: RevisionRecord.php:46
ParserOutput
Definition: ParserOutput.php:31
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:1844
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
Revision\RevisionRenderer
The RevisionRenderer service provides access to rendered output for revisions.
Definition: RevisionRenderer.php:45
PoolWorkArticleView\getLogger
getLogger( $name='PoolWorkArticleView')
Definition: PoolWorkArticleView.php:196
PoolWorkArticleView\saveInCache
saveInCache(ParserOutput $output, string $cacheTime)
Place the output in the cache from which getCachedWork() will retrieve it.
Definition: PoolWorkArticleView.php:168
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:186
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:178