User: Avoid deprecated Linker::link()
[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 = [];
35
36 /**
37 * @see JobQueue::doBatchPush
38 *
39 * @param IJobSpecification[] $jobs
40 * @param int $flags
41 */
42 protected function doBatchPush( array $jobs, $flags ) {
43 $unclaimed =& $this->getQueueData( 'unclaimed', [] );
44
45 foreach ( $jobs as $job ) {
46 if ( $job->ignoreDuplicates() ) {
47 $sha1 = Wikimedia\base_convert(
48 sha1( serialize( $job->getDeduplicationInfo() ) ),
49 16, 36, 31
50 );
51 if ( !isset( $unclaimed[$sha1] ) ) {
52 $unclaimed[$sha1] = $job;
53 }
54 } else {
55 $unclaimed[] = $job;
56 }
57 }
58 }
59
60 /**
61 * @see JobQueue::supportedOrders
62 *
63 * @return string[]
64 */
65 protected function supportedOrders() {
66 return [ 'random', 'timestamp', 'fifo' ];
67 }
68
69 /**
70 * @see JobQueue::optimalOrder
71 *
72 * @return string
73 */
74 protected function optimalOrder() {
75 return 'fifo';
76 }
77
78 /**
79 * @see JobQueue::doIsEmpty
80 *
81 * @return bool
82 */
83 protected function doIsEmpty() {
84 return ( $this->doGetSize() == 0 );
85 }
86
87 /**
88 * @see JobQueue::doGetSize
89 *
90 * @return int
91 */
92 protected function doGetSize() {
93 $unclaimed = $this->getQueueData( 'unclaimed' );
94
95 return $unclaimed ? count( $unclaimed ) : 0;
96 }
97
98 /**
99 * @see JobQueue::doGetAcquiredCount
100 *
101 * @return int
102 */
103 protected function doGetAcquiredCount() {
104 $claimed = $this->getQueueData( 'claimed' );
105
106 return $claimed ? count( $claimed ) : 0;
107 }
108
109 /**
110 * @see JobQueue::doPop
111 *
112 * @return Job|bool
113 */
114 protected function doPop() {
115 if ( $this->doGetSize() == 0 ) {
116 return false;
117 }
118
119 $unclaimed =& $this->getQueueData( 'unclaimed' );
120 $claimed =& $this->getQueueData( 'claimed', [] );
121
122 if ( $this->order === 'random' ) {
123 $key = array_rand( $unclaimed );
124 } else {
125 reset( $unclaimed );
126 $key = key( $unclaimed );
127 }
128
129 $spec = $unclaimed[$key];
130 unset( $unclaimed[$key] );
131 $claimed[] = $spec;
132
133 $job = $this->jobFromSpecInternal( $spec );
134
135 end( $claimed );
136 $job->metadata['claimId'] = key( $claimed );
137
138 return $job;
139 }
140
141 /**
142 * @see JobQueue::doAck
143 *
144 * @param Job $job
145 */
146 protected function doAck( Job $job ) {
147 if ( $this->getAcquiredCount() == 0 ) {
148 return;
149 }
150
151 $claimed =& $this->getQueueData( 'claimed' );
152 unset( $claimed[$job->metadata['claimId']] );
153 }
154
155 /**
156 * @see JobQueue::doDelete
157 */
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] );
163 }
164 }
165 }
166
167 /**
168 * @see JobQueue::getAllQueuedJobs
169 *
170 * @return Iterator of Job objects.
171 */
172 public function getAllQueuedJobs() {
173 $unclaimed = $this->getQueueData( 'unclaimed' );
174 if ( !$unclaimed ) {
175 return new ArrayIterator( [] );
176 }
177
178 return new MappedIterator(
179 $unclaimed,
180 function ( $value ) {
181 $this->jobFromSpecInternal( $value );
182 }
183 );
184 }
185
186 /**
187 * @see JobQueue::getAllAcquiredJobs
188 *
189 * @return Iterator of Job objects.
190 */
191 public function getAllAcquiredJobs() {
192 $claimed = $this->getQueueData( 'claimed' );
193 if ( !$claimed ) {
194 return new ArrayIterator( [] );
195 }
196
197 return new MappedIterator(
198 $claimed,
199 function ( $value ) {
200 $this->jobFromSpecInternal( $value );
201 }
202 );
203 }
204
205 /**
206 * @param IJobSpecification $spec
207 *
208 * @return Job
209 */
210 public function jobFromSpecInternal( IJobSpecification $spec ) {
211 return Job::factory( $spec->getType(), $spec->getTitle(), $spec->getParams() );
212 }
213
214 /**
215 * @param string $field
216 * @param mixed $init
217 *
218 * @return mixed
219 */
220 private function &getQueueData( $field, $init = null ) {
221 if ( !isset( self::$data[$this->type][$this->wiki][$field] ) ) {
222 if ( $init !== null ) {
223 self::$data[$this->type][$this->wiki][$field] = $init;
224 } else {
225 return $init;
226 }
227 }
228
229 return self::$data[$this->type][$this->wiki][$field];
230 }
231 }