Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 111
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
FileSelectQueryBuilder
0.00% covered (danger)
0.00%
0 / 111
0.00% covered (danger)
0.00%
0 / 7
272
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
20
 newForFile
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 newForOldFile
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 newForArchivedFile
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 initFile
0.00% covered (danger)
0.00%
0 / 31
0.00% covered (danger)
0.00%
0 / 1
12
 initOldFile
0.00% covered (danger)
0.00%
0 / 32
0.00% covered (danger)
0.00%
0 / 1
12
 initArchivedFile
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2/**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21namespace MediaWiki\FileRepo\File;
22
23use InvalidArgumentException;
24use Wikimedia\Rdbms\IReadableDatabase;
25use Wikimedia\Rdbms\SelectQueryBuilder;
26
27class FileSelectQueryBuilder extends SelectQueryBuilder {
28
29    /**
30     * @internal use ::newFor* instead.
31     * @param IReadableDatabase $db
32     * @param string $type either 'file', 'oldfile' or 'archivedfile'
33     * @param array $options
34     *   - omit-lazy: Omit fields that are lazily cached.
35     */
36    public function __construct( IReadableDatabase $db, string $type = 'file', array $options = [] ) {
37        parent::__construct( $db );
38        if ( $type === 'file' ) {
39            $this->initFile( $options );
40        } elseif ( $type === 'oldfile' ) {
41            $this->initOldFile( $options );
42        } elseif ( $type === 'archivedfile' ) {
43            $this->initArchivedFile( $options );
44        } else {
45            throw new InvalidArgumentException( "Type $type is not among accepted values" );
46        }
47    }
48
49    public static function newForFile( IReadableDatabase $db, array $options = [] ): FileSelectQueryBuilder {
50        return new FileSelectQueryBuilder( $db, 'file', $options );
51    }
52
53    public static function newForOldFile( IReadableDatabase $db, array $options = [] ): FileSelectQueryBuilder {
54        return new FileSelectQueryBuilder( $db, 'oldfile', $options );
55    }
56
57    public static function newForArchivedFile( IReadableDatabase $db, array $options = [] ): FileSelectQueryBuilder {
58        return new FileSelectQueryBuilder( $db, 'archivedfile', $options );
59    }
60
61    private function initFile( $options ) {
62        $this->table( 'image' )
63            ->join( 'actor', 'image_actor', 'actor_id=img_actor' )
64            ->join(
65                'comment',
66                'comment_img_description',
67                'comment_img_description.comment_id = img_description_id'
68            );
69
70        if ( !in_array( 'omit-nonlazy', $options, true ) ) {
71            $this->fields(
72                [
73                    'img_name',
74                    'img_size',
75                    'img_width',
76                    'img_height',
77                    'img_metadata',
78                    'img_bits',
79                    'img_media_type',
80                    'img_major_mime',
81                    'img_minor_mime',
82                    'img_timestamp',
83                    'img_sha1',
84                    'img_actor',
85                    'img_user' => 'image_actor.actor_user',
86                    'img_user_text' => 'image_actor.actor_name',
87                    'img_description_text' => 'comment_img_description.comment_text',
88                    'img_description_data' => 'comment_img_description.comment_data',
89                    'img_description_cid' => 'comment_img_description.comment_id'
90                ]
91            );
92        }
93        if ( !in_array( 'omit-lazy', $options, true ) ) {
94            // Note: Keep this in sync with LocalFile::getLazyCacheFields() and
95            // LocalFile::loadExtraFromDB()
96            $this->field( 'img_metadata' );
97        }
98    }
99
100    private function initOldFile( $options ) {
101        $this->table( 'oldimage' )
102            ->join( 'actor', 'oldimage_actor', 'actor_id=oi_actor' )
103            ->join(
104                'comment',
105                'comment_oi_description',
106                'comment_oi_description.comment_id = oi_description_id'
107            );
108
109        if ( !in_array( 'omit-nonlazy', $options, true ) ) {
110            $this->fields(
111                [
112                    'oi_name',
113                    'oi_archive_name',
114                    'oi_size',
115                    'oi_width',
116                    'oi_height',
117                    'oi_bits',
118                    'oi_media_type',
119                    'oi_major_mime',
120                    'oi_minor_mime',
121                    'oi_timestamp',
122                    'oi_deleted',
123                    'oi_sha1',
124                    'oi_actor',
125                    'oi_user' => 'oldimage_actor.actor_user',
126                    'oi_user_text' => 'oldimage_actor.actor_name',
127                    'oi_description_text' => 'comment_oi_description.comment_text',
128                    'oi_description_data' => 'comment_oi_description.comment_data',
129                    'oi_description_cid' => 'comment_oi_description.comment_id'
130                ]
131            );
132        }
133        if ( !in_array( 'omit-lazy', $options, true ) ) {
134            // Note: Keep this in sync with LocalFile::getLazyCacheFields() and
135            // LocalFile::loadExtraFromDB()
136            $this->field( 'oi_metadata' );
137        }
138    }
139
140    private function initArchivedFile( $options ) {
141        $this->table( 'filearchive' )
142            ->join( 'actor', 'filearchive_actor', 'actor_id=fa_actor' )
143            ->join(
144                'comment',
145                'comment_fa_description',
146                'comment_fa_description.comment_id = fa_description_id'
147            );
148
149        if ( !in_array( 'omit-nonlazy', $options, true ) ) {
150            $this->fields(
151                [
152                    'fa_id',
153                    'fa_name',
154                    'fa_archive_name',
155                    'fa_storage_key',
156                    'fa_storage_group',
157                    'fa_size',
158                    'fa_bits',
159                    'fa_width',
160                    'fa_height',
161                    'fa_metadata',
162                    'fa_media_type',
163                    'fa_major_mime',
164                    'fa_minor_mime',
165                    'fa_timestamp',
166                    'fa_deleted',
167                    'fa_deleted_timestamp', /* Used by LocalFileRestoreBatch */
168                    'fa_sha1',
169                    'fa_actor',
170                    'fa_user' => 'filearchive_actor.actor_user',
171                    'fa_user_text' => 'filearchive_actor.actor_name',
172                    'fa_description_text' => 'comment_fa_description.comment_text',
173                    'fa_description_data' => 'comment_fa_description.comment_data',
174                    'fa_description_cid' => 'comment_fa_description.comment_id'
175                ]
176            );
177        }
178        if ( !in_array( 'omit-lazy', $options, true ) ) {
179            // Note: Keep this in sync with LocalFile::getLazyCacheFields() and
180            // LocalFile::loadExtraFromDB()
181            $this->field( 'fa_metadata' );
182        }
183    }
184}