27 private const LINKS =
'links';
28 private const TEMPLATES =
'templates';
30 private string $table;
31 private string $prefix;
32 private string $titlesParam;
33 private string $helpUrl;
34 private ?
string $virtualdomain;
45 switch ( $moduleName ) {
47 $this->table =
'pagelinks';
49 $this->titlesParam =
'titles';
50 $this->helpUrl =
'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Links';
51 $this->virtualdomain = PageLinksTable::VIRTUAL_DOMAIN;
54 $this->table =
'templatelinks';
56 $this->titlesParam =
'templates';
57 $this->helpUrl =
'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Templates';
58 $this->virtualdomain = TemplateLinksTable::VIRTUAL_DOMAIN;
64 parent::__construct( $query, $moduleName, $this->prefix );
65 $this->linkBatchFactory = $linkBatchFactory;
66 $this->linksMigration = $linksMigration;
80 $this->run( $resultPageSet );
86 private function run( $resultPageSet =
null ) {
88 if ( $pages === [] ) {
96 if ( isset( $this->linksMigration::$mapping[$this->table] ) ) {
97 [ $nsField, $titleField ] = $this->linksMigration->getTitleFields( $this->table );
98 $queryInfo = $this->linksMigration->getQueryInfo( $this->table );
103 $nsField = $this->prefix .
'_namespace';
104 $titleField = $this->prefix .
'_title';
108 'pl_from' => $this->prefix .
'_from',
109 'pl_namespace' => $nsField,
110 'pl_title' => $titleField,
113 $this->
addWhereFld( $this->prefix .
'_from', array_keys( $pages ) );
117 if ( $params[$this->titlesParam] ) {
119 $filterNS =
$params[
'namespace'] ? array_fill_keys( $params[
'namespace'],
true ) : false;
121 $lb = $this->linkBatchFactory->newLinkBatch();
122 foreach ( $params[$this->titlesParam] as $t ) {
123 $title = Title::newFromText( $t );
124 if ( !$title || $title->isExternal() ) {
126 } elseif ( !$filterNS || isset( $filterNS[$title->getNamespace()] ) ) {
127 $lb->addObj( $title );
130 if ( $lb->isEmpty() ) {
134 $cond = $lb->constructSet( $this->prefix, $this->
getDB() );
136 $multiNS = count( $lb->data ) !== 1;
137 $multiTitle = count( array_merge( ...$lb->data ) ) !== 1;
138 } elseif ( $params[
'namespace'] ) {
139 $this->
addWhereFld( $nsField, $params[
'namespace'] );
140 $multiNS =
$params[
'namespace'] ===
null || count( $params[
'namespace'] ) !== 1;
143 if ( $params[
'continue'] !==
null ) {
145 $op =
$params[
'dir'] ==
'descending' ?
'<=' :
'>=';
147 "{$this->prefix}_from" => $cont[0],
148 $nsField => $cont[1],
149 $titleField => $cont[2],
153 $sort = (
$params[
'dir'] ==
'descending' ?
' DESC' :
'' );
160 if ( count( $pages ) !== 1 ) {
161 $order[] = $this->prefix .
'_from' . $sort;
164 $order[] = $nsField . $sort;
167 $order[] = $titleField . $sort;
172 $this->
addOption(
'LIMIT', $params[
'limit'] + 1 );
174 $res = $this->
select( __METHOD__ );
176 if ( $resultPageSet ===
null ) {
180 foreach ( $res as $row ) {
181 if ( ++$count > $params[
'limit'] ) {
185 "{$row->pl_from}|{$row->pl_namespace}|{$row->pl_title}" );
193 "{$row->pl_from}|{$row->pl_namespace}|{$row->pl_title}" );
200 foreach ( $res as $row ) {
201 if ( ++$count > $params[
'limit'] ) {
205 "{$row->pl_from}|{$row->pl_namespace}|{$row->pl_title}" );
210 $resultPageSet->populateFromTitles( $titles );
218 ParamValidator::PARAM_TYPE =>
'namespace',
219 ParamValidator::PARAM_ISMULTI =>
true,
223 ParamValidator::PARAM_DEFAULT => 10,
224 ParamValidator::PARAM_TYPE =>
'limit',
225 IntegerDef::PARAM_MIN => 1,
232 $this->titlesParam => [
233 ParamValidator::PARAM_ISMULTI =>
true,
236 ParamValidator::PARAM_DEFAULT =>
'ascending',
237 ParamValidator::PARAM_TYPE => [
249 $title = Title::newMainPage()->getPrefixedText();
250 $mp = rawurlencode( $title );
253 "action=query&prop={$name}&titles={$mp}"
254 =>
"apihelp-{$path}-example-simple",
255 "action=query&generator={$name}&titles={$mp}&prop=info"
256 =>
"apihelp-{$path}-example-generator",
257 "action=query&prop={$name}&titles={$mp}&{$this->prefix}namespace=2|10"
258 =>
"apihelp-{$path}-example-namespaces",
264 return $this->helpUrl;
269class_alias( ApiQueryLinks::class,
'ApiQueryLinks' );
wfEscapeWikiText( $input)
Escapes the given text so that it may be output using addWikiText() without any linking,...
Factory for LinkBatch objects to batch query page metadata.