Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 94 |
|
0.00% |
0 / 8 |
CRAP | |
0.00% |
0 / 1 |
| ApiQueryImages | |
0.00% |
0 / 93 |
|
0.00% |
0 / 8 |
600 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| execute | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| executeGenerator | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| run | |
0.00% |
0 / 58 |
|
0.00% |
0 / 1 |
306 | |||
| getCacheMode | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getAllowedParams | |
0.00% |
0 / 22 |
|
0.00% |
0 / 1 |
2 | |||
| getExamplesMessages | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 | |||
| getHelpUrls | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com" |
| 4 | * |
| 5 | * @license GPL-2.0-or-later |
| 6 | * @file |
| 7 | */ |
| 8 | |
| 9 | namespace MediaWiki\Api; |
| 10 | |
| 11 | use MediaWiki\Deferred\LinksUpdate\ImageLinksTable; |
| 12 | use MediaWiki\Title\Title; |
| 13 | use Wikimedia\ParamValidator\ParamValidator; |
| 14 | use Wikimedia\ParamValidator\TypeDef\IntegerDef; |
| 15 | |
| 16 | /** |
| 17 | * This query adds an "<images>" subelement to all pages with the list of |
| 18 | * images embedded into those pages. |
| 19 | * |
| 20 | * @ingroup API |
| 21 | */ |
| 22 | class ApiQueryImages extends ApiQueryGeneratorBase { |
| 23 | |
| 24 | public function __construct( ApiQuery $query, string $moduleName ) { |
| 25 | parent::__construct( $query, $moduleName, 'im' ); |
| 26 | } |
| 27 | |
| 28 | public function execute() { |
| 29 | $this->run(); |
| 30 | } |
| 31 | |
| 32 | /** @inheritDoc */ |
| 33 | public function executeGenerator( $resultPageSet ) { |
| 34 | $this->run( $resultPageSet ); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * @param ApiPageSet|null $resultPageSet |
| 39 | */ |
| 40 | private function run( $resultPageSet = null ) { |
| 41 | $pages = $this->getPageSet()->getGoodPages(); |
| 42 | if ( $pages === [] ) { |
| 43 | return; // nothing to do |
| 44 | } |
| 45 | |
| 46 | $params = $this->extractRequestParams(); |
| 47 | |
| 48 | $this->addFields( [ |
| 49 | 'il_from', |
| 50 | 'il_to' |
| 51 | ] ); |
| 52 | $this->addTables( 'imagelinks' ); |
| 53 | $this->addWhereFld( 'il_from', array_keys( $pages ) ); |
| 54 | if ( $params['continue'] !== null ) { |
| 55 | $cont = $this->parseContinueParamOrDie( $params['continue'], [ 'int', 'string' ] ); |
| 56 | $op = $params['dir'] == 'descending' ? '<=' : '>='; |
| 57 | $this->addWhere( $this->getDB()->buildComparison( $op, [ |
| 58 | 'il_from' => $cont[0], |
| 59 | 'il_to' => $cont[1], |
| 60 | ] ) ); |
| 61 | } |
| 62 | |
| 63 | $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' ); |
| 64 | // Don't order by il_from if it's constant in the WHERE clause |
| 65 | if ( count( $pages ) === 1 ) { |
| 66 | $this->addOption( 'ORDER BY', 'il_to' . $sort ); |
| 67 | } else { |
| 68 | $this->addOption( 'ORDER BY', [ |
| 69 | 'il_from' . $sort, |
| 70 | 'il_to' . $sort |
| 71 | ] ); |
| 72 | } |
| 73 | $this->addOption( 'LIMIT', $params['limit'] + 1 ); |
| 74 | |
| 75 | if ( $params['images'] ) { |
| 76 | $images = []; |
| 77 | foreach ( $params['images'] as $img ) { |
| 78 | $title = Title::newFromText( $img ); |
| 79 | if ( !$title || $title->getNamespace() !== NS_FILE ) { |
| 80 | $this->addWarning( [ 'apiwarn-notfile', wfEscapeWikiText( $img ) ] ); |
| 81 | } else { |
| 82 | $images[] = $title->getDBkey(); |
| 83 | } |
| 84 | } |
| 85 | if ( !$images ) { |
| 86 | // No titles so no results |
| 87 | return; |
| 88 | } |
| 89 | $this->addWhereFld( 'il_to', $images ); |
| 90 | } |
| 91 | |
| 92 | $this->setVirtualDomain( ImageLinksTable::VIRTUAL_DOMAIN ); |
| 93 | $res = $this->select( __METHOD__ ); |
| 94 | $this->resetVirtualDomain(); |
| 95 | |
| 96 | if ( $resultPageSet === null ) { |
| 97 | $count = 0; |
| 98 | foreach ( $res as $row ) { |
| 99 | if ( ++$count > $params['limit'] ) { |
| 100 | // We've reached the one extra which shows that |
| 101 | // there are additional pages to be had. Stop here... |
| 102 | $this->setContinueEnumParameter( 'continue', $row->il_from . '|' . $row->il_to ); |
| 103 | break; |
| 104 | } |
| 105 | $vals = []; |
| 106 | ApiQueryBase::addTitleInfo( $vals, Title::makeTitle( NS_FILE, $row->il_to ) ); |
| 107 | $fit = $this->addPageSubItem( $row->il_from, $vals ); |
| 108 | if ( !$fit ) { |
| 109 | $this->setContinueEnumParameter( 'continue', $row->il_from . '|' . $row->il_to ); |
| 110 | break; |
| 111 | } |
| 112 | } |
| 113 | } else { |
| 114 | $titles = []; |
| 115 | $count = 0; |
| 116 | foreach ( $res as $row ) { |
| 117 | if ( ++$count > $params['limit'] ) { |
| 118 | // We've reached the one extra which shows that |
| 119 | // there are additional pages to be had. Stop here... |
| 120 | $this->setContinueEnumParameter( 'continue', $row->il_from . '|' . $row->il_to ); |
| 121 | break; |
| 122 | } |
| 123 | $titles[] = Title::makeTitle( NS_FILE, $row->il_to ); |
| 124 | } |
| 125 | $resultPageSet->populateFromTitles( $titles ); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | /** @inheritDoc */ |
| 130 | public function getCacheMode( $params ) { |
| 131 | return 'public'; |
| 132 | } |
| 133 | |
| 134 | /** @inheritDoc */ |
| 135 | public function getAllowedParams() { |
| 136 | return [ |
| 137 | 'limit' => [ |
| 138 | ParamValidator::PARAM_DEFAULT => 10, |
| 139 | ParamValidator::PARAM_TYPE => 'limit', |
| 140 | IntegerDef::PARAM_MIN => 1, |
| 141 | IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1, |
| 142 | IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2 |
| 143 | ], |
| 144 | 'continue' => [ |
| 145 | ApiBase::PARAM_HELP_MSG => 'api-help-param-continue', |
| 146 | ], |
| 147 | 'images' => [ |
| 148 | ParamValidator::PARAM_ISMULTI => true, |
| 149 | ], |
| 150 | 'dir' => [ |
| 151 | ParamValidator::PARAM_DEFAULT => 'ascending', |
| 152 | ParamValidator::PARAM_TYPE => [ |
| 153 | 'ascending', |
| 154 | 'descending' |
| 155 | ] |
| 156 | ], |
| 157 | ]; |
| 158 | } |
| 159 | |
| 160 | /** @inheritDoc */ |
| 161 | protected function getExamplesMessages() { |
| 162 | $title = Title::newMainPage()->getPrefixedText(); |
| 163 | $mp = rawurlencode( $title ); |
| 164 | |
| 165 | return [ |
| 166 | "action=query&prop=images&titles={$mp}" |
| 167 | => 'apihelp-query+images-example-simple', |
| 168 | "action=query&generator=images&titles={$mp}&prop=info" |
| 169 | => 'apihelp-query+images-example-generator', |
| 170 | ]; |
| 171 | } |
| 172 | |
| 173 | /** @inheritDoc */ |
| 174 | public function getHelpUrls() { |
| 175 | return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Images'; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | /** @deprecated class alias since 1.43 */ |
| 180 | class_alias( ApiQueryImages::class, 'ApiQueryImages' ); |