]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - includes/RefreshLinksJob.php
MediaWiki 1.15.0
[autoinstallsdev/mediawiki.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                 $titles = $this->title->getBacklinkCache()->getLinks( 
86                         'templatelinks', $this->params['start'], $this->params['end']);
87                 
88                 # Not suitable for page load triggered job running!
89                 # Gracefully switch to refreshLinks jobs if this happens.
90                 if( php_sapi_name() != 'cli' ) {
91                         $jobs = array();
92                         foreach ( $titles as $title ) {
93                                 $jobs[] = new RefreshLinksJob( $title, '' );
94                         }
95                         Job::batchInsert( $jobs );
96                         return true;
97                 }
98                 # Re-parse each page that transcludes this page and update their tracking links...
99                 foreach ( $titles as $title ) {
100                         $revision = Revision::newFromTitle( $title );
101                         if ( !$revision ) {
102                                 $this->error = 'refreshLinks: Article not found "' . $title->getPrefixedDBkey() . '"';
103                                 wfProfileOut( __METHOD__ );
104                                 return false;
105                         }
106                         wfProfileIn( __METHOD__.'-parse' );
107                         $options = new ParserOptions;
108                         $parserOutput = $wgParser->parse( $revision->getText(), $title, $options, true, true, $revision->getId() );
109                         wfProfileOut( __METHOD__.'-parse' );
110                         wfProfileIn( __METHOD__.'-update' );
111                         $update = new LinksUpdate( $title, $parserOutput, false );
112                         $update->doUpdate();
113                         wfProfileOut( __METHOD__.'-update' );
114                         wfProfileOut( __METHOD__ );
115                 }
116
117                 return true;
118         }
119 }