use ParserOutput::getSecondaryDataUpdates() as the primary method of getting pending...
[lhc/web/wiklou.git] / includes / job / RefreshLinksJob.php
1 <?php
2 /**
3 * Job to update links for a given title.
4 *
5 * @file
6 * @ingroup JobQueue
7 */
8
9 /**
10 * Background job to update links for a given title.
11 *
12 * @ingroup JobQueue
13 */
14 class RefreshLinksJob extends Job {
15
16 function __construct( $title, $params = '', $id = 0 ) {
17 parent::__construct( 'refreshLinks', $title, $params, $id );
18 }
19
20 /**
21 * Run a refreshLinks job
22 * @return boolean success
23 */
24 function run() {
25 global $wgParser, $wgContLang;
26 wfProfileIn( __METHOD__ );
27
28 $linkCache = LinkCache::singleton();
29 $linkCache->clear();
30
31 if ( is_null( $this->title ) ) {
32 $this->error = "refreshLinks: Invalid title";
33 wfProfileOut( __METHOD__ );
34 return false;
35 }
36
37 $revision = Revision::newFromTitle( $this->title );
38 if ( !$revision ) {
39 $this->error = 'refreshLinks: Article not found "' . $this->title->getPrefixedDBkey() . '"';
40 wfProfileOut( __METHOD__ );
41 return false;
42 }
43
44 wfProfileIn( __METHOD__.'-parse' );
45 $options = ParserOptions::newFromUserAndLang( new User, $wgContLang );
46 $parserOutput = $wgParser->parse( $revision->getText(), $this->title, $options, true, true, $revision->getId() );
47 wfProfileOut( __METHOD__.'-parse' );
48 wfProfileIn( __METHOD__.'-update' );
49
50 $updates = $parserOutput->getSecondaryDataUpdates( $this->title, false );
51 SecondaryDataUpdate::runUpdates( $updates );
52
53 wfProfileOut( __METHOD__.'-update' );
54 wfProfileOut( __METHOD__ );
55 return true;
56 }
57 }
58
59 /**
60 * Background job to update links for a given title.
61 * Newer version for high use templates.
62 *
63 * @ingroup JobQueue
64 */
65 class RefreshLinksJob2 extends Job {
66
67 function __construct( $title, $params, $id = 0 ) {
68 parent::__construct( 'refreshLinks2', $title, $params, $id );
69 }
70
71 /**
72 * Run a refreshLinks2 job
73 * @return boolean success
74 */
75 function run() {
76 global $wgParser, $wgContLang;
77
78 wfProfileIn( __METHOD__ );
79
80 $linkCache = LinkCache::singleton();
81 $linkCache->clear();
82
83 if( is_null( $this->title ) ) {
84 $this->error = "refreshLinks2: Invalid title";
85 wfProfileOut( __METHOD__ );
86 return false;
87 }
88 if( !isset($this->params['start']) || !isset($this->params['end']) ) {
89 $this->error = "refreshLinks2: Invalid params";
90 wfProfileOut( __METHOD__ );
91 return false;
92 }
93 // Back compat for pre-r94435 jobs
94 $table = isset( $this->params['table'] ) ? $this->params['table'] : 'templatelinks';
95 $titles = $this->title->getBacklinkCache()->getLinks(
96 $table, $this->params['start'], $this->params['end']);
97
98 # Not suitable for page load triggered job running!
99 # Gracefully switch to refreshLinks jobs if this happens.
100 if( php_sapi_name() != 'cli' ) {
101 $jobs = array();
102 foreach ( $titles as $title ) {
103 $jobs[] = new RefreshLinksJob( $title, '' );
104 }
105 Job::batchInsert( $jobs );
106
107 wfProfileOut( __METHOD__ );
108 return true;
109 }
110 $options = ParserOptions::newFromUserAndLang( new User, $wgContLang );
111 # Re-parse each page that transcludes this page and update their tracking links...
112 foreach ( $titles as $title ) {
113 $revision = Revision::newFromTitle( $title );
114 if ( !$revision ) {
115 $this->error = 'refreshLinks: Article not found "' . $title->getPrefixedDBkey() . '"';
116 wfProfileOut( __METHOD__ );
117 return false;
118 }
119 wfProfileIn( __METHOD__.'-parse' );
120 $parserOutput = $wgParser->parse( $revision->getText(), $title, $options, true, true, $revision->getId() );
121 wfProfileOut( __METHOD__.'-parse' );
122 wfProfileIn( __METHOD__.'-update' );
123
124 $updates = $parserOutput->getSecondaryDataUpdates( $title, false );
125 SecondaryDataUpdate::runUpdates( $updates );
126
127 wfProfileOut( __METHOD__.'-update' );
128 wfWaitForSlaves();
129 }
130 wfProfileOut( __METHOD__ );
131
132 return true;
133 }
134 }