39 private const LINKS =
'links';
40 private const TEMPLATES =
'templates';
42 private string $table;
43 private string $prefix;
44 private string $titlesParam;
45 private string $helpUrl;
56 switch ( $moduleName ) {
58 $this->table =
'pagelinks';
60 $this->titlesParam =
'titles';
61 $this->helpUrl =
'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Links';
64 $this->table =
'templatelinks';
66 $this->titlesParam =
'templates';
67 $this->helpUrl =
'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Templates';
73 parent::__construct( $query, $moduleName, $this->prefix );
74 $this->linkBatchFactory = $linkBatchFactory;
75 $this->linksMigration = $linksMigration;
87 $this->
run( $resultPageSet );
93 private function run( $resultPageSet =
null ) {
95 if ( $pages === [] ) {
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'] );
108 $nsField = $this->prefix .
'_namespace';
109 $titleField = $this->prefix .
'_title';
113 'pl_from' => $this->prefix .
'_from',
114 'pl_namespace' => $nsField,
115 'pl_title' => $titleField,
118 $this->
addWhereFld( $this->prefix .
'_from', array_keys( $pages ) );
122 if (
$params[$this->titlesParam] ) {
124 $filterNS =
$params[
'namespace'] ? array_fill_keys(
$params[
'namespace'],
true ) : false;
126 $lb = $this->linkBatchFactory->newLinkBatch();
127 foreach (
$params[$this->titlesParam] as $t ) {
128 $title = Title::newFromText( $t );
129 if ( !$title || $title->isExternal() ) {
131 } elseif ( !$filterNS || isset( $filterNS[$title->getNamespace()] ) ) {
132 $lb->addObj( $title );
135 if ( $lb->isEmpty() ) {
139 $cond = $lb->constructSet( $this->prefix, $this->
getDB() );
141 $multiNS = count( $lb->data ) !== 1;
142 $multiTitle = count( array_merge( ...$lb->data ) ) !== 1;
143 } elseif (
$params[
'namespace'] ) {
145 $multiNS =
$params[
'namespace'] ===
null || count(
$params[
'namespace'] ) !== 1;
148 if (
$params[
'continue'] !==
null ) {
149 $db = $this->
getDB();
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],
159 $sort = (
$params[
'dir'] ==
'descending' ?
' DESC' :
'' );
166 if ( count( $pages ) !== 1 ) {
167 $order[] = $this->prefix .
'_from' . $sort;
170 $order[] = $nsField . $sort;
173 $order[] = $titleField . $sort;
180 $res = $this->
select( __METHOD__ );
182 if ( $resultPageSet ===
null ) {
186 foreach ( $res as $row ) {
187 if ( ++$count >
$params[
'limit'] ) {
191 "{$row->pl_from}|{$row->pl_namespace}|{$row->pl_title}" );
199 "{$row->pl_from}|{$row->pl_namespace}|{$row->pl_title}" );
206 foreach ( $res as $row ) {
207 if ( ++$count >
$params[
'limit'] ) {
211 "{$row->pl_from}|{$row->pl_namespace}|{$row->pl_title}" );
214 $titles[] = Title::makeTitle( $row->pl_namespace, $row->pl_title );
216 $resultPageSet->populateFromTitles( $titles );
223 ParamValidator::PARAM_TYPE =>
'namespace',
224 ParamValidator::PARAM_ISMULTI =>
true,
228 ParamValidator::PARAM_DEFAULT => 10,
229 ParamValidator::PARAM_TYPE =>
'limit',
230 IntegerDef::PARAM_MIN => 1,
237 $this->titlesParam => [
238 ParamValidator::PARAM_ISMULTI =>
true,
241 ParamValidator::PARAM_DEFAULT =>
'ascending',
242 ParamValidator::PARAM_TYPE => [
253 $title = Title::newMainPage()->getPrefixedText();
254 $mp = rawurlencode( $title );
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",
267 return $this->helpUrl;
272class_alias( ApiQueryLinks::class,
'ApiQueryLinks' );
wfEscapeWikiText( $input)
Escapes the given text so that it may be output using addWikiText() without any linking,...
array $params
The job parameters.