| 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\Cache\LinkBatchFactory; |
| 26 |
use MediaWiki\Linker\LinksMigration; |
| 27 |
use MediaWiki\ParamValidator\TypeDef\NamespaceDef; |
| 28 |
use MediaWiki\Title\Title; |
| 29 |
use Wikimedia\ParamValidator\ParamValidator; |
| 30 |
use Wikimedia\ParamValidator\TypeDef\IntegerDef; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
class ApiQueryLinks extends ApiQueryGeneratorBase { |
| 38 |
|
| 39 |
private const LINKS = 'links'; |
| 40 |
private const TEMPLATES = 'templates'; |
| 41 |
|
| 42 |
private string $table; |
| 43 |
private string $prefix; |
| 44 |
private string $titlesParam; |
| 45 |
private string $helpUrl; |
| 46 |
|
| 47 |
private LinkBatchFactory $linkBatchFactory; |
| 48 |
private LinksMigration $linksMigration; |
| 49 |
|
| 50 |
public function __construct( |
| 51 |
ApiQuery $query, |
| 52 |
string $moduleName, |
| 53 |
LinkBatchFactory $linkBatchFactory, |
| 54 |
LinksMigration $linksMigration |
| 55 |
) { |
| 56 |
switch ( $moduleName ) { |
| 57 |
case self::LINKS: |
| 58 |
$this->table = 'pagelinks'; |
| 59 |
$this->prefix = 'pl'; |
| 60 |
$this->titlesParam = 'titles'; |
| 61 |
$this->helpUrl = 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Links'; |
| 62 |
break; |
| 63 |
case self::TEMPLATES: |
| 64 |
$this->table = 'templatelinks'; |
| 65 |
$this->prefix = 'tl'; |
| 66 |
$this->titlesParam = 'templates'; |
| 67 |
$this->helpUrl = 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Templates'; |
| 68 |
break; |
| 69 |
default: |
| 70 |
ApiBase::dieDebug( __METHOD__, 'Unknown module name' ); |
| 71 |
} |
| 72 |
|
| 73 |
parent::__construct( $query, $moduleName, $this->prefix ); |
| 74 |
$this->linkBatchFactory = $linkBatchFactory; |
| 75 |
$this->linksMigration = $linksMigration; |
| 76 |
} |
| 77 |
|
| 78 |
public function execute() { |
| 79 |
$this->run(); |
| 80 |
} |
| 81 |
|
| 82 |
public function getCacheMode( $params ) { |
| 83 |
return 'public'; |
| 84 |
} |
| 85 |
|
| 86 |
public function executeGenerator( $resultPageSet ) { |
| 87 |
$this->run( $resultPageSet ); |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
private function run( $resultPageSet = null ) { |
| 94 |
$pages = $this->getPageSet()->getGoodPages(); |
| 95 |
if ( $pages === [] ) { |
| 96 |
return; |
| 97 |
} |
| 98 |
|
| 99 |
$params = $this->extractRequestParams(); |
| 100 |
|
| 101 |
if ( isset( $this->linksMigration::$mapping[$this->table] ) ) { |
| 102 |
[ $nsField, $titleField ] = $this->linksMigration->getTitleFields( $this->table ); |
| 103 |
$queryInfo = $this->linksMigration->getQueryInfo( $this->table ); |
| 104 |
$this->addTables( $queryInfo['tables'] ); |
| 105 |
$this->addJoinConds( $queryInfo['joins'] ); |
| 106 |
} else { |
| 107 |
$this->addTables( $this->table ); |
| 108 |
$nsField = $this->prefix . '_namespace'; |
| 109 |
$titleField = $this->prefix . '_title'; |
| 110 |
} |
| 111 |
|
| 112 |
$this->addFields( [ |
| 113 |
'pl_from' => $this->prefix . '_from', |
| 114 |
'pl_namespace' => $nsField, |
| 115 |
'pl_title' => $titleField, |
| 116 |
] ); |
| 117 |
|
| 118 |
$this->addWhereFld( $this->prefix . '_from', array_keys( $pages ) ); |
| 119 |
|
| 120 |
$multiNS = true; |
| 121 |
$multiTitle = true; |
| 122 |
if ( $params[$this->titlesParam] ) { |
| 123 |
|
| 124 |
$filterNS = $params['namespace'] ? array_fill_keys( $params['namespace'], true ) : false; |
| 125 |
|
| 126 |
$lb = $this->linkBatchFactory->newLinkBatch(); |
| 127 |
foreach ( $params[$this->titlesParam] as $t ) { |
| 128 |
$title = Title::newFromText( $t ); |
| 129 |
if ( !$title || $title->isExternal() ) { |
| 130 |
$this->addWarning( [ 'apiwarn-invalidtitle', wfEscapeWikiText( $t ) ] ); |
| 131 |
} elseif ( !$filterNS || isset( $filterNS[$title->getNamespace()] ) ) { |
| 132 |
$lb->addObj( $title ); |
| 133 |
} |
| 134 |
} |
| 135 |
if ( $lb->isEmpty() ) { |
| 136 |
|
| 137 |
return; |
| 138 |
} |
| 139 |
$cond = $lb->constructSet( $this->prefix, $this->getDB() ); |
| 140 |
$this->addWhere( $cond ); |
| 141 |
$multiNS = count( $lb->data ) !== 1; |
| 142 |
$multiTitle = count( array_merge( ...$lb->data ) ) !== 1; |
| 143 |
} elseif ( $params['namespace'] ) { |
| 144 |
$this->addWhereFld( $nsField, $params['namespace'] ); |
| 145 |
$multiNS = $params['namespace'] === null || count( $params['namespace'] ) !== 1; |
| 146 |
} |
| 147 |
|
| 148 |
if ( $params['continue'] !== null ) { |
| 149 |
$db = $this->getDB(); |
| 150 |
$cont = $this->parseContinueParamOrDie( $params['continue'], [ 'int', 'int', 'string' ] ); |
| 151 |
$op = $params['dir'] == 'descending' ? '<=' : '>='; |
| 152 |
$this->addWhere( $db->buildComparison( $op, [ |
| 153 |
"{$this->prefix}_from" => $cont[0], |
| 154 |
$nsField => $cont[1], |
| 155 |
$titleField => $cont[2], |
| 156 |
] ) ); |
| 157 |
} |
| 158 |
|
| 159 |
$sort = ( $params['dir'] == 'descending' ? ' DESC' : '' ); |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
$order = []; |
| 166 |
if ( count( $pages ) !== 1 ) { |
| 167 |
$order[] = $this->prefix . '_from' . $sort; |
| 168 |
} |
| 169 |
if ( $multiNS ) { |
| 170 |
$order[] = $nsField . $sort; |
| 171 |
} |
| 172 |
if ( $multiTitle ) { |
| 173 |
$order[] = $titleField . $sort; |
| 174 |
} |
| 175 |
if ( $order ) { |
| 176 |
$this->addOption( 'ORDER BY', $order ); |
| 177 |
} |
| 178 |
$this->addOption( 'LIMIT', $params['limit'] + 1 ); |
| 179 |
|
| 180 |
$res = $this->select( __METHOD__ ); |
| 181 |
|
| 182 |
if ( $resultPageSet === null ) { |
| 183 |
$this->executeGenderCacheFromResultWrapper( $res, __METHOD__, 'pl' ); |
| 184 |
|
| 185 |
$count = 0; |
| 186 |
foreach ( $res as $row ) { |
| 187 |
if ( ++$count > $params['limit'] ) { |
| 188 |
|
| 189 |
|
| 190 |
$this->setContinueEnumParameter( 'continue', |
| 191 |
"{$row->pl_from}|{$row->pl_namespace}|{$row->pl_title}" ); |
| 192 |
break; |
| 193 |
} |
| 194 |
$vals = []; |
| 195 |
ApiQueryBase::addTitleInfo( $vals, Title::makeTitle( $row->pl_namespace, $row->pl_title ) ); |
| 196 |
$fit = $this->addPageSubItem( $row->pl_from, $vals ); |
| 197 |
if ( !$fit ) { |
| 198 |
$this->setContinueEnumParameter( 'continue', |
| 199 |
"{$row->pl_from}|{$row->pl_namespace}|{$row->pl_title}" ); |
| 200 |
break; |
| 201 |
} |
| 202 |
} |
| 203 |
} else { |
| 204 |
$titles = []; |
| 205 |
$count = 0; |
| 206 |
foreach ( $res as $row ) { |
| 207 |
if ( ++$count > $params['limit'] ) { |
| 208 |
|
| 209 |
|
| 210 |
$this->setContinueEnumParameter( 'continue', |
| 211 |
"{$row->pl_from}|{$row->pl_namespace}|{$row->pl_title}" ); |
| 212 |
break; |
| 213 |
} |
| 214 |
$titles[] = Title::makeTitle( $row->pl_namespace, $row->pl_title ); |
| 215 |
} |
| 216 |
$resultPageSet->populateFromTitles( $titles ); |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
public function getAllowedParams() { |
| 221 |
return [ |
| 222 |
'namespace' => [ |
| 223 |
ParamValidator::PARAM_TYPE => 'namespace', |
| 224 |
ParamValidator::PARAM_ISMULTI => true, |
| 225 |
NamespaceDef::PARAM_EXTRA_NAMESPACES => [ NS_MEDIA, NS_SPECIAL ], |
| 226 |
], |
| 227 |
'limit' => [ |
| 228 |
ParamValidator::PARAM_DEFAULT => 10, |
| 229 |
ParamValidator::PARAM_TYPE => 'limit', |
| 230 |
IntegerDef::PARAM_MIN => 1, |
| 231 |
IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1, |
| 232 |
IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2 |
| 233 |
], |
| 234 |
'continue' => [ |
| 235 |
ApiBase::PARAM_HELP_MSG => 'api-help-param-continue', |
| 236 |
], |
| 237 |
$this->titlesParam => [ |
| 238 |
ParamValidator::PARAM_ISMULTI => true, |
| 239 |
], |
| 240 |
'dir' => [ |
| 241 |
ParamValidator::PARAM_DEFAULT => 'ascending', |
| 242 |
ParamValidator::PARAM_TYPE => [ |
| 243 |
'ascending', |
| 244 |
'descending' |
| 245 |
] |
| 246 |
], |
| 247 |
]; |
| 248 |
} |
| 249 |
|
| 250 |
protected function getExamplesMessages() { |
| 251 |
$name = $this->getModuleName(); |
| 252 |
$path = $this->getModulePath(); |
| 253 |
$title = Title::newMainPage()->getPrefixedText(); |
| 254 |
$mp = rawurlencode( $title ); |
| 255 |
|
| 256 |
return [ |
| 257 |
"action=query&prop={$name}&titles={$mp}" |
| 258 |
=> "apihelp-{$path}-example-simple", |
| 259 |
"action=query&generator={$name}&titles={$mp}&prop=info" |
| 260 |
=> "apihelp-{$path}-example-generator", |
| 261 |
"action=query&prop={$name}&titles={$mp}&{$this->prefix}namespace=2|10" |
| 262 |
=> "apihelp-{$path}-example-namespaces", |
| 263 |
]; |
| 264 |
} |
| 265 |
|
| 266 |
public function getHelpUrls() { |
| 267 |
return $this->helpUrl; |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
class_alias( ApiQueryLinks::class, 'ApiQueryLinks' ); |