MediaWiki  1.30.0
JobQueueMemory.php
Go to the documentation of this file.
1 <?php
31 class JobQueueMemory extends JobQueue {
33  protected static $data = [];
34 
41  protected function doBatchPush( array $jobs, $flags ) {
42  $unclaimed =& $this->getQueueData( 'unclaimed', [] );
43 
44  foreach ( $jobs as $job ) {
45  if ( $job->ignoreDuplicates() ) {
46  $sha1 = Wikimedia\base_convert(
47  sha1( serialize( $job->getDeduplicationInfo() ) ),
48  16, 36, 31
49  );
50  if ( !isset( $unclaimed[$sha1] ) ) {
51  $unclaimed[$sha1] = $job;
52  }
53  } else {
54  $unclaimed[] = $job;
55  }
56  }
57  }
58 
64  protected function supportedOrders() {
65  return [ 'random', 'timestamp', 'fifo' ];
66  }
67 
73  protected function optimalOrder() {
74  return 'fifo';
75  }
76 
82  protected function doIsEmpty() {
83  return ( $this->doGetSize() == 0 );
84  }
85 
91  protected function doGetSize() {
92  $unclaimed = $this->getQueueData( 'unclaimed' );
93 
94  return $unclaimed ? count( $unclaimed ) : 0;
95  }
96 
102  protected function doGetAcquiredCount() {
103  $claimed = $this->getQueueData( 'claimed' );
104 
105  return $claimed ? count( $claimed ) : 0;
106  }
107 
113  protected function doPop() {
114  if ( $this->doGetSize() == 0 ) {
115  return false;
116  }
117 
118  $unclaimed =& $this->getQueueData( 'unclaimed' );
119  $claimed =& $this->getQueueData( 'claimed', [] );
120 
121  if ( $this->order === 'random' ) {
122  $key = array_rand( $unclaimed );
123  } else {
124  reset( $unclaimed );
125  $key = key( $unclaimed );
126  }
127 
128  $spec = $unclaimed[$key];
129  unset( $unclaimed[$key] );
130  $claimed[] = $spec;
131 
132  $job = $this->jobFromSpecInternal( $spec );
133 
134  end( $claimed );
135  $job->metadata['claimId'] = key( $claimed );
136 
137  return $job;
138  }
139 
145  protected function doAck( Job $job ) {
146  if ( $this->getAcquiredCount() == 0 ) {
147  return;
148  }
149 
150  $claimed =& $this->getQueueData( 'claimed' );
151  unset( $claimed[$job->metadata['claimId']] );
152  }
153 
157  protected function doDelete() {
158  if ( isset( self::$data[$this->type][$this->wiki] ) ) {
159  unset( self::$data[$this->type][$this->wiki] );
160  if ( !self::$data[$this->type] ) {
161  unset( self::$data[$this->type] );
162  }
163  }
164  }
165 
171  public function getAllQueuedJobs() {
172  $unclaimed = $this->getQueueData( 'unclaimed' );
173  if ( !$unclaimed ) {
174  return new ArrayIterator( [] );
175  }
176 
177  return new MappedIterator(
178  $unclaimed,
179  function ( $value ) {
180  return $this->jobFromSpecInternal( $value );
181  }
182  );
183  }
184 
190  public function getAllAcquiredJobs() {
191  $claimed = $this->getQueueData( 'claimed' );
192  if ( !$claimed ) {
193  return new ArrayIterator( [] );
194  }
195 
196  return new MappedIterator(
197  $claimed,
198  function ( $value ) {
199  return $this->jobFromSpecInternal( $value );
200  }
201  );
202  }
203 
209  public function jobFromSpecInternal( IJobSpecification $spec ) {
210  return Job::factory( $spec->getType(), $spec->getTitle(), $spec->getParams() );
211  }
212 
219  private function &getQueueData( $field, $init = null ) {
220  if ( !isset( self::$data[$this->type][$this->wiki][$field] ) ) {
221  if ( $init !== null ) {
222  self::$data[$this->type][$this->wiki][$field] = $init;
223  } else {
224  return $init;
225  }
226  }
227 
228  return self::$data[$this->type][$this->wiki][$field];
229  }
230 }
MappedIterator
Convenience class for generating iterators from iterators.
Definition: MappedIterator.php:28
type
This document describes the state of Postgres support in and is fairly well maintained The main code is very well while extensions are very hit and miss it is probably the most supported database after MySQL Much of the work in making MediaWiki database agnostic came about through the work of creating Postgres as and are nearing end of but without copying over all the usage comments General notes on the but these can almost always be programmed around *Although Postgres has a true BOOLEAN type
Definition: postgres.txt:22
JobQueueMemory\optimalOrder
optimalOrder()
Definition: JobQueueMemory.php:73
JobQueueMemory\getAllQueuedJobs
getAllQueuedJobs()
Definition: JobQueueMemory.php:171
captcha-old.count
count
Definition: captcha-old.py:249
wiki
Prior to maintenance scripts were a hodgepodge of code that had no cohesion or formal method of action Beginning maintenance scripts have been cleaned up to use a unified class Directory structure How to run a script How to write your own DIRECTORY STRUCTURE The maintenance directory of a MediaWiki installation contains several all of which have unique purposes HOW TO RUN A SCRIPT Ridiculously just call php someScript php that s in the top level maintenance directory if not default wiki
Definition: maintenance.txt:1
IJobSpecification\getTitle
getTitle()
serialize
serialize()
Definition: ApiMessage.php:177
JobQueueMemory\$data
static array[] $data
Definition: JobQueueMemory.php:33
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
key
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at etc Handles the details of getting and saving to the user table of the and dealing with sessions and cookies OutputPage Encapsulates the entire HTML page that will be sent in response to any server request It is used by calling its functions to add in any and then calling but I prefer the flexibility This should also do the output encoding The system allocates a global one in $wgOut Title Represents the title of an and does all the work of translating among various forms such as plain database key
Definition: design.txt:25
Job
Class to both describe a background job and handle jobs.
Definition: Job.php:31
IJobSpecification\getType
getType()
Job\factory
static factory( $command, Title $title, $params=[])
Create the appropriate object to handle a specific job.
Definition: Job.php:68
JobQueueMemory\jobFromSpecInternal
jobFromSpecInternal(IJobSpecification $spec)
Definition: JobQueueMemory.php:209
JobQueue\getAcquiredCount
getAcquiredCount()
Get the number of acquired jobs (these are temporarily out of the queue).
Definition: JobQueue.php:235
JobQueueMemory\doIsEmpty
doIsEmpty()
Definition: JobQueueMemory.php:82
JobQueue\$type
string $type
Job type.
Definition: JobQueue.php:35
JobQueueMemory
Class to handle job queues stored in PHP memory for testing.
Definition: JobQueueMemory.php:31
IJobSpecification\getParams
getParams()
JobQueueMemory\getQueueData
& getQueueData( $field, $init=null)
Definition: JobQueueMemory.php:219
$value
$value
Definition: styleTest.css.php:45
JobQueueMemory\doBatchPush
doBatchPush(array $jobs, $flags)
Definition: JobQueueMemory.php:41
JobQueueMemory\doPop
doPop()
Definition: JobQueueMemory.php:113
JobQueueMemory\doGetAcquiredCount
doGetAcquiredCount()
Definition: JobQueueMemory.php:102
JobQueueMemory\getAllAcquiredJobs
getAllAcquiredJobs()
Definition: JobQueueMemory.php:190
JobQueue\$wiki
string $wiki
Wiki ID.
Definition: JobQueue.php:33
$job
if(count( $args)< 1) $job
Definition: recompressTracked.php:47
JobQueueMemory\doAck
doAck(Job $job)
Definition: JobQueueMemory.php:145
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
JobQueue
Class to handle enqueueing and running of background jobs.
Definition: JobQueue.php:31
JobQueueMemory\doGetSize
doGetSize()
Definition: JobQueueMemory.php:91
order
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at etc Handles the details of getting and saving to the user table of the and dealing with sessions and cookies OutputPage Encapsulates the entire HTML page that will be sent in response to any server request It is used by calling its functions to add in any order
Definition: design.txt:12
IJobSpecification
Job queue task description interface.
Definition: JobSpecification.php:30
$flags
it s the revision text itself In either if gzip is the revision text is gzipped $flags
Definition: hooks.txt:2801
JobQueueMemory\supportedOrders
supportedOrders()
Definition: JobQueueMemory.php:64
array
the array() calling protocol came about after MediaWiki 1.4rc1.
JobQueueMemory\doDelete
doDelete()
Definition: JobQueueMemory.php:157