summaryrefslogtreecommitdiff
path: root/includes/poolcounter/PoolWorkArticleView.php
blob: a702d2e885cfb07690f1166817d98a30b7f53016 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php
/**
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 * http://www.gnu.org/copyleft/gpl.html
 *
 * @file
 */

class PoolWorkArticleView extends PoolCounterWork {
	/** @var Page */
	private $page;

	/** @var string */
	private $cacheKey;

	/** @var int */
	private $revid;

	/** @var ParserOptions */
	private $parserOptions;

	/** @var Content|null */
	private $content = null;

	/** @var ParserOutput|bool */
	private $parserOutput = false;

	/** @var bool */
	private $isDirty = false;

	/** @var Status|bool */
	private $error = false;

	/**
	 * @param Page $page
	 * @param ParserOptions $parserOptions ParserOptions to use for the parse
	 * @param int $revid ID of the revision being parsed.
	 * @param bool $useParserCache Whether to use the parser cache.
	 *   operation.
	 * @param Content|string $content Content to parse or null to load it; may
	 *   also be given as a wikitext string, for BC.
	 */
	public function __construct( Page $page, ParserOptions $parserOptions,
		$revid, $useParserCache, $content = null
	) {
		if ( is_string( $content ) ) { // BC: old style call
			$modelId = $page->getRevision()->getContentModel();
			$format = $page->getRevision()->getContentFormat();
			$content = ContentHandler::makeContent( $content, $page->getTitle(), $modelId, $format );
		}

		$this->page = $page;
		$this->revid = $revid;
		$this->cacheable = $useParserCache;
		$this->parserOptions = $parserOptions;
		$this->content = $content;
		$this->cacheKey = ParserCache::singleton()->getKey( $page, $parserOptions );
		$keyPrefix = $this->cacheKey ?: wfMemcKey( 'articleview', 'missingcachekey' );
		parent::__construct( 'ArticleView', $keyPrefix . ':revid:' . $revid );
	}

	/**
	 * Get the ParserOutput from this object, or false in case of failure
	 *
	 * @return ParserOutput
	 */
	public function getParserOutput() {
		return $this->parserOutput;
	}

	/**
	 * Get whether the ParserOutput is a dirty one (i.e. expired)
	 *
	 * @return bool
	 */
	public function getIsDirty() {
		return $this->isDirty;
	}

	/**
	 * Get a Status object in case of error or false otherwise
	 *
	 * @return Status|bool
	 */
	public function getError() {
		return $this->error;
	}

	/**
	 * @return bool
	 */
	public function doWork() {
		global $wgUseFileCache;

		// @todo several of the methods called on $this->page are not declared in Page, but present
		//        in WikiPage and delegated by Article.

		$isCurrent = $this->revid === $this->page->getLatest();

		if ( $this->content !== null ) {
			$content = $this->content;
		} elseif ( $isCurrent ) {
			// XXX: why use RAW audience here, and PUBLIC (default) below?
			$content = $this->page->getContent( Revision::RAW );
		} else {
			$rev = Revision::newFromTitle( $this->page->getTitle(), $this->revid );

			if ( $rev === null ) {
				$content = null;
			} else {
				// XXX: why use PUBLIC audience here (default), and RAW above?
				$content = $rev->getContent();
			}
		}

		if ( $content === null ) {
			return false;
		}

		// Reduce effects of race conditions for slow parses (bug 46014)
		$cacheTime = wfTimestampNow();

		$time = - microtime( true );
		$this->parserOutput = $content->getParserOutput(
			$this->page->getTitle(),
			$this->revid,
			$this->parserOptions
		);
		$time += microtime( true );

		// Timing hack
		if ( $time > 3 ) {
			wfDebugLog( 'slow-parse', sprintf( "%-5.2f %s", $time,
				$this->page->getTitle()->getPrefixedDBkey() ) );
		}

		if ( $this->cacheable && $this->parserOutput->isCacheable() && $isCurrent ) {
			ParserCache::singleton()->save(
				$this->parserOutput, $this->page, $this->parserOptions, $cacheTime, $this->revid );
		}

		// Make sure file cache is not used on uncacheable content.
		// Output that has magic words in it can still use the parser cache
		// (if enabled), though it will generally expire sooner.
		if ( !$this->parserOutput->isCacheable() ) {
			$wgUseFileCache = false;
		}

		if ( $isCurrent ) {
			$this->page->triggerOpportunisticLinksUpdate( $this->parserOutput );
		}

		return true;
	}

	/**
	 * @return bool
	 */
	public function getCachedWork() {
		$this->parserOutput = ParserCache::singleton()->get( $this->page, $this->parserOptions );

		if ( $this->parserOutput === false ) {
			wfDebug( __METHOD__ . ": parser cache miss\n" );
			return false;
		} else {
			wfDebug( __METHOD__ . ": parser cache hit\n" );
			return true;
		}
	}

	/**
	 * @return bool
	 */
	public function fallback() {
		$this->parserOutput = ParserCache::singleton()->getDirty( $this->page, $this->parserOptions );

		if ( $this->parserOutput === false ) {
			wfDebugLog( 'dirty', 'dirty missing' );
			wfDebug( __METHOD__ . ": no dirty cache\n" );
			return false;
		} else {
			wfDebug( __METHOD__ . ": sending dirty output\n" );
			wfDebugLog( 'dirty', "dirty output {$this->cacheKey}" );
			$this->isDirty = true;
			return true;
		}
	}

	/**
	 * @param Status $status
	 * @return bool
	 */
	public function error( $status ) {
		$this->error = $status;
		return false;
	}
}