summaryrefslogtreecommitdiff
path: root/includes/job/RefreshLinksJob.php
blob: b23951c6f598f4e5c7856a10f65d03d4f410f9cd (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
<?php
/**
 * Job to update links for a given title.
 *
 * 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
 * @ingroup JobQueue
 */

/**
 * Background job to update links for a given title.
 *
 * @ingroup JobQueue
 */
class RefreshLinksJob extends Job {

	function __construct( $title, $params = '', $id = 0 ) {
		parent::__construct( 'refreshLinks', $title, $params, $id );
	}

	/**
	 * Run a refreshLinks job
	 * @return boolean success
	 */
	function run() {
		wfProfileIn( __METHOD__ );

		$linkCache = LinkCache::singleton();
		$linkCache->clear();

		if ( is_null( $this->title ) ) {
			$this->error = "refreshLinks: Invalid title";
			wfProfileOut( __METHOD__ );
			return false;
		}

		# Wait for the DB of the current/next slave DB handle to catch up to the master.
		# This way, we get the correct page_latest for templates or files that just changed
		# milliseconds ago, having triggered this job to begin with.
		if ( isset( $this->params['masterPos'] ) ) {
			wfGetLB()->waitFor( $this->params['masterPos'] );
		}

		$revision = Revision::newFromTitle( $this->title, false, Revision::READ_NORMAL );
		if ( !$revision ) {
			$this->error = 'refreshLinks: Article not found "' .
				$this->title->getPrefixedDBkey() . '"';
			wfProfileOut( __METHOD__ );
			return false; // XXX: what if it was just deleted?
		}

		self::runForTitleInternal( $this->title, $revision, __METHOD__ );

		wfProfileOut( __METHOD__ );
		return true;
	}

	public static function runForTitleInternal( Title $title, Revision $revision, $fname ) {
		global $wgParser, $wgContLang;

		wfProfileIn( $fname . '-parse' );
		$options = ParserOptions::newFromUserAndLang( new User, $wgContLang );
		$parserOutput = $wgParser->parse(
			$revision->getText(), $title, $options, true, true, $revision->getId() );
		wfProfileOut( $fname . '-parse' );

		wfProfileIn( $fname . '-update' );
		$updates = $parserOutput->getSecondaryDataUpdates( $title, false );
		DataUpdate::runUpdates( $updates );
		wfProfileOut( $fname . '-update' );
	}
}

/**
 * Background job to update links for a given title.
 * Newer version for high use templates.
 *
 * @ingroup JobQueue
 */
class RefreshLinksJob2 extends Job {
	const MAX_TITLES_RUN = 10;

	function __construct( $title, $params, $id = 0 ) {
		parent::__construct( 'refreshLinks2', $title, $params, $id );
	}

	/**
	 * Run a refreshLinks2 job
	 * @return boolean success
	 */
	function run() {
		wfProfileIn( __METHOD__ );

		$linkCache = LinkCache::singleton();
		$linkCache->clear();

		if ( is_null( $this->title ) ) {
			$this->error = "refreshLinks2: Invalid title";
			wfProfileOut( __METHOD__ );
			return false;
		} elseif ( !isset( $this->params['start'] ) || !isset( $this->params['end'] ) ) {
			$this->error = "refreshLinks2: Invalid params";
			wfProfileOut( __METHOD__ );
			return false;
		}

		// Back compat for pre-r94435 jobs
		$table = isset( $this->params['table'] ) ? $this->params['table'] : 'templatelinks';

		// Avoid slave lag when fetching templates
		if ( isset( $this->params['masterPos'] ) ) {
			$masterPos = $this->params['masterPos'];
		} elseif ( wfGetLB()->getServerCount() > 1  ) {
			$masterPos = wfGetLB()->getMasterPos();
		} else {
			$masterPos = false;
		}

		$titles = $this->title->getBacklinkCache()->getLinks(
			$table, $this->params['start'], $this->params['end'] );

		if ( $titles->count() > self::MAX_TITLES_RUN ) {
			# We don't want to parse too many pages per job as it can starve other jobs.
			# If there are too many pages to parse, break this up into smaller jobs. By passing
			# in the master position here we can cut down on the time spent waiting for slaves to
			# catch up by the runners handling these jobs since time will have passed between now
			# and when they pop these jobs off the queue.
			$start = 0; // batch start
			$end   = 0; // batch end
			$bsize = 0; // batch size
			$first = true; // first of batch
			$jobs  = array();
			foreach ( $titles as $title ) {
				$start = $first ? $title->getArticleId() : $start;
				$end   = $title->getArticleId();
				$first = false;
				if ( ++$bsize >= self::MAX_TITLES_RUN ) {
					$jobs[] = new RefreshLinksJob2( $this->title, array(
						'table'     => $table,
						'start'     => $start,
						'end'       => $end,
						'masterPos' => $masterPos
					) );
					$first = true;
					$start = $end = $bsize = 0;
				}
			}
			if ( $bsize > 0 ) { // group remaining pages into a job
				$jobs[] = new RefreshLinksJob2( $this->title, array(
					'table'     => $table,
					'start'     => $start,
					'end'       => $end,
					'masterPos' => $masterPos
				) );
			}
			Job::batchInsert( $jobs );
		} elseif ( php_sapi_name() != 'cli' ) {
			# Not suitable for page load triggered job running!
			# Gracefully switch to refreshLinks jobs if this happens.
			$jobs = array();
			foreach ( $titles as $title ) {
				$jobs[] = new RefreshLinksJob( $title, array( 'masterPos' => $masterPos ) );
			}
			Job::batchInsert( $jobs );
		} else {
			# Wait for the DB of the current/next slave DB handle to catch up to the master.
			# This way, we get the correct page_latest for templates or files that just changed
			# milliseconds ago, having triggered this job to begin with.
			if ( $masterPos ) {
				wfGetLB()->waitFor( $masterPos );
			}
			# Re-parse each page that transcludes this page and update their tracking links...
			foreach ( $titles as $title ) {
				$revision = Revision::newFromTitle( $title, false, Revision::READ_NORMAL );
				if ( !$revision ) {
					$this->error = 'refreshLinks: Article not found "' .
						$title->getPrefixedDBkey() . '"';
					continue; // skip this page
				}
				RefreshLinksJob::runForTitleInternal( $title, $revision, __METHOD__ );
				wfWaitForSlaves();
			}
		}

		wfProfileOut( __METHOD__ );
		return true;
	}
}