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