From 3413839cb802999ba47a03f93557d1862e6de4c4 Mon Sep 17 00:00:00 2001 From: Kunal Mehta Date: Sun, 16 Oct 2016 13:18:05 -0700 Subject: [PATCH] API: Expose wfIsBadImage() in prop=imageinfo If iiprop=badfile is set, expose the wfIsBadImage() status as a boolean. This is mainly intended for use by Parsoid, but could be useful for other API users too. The context title can be set by using the badfilecontextitle parameter. Also be a little more paranoid and make sure wfIsBadImage() always returns a boolean. Bug: T148343 Change-Id: I3432ead92b8d2a17de5243285f2760fc39b501ae --- includes/GlobalFunctions.php | 2 +- includes/api/ApiQueryImageInfo.php | 32 ++++++++++++++++++++++++------ includes/api/i18n/en.json | 2 ++ includes/api/i18n/qqq.json | 2 ++ 4 files changed, 31 insertions(+), 7 deletions(-) diff --git a/includes/GlobalFunctions.php b/includes/GlobalFunctions.php index b3ccc5675a..f9f499a10d 100644 --- a/includes/GlobalFunctions.php +++ b/includes/GlobalFunctions.php @@ -3518,7 +3518,7 @@ function wfIsBadImage( $name, $contextTitle = false, $blacklist = null ) { # Run the extension hook $bad = false; if ( !Hooks::run( 'BadImage', [ $name, &$bad ] ) ) { - return $bad; + return (bool)$bad; } $cache = ObjectCache::getLocalServerInstance( 'hash' ); diff --git a/includes/api/ApiQueryImageInfo.php b/includes/api/ApiQueryImageInfo.php index d1fcfa3f07..663751fa36 100644 --- a/includes/api/ApiQueryImageInfo.php +++ b/includes/api/ApiQueryImageInfo.php @@ -58,6 +58,15 @@ class ApiQueryImageInfo extends ApiQueryBase { 'revdelUser' => $this->getUser(), ]; + if ( isset( $params['badfilecontexttitle'] ) ) { + $badFileContextTitle = Title::newFromText( $params['badfilecontexttitle'] ); + if ( !$badFileContextTitle ) { + $this->dieUsage( 'Invalid title in badfilecontexttitle parameter', 'invalid-title' ); + } + } else { + $badFileContextTitle = false; + } + $pageIds = $this->getPageSet()->getGoodAndMissingTitlesByNamespace(); if ( !empty( $pageIds[NS_FILE] ) ) { $titles = array_keys( $pageIds[NS_FILE] ); @@ -95,13 +104,16 @@ class ApiQueryImageInfo extends ApiQueryBase { $result = $this->getResult(); foreach ( $titles as $title ) { + $info = []; $pageId = $pageIds[NS_FILE][$title]; $start = $title === $fromTitle ? $fromTimestamp : $params['start']; if ( !isset( $images[$title] ) ) { - if ( isset( $prop['uploadwarning'] ) ) { - // Uploadwarning needs info about non-existing files + if ( isset( $prop['uploadwarning'] ) || isset( $prop['badfile'] ) ) { + // uploadwarning and badfile need info about non-existing files $images[$title] = wfLocalFile( $title ); + // Doesn't exist, so set an empty image repository + $info['imagerepository'] = ''; } else { $result->addValue( [ 'query', 'pages', intval( $pageId ) ], @@ -128,10 +140,14 @@ class ApiQueryImageInfo extends ApiQueryBase { break; } - $fit = $result->addValue( - [ 'query', 'pages', intval( $pageId ) ], - 'imagerepository', $img->getRepoName() - ); + if ( !isset( $info['imagerepository'] ) ) { + $info['imagerepository'] = $img->getRepoName(); + } + if ( isset( $prop['badfile'] ) ) { + $info['badfile'] = (bool)wfIsBadImage( $title, $badFileContextTitle ); + } + + $fit = $result->addValue( [ 'query', 'pages' ], intval( $pageId ), $info ); if ( !$fit ) { if ( count( $pageIds[NS_FILE] ) == 1 ) { // The user is screwed. imageinfo can't be solely @@ -689,6 +705,9 @@ class ApiQueryImageInfo extends ApiQueryBase { ApiBase::PARAM_DFLT => '', ApiBase::PARAM_TYPE => 'string', ], + 'badfilecontexttitle' => [ + ApiBase::PARAM_TYPE => 'string', + ], 'continue' => [ ApiBase::PARAM_HELP_MSG => 'api-help-param-continue', ], @@ -734,6 +753,7 @@ class ApiQueryImageInfo extends ApiQueryBase { 'archivename' => 'apihelp-query+imageinfo-paramvalue-prop-archivename', 'bitdepth' => 'apihelp-query+imageinfo-paramvalue-prop-bitdepth', 'uploadwarning' => 'apihelp-query+imageinfo-paramvalue-prop-uploadwarning', + 'badfile' => 'apihelp-query+imageinfo-paramvalue-prop-badfile', ], array_flip( $filter ) ); diff --git a/includes/api/i18n/en.json b/includes/api/i18n/en.json index 28cd746adc..319cec1153 100644 --- a/includes/api/i18n/en.json +++ b/includes/api/i18n/en.json @@ -803,6 +803,7 @@ "apihelp-query+imageinfo-paramvalue-prop-archivename": "Adds the filename of the archive version for non-latest versions.", "apihelp-query+imageinfo-paramvalue-prop-bitdepth": "Adds the bit depth of the version.", "apihelp-query+imageinfo-paramvalue-prop-uploadwarning": "Used by the Special:Upload page to get information about an existing file. Not intended for use outside MediaWiki core.", + "apihelp-query+imageinfo-paramvalue-prop-badfile": "Adds whether the file is on the [[MediaWiki:Bad image list]]", "apihelp-query+imageinfo-param-limit": "How many file revisions to return per file.", "apihelp-query+imageinfo-param-start": "Timestamp to start listing from.", "apihelp-query+imageinfo-param-end": "Timestamp to stop listing at.", @@ -813,6 +814,7 @@ "apihelp-query+imageinfo-param-extmetadatamultilang": "If translations for extmetadata property are available, fetch all of them.", "apihelp-query+imageinfo-param-extmetadatafilter": "If specified and non-empty, only these keys will be returned for $1prop=extmetadata.", "apihelp-query+imageinfo-param-urlparam": "A handler specific parameter string. For example, PDFs might use page15-100px. $1urlwidth must be used and be consistent with $1urlparam.", + "apihelp-query+imageinfo-param-badfilecontexttitle": "If $2prop=badfile is set, this is the page title used when evaluting the [[MediaWiki:Bad image list]]", "apihelp-query+imageinfo-param-localonly": "Look only for files in the local repository.", "apihelp-query+imageinfo-example-simple": "Fetch information about the current version of [[:File:Albert Einstein Head.jpg]].", "apihelp-query+imageinfo-example-dated": "Fetch information about versions of [[:File:Test.jpg]] from 2008 and later.", diff --git a/includes/api/i18n/qqq.json b/includes/api/i18n/qqq.json index fd6a4dd609..12761d636d 100644 --- a/includes/api/i18n/qqq.json +++ b/includes/api/i18n/qqq.json @@ -751,6 +751,7 @@ "apihelp-query+imageinfo-paramvalue-prop-archivename": "{{doc-apihelp-paramvalue|query+imageinfo|prop|archivename}}", "apihelp-query+imageinfo-paramvalue-prop-bitdepth": "{{doc-apihelp-paramvalue|query+imageinfo|prop|bitdepth}}", "apihelp-query+imageinfo-paramvalue-prop-uploadwarning": "{{doc-apihelp-paramvalue|query+imageinfo|prop|uploadwarning}}", + "apihelp-query+imageinfo-paramvalue-prop-badfile": "{{doc-apihelp-paramvalue|query+imageinfo|prop|badfile}}", "apihelp-query+imageinfo-param-limit": "{{doc-apihelp-param|query+imageinfo|limit}}", "apihelp-query+imageinfo-param-start": "{{doc-apihelp-param|query+imageinfo|start}}", "apihelp-query+imageinfo-param-end": "{{doc-apihelp-param|query+imageinfo|end}}", @@ -761,6 +762,7 @@ "apihelp-query+imageinfo-param-extmetadatamultilang": "{{doc-apihelp-param|query+imageinfo|extmetadatamultilang}}", "apihelp-query+imageinfo-param-extmetadatafilter": "{{doc-apihelp-param|query+imageinfo|extmetadatafilter}}", "apihelp-query+imageinfo-param-urlparam": "{{doc-apihelp-param|query+imageinfo|urlparam}}", + "apihelp-query+imageinfo-param-badfilecontexttitle": "{{doc-apihelp-param|query+imageinfo|badfilecontexttitle}}", "apihelp-query+imageinfo-param-localonly": "{{doc-apihelp-param|query+imageinfo|localonly}}", "apihelp-query+imageinfo-example-simple": "{{doc-apihelp-example|query+imageinfo}}", "apihelp-query+imageinfo-example-dated": "{{doc-apihelp-example|query+imageinfo}}", -- 2.20.1