summaryrefslogtreecommitdiff
path: root/includes/RefreshLinksJob.php
blob: 91cff40b7a6c495077309f0f2a35b0c35c34019f (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
<?php

/**
 * 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() {
		global $wgParser;
		wfProfileIn( __METHOD__ );

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

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

		$revision = Revision::newFromTitle( $this->title );
		if ( !$revision ) {
			$this->error = 'refreshLinks: Article not found "' . $this->title->getPrefixedDBkey() . '"';
			wfProfileOut( __METHOD__ );
			return false;
		}

		wfProfileIn( __METHOD__.'-parse' );
		$options = new ParserOptions;
		$parserOutput = $wgParser->parse( $revision->getText(), $this->title, $options, true, true, $revision->getId() );
		wfProfileOut( __METHOD__.'-parse' );
		wfProfileIn( __METHOD__.'-update' );
		$update = new LinksUpdate( $this->title, $parserOutput, false );
		$update->doUpdate();
		wfProfileOut( __METHOD__.'-update' );
		wfProfileOut( __METHOD__ );
		return true;
	}
}

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

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

	/**
	 * Run a refreshLinks2 job
	 * @return boolean success
	 */
	function run() {
		global $wgParser;

		wfProfileIn( __METHOD__ );

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

		if( is_null( $this->title ) ) {
			$this->error = "refreshLinks2: Invalid title";
			wfProfileOut( __METHOD__ );
			return false;
		}
		if( !isset($this->params['start']) || !isset($this->params['end']) ) {
			$this->error = "refreshLinks2: Invalid params";
			wfProfileOut( __METHOD__ );
			return false;
		}
		$titles = $this->title->getBacklinkCache()->getLinks( 
			'templatelinks', $this->params['start'], $this->params['end']);
		
		# Not suitable for page load triggered job running!
		# Gracefully switch to refreshLinks jobs if this happens.
		if( php_sapi_name() != 'cli' ) {
			$jobs = array();
			foreach ( $titles as $title ) {
				$jobs[] = new RefreshLinksJob( $title, '' );
			}
			Job::batchInsert( $jobs );
			return true;
		}
		# Re-parse each page that transcludes this page and update their tracking links...
		foreach ( $titles as $title ) {
			$revision = Revision::newFromTitle( $title );
			if ( !$revision ) {
				$this->error = 'refreshLinks: Article not found "' . $title->getPrefixedDBkey() . '"';
				wfProfileOut( __METHOD__ );
				return false;
			}
			wfProfileIn( __METHOD__.'-parse' );
			$options = new ParserOptions;
			$parserOutput = $wgParser->parse( $revision->getText(), $title, $options, true, true, $revision->getId() );
			wfProfileOut( __METHOD__.'-parse' );
			wfProfileIn( __METHOD__.'-update' );
			$update = new LinksUpdate( $title, $parserOutput, false );
			$update->doUpdate();
			wfProfileOut( __METHOD__.'-update' );
			wfProfileOut( __METHOD__ );
		}

		return true;
	}
}