Merge "Use local context to get messages"
[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 * @param $title
39 * @param $table
40 */
41 public static function addHTMLCacheUpdate( $title, $table ) {
42 self::addUpdate( new HTMLCacheUpdate( $title, $table ) );
43 }
44
45 /**
46 * Do any deferred updates and clear the list
47 *
48 * @param $commit String: set to 'commit' to commit after every update to
49 * prevent lock contention
50 */
51 public static function doUpdates( $commit = '' ) {
52 global $wgDeferredUpdateList;
53
54 wfProfileIn( __METHOD__ );
55
56 $updates = array_merge( $wgDeferredUpdateList, self::$updates );
57
58 // No need to get master connections in case of empty updates array
59 if ( !count( $updates ) ) {
60 wfProfileOut( __METHOD__ );
61 return;
62 }
63
64 $doCommit = $commit == 'commit';
65 if ( $doCommit ) {
66 $dbw = wfGetDB( DB_MASTER );
67 }
68
69 foreach ( $updates as $update ) {
70 $update->doUpdate();
71
72 if ( $doCommit && $dbw->trxLevel() ) {
73 $dbw->commit( __METHOD__ );
74 }
75 }
76
77 self::clearPendingUpdates();
78 wfProfileOut( __METHOD__ );
79 }
80
81 /**
82 * Clear all pending updates without performing them. Generally, you don't
83 * want or need to call this. Unit tests need it though.
84 */
85 public static function clearPendingUpdates() {
86 global $wgDeferredUpdateList;
87 $wgDeferredUpdateList = self::$updates = array();
88 }
89 }