summaryrefslogtreecommitdiff
blob: d9f7dcb236b60bb5e483c9d4b29d39fc5529fad7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php

namespace SMW\Tests\Util;

use SMW\DBConnectionProvider;

use Job;

/**
 * Partly copied from the MW 1.19 RunJobs maintenance script
 *
 * @ingroup Test
 *
 * @group SMW
 * @group SMWExtension
 *
 * @license GNU GPL v2+
 * @since 1.9.2
 */
class JobQueueRunner {

	protected $type = null;
	protected $status = array();
	protected $dbConnectionProvider = null;

	/**
	 * @since 1.9.2
	 *
	 * @param string|null $type
	 * @param DBConnectionProvider|null $dbConnectionProvider
	 */
	public function __construct( $type = null, DBConnectionProvider $dbConnectionProvider = null ) {
		$this->type = $type;
		$this->dbConnectionProvider = $dbConnectionProvider;

		if ( $this->dbConnectionProvider === null ) {
			$this->dbConnectionProvider = new MwDBConnectionProvider();
		}
	}

	/**
	 * @since 1.9.2
	 */
	public function run() {

		$conds = '';

		if ( $this->type !== null ) {
			$conds = "job_cmd = " . $this->dbConnectionProvider->getConnection()->addQuotes( $this->type );
		}

		while ( $this->dbConnectionProvider->getConnection()->selectField( 'job', 'job_id', $conds, __METHOD__ ) ) {
			$offset = 0;

			$job = $this->type === null ? Job::pop( $offset ) : Job::pop_type( $this->type );

			if ( !$job ) {
				break;
			}

			wfWaitForSlaves();

			$this->status[] = array(
				'type'   => $job->command,
				'status' => $job->run()
			);
		}
	}

	/**
	 * @since  2.0
	 */
	public function deleteAllJobs() {

		$conditions = '*';

		if ( $this->type !== null ) {
			$conditions = "job_cmd = " . $this->dbConnectionProvider->getConnection()->addQuotes( $this->type );
		}

		$this->dbConnectionProvider->getConnection()->delete(
			'job',
			$conditions,
			__METHOD__
		);
	}

	/**
	 * @since 1.9.2
	 *
	 * @return array
	 */
	public function getStatus() {
		return $this->status;
	}

}