MediaWiki REL1_27
DeferredUpdates.php
Go to the documentation of this file.
1<?php
41 private static $preSendUpdates = [];
43 private static $postSendUpdates = [];
44
45 const ALL = 0; // all updates
46 const PRESEND = 1; // for updates that should run before flushing output buffer
47 const POSTSEND = 2; // for updates that should run after flushing output buffer
48
55 public static function addUpdate( DeferrableUpdate $update, $type = self::POSTSEND ) {
56 if ( $type === self::PRESEND ) {
57 self::push( self::$preSendUpdates, $update );
58 } else {
59 self::push( self::$postSendUpdates, $update );
60 }
61 }
62
72 public static function addCallableUpdate( $callable, $type = self::POSTSEND ) {
73 self::addUpdate( new MWCallableUpdate( $callable ), $type );
74 }
75
82 public static function doUpdates( $mode = 'run', $type = self::ALL ) {
83 if ( $type === self::ALL || $type == self::PRESEND ) {
84 self::execute( self::$preSendUpdates, $mode );
85 }
86
87 if ( $type === self::ALL || $type == self::POSTSEND ) {
88 self::execute( self::$postSendUpdates, $mode );
89 }
90 }
91
92 private static function push( array &$queue, DeferrableUpdate $update ) {
94
95 if ( $update instanceof MergeableUpdate ) {
96 $class = get_class( $update ); // fully-qualified class
97 if ( isset( $queue[$class] ) ) {
99 $existingUpdate = $queue[$class];
100 $existingUpdate->merge( $update );
101 } else {
102 $queue[$class] = $update;
103 }
104 } else {
105 $queue[] = $update;
106 }
107
108 // CLI scripts may forget to periodically flush these updates,
109 // so try to handle that rather than OOMing and losing them entirely.
110 // Try to run the updates as soon as there is no current wiki transaction.
111 static $waitingOnTrx = false; // de-duplicate callback
112 if ( $wgCommandLineMode && !$waitingOnTrx ) {
113 $lb = wfGetLB();
114 $dbw = $lb->getAnyOpenConnection( $lb->getWriterIndex() );
115 // Do the update as soon as there is no transaction
116 if ( $dbw && $dbw->trxLevel() ) {
117 $waitingOnTrx = true;
118 $dbw->onTransactionIdle( function() use ( &$waitingOnTrx ) {
120 $waitingOnTrx = false;
121 } );
122 } else {
123 self::doUpdates();
124 }
125 }
126 }
127
128 public static function execute( array &$queue, $mode ) {
129 $updates = $queue; // snapshot of queue
130
131 // Keep doing rounds of updates until none get enqueued
132 while ( count( $updates ) ) {
133 $queue = []; // clear the queue
135 $dataUpdates = [];
137 $otherUpdates = [];
138 foreach ( $updates as $update ) {
139 if ( $update instanceof DataUpdate ) {
140 $dataUpdates[] = $update;
141 } else {
142 $otherUpdates[] = $update;
143 }
144 }
145
146 // Delegate DataUpdate execution to the DataUpdate class
147 DataUpdate::runUpdates( $dataUpdates, $mode );
148 // Execute the non-DataUpdate tasks
149 foreach ( $otherUpdates as $update ) {
150 try {
151 $update->doUpdate();
152 wfGetLBFactory()->commitMasterChanges( __METHOD__ );
153 } catch ( Exception $e ) {
154 // We don't want exceptions thrown during deferred updates to
155 // be reported to the user since the output is already sent
156 if ( !$e instanceof ErrorPageError ) {
157 MWExceptionHandler::logException( $e );
158 }
159 // Make sure incomplete transactions are not committed and end any
160 // open atomic sections so that other DB updates have a chance to run
161 wfGetLBFactory()->rollbackMasterChanges( __METHOD__ );
162 }
163 }
164
165 $updates = $queue; // new snapshot of queue (check for new entries)
166 }
167 }
168
173 public static function clearPendingUpdates() {
174 self::$preSendUpdates = [];
175 self::$postSendUpdates = [];
176 }
177}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
wfGetLB( $wiki=false)
Get a load balancer object.
wfGetLBFactory()
Get the load balancer factory object.
global $wgCommandLineMode
Definition Setup.php:513
Abstract base class for update jobs that do something with some secondary data extracted from article...
static runUpdates(array $updates, $mode='run')
Convenience method, calls doUpdate() on every DataUpdate in the array.
Class for managing the deferred updates.
static doUpdates( $mode='run', $type=self::ALL)
Do any deferred updates and clear the list.
static push(array &$queue, DeferrableUpdate $update)
static clearPendingUpdates()
Clear all pending updates without performing them.
static addUpdate(DeferrableUpdate $update, $type=self::POSTSEND)
Add an update to the deferred list.
static DeferrableUpdate[] $preSendUpdates
Updates to be deferred until before request end.
static addCallableUpdate( $callable, $type=self::POSTSEND)
Add a callable update.
static DeferrableUpdate[] $postSendUpdates
Updates to be deferred until after request end.
static execute(array &$queue, $mode)
An error page which can definitely be safely rendered using the OutputPage.
Deferrable Update for closure/callback.
when a variable name is used in a it is silently declared as a new local masking the global
Definition design.txt:95
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
the array() calling protocol came about after MediaWiki 1.4rc1.
do that in ParserLimitReportFormat instead use this to modify the parameters of the image and a DIV can begin in one section and end in another Make sure your code can handle that case gracefully See the EditSectionClearerLink extension for an example zero but section is usually empty its values are the globals values before the output is cached one of or reset my talk my contributions etc etc otherwise the built in rate limiting checks are if enabled allows for interception of redirect as a string mapping parameter names to values & $type
Definition hooks.txt:2413
returning false will NOT prevent logging $e
Definition hooks.txt:1940
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37
Interface that deferrable updates should implement.
Interface that deferrable updates can implement.
$batch execute()