Merge "Special:Block: Disallow to add an expiry time in the past"
[lhc/web/wiklou.git] / includes / jobqueue / JobQueueMemory.php
1 <?php
2 /**
3 * PHP memory-backed job queue code.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @author Aaron Schulz
22 */
23
24 /**
25 * Class to handle job queues stored in PHP memory for testing
26 *
27 * JobQueueGroup does not remember every queue instance, so statically track it here
28 *
29 * @ingroup JobQueue
30 * @since 1.27
31 */
32 class JobQueueMemory extends JobQueue {
33 /** @var array[] */
34 protected static $data = array();
35
36 protected function doBatchPush( array $jobs, $flags ) {
37 $unclaimed =& $this->getQueueData( 'unclaimed', array() );
38
39 /** @var IJobSpecification[] $jobs */
40 foreach ( $jobs as $job ) {
41 if ( $job->ignoreDuplicates() ) {
42 $sha1 = Wikimedia\base_convert(
43 sha1( serialize( $job->getDeduplicationInfo() ) ),
44 16, 36, 31
45 );
46 if ( !isset( $unclaimed[$sha1] ) ) {
47 $unclaimed[$sha1] = $job;
48 }
49 } else {
50 $unclaimed[] = $job;
51 }
52 }
53 }
54
55 protected function supportedOrders() {
56 return array( 'random', 'timestamp', 'fifo' );
57 }
58
59 protected function optimalOrder() {
60 return 'fifo';
61 }
62
63 protected function doIsEmpty() {
64 return ( $this->doGetSize() == 0 );
65 }
66
67 protected function doGetSize() {
68 $unclaimed = $this->getQueueData( 'unclaimed' );
69
70 return $unclaimed ? count( $unclaimed ) : 0;
71 }
72
73 protected function doGetAcquiredCount() {
74 $claimed = $this->getQueueData( 'claimed' );
75
76 return $claimed ? count( $claimed ) : 0;
77 }
78
79 protected function doPop() {
80 if ( $this->doGetSize() == 0 ) {
81 return false;
82 }
83
84 $unclaimed =& $this->getQueueData( 'unclaimed' );
85 $claimed =& $this->getQueueData( 'claimed', array() );
86
87 if ( $this->order === 'random' ) {
88 $key = array_rand( $unclaimed );
89 } else {
90 reset( $unclaimed );
91 $key = key( $unclaimed );
92 }
93
94 $spec = $unclaimed[$key];
95 unset( $unclaimed[$key] );
96 $claimed[] = $spec;
97
98 $job = $this->jobFromSpecInternal( $spec );
99
100 end( $claimed );
101 $job->metadata['claimId'] = key( $claimed );
102
103 return $job;
104 }
105
106 protected function doAck( Job $job ) {
107 if ( $this->getAcquiredCount() == 0 ) {
108 return;
109 }
110
111 $claimed =& $this->getQueueData( 'claimed' );
112 unset( $claimed[$job->metadata['claimId']] );
113 }
114
115 protected function doDelete() {
116 if ( isset( self::$data[$this->type][$this->wiki] ) ) {
117 unset( self::$data[$this->type][$this->wiki] );
118 if ( !self::$data[$this->type] ) {
119 unset( self::$data[$this->type] );
120 }
121 }
122 }
123
124 public function getAllQueuedJobs() {
125 $unclaimed = $this->getQueueData( 'unclaimed' );
126 if ( !$unclaimed ) {
127 return new ArrayIterator( array() );
128 }
129
130 $that = $this;
131 return new MappedIterator(
132 $unclaimed,
133 function ( $value ) use ( $that ) {
134 $that->jobFromSpecInternal( $value );
135 }
136 );
137 }
138
139 public function getAllAcquiredJobs() {
140 $claimed = $this->getQueueData( 'claimed' );
141 if ( !$claimed ) {
142 return new ArrayIterator( array() );
143 }
144
145 $that = $this;
146 return new MappedIterator(
147 $claimed,
148 function ( $value ) use ( $that ) {
149 $that->jobFromSpecInternal( $value );
150 }
151 );
152 }
153
154 public function jobFromSpecInternal( IJobSpecification $spec ) {
155 return Job::factory( $spec->getType(), $spec->getTitle(), $spec->getParams() );
156 }
157
158 private function &getQueueData( $field, $init = null ) {
159 if ( !isset( self::$data[$this->type][$this->wiki][$field] ) ) {
160 if ( $init !== null ) {
161 self::$data[$this->type][$this->wiki][$field] = $init;
162 } else {
163 return $init;
164 }
165 }
166
167 return self::$data[$this->type][$this->wiki][$field];
168 }
169 }