| 1 | <?php |
| 2 | /** |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; either version 2 of the License, or |
| 6 | * (at your option) any later version. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License along |
| 14 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 16 | * http://www.gnu.org/copyleft/gpl.html |
| 17 | * |
| 18 | * @file |
| 19 | */ |
| 20 | |
| 21 | namespace MediaWiki\Deferred; |
| 22 | |
| 23 | use LogicException; |
| 24 | |
| 25 | /** |
| 26 | * DeferredUpdates helper class for tracking DeferrableUpdate::doUpdate() nesting levels |
| 27 | * caused by nested calls to DeferredUpdates::doUpdates() |
| 28 | * |
| 29 | * @internal For use by DeferredUpdates only |
| 30 | * @since 1.36 |
| 31 | */ |
| 32 | class DeferredUpdatesScopeStack { |
| 33 | /** @var DeferredUpdatesScope[] Stack of root scope and any recursive scopes */ |
| 34 | private $stack; |
| 35 | |
| 36 | public function __construct() { |
| 37 | $this->stack = [ DeferredUpdatesScope::newRootScope() ]; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * @return DeferredUpdatesScope The innermost scope |
| 42 | */ |
| 43 | public function current() { |
| 44 | return $this->stack[count( $this->stack ) - 1]; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Make a new child scope, push it onto the stack, and return it |
| 49 | * |
| 50 | * @param int $activeStage The in-progress stage; one of DeferredUpdates::STAGES |
| 51 | * @param DeferrableUpdate $update The deferred update that owns this scope |
| 52 | * @return DeferredUpdatesScope Scope for the case of an in-progress deferred update |
| 53 | */ |
| 54 | public function descend( $activeStage, DeferrableUpdate $update ) { |
| 55 | $scope = DeferredUpdatesScope::newChildScope( $activeStage, $update, $this->current() ); |
| 56 | $this->stack[count( $this->stack )] = $scope; |
| 57 | |
| 58 | return $scope; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Pop the innermost scope from the stack |
| 63 | * |
| 64 | * @return DeferredUpdatesScope |
| 65 | */ |
| 66 | public function ascend() { |
| 67 | if ( count( $this->stack ) <= 1 ) { |
| 68 | throw new LogicException( "Cannot pop root stack scope; something is out of sync" ); |
| 69 | } |
| 70 | |
| 71 | return array_pop( $this->stack ); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Get the depth of the scope stack below the root scope |
| 76 | * |
| 77 | * @return int |
| 78 | */ |
| 79 | public function getRecursiveDepth() { |
| 80 | return count( $this->stack ) - 1; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Whether DeferredUpdates::addUpdate() may run the update right away |
| 85 | * |
| 86 | * @return bool |
| 87 | */ |
| 88 | public function allowOpportunisticUpdates(): bool { |
| 89 | // Overridden in DeferredUpdatesScopeMediaWikiStack::allowOpportunisticUpdates |
| 90 | return false; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Queue an EnqueueableDataUpdate as a job instead |
| 95 | * |
| 96 | * @see JobQueueGroup::push |
| 97 | * @param EnqueueableDataUpdate $update |
| 98 | */ |
| 99 | public function queueDataUpdate( EnqueueableDataUpdate $update ): void { |
| 100 | throw new LogicException( 'Cannot queue jobs from DeferredUpdates in standalone mode' ); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * @param DeferrableUpdate $update |
| 105 | */ |
| 106 | public function onRunUpdateStart( DeferrableUpdate $update ): void { |
| 107 | // No-op |
| 108 | // Overridden in DeferredUpdatesScopeMediaWikiStack::onRunUpdateStart |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * @param DeferrableUpdate $update |
| 113 | */ |
| 114 | public function onRunUpdateEnd( DeferrableUpdate $update ): void { |
| 115 | // No-op |
| 116 | // Overridden in DeferredUpdatesScopeMediaWikiStack::onRunUpdateEnd |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * @param DeferrableUpdate $update |
| 121 | */ |
| 122 | public function onRunUpdateFailed( DeferrableUpdate $update ): void { |
| 123 | // No-op |
| 124 | // Overridden in DeferredUpdatesScopeMediaWikiStack::onRunUpdateFailed |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | /** @deprecated class alias since 1.42 */ |
| 129 | class_alias( DeferredUpdatesScopeStack::class, 'DeferredUpdatesScopeStack' ); |