Merge "StringUtils: Add a utility for checking if a string is a valid regex"
[lhc/web/wiklou.git] / includes / api / ApiQueryImageInfo.php
index 0791426..285c0bf 100644 (file)
@@ -63,7 +63,7 @@ class ApiQueryImageInfo extends ApiQueryBase {
                                $this->dieWithError( [ 'apierror-bad-badfilecontexttitle', $p ], 'invalid-title' );
                        }
                } else {
-                       $badFileContextTitle = false;
+                       $badFileContextTitle = null;
                }
 
                $pageIds = $this->getPageSet()->getGoodAndMissingTitlesByNamespace();
@@ -144,7 +144,8 @@ class ApiQueryImageInfo extends ApiQueryBase {
                                        $info['imagerepository'] = $img->getRepoName();
                                }
                                if ( isset( $prop['badfile'] ) ) {
-                                       $info['badfile'] = (bool)wfIsBadImage( $title, $badFileContextTitle );
+                                       $info['badfile'] = (bool)MediaWikiServices::getInstance()->getBadFileLookup()
+                                               ->isBadFile( $title, $badFileContextTitle );
                                }
 
                                $fit = $result->addValue( [ 'query', 'pages' ], (int)$pageId, $info );
@@ -386,9 +387,13 @@ class ApiQueryImageInfo extends ApiQueryBase {
                $vals = [
                        ApiResult::META_TYPE => 'assoc',
                ];
+
+               // Some information will be unavailable if the file does not exist. T221812
+               $exists = $file->exists();
+
                // Timestamp is shown even if the file is revdelete'd in interface
                // so do same here.
-               if ( isset( $prop['timestamp'] ) ) {
+               if ( isset( $prop['timestamp'] ) && $exists ) {
                        $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $file->getTimestamp() );
                }
 
@@ -407,7 +412,7 @@ class ApiQueryImageInfo extends ApiQueryBase {
                $user = isset( $prop['user'] );
                $userid = isset( $prop['userid'] );
 
-               if ( $user || $userid ) {
+               if ( ( $user || $userid ) && $exists ) {
                        if ( $file->isDeleted( File::DELETED_USER ) ) {
                                $vals['userhidden'] = true;
                                $anyHidden = true;
@@ -427,7 +432,7 @@ class ApiQueryImageInfo extends ApiQueryBase {
 
                // This is shown even if the file is revdelete'd in interface
                // so do same here.
-               if ( isset( $prop['size'] ) || isset( $prop['dimensions'] ) ) {
+               if ( ( isset( $prop['size'] ) || isset( $prop['dimensions'] ) ) && $exists ) {
                        $vals['size'] = (int)$file->getSize();
                        $vals['width'] = (int)$file->getWidth();
                        $vals['height'] = (int)$file->getHeight();
@@ -448,7 +453,7 @@ class ApiQueryImageInfo extends ApiQueryBase {
                $pcomment = isset( $prop['parsedcomment'] );
                $comment = isset( $prop['comment'] );
 
-               if ( $pcomment || $comment ) {
+               if ( ( $pcomment || $comment ) && $exists ) {
                        if ( $file->isDeleted( File::DELETED_COMMENT ) ) {
                                $vals['commenthidden'] = true;
                                $anyHidden = true;
@@ -499,7 +504,7 @@ class ApiQueryImageInfo extends ApiQueryBase {
                }
 
                if ( $url ) {
-                       if ( $file->exists() ) {
+                       if ( $exists ) {
                                if ( !is_null( $thumbParams ) ) {
                                        $mto = $file->transform( $thumbParams );
                                        self::$transformCount++;
@@ -522,12 +527,12 @@ class ApiQueryImageInfo extends ApiQueryBase {
                                                        $vals['thumbmime'] = $mime;
                                                }
                                        } elseif ( $mto && $mto->isError() ) {
+                                               /** @var MediaTransformError $mto */
+                                               '@phan-var MediaTransformError $mto';
                                                $vals['thumberror'] = $mto->toText();
                                        }
                                }
                                $vals['url'] = wfExpandUrl( $file->getFullUrl(), PROTO_CURRENT );
-                       } else {
-                               $vals['filemissing'] = true;
                        }
                        $vals['descriptionurl'] = wfExpandUrl( $file->getDescriptionUrl(), PROTO_CURRENT );
 
@@ -537,11 +542,15 @@ class ApiQueryImageInfo extends ApiQueryBase {
                        }
                }
 
-               if ( $sha1 ) {
+               if ( !$exists ) {
+                       $vals['filemissing'] = true;
+               }
+
+               if ( $sha1 && $exists ) {
                        $vals['sha1'] = Wikimedia\base_convert( $file->getSha1(), 36, 16, 40 );
                }
 
-               if ( $meta ) {
+               if ( $meta && $exists ) {
                        Wikimedia\suppressWarnings();
                        $metadata = unserialize( $file->getMetadata() );
                        Wikimedia\restoreWarnings();
@@ -550,17 +559,18 @@ class ApiQueryImageInfo extends ApiQueryBase {
                        }
                        $vals['metadata'] = $metadata ? static::processMetaData( $metadata, $result ) : null;
                }
-               if ( $commonmeta ) {
+               if ( $commonmeta && $exists ) {
                        $metaArray = $file->getCommonMetaArray();
                        $vals['commonmetadata'] = $metaArray ? static::processMetaData( $metaArray, $result ) : [];
                }
 
-               if ( $extmetadata ) {
+               if ( $extmetadata && $exists ) {
                        // Note, this should return an array where all the keys
                        // start with a letter, and all the values are strings.
                        // Thus there should be no issue with format=xml.
                        $format = new FormatMetadata;
                        $format->setSingleLanguage( !$opts['multilang'] );
+                       // @phan-suppress-next-line PhanUndeclaredMethod
                        $format->getContext()->setLanguage( $opts['language'] );
                        $extmetaArray = $format->fetchExtendedMetadata( $file );
                        if ( $opts['extmetadatafilter'] ) {
@@ -571,19 +581,21 @@ class ApiQueryImageInfo extends ApiQueryBase {
                        $vals['extmetadata'] = $extmetaArray;
                }
 
-               if ( $mime ) {
+               if ( $mime && $exists ) {
                        $vals['mime'] = $file->getMimeType();
                }
 
-               if ( $mediatype ) {
+               if ( $mediatype && $exists ) {
                        $vals['mediatype'] = $file->getMediaType();
                }
 
                if ( $archive && $file->isOld() ) {
+                       /** @var OldLocalFile $file */
+                       '@phan-var OldLocalFile $file';
                        $vals['archivename'] = $file->getArchiveName();
                }
 
-               if ( $bitdepth ) {
+               if ( $bitdepth && $exists ) {
                        $vals['bitdepth'] = $file->getBitDepth();
                }