PHPSessionHandler: Implement SessionHandlerInterface
[mediawiki.git] / includes / jobqueue / JobQueueMemory.php
blob7dad748798531bb68016c5410b882dc10c6d674d
1 <?php
2 /**
3 * PHP memory-backed job queue code.
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.
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.
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
20 * @file
21 * @author Aaron Schulz
24 /**
25 * Class to handle job queues stored in PHP memory for testing
27 * JobQueueGroup does not remember every queue instance, so statically track it here
29 * @ingroup JobQueue
30 * @since 1.27
32 class JobQueueMemory extends JobQueue {
33 /** @var array[] */
34 protected static $data = array();
36 /**
37 * @see JobQueue::doBatchPush
39 * @param IJobSpecification[] $jobs
40 * @param int $flags
42 protected function doBatchPush( array $jobs, $flags ) {
43 $unclaimed =& $this->getQueueData( 'unclaimed', array() );
45 foreach ( $jobs as $job ) {
46 if ( $job->ignoreDuplicates() ) {
47 $sha1 = Wikimedia\base_convert(
48 sha1( serialize( $job->getDeduplicationInfo() ) ),
49 16, 36, 31
51 if ( !isset( $unclaimed[$sha1] ) ) {
52 $unclaimed[$sha1] = $job;
54 } else {
55 $unclaimed[] = $job;
60 /**
61 * @see JobQueue::supportedOrders
63 * @return string[]
65 protected function supportedOrders() {
66 return array( 'random', 'timestamp', 'fifo' );
69 /**
70 * @see JobQueue::optimalOrder
72 * @return string
74 protected function optimalOrder() {
75 return 'fifo';
78 /**
79 * @see JobQueue::doIsEmpty
81 * @return bool
83 protected function doIsEmpty() {
84 return ( $this->doGetSize() == 0 );
87 /**
88 * @see JobQueue::doGetSize
90 * @return int
92 protected function doGetSize() {
93 $unclaimed = $this->getQueueData( 'unclaimed' );
95 return $unclaimed ? count( $unclaimed ) : 0;
98 /**
99 * @see JobQueue::doGetAcquiredCount
101 * @return int
103 protected function doGetAcquiredCount() {
104 $claimed = $this->getQueueData( 'claimed' );
106 return $claimed ? count( $claimed ) : 0;
110 * @see JobQueue::doPop
112 * @return Job|bool
114 protected function doPop() {
115 if ( $this->doGetSize() == 0 ) {
116 return false;
119 $unclaimed =& $this->getQueueData( 'unclaimed' );
120 $claimed =& $this->getQueueData( 'claimed', array() );
122 if ( $this->order === 'random' ) {
123 $key = array_rand( $unclaimed );
124 } else {
125 reset( $unclaimed );
126 $key = key( $unclaimed );
129 $spec = $unclaimed[$key];
130 unset( $unclaimed[$key] );
131 $claimed[] = $spec;
133 $job = $this->jobFromSpecInternal( $spec );
135 end( $claimed );
136 $job->metadata['claimId'] = key( $claimed );
138 return $job;
142 * @see JobQueue::doAck
144 * @param Job $job
146 protected function doAck( Job $job ) {
147 if ( $this->getAcquiredCount() == 0 ) {
148 return;
151 $claimed =& $this->getQueueData( 'claimed' );
152 unset( $claimed[$job->metadata['claimId']] );
156 * @see JobQueue::doDelete
158 protected function doDelete() {
159 if ( isset( self::$data[$this->type][$this->wiki] ) ) {
160 unset( self::$data[$this->type][$this->wiki] );
161 if ( !self::$data[$this->type] ) {
162 unset( self::$data[$this->type] );
168 * @see JobQueue::getAllQueuedJobs
170 * @return Iterator of Job objects.
172 public function getAllQueuedJobs() {
173 $unclaimed = $this->getQueueData( 'unclaimed' );
174 if ( !$unclaimed ) {
175 return new ArrayIterator( array() );
178 $that = $this;
179 return new MappedIterator(
180 $unclaimed,
181 function ( $value ) use ( $that ) {
182 $that->jobFromSpecInternal( $value );
188 * @see JobQueue::getAllAcquiredJobs
190 * @return Iterator of Job objects.
192 public function getAllAcquiredJobs() {
193 $claimed = $this->getQueueData( 'claimed' );
194 if ( !$claimed ) {
195 return new ArrayIterator( array() );
198 $that = $this;
199 return new MappedIterator(
200 $claimed,
201 function ( $value ) use ( $that ) {
202 $that->jobFromSpecInternal( $value );
208 * @param IJobSpecification $spec
210 * @return Job
212 public function jobFromSpecInternal( IJobSpecification $spec ) {
213 return Job::factory( $spec->getType(), $spec->getTitle(), $spec->getParams() );
217 * @param string $field
218 * @param mixed $init
220 * @return mixed
222 private function &getQueueData( $field, $init = null ) {
223 if ( !isset( self::$data[$this->type][$this->wiki][$field] ) ) {
224 if ( $init !== null ) {
225 self::$data[$this->type][$this->wiki][$field] = $init;
226 } else {
227 return $init;
231 return self::$data[$this->type][$this->wiki][$field];