[ Index ]

PHP Cross Reference of MediaWiki-1.24.0

title

Body

[close]

/includes/deferred/ -> DeferredUpdates.php (source)

   1  <?php
   2  /**
   3   * Interface and manager for deferred updates.
   4   *
   5   * This program is free software; you can redistribute it and/or modify
   6   * it under the terms of the GNU General Public License as published by
   7   * the Free Software Foundation; either version 2 of the License, or
   8   * (at your option) any later version.
   9   *
  10   * This program is distributed in the hope that it will be useful,
  11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13   * GNU General Public License for more details.
  14   *
  15   * You should have received a copy of the GNU General Public License along
  16   * with this program; if not, write to the Free Software Foundation, Inc.,
  17   * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18   * http://www.gnu.org/copyleft/gpl.html
  19   *
  20   * @file
  21   */
  22  
  23  /**
  24   * Interface that deferrable updates should implement. Basically required so we
  25   * can validate input on DeferredUpdates::addUpdate()
  26   *
  27   * @since 1.19
  28   */
  29  interface DeferrableUpdate {
  30      /**
  31       * Perform the actual work
  32       */
  33  	function doUpdate();
  34  }
  35  
  36  /**
  37   * Class for managing the deferred updates.
  38   *
  39   * @since 1.19
  40   */
  41  class DeferredUpdates {
  42      /**
  43       * Store of updates to be deferred until the end of the request.
  44       */
  45      private static $updates = array();
  46  
  47      /**
  48       * Add an update to the deferred list
  49       * @param DeferrableUpdate $update Some object that implements doUpdate()
  50       */
  51  	public static function addUpdate( DeferrableUpdate $update ) {
  52          array_push( self::$updates, $update );
  53      }
  54  
  55      /**
  56       * HTMLCacheUpdates are the most common deferred update people use. This
  57       * is a shortcut method for that.
  58       * @see HTMLCacheUpdate::__construct()
  59       * @param Title $title
  60       * @param string $table
  61       */
  62  	public static function addHTMLCacheUpdate( $title, $table ) {
  63          self::addUpdate( new HTMLCacheUpdate( $title, $table ) );
  64      }
  65  
  66      /**
  67       * Add a callable update.  In a lot of cases, we just need a callback/closure,
  68       * defining a new DeferrableUpdate object is not necessary
  69       * @see MWCallableUpdate::__construct()
  70       * @param callable $callable
  71       */
  72  	public static function addCallableUpdate( $callable ) {
  73          self::addUpdate( new MWCallableUpdate( $callable ) );
  74      }
  75  
  76      /**
  77       * Do any deferred updates and clear the list
  78       *
  79       * @param string $commit Set to 'commit' to commit after every update to
  80       *   prevent lock contention
  81       */
  82  	public static function doUpdates( $commit = '' ) {
  83          global $wgDeferredUpdateList;
  84  
  85          wfProfileIn( __METHOD__ );
  86  
  87          $updates = array_merge( $wgDeferredUpdateList, self::$updates );
  88  
  89          // No need to get master connections in case of empty updates array
  90          if ( !count( $updates ) ) {
  91              wfProfileOut( __METHOD__ );
  92  
  93              return;
  94          }
  95  
  96          $dbw = false;
  97          $doCommit = $commit == 'commit';
  98          if ( $doCommit ) {
  99              $dbw = wfGetDB( DB_MASTER );
 100          }
 101  
 102          while ( $updates ) {
 103              self::clearPendingUpdates();
 104  
 105              /** @var DeferrableUpdate $update */
 106              foreach ( $updates as $update ) {
 107                  try {
 108                      $update->doUpdate();
 109  
 110                      if ( $doCommit && $dbw->trxLevel() ) {
 111                          $dbw->commit( __METHOD__, 'flush' );
 112                      }
 113                  } catch ( MWException $e ) {
 114                      // We don't want exceptions thrown during deferred updates to
 115                      // be reported to the user since the output is already sent.
 116                      // Instead we just log them.
 117                      if ( !$e instanceof ErrorPageError ) {
 118                          MWExceptionHandler::logException( $e );
 119                      }
 120                  }
 121              }
 122              $updates = array_merge( $wgDeferredUpdateList, self::$updates );
 123          }
 124  
 125          wfProfileOut( __METHOD__ );
 126      }
 127  
 128      /**
 129       * Clear all pending updates without performing them. Generally, you don't
 130       * want or need to call this. Unit tests need it though.
 131       */
 132  	public static function clearPendingUpdates() {
 133          global $wgDeferredUpdateList;
 134          $wgDeferredUpdateList = self::$updates = array();
 135      }
 136  }


Generated: Fri Nov 28 14:03:12 2014 Cross-referenced by PHPXref 0.7.1