Debian Codegraph @

includes/poolcounter/PoolWorkArticleView.php

raw symbols SCIP
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
21 namespace MediaWiki\PoolCounter;
22
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;
33
34 /**
35 * PoolCounter protected work wrapping RenderedRevision->getRevisionParserOutput.
36 * Caching behavior may be defined by subclasses.
37 *
38 * @note No audience checks are applied.
39 *
40 * @internal
41 */
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;
51
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
58 */
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;
71 }
72
73 /**
74 * @return Status
75 */
76 public function doWork() {
77 return $this->renderRevision();
78 }
79
80 /**
81 * Render the given revision.
82 *
83 * @see ParserOutputAccess::renderRevision
84 *
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
90 */
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,
100 [
101 'audience' => RevisionRecord::RAW,
102 'previous-output' => $previousOutput,
103 ]
104 );
105
106 $parserOutput = $renderedRevision->getRevisionParserOutput();
107
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',
119 ];
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' ) );
128 }
129
130 return Status::newGood( $parserOutput );
131 }
132
133 /**
134 * @param Status $status
135 * @return Status
136 */
137 public function error( $status ) {
138 return $status;
139 }
140
141 }
142
143 /** @deprecated class alias since 1.42 */
144 class_alias( PoolWorkArticleView::class, 'PoolWorkArticleView' );