add since tags
[lhc/web/wiklou.git] / includes / SecondaryDBDataUpdate.php
1 <?php
2 /**
3 * See docs/deferred.txt
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 * Abstract base class for update jobs that put some secondary data extracted
21 * from article content into the database.
22 *
23 * @since WD.1
24 */
25 abstract class SecondaryDBDataUpdate extends SecondaryDataUpdate {
26
27 /**@{{
28 * @private
29 */
30 var $mDb, //!< Database connection reference
31 $mOptions, //!< SELECT options to be used (array)
32 $mHasTransaction;//!< bool whether a transaction is open on this object (internal use only!)
33 /**@}}*/
34
35 /**
36 * Constructor
37 **/
38 public function __construct( ) {
39 global $wgAntiLockFlags;
40
41 parent::__construct( );
42
43 if ( $wgAntiLockFlags & ALF_NO_LINK_LOCK ) {
44 $this->mOptions = array();
45 } else {
46 $this->mOptions = array( 'FOR UPDATE' );
47 }
48 $this->mDb = wfGetDB( DB_MASTER );
49 $this->mHasTransaction = false;
50 }
51
52 /**
53 * Begin a database transaction.
54 *
55 * Because nested transactions are not supportred by the Database class, this implementation
56 * checked Database::trxLevel() and only opens a transaction if none is yet active.
57 *
58 * @since WD.1
59 */
60 public function beginTransaction() {
61 // NOTE: nested transactions are not supported, only start a transaction if none is open
62 if ( $this->mDb->trxLevel() === 0 ) {
63 $this->mDb->begin( get_class( $this ) . '::beginTransaction' );
64 $this->mHasTransaction = true;
65 }
66 }
67
68 /**
69 * Commit the database transaction started via beginTransaction (if any).
70 *
71 * @since WD.1
72 */
73 public function commitTransaction() {
74 if ( $this->mHasTransaction ) {
75 $this->mDb->commit( get_class( $this ) . '::commitTransaction' );
76 }
77 }
78
79 /**
80 * Abort the database transaction started via beginTransaction (if any).
81 *
82 * @since WD.1
83 */
84 public function abortTransaction() {
85 if ( $this->mHasTransaction ) {
86 $this->mDb->rollback( get_class( $this ) . '::abortTransaction' );
87 }
88 }
89
90 /**
91 * Invalidate the cache of a list of pages from a single namespace.
92 * This is intended for use by subclasses.
93 *
94 * @since WD.1
95 *
96 * @param $namespace Integer
97 * @param $dbkeys Array
98 */
99 protected function invalidatePages( $namespace, $dbkeys ) {
100 if ( !count( $dbkeys ) ) {
101 return;
102 }
103
104 /**
105 * Determine which pages need to be updated
106 * This is necessary to prevent the job queue from smashing the DB with
107 * large numbers of concurrent invalidations of the same page
108 */
109 $now = $this->mDb->timestamp();
110 $ids = array();
111 $res = $this->mDb->select( 'page', array( 'page_id' ),
112 array(
113 'page_namespace' => $namespace,
114 'page_title IN (' . $this->mDb->makeList( $dbkeys ) . ')',
115 'page_touched < ' . $this->mDb->addQuotes( $now )
116 ), __METHOD__
117 );
118 foreach ( $res as $row ) {
119 $ids[] = $row->page_id;
120 }
121 if ( !count( $ids ) ) {
122 return;
123 }
124
125 /**
126 * Do the update
127 * We still need the page_touched condition, in case the row has changed since
128 * the non-locking select above.
129 */
130 $this->mDb->update( 'page', array( 'page_touched' => $now ),
131 array(
132 'page_id IN (' . $this->mDb->makeList( $ids ) . ')',
133 'page_touched < ' . $this->mDb->addQuotes( $now )
134 ), __METHOD__
135 );
136 }
137
138 }