Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 45
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
TimedMediaMaintenance
0.00% covered (danger)
0.00%
0 / 45
0.00% covered (danger)
0.00%
0 / 2
240
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 38
0.00% covered (danger)
0.00%
0 / 1
210
 processFile
n/a
0 / 0
n/a
0 / 0
0
1<?php
2
3use MediaWiki\TimedMediaHandler\TimedMediaHandler;
4use MediaWiki\Title\Title;
5use Wikimedia\Rdbms\SelectQueryBuilder;
6
7abstract class TimedMediaMaintenance extends Maintenance {
8
9    public function __construct() {
10        parent::__construct();
11        $this->addOption( "file", "process only the given file", false, true );
12        $this->addOption( "start", "(re)start batch at the given file", false, true );
13        $this->addOption( "audio", "process audio files (defaults to all media types)" );
14        $this->addOption( "video", "process video files (defaults to all media types)" );
15        $this->addOption( "mime", "mime type to filter on (e.g. audio/midi)", false, true );
16        $this->requireExtension( 'TimedMediaHandler' );
17    }
18
19    public function execute() {
20        $dbr = $this->getServiceContainer()->getDBLoadBalancerFactory()->getReplicaDatabase();
21        $types = [];
22        if ( $this->hasOption( 'audio' ) ) {
23            $types[] = 'AUDIO';
24        }
25        if ( $this->hasOption( 'video' ) ) {
26            $types[] = 'VIDEO';
27        }
28        if ( !$types ) {
29            // Default to all if none specified
30            $types = [ 'AUDIO', 'VIDEO' ];
31        }
32        $where = [ 'img_media_type' => $types ];
33
34        if ( $this->hasOption( 'mime' ) ) {
35            [ $major, $minor ] = File::splitMime( $this->getOption( 'mime' ) );
36            $where['img_major_mime'] = $major;
37            $where['img_minor_mime'] = $minor;
38        }
39
40        if ( $this->hasOption( 'file' ) ) {
41            $title = Title::newFromText( $this->getOption( 'file' ), NS_FILE );
42            if ( !$title ) {
43                $this->error( "Invalid --file option provided" );
44                return;
45            }
46            $where['img_name'] = $title->getDBkey();
47        }
48        if ( $this->hasOption( 'start' ) ) {
49            $title = Title::newFromText( $this->getOption( 'start' ), NS_FILE );
50            if ( !$title ) {
51                $this->error( "Invalid --start option provided" );
52                return;
53            }
54            $where[] = $dbr->expr( 'img_name', '>=', $title->getDBkey() );
55        }
56        $res = $dbr->newSelectQueryBuilder()
57            ->select( [ 'img_name' ] )
58            ->from( 'image' )
59            ->where( $where )
60            ->orderBy( [ 'img_media_type', 'img_name' ], SelectQueryBuilder::SORT_ASC )
61            ->caller( __METHOD__ )->fetchResultSet();
62
63        $localRepo = $this->getServiceContainer()->getRepoGroup()->getLocalRepo();
64        foreach ( $res as $row ) {
65            $title = Title::newFromText( $row->img_name, NS_FILE );
66            $file = $localRepo->newFile( $title );
67            $handler = $file ? $file->getHandler() : null;
68            if ( $file && $handler && $handler instanceof TimedMediaHandler ) {
69                $this->processFile( $file );
70            }
71        }
72    }
73
74    /**
75     * @param File $file
76     */
77    abstract public function processFile( File $file );
78}