Completed
Push — master ( 3294f9...cc37b1 )
by mw
19s
created

DeferredCallableUpdate::setOrigin()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 3
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
3
namespace SMW;
4
5
use Closure;
6
use DeferrableUpdate;
7
use DeferredUpdates;
8
use RuntimeException;
9
10
/**
11
 * @see MWCallableUpdate
12
 *
13
 * @license GNU GPL v2+
14
 * @since 2.4
15
 */
16
class DeferredCallableUpdate implements DeferrableUpdate {
17
18
	/**
19
	 * @var Closure|callable
20
	 */
21
	private $callback;
22
23
	/**
24
	 * @var boolean
25
	 */
26
	private $enabledDeferredUpdate = true;
27
28
	/**
29
	 * @var boolean
30
	 */
31
	private $isPending = false;
32
33
	/**
34
	 * @var string
35
	 */
36
	private $origin = '';
37
38
	/**
39
	 * @var array
40
	 */
41
	private static $pendingUpdates = array();
42
43
	/**
44
	 * @since 2.4
45
	 *
46
	 * @param Closure $callback
47
	 * @throws RuntimeException
48
	 */
49 236
	public function __construct( Closure $callback ) {
50
51 236
		if ( !is_callable( $callback ) ) {
52
			throw new RuntimeException( 'Expected a valid callback/closure!' );
53
		}
54
55 236
		$this->callback = $callback;
56 236
	}
57
58
	/**
59
	 * @note Unit/Integration tests in MW 1.26- showed ambiguous behaviour when
60
	 * run in deferred mode because not all MW operations were supporting late
61
	 * execution.
62
	 *
63
	 * @since 2.4
64
	 */
65 232
	public function enabledDeferredUpdate( $enabledDeferredUpdate = true ) {
66 232
		$this->enabledDeferredUpdate = $enabledDeferredUpdate;
67 232
	}
68
69
	/**
70
	 * @note If wgCommandLineMode = true (e.g. MW is in CLI mode) then
71
	 * DeferredUpdates::addUpdate pushes updates directly into execution mode
72
	 * which may not be desirable for all update processes therefore hold on to it
73
	 * by using an internal waitableUpdate list and release them at convenience.
74
	 *
75
	 * @since 2.4
76
	 */
77 57
	public function markAsPending( $isPending = false ) {
78 57
		$this->isPending = $isPending;
79 57
	}
80
81
	/**
82
	 * @since 2.5
83
	 *
84
	 * @param string $origin
85
	 */
86 232
	public function setOrigin( $origin ) {
87 232
		$this->origin = $origin;
88 232
	}
89
90
	/**
91
	 * @see DeferrableCallback::getOrigin
92
	 *
93
	 * @since 2.5
94
	 *
95
	 * @return string
96
	 */
97 1
	public function getOrigin() {
98 1
		return $this->origin;
99
	}
100
101
	/**
102
	 * @since 2.4
103
	 */
104 264
	public static function releasePendingUpdates() {
105 264
		foreach ( self::$pendingUpdates as $update ) {
106 57
			DeferredUpdates::addUpdate( $update );
107
		}
108
109 264
		self::$pendingUpdates = array();
110 264
	}
111
112
	/**
113
	 * @see DeferrableUpdate::doUpdate
114
	 *
115
	 * @since 2.4
116
	 */
117 234
	public function doUpdate() {
118 234
		wfDebugLog( 'smw', $this->origin . ' doUpdate' );
119 234
		call_user_func( $this->callback );
120 234
	}
121
122
	/**
123
	 * @since 2.4
124
	 * @deprecated since 2.5, use DeferredCallableUpdate::pushUpdate
125
	 */
126
	public function pushToDeferredUpdateList() {
127
		$this->pushUpdate();
128
	}
129
130
	/**
131
	 * @since 2.5
132
	 */
133 234
	public function pushUpdate() {
134
135 234
		if ( $this->isPending && $this->enabledDeferredUpdate ) {
136 57
			wfDebugLog( 'smw', $this->origin . ' (as pending DeferredCallableUpdate)' );
137 57
			return self::$pendingUpdates[] = $this;
138
		}
139
140 233
		if ( $this->enabledDeferredUpdate ) {
141 177
			wfDebugLog( 'smw', $this->origin . ' (as DeferredCallableUpdate)' );
142 177
			return DeferredUpdates::addUpdate( $this );
143
		}
144
145 232
		$this->doUpdate();
146 232
	}
147
148
}
149