aboutsummaryrefslogtreecommitdiffstats
path: root/includes/deferred/DeferredUpdatesScopeStack.php
blob: 56056d866e3f1d1384c0e3b3a8ba760cfed625a8 (plain) (blame)
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
/**
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 * http://www.gnu.org/copyleft/gpl.html
 *
 * @file
 */

namespace MediaWiki\Deferred;

use LogicException;

/**
 * DeferredUpdates helper class for tracking DeferrableUpdate::doUpdate() nesting levels
 * caused by nested calls to DeferredUpdates::doUpdates()
 *
 * @internal For use by DeferredUpdates only
 * @since 1.36
 */
class DeferredUpdatesScopeStack {
	/** @var DeferredUpdatesScope[] Stack of root scope and any recursive scopes */
	private $stack;

	public function __construct() {
		$this->stack = [ DeferredUpdatesScope::newRootScope() ];
	}

	/**
	 * @return DeferredUpdatesScope The innermost scope
	 */
	public function current() {
		return $this->stack[count( $this->stack ) - 1];
	}

	/**
	 * Make a new child scope, push it onto the stack, and return it
	 *
	 * @param int $activeStage The in-progress stage; one of DeferredUpdates::STAGES
	 * @param DeferrableUpdate $update The deferred update that owns this scope
	 * @return DeferredUpdatesScope Scope for the case of an in-progress deferred update
	 */
	public function descend( $activeStage, DeferrableUpdate $update ) {
		$scope = DeferredUpdatesScope::newChildScope( $activeStage, $update, $this->current() );
		$this->stack[count( $this->stack )] = $scope;

		return $scope;
	}

	/**
	 * Pop the innermost scope from the stack
	 *
	 * @return DeferredUpdatesScope
	 */
	public function ascend() {
		if ( count( $this->stack ) <= 1 ) {
			throw new LogicException( "Cannot pop root stack scope; something is out of sync" );
		}

		return array_pop( $this->stack );
	}

	/**
	 * Get the depth of the scope stack below the root scope
	 *
	 * @return int
	 */
	public function getRecursiveDepth() {
		return count( $this->stack ) - 1;
	}

	/**
	 * Whether DeferredUpdates::addUpdate() may run the update right away
	 *
	 * @return bool
	 */
	public function allowOpportunisticUpdates(): bool {
		// Overridden in DeferredUpdatesScopeMediaWikiStack::allowOpportunisticUpdates
		return false;
	}

	/**
	 * Queue an EnqueueableDataUpdate as a job instead
	 *
	 * @see JobQueueGroup::push
	 * @param EnqueueableDataUpdate $update
	 */
	public function queueDataUpdate( EnqueueableDataUpdate $update ): void {
		throw new LogicException( 'Cannot queue jobs from DeferredUpdates in standalone mode' );
	}

	/**
	 * @param DeferrableUpdate $update
	 */
	public function onRunUpdateStart( DeferrableUpdate $update ): void {
		// No-op
		// Overridden in DeferredUpdatesScopeMediaWikiStack::onRunUpdateStart
	}

	/**
	 * @param DeferrableUpdate $update
	 */
	public function onRunUpdateEnd( DeferrableUpdate $update ): void {
		// No-op
		// Overridden in DeferredUpdatesScopeMediaWikiStack::onRunUpdateEnd
	}

	/**
	 * @param DeferrableUpdate $update
	 */
	public function onRunUpdateFailed( DeferrableUpdate $update ): void {
		// No-op
		// Overridden in DeferredUpdatesScopeMediaWikiStack::onRunUpdateFailed
	}
}

/** @deprecated class alias since 1.42 */
class_alias( DeferredUpdatesScopeStack::class, 'DeferredUpdatesScopeStack' );