Conditions | 45 |
Paths | > 20000 |
Total Lines | 205 |
Code Lines | 143 |
Lines | 26 |
Ratio | 12.68 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
40 | public function execute() { |
||
41 | $user = $this->getUser(); |
||
42 | // Before doing anything at all, let's check permissions |
||
43 | if ( !$user->isAllowed( 'deletedhistory' ) ) { |
||
44 | $this->dieUsage( |
||
45 | 'You don\'t have permission to view deleted file information', |
||
46 | 'permissiondenied' |
||
47 | ); |
||
48 | } |
||
49 | |||
50 | $db = $this->getDB(); |
||
51 | |||
52 | $params = $this->extractRequestParams(); |
||
53 | |||
54 | $prop = array_flip( $params['prop'] ); |
||
55 | $fld_sha1 = isset( $prop['sha1'] ); |
||
56 | $fld_timestamp = isset( $prop['timestamp'] ); |
||
57 | $fld_user = isset( $prop['user'] ); |
||
58 | $fld_size = isset( $prop['size'] ); |
||
59 | $fld_dimensions = isset( $prop['dimensions'] ); |
||
60 | $fld_description = isset( $prop['description'] ) || isset( $prop['parseddescription'] ); |
||
61 | $fld_mime = isset( $prop['mime'] ); |
||
62 | $fld_mediatype = isset( $prop['mediatype'] ); |
||
63 | $fld_metadata = isset( $prop['metadata'] ); |
||
64 | $fld_bitdepth = isset( $prop['bitdepth'] ); |
||
65 | $fld_archivename = isset( $prop['archivename'] ); |
||
66 | |||
67 | $this->addTables( 'filearchive' ); |
||
68 | |||
69 | $this->addFields( ArchivedFile::selectFields() ); |
||
70 | $this->addFields( [ 'fa_id', 'fa_name', 'fa_timestamp', 'fa_deleted' ] ); |
||
71 | $this->addFieldsIf( 'fa_sha1', $fld_sha1 ); |
||
72 | $this->addFieldsIf( [ 'fa_user', 'fa_user_text' ], $fld_user ); |
||
73 | $this->addFieldsIf( [ 'fa_height', 'fa_width', 'fa_size' ], $fld_dimensions || $fld_size ); |
||
74 | $this->addFieldsIf( 'fa_description', $fld_description ); |
||
75 | $this->addFieldsIf( [ 'fa_major_mime', 'fa_minor_mime' ], $fld_mime ); |
||
76 | $this->addFieldsIf( 'fa_media_type', $fld_mediatype ); |
||
77 | $this->addFieldsIf( 'fa_metadata', $fld_metadata ); |
||
78 | $this->addFieldsIf( 'fa_bits', $fld_bitdepth ); |
||
79 | $this->addFieldsIf( 'fa_archive_name', $fld_archivename ); |
||
80 | |||
81 | View Code Duplication | if ( !is_null( $params['continue'] ) ) { |
|
82 | $cont = explode( '|', $params['continue'] ); |
||
83 | $this->dieContinueUsageIf( count( $cont ) != 3 ); |
||
84 | $op = $params['dir'] == 'descending' ? '<' : '>'; |
||
85 | $cont_from = $db->addQuotes( $cont[0] ); |
||
86 | $cont_timestamp = $db->addQuotes( $db->timestamp( $cont[1] ) ); |
||
87 | $cont_id = (int)$cont[2]; |
||
88 | $this->dieContinueUsageIf( $cont[2] !== (string)$cont_id ); |
||
89 | $this->addWhere( "fa_name $op $cont_from OR " . |
||
90 | "(fa_name = $cont_from AND " . |
||
91 | "(fa_timestamp $op $cont_timestamp OR " . |
||
92 | "(fa_timestamp = $cont_timestamp AND " . |
||
93 | "fa_id $op= $cont_id )))" |
||
94 | ); |
||
95 | } |
||
96 | |||
97 | // Image filters |
||
98 | $dir = ( $params['dir'] == 'descending' ? 'older' : 'newer' ); |
||
99 | $from = ( $params['from'] === null ? null : $this->titlePartToKey( $params['from'], NS_FILE ) ); |
||
100 | $to = ( $params['to'] === null ? null : $this->titlePartToKey( $params['to'], NS_FILE ) ); |
||
101 | $this->addWhereRange( 'fa_name', $dir, $from, $to ); |
||
102 | View Code Duplication | if ( isset( $params['prefix'] ) ) { |
|
103 | $this->addWhere( 'fa_name' . $db->buildLike( |
||
104 | $this->titlePartToKey( $params['prefix'], NS_FILE ), |
||
105 | $db->anyString() ) ); |
||
106 | } |
||
107 | |||
108 | $sha1Set = isset( $params['sha1'] ); |
||
109 | $sha1base36Set = isset( $params['sha1base36'] ); |
||
110 | if ( $sha1Set || $sha1base36Set ) { |
||
111 | $sha1 = false; |
||
112 | if ( $sha1Set ) { |
||
113 | $sha1 = strtolower( $params['sha1'] ); |
||
114 | if ( !$this->validateSha1Hash( $sha1 ) ) { |
||
115 | $this->dieUsage( 'The SHA1 hash provided is not valid', 'invalidsha1hash' ); |
||
116 | } |
||
117 | $sha1 = Wikimedia\base_convert( $sha1, 16, 36, 31 ); |
||
118 | View Code Duplication | } elseif ( $sha1base36Set ) { |
|
119 | $sha1 = strtolower( $params['sha1base36'] ); |
||
120 | if ( !$this->validateSha1Base36Hash( $sha1 ) ) { |
||
121 | $this->dieUsage( 'The SHA1Base36 hash provided is not valid', 'invalidsha1base36hash' ); |
||
122 | } |
||
123 | } |
||
124 | if ( $sha1 ) { |
||
|
|||
125 | $this->addWhereFld( 'fa_sha1', $sha1 ); |
||
126 | } |
||
127 | } |
||
128 | |||
129 | // Exclude files this user can't view. |
||
130 | if ( !$user->isAllowed( 'deletedtext' ) ) { |
||
131 | $bitmask = File::DELETED_FILE; |
||
132 | } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) { |
||
133 | $bitmask = File::DELETED_FILE | File::DELETED_RESTRICTED; |
||
134 | } else { |
||
135 | $bitmask = 0; |
||
136 | } |
||
137 | if ( $bitmask ) { |
||
138 | $this->addWhere( $this->getDB()->bitAnd( 'fa_deleted', $bitmask ) . " != $bitmask" ); |
||
139 | } |
||
140 | |||
141 | $limit = $params['limit']; |
||
142 | $this->addOption( 'LIMIT', $limit + 1 ); |
||
143 | $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' ); |
||
144 | $this->addOption( 'ORDER BY', [ |
||
145 | 'fa_name' . $sort, |
||
146 | 'fa_timestamp' . $sort, |
||
147 | 'fa_id' . $sort, |
||
148 | ] ); |
||
149 | |||
150 | $res = $this->select( __METHOD__ ); |
||
151 | |||
152 | $count = 0; |
||
153 | $result = $this->getResult(); |
||
154 | foreach ( $res as $row ) { |
||
155 | if ( ++$count > $limit ) { |
||
156 | // We've reached the one extra which shows that there are |
||
157 | // additional pages to be had. Stop here... |
||
158 | $this->setContinueEnumParameter( |
||
159 | 'continue', "$row->fa_name|$row->fa_timestamp|$row->fa_id" |
||
160 | ); |
||
161 | break; |
||
162 | } |
||
163 | |||
164 | $file = []; |
||
165 | $file['id'] = (int)$row->fa_id; |
||
166 | $file['name'] = $row->fa_name; |
||
167 | $title = Title::makeTitle( NS_FILE, $row->fa_name ); |
||
168 | self::addTitleInfo( $file, $title ); |
||
169 | |||
170 | if ( $fld_description && |
||
171 | Revision::userCanBitfield( $row->fa_deleted, File::DELETED_COMMENT, $user ) |
||
172 | ) { |
||
173 | $file['description'] = $row->fa_description; |
||
174 | if ( isset( $prop['parseddescription'] ) ) { |
||
175 | $file['parseddescription'] = Linker::formatComment( |
||
176 | $row->fa_description, $title ); |
||
177 | } |
||
178 | } |
||
179 | if ( $fld_user && |
||
180 | Revision::userCanBitfield( $row->fa_deleted, File::DELETED_USER, $user ) |
||
181 | ) { |
||
182 | $file['userid'] = (int)$row->fa_user; |
||
183 | $file['user'] = $row->fa_user_text; |
||
184 | } |
||
185 | if ( $fld_sha1 ) { |
||
186 | $file['sha1'] = Wikimedia\base_convert( $row->fa_sha1, 36, 16, 40 ); |
||
187 | } |
||
188 | if ( $fld_timestamp ) { |
||
189 | $file['timestamp'] = wfTimestamp( TS_ISO_8601, $row->fa_timestamp ); |
||
190 | } |
||
191 | if ( $fld_size || $fld_dimensions ) { |
||
192 | $file['size'] = $row->fa_size; |
||
193 | |||
194 | $pageCount = ArchivedFile::newFromRow( $row )->pageCount(); |
||
195 | if ( $pageCount !== false ) { |
||
196 | $file['pagecount'] = $pageCount; |
||
197 | } |
||
198 | |||
199 | $file['height'] = $row->fa_height; |
||
200 | $file['width'] = $row->fa_width; |
||
201 | } |
||
202 | if ( $fld_mediatype ) { |
||
203 | $file['mediatype'] = $row->fa_media_type; |
||
204 | } |
||
205 | if ( $fld_metadata ) { |
||
206 | $file['metadata'] = $row->fa_metadata |
||
207 | ? ApiQueryImageInfo::processMetaData( unserialize( $row->fa_metadata ), $result ) |
||
208 | : null; |
||
209 | } |
||
210 | if ( $fld_bitdepth ) { |
||
211 | $file['bitdepth'] = $row->fa_bits; |
||
212 | } |
||
213 | if ( $fld_mime ) { |
||
214 | $file['mime'] = "$row->fa_major_mime/$row->fa_minor_mime"; |
||
215 | } |
||
216 | if ( $fld_archivename && !is_null( $row->fa_archive_name ) ) { |
||
217 | $file['archivename'] = $row->fa_archive_name; |
||
218 | } |
||
219 | |||
220 | if ( $row->fa_deleted & File::DELETED_FILE ) { |
||
221 | $file['filehidden'] = true; |
||
222 | } |
||
223 | if ( $row->fa_deleted & File::DELETED_COMMENT ) { |
||
224 | $file['commenthidden'] = true; |
||
225 | } |
||
226 | if ( $row->fa_deleted & File::DELETED_USER ) { |
||
227 | $file['userhidden'] = true; |
||
228 | } |
||
229 | if ( $row->fa_deleted & File::DELETED_RESTRICTED ) { |
||
230 | // This file is deleted for normal admins |
||
231 | $file['suppressed'] = true; |
||
232 | } |
||
233 | |||
234 | $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $file ); |
||
235 | if ( !$fit ) { |
||
236 | $this->setContinueEnumParameter( |
||
237 | 'continue', "$row->fa_name|$row->fa_timestamp|$row->fa_id" |
||
238 | ); |
||
239 | break; |
||
240 | } |
||
241 | } |
||
242 | |||
243 | $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'fa' ); |
||
244 | } |
||
245 | |||
303 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: