MediaWiki  master
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, wfGetCaller() ), $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  $stats = \MediaWiki\MediaWikiServices::getInstance()->getStatsdDataFactory();
130  $method = RequestContext::getMain()->getRequest()->getMethod();
131 
132  $updates = $queue; // snapshot of queue
133  // Keep doing rounds of updates until none get enqueued
134  while ( count( $updates ) ) {
135  $queue = []; // clear the queue
137  $dataUpdates = [];
139  $otherUpdates = [];
140  foreach ( $updates as $update ) {
141  if ( $update instanceof DataUpdate ) {
142  $dataUpdates[] = $update;
143  } else {
144  $otherUpdates[] = $update;
145  }
146 
147  $name = $update instanceof DeferrableCallback
148  ? get_class( $update ) . '-' . $update->getOrigin()
149  : get_class( $update );
150  $stats->increment( 'deferred_updates.' . $method . '.' . $name );
151  }
152 
153  // Delegate DataUpdate execution to the DataUpdate class
154  try {
155  DataUpdate::runUpdates( $dataUpdates, $mode );
156  } catch ( Exception $e ) {
157  // Let the other updates occur if these had to rollback
159  }
160  // Execute the non-DataUpdate tasks
161  foreach ( $otherUpdates as $update ) {
162  try {
163  $update->doUpdate();
164  wfGetLBFactory()->commitMasterChanges( __METHOD__ );
165  } catch ( Exception $e ) {
166  // We don't want exceptions thrown during deferred updates to
167  // be reported to the user since the output is already sent
168  if ( !$e instanceof ErrorPageError ) {
170  }
171  // Make sure incomplete transactions are not committed and end any
172  // open atomic sections so that other DB updates have a chance to run
173  wfGetLBFactory()->rollbackMasterChanges( __METHOD__ );
174  }
175  }
176 
177  $updates = $queue; // new snapshot of queue (check for new entries)
178  }
179  }
180 
185  public static function clearPendingUpdates() {
186  self::$preSendUpdates = [];
187  self::$postSendUpdates = [];
188  }
189 }
static DeferrableUpdate[] $postSendUpdates
Updates to be deferred until after request end.
static doUpdates($mode= 'run', $type=self::ALL)
Do any deferred updates and clear the list.
static clearPendingUpdates()
Clear all pending updates without performing them.
the array() calling protocol came about after MediaWiki 1.4rc1.
static execute(array &$queue, $mode)
$batch execute()
Interface that deferrable updates should implement.
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException'returning false will NOT prevent logging $e
Definition: hooks.txt:1980
Interface that deferrable updates can implement.
when a variable name is used in a it is silently declared as a new local masking the global
Definition: design.txt:93
Deferrable Update for closure/callback.
static addCallableUpdate($callable, $type=self::POSTSEND)
Add a callable update.
static runUpdates(array $updates, $mode= 'run')
Convenience method, calls doUpdate() on every DataUpdate in the array.
Definition: DataUpdate.php:77
global $wgCommandLineMode
Definition: Setup.php:511
wfGetLB($wiki=false)
Get a load balancer object.
static getMain()
Static methods.
An error page which can definitely be safely rendered using the OutputPage.
Callback wrapper that has an originating method.
Class for managing the deferred updates.
static addUpdate(DeferrableUpdate $update, $type=self::POSTSEND)
Add an update to the deferred list.
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
Definition: distributors.txt:9
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:35
wfGetLBFactory()
Get the load balancer factory object.
Abstract base class for update jobs that do something with some secondary data extracted from article...
Definition: DataUpdate.php:32
static push(array &$queue, DeferrableUpdate $update)
static logException($e)
Log an exception to the exception log (if enabled).
static DeferrableUpdate[] $preSendUpdates
Updates to be deferred until before request end.
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:2376
wfGetCaller($level=2)
Get the name of the function which called this function wfGetCaller( 1 ) is the function with the wfG...
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:310