API bug 10046: incorrect action produces invalid response format
[lhc/web/wiklou.git] / includes / JobQueue.php
index 140130f..a4e4963 100644 (file)
@@ -4,6 +4,8 @@ if ( !defined( 'MEDIAWIKI' ) ) {
        die( "This file is part of MediaWiki, it is not a valid entry point\n" );
 }
 
+require_once('UserMailer.php');
+
 /**
  * Class to both describe a background job and handle jobs.
  */
@@ -36,6 +38,46 @@ abstract class Job {
         * static function queueLinksJobs( $titles ) {}
         */
 
+       /**
+        * Pop a job of a certain type.  This tries less hard than pop() to 
+        * actually find a job; it may be adversely affected by concurrent job 
+        * runners.
+        */
+       static function pop_type($type) {
+               wfProfilein( __METHOD__ );
+
+               $dbw = wfGetDB( DB_MASTER );
+
+
+               $row = $dbw->selectRow( 'job', '*', array( 'job_cmd' => $type ), __METHOD__,
+                               array( 'LIMIT' => 1 ));
+
+               if ($row === false) {
+                       wfProfileOut( __METHOD__ );
+                       return false;
+               }
+
+               /* Ensure we "own" this row */
+               $dbw->delete( 'job', array( 'job_id' => $row->job_id ), __METHOD__ );
+               $affected = $dbw->affectedRows();
+
+               if ($affected == 0) {
+                       wfProfileOut( __METHOD__ );
+                       return false;
+               }
+
+               $namespace = $row->job_namespace;
+               $dbkey = $row->job_title;
+               $title = Title::makeTitleSafe( $namespace, $dbkey );
+               $job = Job::factory( $row->job_cmd, $title, Job::extractBlob( $row->job_params ), $row->job_id );
+
+               $dbw->delete( 'job', $job->insertFields(), __METHOD__ );
+               $dbw->immediateCommit();
+
+               wfProfileOut( __METHOD__ );
+               return $job;
+       }
+
        /**
         * Pop a job off the front of the queue
         * @static
@@ -134,6 +176,10 @@ abstract class Job {
                        case 'htmlCacheUpdate':
                        case 'html_cache_update': # BC
                                return new HTMLCacheUpdateJob( $title, $params['table'], $params['start'], $params['end'], $id );
+                       case 'sendMail':
+                               return new EmaillingJob($params);
+                       case 'enotifNotify':
+                               return new EnotifNotifyJob($title, $params);
                        default:
                                throw new MWException( "Invalid job command \"$command\"" );
                }
@@ -291,4 +337,30 @@ class RefreshLinksJob extends Job {
        }
 }
 
+class EmaillingJob extends Job {
+       function __construct($params) {
+               parent::__construct('sendMail', Title::newMainPage(), $params);
+       }
+
+       function run() {
+               userMailer($this->params['to'], $this->params['from'], $this->params['subj'],
+                               $this->params['body'], $this->params['replyto']);
+       }
+}
+
+class EnotifNotifyJob extends Job {
+       function __construct($title, $params) {
+               parent::__construct('enotifNotify', $title, $params);
+       }
+
+       function run() {
+               $enotif = new EmailNotification();
+               $enotif->actuallyNotifyOnPageChange( User::newFromName($this->params['editor'], false),
+                               $this->title, $this->params['timestamp'],
+                               $this->params['summary'], $this->params['minorEdit'],
+                               $this->params['oldid']);
+       }
+}
+
+
 ?>