| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\FileRepo\File; |
| 22 |
|
| 23 |
use InvalidArgumentException; |
| 24 |
use Wikimedia\Rdbms\IReadableDatabase; |
| 25 |
use Wikimedia\Rdbms\SelectQueryBuilder; |
| 26 |
|
| 27 |
class FileSelectQueryBuilder extends SelectQueryBuilder { |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 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 |
|
| 95 |
|
| 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 |
|
| 135 |
|
| 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', |
| 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 |
|
| 180 |
|
| 181 |
$this->field( 'fa_metadata' ); |
| 182 |
} |
| 183 |
} |
| 184 |
} |