Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / poolcounter / PoolWorkArticleView.php
blobc15e4e84416cc4d272523a36cf98b602f074cfb4
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.
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.
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
18 * @file
21 namespace MediaWiki\PoolCounter;
23 use MediaWiki\Logger\Spi as LoggerSpi;
24 use MediaWiki\MediaWikiServices;
25 use MediaWiki\Page\ParserOutputAccess;
26 use MediaWiki\Parser\ParserOptions;
27 use MediaWiki\Parser\ParserOutput;
28 use MediaWiki\Revision\RevisionRecord;
29 use MediaWiki\Revision\RevisionRenderer;
30 use MediaWiki\Revision\SlotRecord;
31 use MediaWiki\Status\Status;
32 use MediaWiki\WikiMap\WikiMap;
34 /**
35 * PoolCounter protected work wrapping RenderedRevision->getRevisionParserOutput.
36 * Caching behavior may be defined by subclasses.
38 * @note No audience checks are applied.
40 * @internal
42 class PoolWorkArticleView extends PoolCounterWork {
43 /** @var ParserOptions */
44 protected $parserOptions;
45 /** @var RevisionRecord */
46 protected $revision;
47 /** @var RevisionRenderer */
48 private $renderer;
49 /** @var LoggerSpi */
50 protected $loggerSpi;
52 /**
53 * @param string $workKey
54 * @param RevisionRecord $revision Revision to render
55 * @param ParserOptions $parserOptions ParserOptions to use for the parse
56 * @param RevisionRenderer $revisionRenderer
57 * @param LoggerSpi $loggerSpi
59 public function __construct(
60 string $workKey,
61 RevisionRecord $revision,
62 ParserOptions $parserOptions,
63 RevisionRenderer $revisionRenderer,
64 LoggerSpi $loggerSpi
65 ) {
66 parent::__construct( 'ArticleView', $workKey );
67 $this->revision = $revision;
68 $this->parserOptions = $parserOptions;
69 $this->renderer = $revisionRenderer;
70 $this->loggerSpi = $loggerSpi;
73 /**
74 * @return Status
76 public function doWork() {
77 return $this->renderRevision();
80 /**
81 * Render the given revision.
83 * @see ParserOutputAccess::renderRevision
85 * @param ?ParserOutput $previousOutput previously-cached output for this
86 * page (used by Parsoid for selective updates)
87 * @param bool $doSample Whether to collect statistics on this render
88 * @param string $sourceLabel the source label to use on the statistics
89 * @return Status with the value being a ParserOutput or null
91 public function renderRevision(
92 ?ParserOutput $previousOutput = null,
93 bool $doSample = false,
94 string $sourceLabel = ''
95 ): Status {
96 $renderedRevision = $this->renderer->getRenderedRevision(
97 $this->revision,
98 $this->parserOptions,
99 null,
101 'audience' => RevisionRecord::RAW,
102 'previous-output' => $previousOutput,
106 $parserOutput = $renderedRevision->getRevisionParserOutput();
108 if ( $doSample ) {
109 $stats = MediaWikiServices::getInstance()->getStatsFactory();
110 $content = $this->revision->getContent( SlotRecord::MAIN );
111 $labels = [
112 'source' => $sourceLabel,
113 'type' => $previousOutput === null ? 'full' : 'selective',
114 'reason' => $this->parserOptions->getRenderReason(),
115 'parser' => $this->parserOptions->getUseParsoid() ? 'parsoid' : 'legacy',
116 'opportunistic' => 'false',
117 'wiki' => WikiMap::getCurrentWikiId(),
118 'model' => $content ? $content->getModel() : 'unknown',
120 $stats
121 ->getCounter( 'ParserCache_selective_total' )
122 ->setLabels( $labels )
123 ->increment();
124 $stats
125 ->getCounter( 'ParserCache_selective_cpu_seconds' )
126 ->setLabels( $labels )
127 ->incrementBy( $parserOutput->getTimeProfile( 'cpu' ) );
130 return Status::newGood( $parserOutput );
134 * @param Status $status
135 * @return Status
137 public function error( $status ) {
138 return $status;
143 /** @deprecated class alias since 1.42 */
144 class_alias( PoolWorkArticleView::class, 'PoolWorkArticleView' );