generate links and other secondary data for RefreshLinks via ContentHandler
[lhc/web/wiklou.git] / includes / job / RefreshLinksJob.php
1 <?php
2 /**
3 * Job to update links for a given title.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup JobQueue
22 */
23
24 /**
25 * Background job to update links for a given title.
26 *
27 * @ingroup JobQueue
28 */
29 class RefreshLinksJob extends Job {
30
31 function __construct( $title, $params = '', $id = 0 ) {
32 parent::__construct( 'refreshLinks', $title, $params, $id );
33 }
34
35 /**
36 * Run a refreshLinks job
37 * @return boolean success
38 */
39 function run() {
40 global $wgParser, $wgContLang;
41 wfProfileIn( __METHOD__ );
42
43 $linkCache = LinkCache::singleton();
44 $linkCache->clear();
45
46 if ( is_null( $this->title ) ) {
47 $this->error = "refreshLinks: Invalid title";
48 wfProfileOut( __METHOD__ );
49 return false;
50 }
51
52 $revision = Revision::newFromTitle( $this->title );
53 if ( !$revision ) {
54 $this->error = 'refreshLinks: Article not found "' . $this->title->getPrefixedDBkey() . '"';
55 wfProfileOut( __METHOD__ );
56 return false;
57 }
58
59 wfProfileIn( __METHOD__.'-parse' );
60 $options = ParserOptions::newFromUserAndLang( new User, $wgContLang );
61 $content = $revision->getContent();
62 $parserOutput = $content->getParserOutput( $this->title, $revision->getId(), $options, false );
63 wfProfileOut( __METHOD__.'-parse' );
64 wfProfileIn( __METHOD__.'-update' );
65
66 $updates = $content->getContentHandler()->getSecondaryDataUpdates( $content, $this->title, null, false, $parserOutput );
67 DataUpdate::runUpdates( $updates );
68
69 wfProfileOut( __METHOD__.'-update' );
70 wfProfileOut( __METHOD__ );
71 return true;
72 }
73 }
74
75 /**
76 * Background job to update links for a given title.
77 * Newer version for high use templates.
78 *
79 * @ingroup JobQueue
80 */
81 class RefreshLinksJob2 extends Job {
82
83 function __construct( $title, $params, $id = 0 ) {
84 parent::__construct( 'refreshLinks2', $title, $params, $id );
85 }
86
87 /**
88 * Run a refreshLinks2 job
89 * @return boolean success
90 */
91 function run() {
92 global $wgParser, $wgContLang;
93
94 wfProfileIn( __METHOD__ );
95
96 $linkCache = LinkCache::singleton();
97 $linkCache->clear();
98
99 if( is_null( $this->title ) ) {
100 $this->error = "refreshLinks2: Invalid title";
101 wfProfileOut( __METHOD__ );
102 return false;
103 }
104 if( !isset($this->params['start']) || !isset($this->params['end']) ) {
105 $this->error = "refreshLinks2: Invalid params";
106 wfProfileOut( __METHOD__ );
107 return false;
108 }
109 // Back compat for pre-r94435 jobs
110 $table = isset( $this->params['table'] ) ? $this->params['table'] : 'templatelinks';
111 $titles = $this->title->getBacklinkCache()->getLinks(
112 $table, $this->params['start'], $this->params['end']);
113
114 # Not suitable for page load triggered job running!
115 # Gracefully switch to refreshLinks jobs if this happens.
116 if( php_sapi_name() != 'cli' ) {
117 $jobs = array();
118 foreach ( $titles as $title ) {
119 $jobs[] = new RefreshLinksJob( $title, '' );
120 }
121 Job::batchInsert( $jobs );
122
123 wfProfileOut( __METHOD__ );
124 return true;
125 }
126 $options = ParserOptions::newFromUserAndLang( new User, $wgContLang );
127 # Re-parse each page that transcludes this page and update their tracking links...
128 foreach ( $titles as $title ) {
129 $revision = Revision::newFromTitle( $title );
130 if ( !$revision ) {
131 $this->error = 'refreshLinks: Article not found "' . $title->getPrefixedDBkey() . '"';
132 wfProfileOut( __METHOD__ );
133 return false;
134 }
135 wfProfileIn( __METHOD__.'-parse' );
136 $options = ParserOptions::newFromUserAndLang( new User, $wgContLang );
137 $content = $revision->getContent();
138 $parserOutput = $content->getParserOutput( $this->title, $revision->getId(), $options, false );
139 wfProfileOut( __METHOD__.'-parse' );
140 wfProfileIn( __METHOD__.'-update' );
141
142 $updates = $content->getContentHandler()->getSecondaryDataUpdates( $content, $this->title, null, false, $parserOutput );
143 DataUpdate::runUpdates( $updates );
144
145 wfProfileOut( __METHOD__.'-update' );
146 wfWaitForSlaves();
147 }
148 wfProfileOut( __METHOD__ );
149
150 return true;
151 }
152 }