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