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