265aa9c2541d7f7eebc431eafde18fffb4bf8a1e
[lhc/web/wiklou.git] / includes / JobQueue.php
1 <?php
2
3 if ( !defined( 'MEDIAWIKI' ) ) {
4 die( "This file is part of MediaWiki, it is not a valid entry point\n" );
5 }
6
7 class Job {
8 var $command,
9 $title,
10 $params,
11 $removeDuplicates,
12 $error;
13
14 /*-------------------------------------------------------------------------
15 * Static functions
16 *------------------------------------------------------------------------*/
17 /**
18 * Add an array of refreshLinks jobs to the queue
19 * @param array $titles Array of title objects.
20 * @static
21 */
22 function queueLinksJobs( $titles ) {
23 $fname = 'Job::queueLinksJobs';
24 wfProfileIn( $fname );
25 foreach ( $titles as $title ) {
26 $job = new Job( 'refreshLinks', $title );
27 $job->insert();
28 }
29 wfProfileOut( $fname );
30 }
31
32 /**
33 * Pop a job off the front of the queue
34 * @static
35 * @return Job or false if there's no jobs
36 */
37 function pop() {
38 $fname = 'Job::pop';
39 wfProfileIn( $fname );
40
41 // First check to see if there are any jobs in the slave DB
42 $dbr =& wfGetDB( DB_SLAVE );
43 $id = $dbr->selectField( 'job', 'job_id', '', $fname, array( 'LIMIT' => 1 ) );
44 if ( $id === false ) {
45 wfProfileOut( $fname );
46 return false;
47 }
48
49 // Pop an item off the front of the queue
50 // Method due to Domas, may not work on all DBMSes
51 $dbw =& wfGetDB( DB_MASTER );
52 $jobTable = $dbw->tableName( 'job' );
53 $dbw->query( "DELETE FROM $jobTable WHERE " .
54 '(job_cmd = @job_cmd := job_cmd) AND ' .
55 '(job_namespace = @job_namespace := job_namespace) AND ' .
56 '(job_title = @job_title := job_title) AND ' .
57 '(job_params = @job_params := job_params) ' .
58 'LIMIT 1', $fname );
59 $affected = $dbw->affectedRows();
60 // Commit now before 100 other threads pile up behind us
61 $dbw->immediateCommit();
62 if ( !$affected ) {
63 wfProfileOut( $fname );
64 return false;
65 }
66
67 $res = $dbw->query( "SELECT @job_cmd, @job_namespace, @job_title, @job_params", $fname );
68 $row = $dbw->fetchRow( $res );
69 if ( !$row ) {
70 wfProfileOut( $fname );
71 return false;
72 }
73
74 $command = $row['@job_cmd'];
75 $namespace = $row['@job_namespace'];
76 $dbkey = $row['@job_title'];
77 $title = Title::makeTitleSafe( $namespace, $dbkey );
78 $params = $row['@job_params'];
79 $job = new Job( $command, $title, $params );
80 wfProfileOut( $fname );
81 return $job;
82 }
83
84 /*-------------------------------------------------------------------------
85 * Non-static functions
86 *------------------------------------------------------------------------*/
87
88 function Job( $command, $title, $params = '' ) {
89 $this->command = $command;
90 $this->title = $title;
91 $this->params = $params;
92
93 // A bit of premature generalisation
94 // Oh well, the whole class is premature generalisation really
95 $this->removeDuplicates = true;
96 }
97
98 function insert() {
99 $fname = 'Job::insert';
100
101 $fields = array(
102 'job_cmd' => $this->command,
103 'job_namespace' => $this->title->getNamespace(),
104 'job_title' => $this->title->getDBkey(),
105 'job_params' => $this->params
106 );
107
108 $dbw =& wfGetDB( DB_MASTER );
109
110 if ( $this->removeDuplicates ) {
111 $dbw->delete( 'job', $fields, $fname );
112 }
113 $fields['job_id'] = $dbw->nextSequenceValue( 'job_job_id_seq' );
114 $dbw->insert( 'job', $fields, $fname );
115 }
116
117 /**
118 * Run the job
119 * @return boolean success
120 */
121 function run() {
122 $fname = 'Job::run';
123 wfProfileIn( $fname );
124 switch ( $this->command ) {
125 case 'refreshLinks':
126 $retval = $this->refreshLinks();
127 break;
128 default:
129 $this->error = "Invalid job type {$this->command}, ignoring";
130 wfDebug( $this->error . "\n" );
131 $retval = false;
132 }
133 wfProfileOut( $fname );
134 return $retval;
135 }
136
137 /**
138 * Run a refreshLinks job
139 * @return boolean success
140 */
141 function refreshLinks() {
142 global $wgParser;
143
144 $dbw =& wfGetDB( DB_MASTER );
145
146 $linkCache =& LinkCache::singleton();
147 $linkCache->clear();
148
149 if ( is_null( $this->title ) ) {
150 $this->error = "refreshLinks: Invalid title";
151 return false;
152 }
153
154 $revision = Revision::newFromTitle( $this->title );
155 if ( !$revision ) {
156 $this->error = 'refreshLinks: Article not found "' . $this->title->getPrefixedDBkey() . '"';
157 return false;
158 }
159
160 $options = new ParserOptions;
161 $parserOutput = $wgParser->parse( $revision->getText(), $this->title, $options, true, true, $revision->getId() );
162 $update = new LinksUpdate( $this->title, $parserOutput );
163 $update->doUpdate();
164 return true;
165 }
166
167 function toString() {
168 if ( is_object( $this->title ) ) {
169 $s = "{$this->command} " . $this->title->getPrefixedDBkey();
170 if ( $this->params !== '' ) {
171 $s .= ', ' . $this->params;
172 }
173 return $s;
174 } else {
175 return "{$this->command} {$this->params}";
176 }
177 }
178
179 function getLastError() {
180 return $this->error;
181 }
182 }