MediaWiki  REL1_23
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 
00103         foreach ( $updates as $update ) {
00104             try {
00105                 $update->doUpdate();
00106 
00107                 if ( $doCommit && $dbw->trxLevel() ) {
00108                     $dbw->commit( __METHOD__, 'flush' );
00109                 }
00110             } catch ( MWException $e ) {
00111                 // We don't want exceptions thrown during deferred updates to
00112                 // be reported to the user since the output is already sent.
00113                 // Instead we just log them.
00114                 if ( !$e instanceof ErrorPageError ) {
00115                     MWExceptionHandler::logException( $e );
00116                 }
00117             }
00118         }
00119 
00120         self::clearPendingUpdates();
00121         wfProfileOut( __METHOD__ );
00122     }
00123 
00128     public static function clearPendingUpdates() {
00129         global $wgDeferredUpdateList;
00130         $wgDeferredUpdateList = self::$updates = array();
00131     }
00132 }