3 * Base code for update jobs that put some secondary data extracted
4 * from article content into the database.
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.
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.
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
25 * Abstract base class for update jobs that put some secondary data extracted
26 * from article content into the database.
28 * @note: subclasses should NOT start or commit transactions in their doUpdate() method,
29 * a transaction will automatically be wrapped around the update. Starting another
30 * one would break the outer transaction bracket. If need be, subclasses can override
31 * the beginTransaction() and commitTransaction() methods.
33 abstract class SqlDataUpdate
extends DataUpdate
{
35 protected $mDb; //!< Database connection reference
36 protected $mOptions; //!< SELECT options to be used (array)
38 private $mHasTransaction; //!< bool whether a transaction is open on this object (internal use only!)
43 public function __construct( ) {
44 global $wgAntiLockFlags;
46 parent
::__construct( );
48 if ( $wgAntiLockFlags & ALF_NO_LINK_LOCK
) {
49 $this->mOptions
= array();
51 $this->mOptions
= array( 'FOR UPDATE' );
54 // @todo: get connection only when it's needed? make sure that doesn't break anything, especially transactions!
55 $this->mDb
= wfGetDB( DB_MASTER
);
56 $this->mHasTransaction
= false;
60 * Begin a database transaction.
62 * Because nested transactions are not supportred by the Database class, this implementation
63 * checkes Database::trxLevel() and only opens a transaction if none is yet active.
65 public function beginTransaction() {
66 // NOTE: nested transactions are not supported, only start a transaction if none is open
67 if ( $this->mDb
->trxLevel() === 0 ) {
68 $this->mDb
->begin( get_class( $this ) . '::beginTransaction' );
69 $this->mHasTransaction
= true;
74 * Commit the database transaction started via beginTransaction (if any).
76 public function commitTransaction() {
77 if ( $this->mHasTransaction
) {
78 $this->mDb
->commit( get_class( $this ) . '::commitTransaction' );
83 * Abort the database transaction started via beginTransaction (if any).
85 public function abortTransaction() {
86 if ( $this->mHasTransaction
) {
87 $this->mDb
->rollback( get_class( $this ) . '::abortTransaction' );
92 * Invalidate the cache of a list of pages from a single namespace.
93 * This is intended for use by subclasses.
95 * @param $namespace Integer
96 * @param $dbkeys Array
98 protected function invalidatePages( $namespace, Array $dbkeys ) {
99 if ( !count( $dbkeys ) ) {
104 * Determine which pages need to be updated
105 * This is necessary to prevent the job queue from smashing the DB with
106 * large numbers of concurrent invalidations of the same page
108 $now = $this->mDb
->timestamp();
110 $res = $this->mDb
->select( 'page', array( 'page_id' ),
112 'page_namespace' => $namespace,
113 'page_title' => $dbkeys,
114 'page_touched < ' . $this->mDb
->addQuotes( $now )
117 foreach ( $res as $row ) {
118 $ids[] = $row->page_id
;
120 if ( !count( $ids ) ) {
126 * We still need the page_touched condition, in case the row has changed since
127 * the non-locking select above.
129 $this->mDb
->update( 'page', array( 'page_touched' => $now ),
132 'page_touched < ' . $this->mDb
->addQuotes( $now )