revert r113117 per CR
[mediawiki.git] / includes / DeferredUpdates.php
blob262994e380c23303ac593409ba76386d037869e4
1 <?php
2 /**
3 * Interface that deferrable updates should implement. Basically required so we
4 * can validate input on DeferredUpdates::addUpdate()
6 * @since 1.19
7 */
8 interface DeferrableUpdate {
9 /**
10 * Perform the actual work
12 function doUpdate();
15 /**
16 * Class for mananging the deferred updates.
18 * @since 1.19
20 class DeferredUpdates {
21 /**
22 * Store of updates to be deferred until the end of the request.
24 private static $updates = array();
26 /**
27 * Add an update to the deferred list
28 * @param $update DeferrableUpdate Some object that implements doUpdate()
30 public static function addUpdate( DeferrableUpdate $update ) {
31 array_push( self::$updates, $update );
34 /**
35 * HTMLCacheUpdates are the most common deferred update people use. This
36 * is a shortcut method for that.
37 * @see HTMLCacheUpdate::__construct()
38 * @param $title
39 * @param $table
41 public static function addHTMLCacheUpdate( $title, $table ) {
42 self::addUpdate( new HTMLCacheUpdate( $title, $table ) );
45 /**
46 * Do any deferred updates and clear the list
48 * @param $commit String: set to 'commit' to commit after every update to
49 * prevent lock contention
51 public static function doUpdates( $commit = '' ) {
52 global $wgDeferredUpdateList;
54 wfProfileIn( __METHOD__ );
56 $updates = array_merge( $wgDeferredUpdateList, self::$updates );
58 // No need to get master connections in case of empty updates array
59 if ( !count( $updates ) ) {
60 wfProfileOut( __METHOD__ );
61 return;
64 $doCommit = $commit == 'commit';
65 if ( $doCommit ) {
66 $dbw = wfGetDB( DB_MASTER );
69 foreach ( $updates as $update ) {
70 $update->doUpdate();
72 if ( $doCommit && $dbw->trxLevel() ) {
73 $dbw->commit( __METHOD__ );
77 self::clearPendingUpdates();
78 wfProfileOut( __METHOD__ );
81 /**
82 * Clear all pending updates without performing them. Generally, you don't
83 * want or need to call this. Unit tests need it though.
85 public static function clearPendingUpdates() {
86 global $wgDeferredUpdateList;
87 $wgDeferredUpdateList = self::$updates = array();