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