2012-05-08 15:09:30 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
2012-05-20 15:56:43 +00:00
|
|
|
* Base code for update jobs that do something with some secondary
|
|
|
|
|
* data extracted from article.
|
2012-05-08 15:09:30 +00:00
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
2012-05-20 15:56:43 +00:00
|
|
|
* @file
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
2012-05-08 15:09:30 +00:00
|
|
|
* Abstract base class for update jobs that do something with some secondary
|
|
|
|
|
* data extracted from article.
|
2012-07-12 20:50:48 +00:00
|
|
|
*
|
2014-07-24 09:30:25 +00:00
|
|
|
* @note subclasses should NOT start or commit transactions in their doUpdate() method,
|
|
|
|
|
* a transaction will automatically be wrapped around the update. If need be,
|
|
|
|
|
* subclasses can override the beginTransaction() and commitTransaction() methods.
|
2012-05-08 15:09:30 +00:00
|
|
|
*/
|
|
|
|
|
abstract class DataUpdate implements DeferrableUpdate {
|
2013-03-17 15:13:22 +00:00
|
|
|
public function __construct() {
|
2015-09-26 23:42:27 +00:00
|
|
|
// noop
|
2012-05-08 15:09:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Begin an appropriate transaction, if any.
|
|
|
|
|
* This default implementation does nothing.
|
|
|
|
|
*/
|
|
|
|
|
public function beginTransaction() {
|
2015-09-11 13:44:59 +00:00
|
|
|
// noop
|
2012-05-08 15:09:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Commit the transaction started via beginTransaction, if any.
|
|
|
|
|
* This default implementation does nothing.
|
|
|
|
|
*/
|
|
|
|
|
public function commitTransaction() {
|
2015-09-11 13:44:59 +00:00
|
|
|
// noop
|
2012-05-08 15:09:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Abort / roll back the transaction started via beginTransaction, if any.
|
|
|
|
|
* This default implementation does nothing.
|
|
|
|
|
*/
|
|
|
|
|
public function rollbackTransaction() {
|
2015-09-11 13:44:59 +00:00
|
|
|
// noop
|
2012-05-08 15:09:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Convenience method, calls doUpdate() on every DataUpdate in the array.
|
|
|
|
|
*
|
|
|
|
|
* This methods supports transactions logic by first calling beginTransaction()
|
|
|
|
|
* on all updates in the array, then calling doUpdate() on each, and, if all goes well,
|
2013-03-13 07:42:41 +00:00
|
|
|
* then calling commitTransaction() on each update. If an error occurs,
|
|
|
|
|
* rollbackTransaction() will be called on any update object that had beginTransaction()
|
2012-05-08 15:09:30 +00:00
|
|
|
* called but not yet commitTransaction().
|
|
|
|
|
*
|
|
|
|
|
* This allows for limited transactional logic across multiple backends for storing
|
|
|
|
|
* secondary data.
|
|
|
|
|
*
|
2015-09-22 18:43:33 +00:00
|
|
|
* @param DataUpdate[] $updates A list of DataUpdate instances
|
|
|
|
|
* @param string $mode Use "enqueue" to use the job queue when possible [Default: run]
|
2012-10-07 23:35:26 +00:00
|
|
|
* @throws Exception|null
|
2012-05-08 15:09:30 +00:00
|
|
|
*/
|
2015-09-22 18:43:33 +00:00
|
|
|
public static function runUpdates( array $updates, $mode = 'run' ) {
|
|
|
|
|
if ( $mode === 'enqueue' ) {
|
|
|
|
|
// When possible, push updates as jobs instead of calling doUpdate()
|
|
|
|
|
$updates = self::enqueueUpdates( $updates );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( !count( $updates ) ) {
|
|
|
|
|
return; // nothing to do
|
2013-04-20 22:49:30 +00:00
|
|
|
}
|
2012-05-08 15:09:30 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$open_transactions = [];
|
2012-05-08 15:09:30 +00:00
|
|
|
$exception = null;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// begin transactions
|
|
|
|
|
foreach ( $updates as $update ) {
|
|
|
|
|
$update->beginTransaction();
|
|
|
|
|
$open_transactions[] = $update;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// do work
|
|
|
|
|
foreach ( $updates as $update ) {
|
|
|
|
|
$update->doUpdate();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// commit transactions
|
|
|
|
|
while ( count( $open_transactions ) > 0 ) {
|
|
|
|
|
$trans = array_pop( $open_transactions );
|
|
|
|
|
$trans->commitTransaction();
|
|
|
|
|
}
|
|
|
|
|
} catch ( Exception $ex ) {
|
|
|
|
|
$exception = $ex;
|
2014-03-29 10:52:07 +00:00
|
|
|
wfDebug( "Caught exception, will rethrow after rollback: " .
|
|
|
|
|
$ex->getMessage() . "\n" );
|
2012-05-08 15:09:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// rollback remaining transactions
|
|
|
|
|
while ( count( $open_transactions ) > 0 ) {
|
|
|
|
|
$trans = array_pop( $open_transactions );
|
|
|
|
|
$trans->rollbackTransaction();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $exception ) {
|
|
|
|
|
throw $exception; // rethrow after cleanup
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-09-22 18:43:33 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Enqueue jobs for every DataUpdate that support enqueueUpdate()
|
|
|
|
|
* and return the remaining DataUpdate objects (those that do not)
|
|
|
|
|
*
|
|
|
|
|
* @param DataUpdate[] $updates A list of DataUpdate instances
|
|
|
|
|
* @return DataUpdate[]
|
2015-10-07 04:55:42 +00:00
|
|
|
* @since 1.27
|
2015-09-22 18:43:33 +00:00
|
|
|
*/
|
|
|
|
|
protected static function enqueueUpdates( array $updates ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$remaining = [];
|
2015-09-22 18:43:33 +00:00
|
|
|
|
|
|
|
|
foreach ( $updates as $update ) {
|
|
|
|
|
if ( $update instanceof EnqueueableDataUpdate ) {
|
2015-09-24 22:45:09 +00:00
|
|
|
$spec = $update->getAsJobSpecification();
|
|
|
|
|
JobQueueGroup::singleton( $spec['wiki'] )->push( $spec['job'] );
|
2015-09-22 18:43:33 +00:00
|
|
|
} else {
|
|
|
|
|
$remaining[] = $update;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $remaining;
|
|
|
|
|
}
|
2012-05-08 15:09:30 +00:00
|
|
|
}
|
2015-09-22 18:43:33 +00:00
|
|
|
|
|
|
|
|
/**
|
2015-09-24 22:45:09 +00:00
|
|
|
* Interface that marks a DataUpdate as enqueuable via the JobQueue
|
|
|
|
|
*
|
|
|
|
|
* Such updates must be representable using IJobSpecification, so that
|
|
|
|
|
* they can be serialized into jobs and enqueued for later execution
|
|
|
|
|
*
|
2015-10-07 04:55:42 +00:00
|
|
|
* @since 1.27
|
2015-09-22 18:43:33 +00:00
|
|
|
*/
|
|
|
|
|
interface EnqueueableDataUpdate {
|
|
|
|
|
/**
|
2015-09-24 22:45:09 +00:00
|
|
|
* @return array (wiki => wiki ID, job => IJobSpecification)
|
2015-09-22 18:43:33 +00:00
|
|
|
*/
|
2015-09-24 22:45:09 +00:00
|
|
|
public function getAsJobSpecification();
|
2015-09-26 14:58:53 +00:00
|
|
|
}
|