3 * Interface and manager for deferred updates.
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.
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.
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
24 * Interface that deferrable updates should implement. Basically required so we
25 * can validate input on DeferredUpdates::addUpdate()
29 interface DeferrableUpdate
{
31 * Perform the actual work
37 * Class for mananging the deferred updates.
41 class DeferredUpdates
{
43 * Store of updates to be deferred until the end of the request.
45 private static $updates = array();
48 * Add an update to the deferred list
49 * @param $update DeferrableUpdate Some object that implements doUpdate()
51 public static function addUpdate( DeferrableUpdate
$update ) {
52 array_push( self
::$updates, $update );
56 * HTMLCacheUpdates are the most common deferred update people use. This
57 * is a shortcut method for that.
58 * @see HTMLCacheUpdate::__construct()
62 public static function addHTMLCacheUpdate( $title, $table ) {
63 self
::addUpdate( new HTMLCacheUpdate( $title, $table ) );
67 * Do any deferred updates and clear the list
69 * @param $commit String: set to 'commit' to commit after every update to
70 * prevent lock contention
72 public static function doUpdates( $commit = '' ) {
73 global $wgDeferredUpdateList;
75 wfProfileIn( __METHOD__
);
77 $updates = array_merge( $wgDeferredUpdateList, self
::$updates );
79 // No need to get master connections in case of empty updates array
80 if ( !count( $updates ) ) {
81 wfProfileOut( __METHOD__
);
85 $doCommit = $commit == 'commit';
87 $dbw = wfGetDB( DB_MASTER
);
90 foreach ( $updates as $update ) {
93 if ( $doCommit && $dbw->trxLevel() ) {
94 $dbw->commit( __METHOD__
);
98 self
::clearPendingUpdates();
99 wfProfileOut( __METHOD__
);
103 * Clear all pending updates without performing them. Generally, you don't
104 * want or need to call this. Unit tests need it though.
106 public static function clearPendingUpdates() {
107 global $wgDeferredUpdateList;
108 $wgDeferredUpdateList = self
::$updates = array();