| 1 |
<?php |
| 2 |
|
| 3 |
" |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
namespace MediaWiki\Api; |
| 24 |
|
| 25 |
use MediaWiki\Title\Title; |
| 26 |
use Wikimedia\ParamValidator\ParamValidator; |
| 27 |
use Wikimedia\ParamValidator\TypeDef\IntegerDef; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
class ApiQueryImages extends ApiQueryGeneratorBase { |
| 36 |
|
| 37 |
public function __construct( ApiQuery $query, string $moduleName ) { |
| 38 |
parent::__construct( $query, $moduleName, 'im' ); |
| 39 |
} |
| 40 |
|
| 41 |
public function execute() { |
| 42 |
$this->run(); |
| 43 |
} |
| 44 |
|
| 45 |
public function executeGenerator( $resultPageSet ) { |
| 46 |
$this->run( $resultPageSet ); |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
private function run( $resultPageSet = null ) { |
| 53 |
$pages = $this->getPageSet()->getGoodPages(); |
| 54 |
if ( $pages === [] ) { |
| 55 |
return; |
| 56 |
} |
| 57 |
|
| 58 |
$params = $this->extractRequestParams(); |
| 59 |
$this->addFields( [ |
| 60 |
'il_from', |
| 61 |
'il_to' |
| 62 |
] ); |
| 63 |
|
| 64 |
$this->addTables( 'imagelinks' ); |
| 65 |
$this->addWhereFld( 'il_from', array_keys( $pages ) ); |
| 66 |
if ( $params['continue'] !== null ) { |
| 67 |
$db = $this->getDB(); |
| 68 |
$cont = $this->parseContinueParamOrDie( $params['continue'], [ 'int', 'string' ] ); |
| 69 |
$op = $params['dir'] == 'descending' ? '<=' : '>='; |
| 70 |
$this->addWhere( $db->buildComparison( $op, [ |
| 71 |
'il_from' => $cont[0], |
| 72 |
'il_to' => $cont[1], |
| 73 |
] ) ); |
| 74 |
} |
| 75 |
|
| 76 |
$sort = ( $params['dir'] == 'descending' ? ' DESC' : '' ); |
| 77 |
|
| 78 |
if ( count( $pages ) === 1 ) { |
| 79 |
$this->addOption( 'ORDER BY', 'il_to' . $sort ); |
| 80 |
} else { |
| 81 |
$this->addOption( 'ORDER BY', [ |
| 82 |
'il_from' . $sort, |
| 83 |
'il_to' . $sort |
| 84 |
] ); |
| 85 |
} |
| 86 |
$this->addOption( 'LIMIT', $params['limit'] + 1 ); |
| 87 |
|
| 88 |
if ( $params['images'] ) { |
| 89 |
$images = []; |
| 90 |
foreach ( $params['images'] as $img ) { |
| 91 |
$title = Title::newFromText( $img ); |
| 92 |
if ( !$title || $title->getNamespace() !== NS_FILE ) { |
| 93 |
$this->addWarning( [ 'apiwarn-notfile', wfEscapeWikiText( $img ) ] ); |
| 94 |
} else { |
| 95 |
$images[] = $title->getDBkey(); |
| 96 |
} |
| 97 |
} |
| 98 |
if ( !$images ) { |
| 99 |
|
| 100 |
return; |
| 101 |
} |
| 102 |
$this->addWhereFld( 'il_to', $images ); |
| 103 |
} |
| 104 |
|
| 105 |
$res = $this->select( __METHOD__ ); |
| 106 |
|
| 107 |
if ( $resultPageSet === null ) { |
| 108 |
$count = 0; |
| 109 |
foreach ( $res as $row ) { |
| 110 |
if ( ++$count > $params['limit'] ) { |
| 111 |
|
| 112 |
|
| 113 |
$this->setContinueEnumParameter( 'continue', $row->il_from . '|' . $row->il_to ); |
| 114 |
break; |
| 115 |
} |
| 116 |
$vals = []; |
| 117 |
ApiQueryBase::addTitleInfo( $vals, Title::makeTitle( NS_FILE, $row->il_to ) ); |
| 118 |
$fit = $this->addPageSubItem( $row->il_from, $vals ); |
| 119 |
if ( !$fit ) { |
| 120 |
$this->setContinueEnumParameter( 'continue', $row->il_from . '|' . $row->il_to ); |
| 121 |
break; |
| 122 |
} |
| 123 |
} |
| 124 |
} else { |
| 125 |
$titles = []; |
| 126 |
$count = 0; |
| 127 |
foreach ( $res as $row ) { |
| 128 |
if ( ++$count > $params['limit'] ) { |
| 129 |
|
| 130 |
|
| 131 |
$this->setContinueEnumParameter( 'continue', $row->il_from . '|' . $row->il_to ); |
| 132 |
break; |
| 133 |
} |
| 134 |
$titles[] = Title::makeTitle( NS_FILE, $row->il_to ); |
| 135 |
} |
| 136 |
$resultPageSet->populateFromTitles( $titles ); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
public function getCacheMode( $params ) { |
| 141 |
return 'public'; |
| 142 |
} |
| 143 |
|
| 144 |
public function getAllowedParams() { |
| 145 |
return [ |
| 146 |
'limit' => [ |
| 147 |
ParamValidator::PARAM_DEFAULT => 10, |
| 148 |
ParamValidator::PARAM_TYPE => 'limit', |
| 149 |
IntegerDef::PARAM_MIN => 1, |
| 150 |
IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1, |
| 151 |
IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2 |
| 152 |
], |
| 153 |
'continue' => [ |
| 154 |
ApiBase::PARAM_HELP_MSG => 'api-help-param-continue', |
| 155 |
], |
| 156 |
'images' => [ |
| 157 |
ParamValidator::PARAM_ISMULTI => true, |
| 158 |
], |
| 159 |
'dir' => [ |
| 160 |
ParamValidator::PARAM_DEFAULT => 'ascending', |
| 161 |
ParamValidator::PARAM_TYPE => [ |
| 162 |
'ascending', |
| 163 |
'descending' |
| 164 |
] |
| 165 |
], |
| 166 |
]; |
| 167 |
} |
| 168 |
|
| 169 |
protected function getExamplesMessages() { |
| 170 |
$title = Title::newMainPage()->getPrefixedText(); |
| 171 |
$mp = rawurlencode( $title ); |
| 172 |
|
| 173 |
return [ |
| 174 |
"action=query&prop=images&titles={$mp}" |
| 175 |
=> 'apihelp-query+images-example-simple', |
| 176 |
"action=query&generator=images&titles={$mp}&prop=info" |
| 177 |
=> 'apihelp-query+images-example-generator', |
| 178 |
]; |
| 179 |
} |
| 180 |
|
| 181 |
public function getHelpUrls() { |
| 182 |
return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Images'; |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
class_alias( ApiQueryImages::class, 'ApiQueryImages' ); |