MediaWiki  REL1_24
DeferredUpdates.php
Go to the documentation of this file.
00001 <?php
00029 interface DeferrableUpdate {
00033     function doUpdate();
00034 }
00035 
00041 class DeferredUpdates {
00045     private static $updates = array();
00046 
00051     public static function addUpdate( DeferrableUpdate $update ) {
00052         array_push( self::$updates, $update );
00053     }
00054 
00062     public static function addHTMLCacheUpdate( $title, $table ) {
00063         self::addUpdate( new HTMLCacheUpdate( $title, $table ) );
00064     }
00065 
00072     public static function addCallableUpdate( $callable ) {
00073         self::addUpdate( new MWCallableUpdate( $callable ) );
00074     }
00075 
00082     public static function doUpdates( $commit = '' ) {
00083         global $wgDeferredUpdateList;
00084 
00085         wfProfileIn( __METHOD__ );
00086 
00087         $updates = array_merge( $wgDeferredUpdateList, self::$updates );
00088 
00089         // No need to get master connections in case of empty updates array
00090         if ( !count( $updates ) ) {
00091             wfProfileOut( __METHOD__ );
00092 
00093             return;
00094         }
00095 
00096         $dbw = false;
00097         $doCommit = $commit == 'commit';
00098         if ( $doCommit ) {
00099             $dbw = wfGetDB( DB_MASTER );
00100         }
00101 
00102         while ( $updates ) {
00103             self::clearPendingUpdates();
00104 
00106             foreach ( $updates as $update ) {
00107                 try {
00108                     $update->doUpdate();
00109 
00110                     if ( $doCommit && $dbw->trxLevel() ) {
00111                         $dbw->commit( __METHOD__, 'flush' );
00112                     }
00113                 } catch ( MWException $e ) {
00114                     // We don't want exceptions thrown during deferred updates to
00115                     // be reported to the user since the output is already sent.
00116                     // Instead we just log them.
00117                     if ( !$e instanceof ErrorPageError ) {
00118                         MWExceptionHandler::logException( $e );
00119                     }
00120                 }
00121             }
00122             $updates = array_merge( $wgDeferredUpdateList, self::$updates );
00123         }
00124 
00125         wfProfileOut( __METHOD__ );
00126     }
00127 
00132     public static function clearPendingUpdates() {
00133         global $wgDeferredUpdateList;
00134         $wgDeferredUpdateList = self::$updates = array();
00135     }
00136 }