MediaWiki  1.29.1
ApiQueryLinks.php
Go to the documentation of this file.
1 <?php
33 
34  const LINKS = 'links';
35  const TEMPLATES = 'templates';
36 
37  private $table, $prefix, $helpUrl;
38 
39  public function __construct( ApiQuery $query, $moduleName ) {
40  switch ( $moduleName ) {
41  case self::LINKS:
42  $this->table = 'pagelinks';
43  $this->prefix = 'pl';
44  $this->titlesParam = 'titles';
45  $this->helpUrl = 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Links';
46  break;
47  case self::TEMPLATES:
48  $this->table = 'templatelinks';
49  $this->prefix = 'tl';
50  $this->titlesParam = 'templates';
51  $this->helpUrl = 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Templates';
52  break;
53  default:
54  ApiBase::dieDebug( __METHOD__, 'Unknown module name' );
55  }
56 
57  parent::__construct( $query, $moduleName, $this->prefix );
58  }
59 
60  public function execute() {
61  $this->run();
62  }
63 
64  public function getCacheMode( $params ) {
65  return 'public';
66  }
67 
68  public function executeGenerator( $resultPageSet ) {
69  $this->run( $resultPageSet );
70  }
71 
75  private function run( $resultPageSet = null ) {
76  if ( $this->getPageSet()->getGoodTitleCount() == 0 ) {
77  return; // nothing to do
78  }
79 
80  $params = $this->extractRequestParams();
81 
82  $this->addFields( [
83  'pl_from' => $this->prefix . '_from',
84  'pl_namespace' => $this->prefix . '_namespace',
85  'pl_title' => $this->prefix . '_title'
86  ] );
87 
88  $this->addTables( $this->table );
89  $this->addWhereFld( $this->prefix . '_from', array_keys( $this->getPageSet()->getGoodTitles() ) );
90  $this->addWhereFld( $this->prefix . '_namespace', $params['namespace'] );
91 
92  if ( !is_null( $params[$this->titlesParam] ) ) {
93  $lb = new LinkBatch;
94  foreach ( $params[$this->titlesParam] as $t ) {
96  if ( !$title ) {
97  $this->addWarning( [ 'apiwarn-invalidtitle', wfEscapeWikiText( $t ) ] );
98  } else {
99  $lb->addObj( $title );
100  }
101  }
102  $cond = $lb->constructSet( $this->prefix, $this->getDB() );
103  if ( $cond ) {
104  $this->addWhere( $cond );
105  }
106  }
107 
108  if ( !is_null( $params['continue'] ) ) {
109  $cont = explode( '|', $params['continue'] );
110  $this->dieContinueUsageIf( count( $cont ) != 3 );
111  $op = $params['dir'] == 'descending' ? '<' : '>';
112  $plfrom = intval( $cont[0] );
113  $plns = intval( $cont[1] );
114  $pltitle = $this->getDB()->addQuotes( $cont[2] );
115  $this->addWhere(
116  "{$this->prefix}_from $op $plfrom OR " .
117  "({$this->prefix}_from = $plfrom AND " .
118  "({$this->prefix}_namespace $op $plns OR " .
119  "({$this->prefix}_namespace = $plns AND " .
120  "{$this->prefix}_title $op= $pltitle)))"
121  );
122  }
123 
124  $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
125  // Here's some MySQL craziness going on: if you use WHERE foo='bar'
126  // and later ORDER BY foo MySQL doesn't notice the ORDER BY is pointless
127  // but instead goes and filesorts, because the index for foo was used
128  // already. To work around this, we drop constant fields in the WHERE
129  // clause from the ORDER BY clause
130  $order = [];
131  if ( count( $this->getPageSet()->getGoodTitles() ) != 1 ) {
132  $order[] = $this->prefix . '_from' . $sort;
133  }
134  if ( count( $params['namespace'] ) != 1 ) {
135  $order[] = $this->prefix . '_namespace' . $sort;
136  }
137 
138  $order[] = $this->prefix . '_title' . $sort;
139  $this->addOption( 'ORDER BY', $order );
140  $this->addOption( 'USE INDEX', $this->prefix . '_from' );
141  $this->addOption( 'LIMIT', $params['limit'] + 1 );
142 
143  $res = $this->select( __METHOD__ );
144 
145  if ( is_null( $resultPageSet ) ) {
146  $count = 0;
147  foreach ( $res as $row ) {
148  if ( ++$count > $params['limit'] ) {
149  // We've reached the one extra which shows that
150  // there are additional pages to be had. Stop here...
151  $this->setContinueEnumParameter( 'continue',
152  "{$row->pl_from}|{$row->pl_namespace}|{$row->pl_title}" );
153  break;
154  }
155  $vals = [];
156  ApiQueryBase::addTitleInfo( $vals, Title::makeTitle( $row->pl_namespace, $row->pl_title ) );
157  $fit = $this->addPageSubItem( $row->pl_from, $vals );
158  if ( !$fit ) {
159  $this->setContinueEnumParameter( 'continue',
160  "{$row->pl_from}|{$row->pl_namespace}|{$row->pl_title}" );
161  break;
162  }
163  }
164  } else {
165  $titles = [];
166  $count = 0;
167  foreach ( $res as $row ) {
168  if ( ++$count > $params['limit'] ) {
169  // We've reached the one extra which shows that
170  // there are additional pages to be had. Stop here...
171  $this->setContinueEnumParameter( 'continue',
172  "{$row->pl_from}|{$row->pl_namespace}|{$row->pl_title}" );
173  break;
174  }
175  $titles[] = Title::makeTitle( $row->pl_namespace, $row->pl_title );
176  }
177  $resultPageSet->populateFromTitles( $titles );
178  }
179  }
180 
181  public function getAllowedParams() {
182  return [
183  'namespace' => [
184  ApiBase::PARAM_TYPE => 'namespace',
185  ApiBase::PARAM_ISMULTI => true,
187  ],
188  'limit' => [
189  ApiBase::PARAM_DFLT => 10,
190  ApiBase::PARAM_TYPE => 'limit',
191  ApiBase::PARAM_MIN => 1,
194  ],
195  'continue' => [
196  ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
197  ],
198  $this->titlesParam => [
199  ApiBase::PARAM_ISMULTI => true,
200  ],
201  'dir' => [
202  ApiBase::PARAM_DFLT => 'ascending',
204  'ascending',
205  'descending'
206  ]
207  ],
208  ];
209  }
210 
211  protected function getExamplesMessages() {
212  $name = $this->getModuleName();
213  $path = $this->getModulePath();
214 
215  return [
216  "action=query&prop={$name}&titles=Main%20Page"
217  => "apihelp-{$path}-example-simple",
218  "action=query&generator={$name}&titles=Main%20Page&prop=info"
219  => "apihelp-{$path}-example-generator",
220  "action=query&prop={$name}&titles=Main%20Page&{$this->prefix}namespace=2|10"
221  => "apihelp-{$path}-example-namespaces",
222  ];
223  }
224 
225  public function getHelpUrls() {
226  return $this->helpUrl;
227  }
228 }
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:265
ApiQueryBase\addFields
addFields( $value)
Add a set of fields to select to the internal array.
Definition: ApiQueryBase.php:198
ApiQuery
This is the main query class.
Definition: ApiQuery.php:40
ApiBase\addWarning
addWarning( $msg, $code=null, $data=null)
Add a warning for this module.
Definition: ApiBase.php:1720
LinkBatch
Class representing a list of titles The execute() method checks them all for existence and adds them ...
Definition: LinkBatch.php:34
captcha-old.count
count
Definition: captcha-old.py:225
ApiBase\PARAM_HELP_MSG
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition: ApiBase.php:128
ApiBase\PARAM_TYPE
const PARAM_TYPE
(string|string[]) Either an array of allowed value strings, or a string type as described below.
Definition: ApiBase.php:91
$params
$params
Definition: styleTest.css.php:40
$res
$res
Definition: database.txt:21
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:304
ApiQueryBase\addOption
addOption( $name, $value=null)
Add an option such as LIMIT or USE INDEX.
Definition: ApiQueryBase.php:333
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
ApiQueryGeneratorBase\setContinueEnumParameter
setContinueEnumParameter( $paramName, $paramValue)
Overridden to set the generator param if in generator mode.
Definition: ApiQueryGeneratorBase.php:88
$query
null for the wiki Added should default to null in handler for backwards compatibility add a value to it if you want to add a cookie that have to vary cache options can modify $query
Definition: hooks.txt:1572
NS_SPECIAL
const NS_SPECIAL
Definition: Defines.php:51
ApiBase\PARAM_MIN
const PARAM_MIN
(integer) Lowest value allowed for the parameter, for PARAM_TYPE 'integer' and 'limit'.
Definition: ApiBase.php:103
ApiQueryGeneratorBase\getPageSet
getPageSet()
Get the PageSet object to work on.
Definition: ApiQueryGeneratorBase.php:62
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:934
$titles
linkcache txt The LinkCache class maintains a list of article titles and the information about whether or not the article exists in the database This is used to mark up links when displaying a page If the same link appears more than once on any page then it only has to be looked up once In most cases link lookups are done in batches with the LinkBatch class or the equivalent in so the link cache is mostly useful for short snippets of parsed and for links in the navigation areas of the skin The link cache was formerly used to track links used in a document for the purposes of updating the link tables This application is now deprecated To create a you can use the following $titles
Definition: linkcache.txt:17
table
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global then executing the whole list after the page is displayed We don t do anything smart like collating updates to the same table or such because the list is almost always going to have just one item on if so it s not worth the trouble Since there is a job queue in the jobs table
Definition: deferred.txt:11
ApiBase\getModulePath
getModulePath()
Get the path to this module.
Definition: ApiBase.php:554
ApiBase\LIMIT_BIG1
const LIMIT_BIG1
Fast query, standard limit.
Definition: ApiBase.php:203
ApiQueryBase\getDB
getDB()
Get the Query database connection (read-only)
Definition: ApiQueryBase.php:111
ApiBase\PARAM_MAX
const PARAM_MAX
(integer) Max value allowed for the parameter, for PARAM_TYPE 'integer' and 'limit'.
Definition: ApiBase.php:94
ApiQueryBase\addTables
addTables( $tables, $alias=null)
Add a set of tables to the internal array.
Definition: ApiQueryBase.php:164
ApiQueryBase\select
select( $method, $extraQuery=[], array &$hookData=null)
Execute a SELECT query based on the values in the internal arrays.
Definition: ApiQueryBase.php:358
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:514
ApiBase\PARAM_EXTRA_NAMESPACES
const PARAM_EXTRA_NAMESPACES
(int[]) When PARAM_TYPE is 'namespace', include these as additional possible values.
Definition: ApiBase.php:189
$sort
$sort
Definition: profileinfo.php:323
ApiBase\extractRequestParams
extractRequestParams( $parseLimit=true)
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:718
NS_MEDIA
const NS_MEDIA
Definition: Defines.php:50
ApiBase\dieContinueUsageIf
dieContinueUsageIf( $condition)
Die with the 'badcontinue' error.
Definition: ApiBase.php:1950
wfEscapeWikiText
wfEscapeWikiText( $text)
Escapes the given text so that it may be output using addWikiText() without any linking,...
Definition: GlobalFunctions.php:1657
ApiQueryBase\addWhereFld
addWhereFld( $field, $value)
Equivalent to addWhere(array($field => $value))
Definition: ApiQueryBase.php:266
ApiQueryGeneratorBase
Definition: ApiQueryGeneratorBase.php:30
ApiBase\LIMIT_BIG2
const LIMIT_BIG2
Fast query, apihighlimits limit.
Definition: ApiBase.php:205
$path
$path
Definition: NoLocalSettings.php:26
ApiBase\PARAM_DFLT
const PARAM_DFLT
(null|boolean|integer|string) Default value of the parameter.
Definition: ApiBase.php:52
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
ApiBase\getModuleName
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:490
ApiBase\PARAM_ISMULTI
const PARAM_ISMULTI
(boolean) Accept multiple pipe-separated values for this parameter (e.g.
Definition: ApiBase.php:55
ApiBase\PARAM_MAX2
const PARAM_MAX2
(integer) Max value allowed for the parameter for users with the apihighlimits right,...
Definition: ApiBase.php:100
ApiQueryBase\addWhere
addWhere( $value)
Add a set of WHERE clauses to the internal array.
Definition: ApiQueryBase.php:233
$t
$t
Definition: testCompression.php:67
ApiQueryBase\addPageSubItem
addPageSubItem( $pageId, $item, $elemname=null)
Same as addPageSubItems(), but one element of $data at a time.
Definition: ApiQueryBase.php:514
ApiBase\dieDebug
static dieDebug( $method, $message)
Internal code errors should be reported with this method.
Definition: ApiBase.php:1962
ApiQueryBase\addTitleInfo
static addTitleInfo(&$arr, $title, $prefix='')
Add information (title and namespace) about a Title object to a result array.
Definition: ApiQueryBase.php:486