Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / poolcounter / PoolCounterWorkViaCallback.php
blobb2d640d164367414aa945b41d407dab1b07b3c43
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
18 * @file
21 namespace MediaWiki\PoolCounter;
23 use InvalidArgumentException;
25 /**
26 * Convenience class for dealing with PoolCounter using callbacks
27 * @since 1.22
28 * @newable
29 * @note marked as newable in 1.35 for lack of a better alternative,
30 * but should use a factory in the future.
32 class PoolCounterWorkViaCallback extends PoolCounterWork {
33 /** @var callable|null */
34 protected $doWork;
35 /** @var callable|null */
36 protected $doCachedWork;
37 /** @var callable|null */
38 protected $fallback;
39 /** @var callable|null */
40 protected $error;
42 /**
43 * Build a PoolCounterWork class from a type, key, and callback map.
45 * The callback map must at least have a callback for the 'doWork' method.
46 * Additionally, callbacks can be provided for the 'doCachedWork', 'fallback',
47 * and 'error' methods. Methods without callbacks will be no-ops that return false.
48 * If a 'doCachedWork' callback is provided, then execute() may wait for any prior
49 * process in the pool to finish and reuse its cached result.
51 * @stable to call
52 * @param string $type The class of actions to limit concurrency for
53 * @param string $key
54 * @param array $callbacks Map of callbacks
56 public function __construct( $type, $key, array $callbacks ) {
57 parent::__construct( $type, $key );
58 foreach ( [ 'doWork', 'doCachedWork', 'fallback', 'error' ] as $name ) {
59 if ( isset( $callbacks[$name] ) ) {
60 if ( !is_callable( $callbacks[$name] ) ) {
61 throw new InvalidArgumentException( "Invalid callback provided for '$name' function." );
63 $this->$name = $callbacks[$name];
66 if ( !$this->doWork ) {
67 throw new InvalidArgumentException( "No callback provided for 'doWork' function." );
69 $this->cacheable = (bool)$this->doCachedWork;
72 public function doWork() {
73 return ( $this->doWork )();
76 public function getCachedWork() {
77 if ( $this->doCachedWork ) {
78 return ( $this->doCachedWork )();
80 return false;
83 public function fallback( $fast ) {
84 if ( $this->fallback ) {
85 return ( $this->fallback )( $fast );
87 return false;
90 public function error( $status ) {
91 if ( $this->error ) {
92 return ( $this->error )( $status );
94 return false;
98 /** @deprecated class alias since 1.42 */
99 class_alias( PoolCounterWorkViaCallback::class, 'PoolCounterWorkViaCallback' );