Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
ChunkBuilder
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
132
0.00% covered (danger)
0.00%
0 / 1
 build
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
132
1<?php
2
3namespace CirrusSearch\Maintenance;
4
5/**
6 * Splits maintenance scripts into chunks and prints out the commands to run
7 * the chunks.
8 *
9 * @license GPL-2.0-or-later
10 */
11class ChunkBuilder {
12    /**
13     * @param string $self Name of maintenance script
14     * @param array $options
15     * @param string|int $buildChunks If specified as a number then chunks no
16     *  larger than that size are spat out.  If specified as a number followed
17     *  by the word "total" without a space between them then that many chunks
18     *  will be spat out sized to cover the entire wiki.
19     * @param int $fromPageId
20     * @param int $toPageId
21     */
22    public function build( $self, array $options, $buildChunks, $fromPageId, $toPageId ) {
23        $fixedChunkSize = strpos( $buildChunks, 'total' ) === false;
24        $buildChunks = intval( $buildChunks );
25        if ( $fixedChunkSize ) {
26            $chunkSize = $buildChunks;
27        } else {
28            $chunkSize = max( 1, ceil( ( $toPageId - $fromPageId ) / $buildChunks ) );
29        }
30        for ( $pageId = $fromPageId; $pageId < $toPageId; $pageId += $chunkSize ) {
31            $chunkToId = min( $toPageId, $pageId + $chunkSize );
32            print "php $self";
33            foreach ( $options as $optName => $optVal ) {
34                if ( $optVal === null || $optVal === false || $optName === 'fromId' ||
35                    $optName === 'toId' || $optName === 'buildChunks' ||
36                    ( $optName === 'memory-limit' && $optVal === 'max' )
37                ) {
38                    continue;
39                }
40                print " --$optName $optVal";
41            }
42            print " --fromId $pageId --toId $chunkToId\n";
43        }
44    }
45}