(bug 37183) Removed hard coded parentheses in SpecialListfiles.php
[lhc/web/wiklou.git] / includes / SqlDataUpdate.php
1 <?php
2 /**
3 * Base code for update jobs that put some secondary data extracted
4 * from article content into the database.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
20 *
21 * @file
22 */
23
24 /**
25 * Abstract base class for update jobs that put some secondary data extracted
26 * from article content into the database.
27 */
28 abstract class SqlDataUpdate extends DataUpdate {
29
30 protected $mDb; //!< Database connection reference
31 protected $mOptions; //!< SELECT options to be used (array)
32
33 private $mHasTransaction; //!< bool whether a transaction is open on this object (internal use only!)
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
49 // @todo: get connection only when it's needed? make sure that doesn't break anything, especially transactions!
50 $this->mDb = wfGetDB( DB_MASTER );
51 $this->mHasTransaction = false;
52 }
53
54 /**
55 * Begin a database transaction.
56 *
57 * Because nested transactions are not supportred by the Database class, this implementation
58 * checkes Database::trxLevel() and only opens a transaction if none is yet active.
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 public function commitTransaction() {
72 if ( $this->mHasTransaction ) {
73 $this->mDb->commit( get_class( $this ) . '::commitTransaction' );
74 }
75 }
76
77 /**
78 * Abort the database transaction started via beginTransaction (if any).
79 */
80 public function abortTransaction() {
81 if ( $this->mHasTransaction ) {
82 $this->mDb->rollback( get_class( $this ) . '::abortTransaction' );
83 }
84 }
85
86 /**
87 * Invalidate the cache of a list of pages from a single namespace.
88 * This is intended for use by subclasses.
89 *
90 * @param $namespace Integer
91 * @param $dbkeys Array
92 */
93 protected function invalidatePages( $namespace, Array $dbkeys ) {
94 if ( !count( $dbkeys ) ) {
95 return;
96 }
97
98 /**
99 * Determine which pages need to be updated
100 * This is necessary to prevent the job queue from smashing the DB with
101 * large numbers of concurrent invalidations of the same page
102 */
103 $now = $this->mDb->timestamp();
104 $ids = array();
105 $res = $this->mDb->select( 'page', array( 'page_id' ),
106 array(
107 'page_namespace' => $namespace,
108 'page_title' => $dbkeys,
109 'page_touched < ' . $this->mDb->addQuotes( $now )
110 ), __METHOD__
111 );
112 foreach ( $res as $row ) {
113 $ids[] = $row->page_id;
114 }
115 if ( !count( $ids ) ) {
116 return;
117 }
118
119 /**
120 * Do the update
121 * We still need the page_touched condition, in case the row has changed since
122 * the non-locking select above.
123 */
124 $this->mDb->update( 'page', array( 'page_touched' => $now ),
125 array(
126 'page_id' => $ids,
127 'page_touched < ' . $this->mDb->addQuotes( $now )
128 ), __METHOD__
129 );
130 }
131
132 }