MediaWiki  1.23.13
ApiQueryLinks.php
Go to the documentation of this file.
1 <?php
33 
34  const LINKS = 'links';
35  const TEMPLATES = 'templates';
36 
38 
39  public function __construct( $query, $moduleName ) {
40  switch ( $moduleName ) {
41  case self::LINKS:
42  $this->table = 'pagelinks';
43  $this->prefix = 'pl';
44  $this->description = 'link';
45  $this->titlesParam = 'titles';
46  $this->titlesParamDescription = 'Only list links to these titles. Useful ' .
47  'for checking whether a certain page links to a certain title.';
48  $this->helpUrl = 'https://www.mediawiki.org/wiki/API:Properties#links_.2F_pl';
49  break;
50  case self::TEMPLATES:
51  $this->table = 'templatelinks';
52  $this->prefix = 'tl';
53  $this->description = 'template';
54  $this->titlesParam = 'templates';
55  $this->titlesParamDescription = 'Only list these templates. Useful ' .
56  'for checking whether a certain page uses a certain template.';
57  $this->helpUrl = 'https://www.mediawiki.org/wiki/API:Properties#templates_.2F_tl';
58  break;
59  default:
60  ApiBase::dieDebug( __METHOD__, 'Unknown module name' );
61  }
62 
63  parent::__construct( $query, $moduleName, $this->prefix );
64  }
65 
66  public function execute() {
67  $this->run();
68  }
69 
70  public function getCacheMode( $params ) {
71  return 'public';
72  }
73 
74  public function executeGenerator( $resultPageSet ) {
75  $this->run( $resultPageSet );
76  }
77 
81  private function run( $resultPageSet = null ) {
82  if ( $this->getPageSet()->getGoodTitleCount() == 0 ) {
83  return; // nothing to do
84  }
85 
86  $params = $this->extractRequestParams();
87 
88  $this->addFields( array(
89  'pl_from' => $this->prefix . '_from',
90  'pl_namespace' => $this->prefix . '_namespace',
91  'pl_title' => $this->prefix . '_title'
92  ) );
93 
94  $this->addTables( $this->table );
95  $this->addWhereFld( $this->prefix . '_from', array_keys( $this->getPageSet()->getGoodTitles() ) );
96  $this->addWhereFld( $this->prefix . '_namespace', $params['namespace'] );
97 
98  if ( !is_null( $params[$this->titlesParam] ) ) {
99  $lb = new LinkBatch;
100  foreach ( $params[$this->titlesParam] as $t ) {
102  if ( !$title ) {
103  $this->setWarning( "\"$t\" is not a valid title" );
104  } else {
105  $lb->addObj( $title );
106  }
107  }
108  $cond = $lb->constructSet( $this->prefix, $this->getDB() );
109  if ( $cond ) {
110  $this->addWhere( $cond );
111  }
112  }
113 
114  if ( !is_null( $params['continue'] ) ) {
115  $cont = explode( '|', $params['continue'] );
116  $this->dieContinueUsageIf( count( $cont ) != 3 );
117  $op = $params['dir'] == 'descending' ? '<' : '>';
118  $plfrom = intval( $cont[0] );
119  $plns = intval( $cont[1] );
120  $pltitle = $this->getDB()->addQuotes( $cont[2] );
121  $this->addWhere(
122  "{$this->prefix}_from $op $plfrom OR " .
123  "({$this->prefix}_from = $plfrom AND " .
124  "({$this->prefix}_namespace $op $plns OR " .
125  "({$this->prefix}_namespace = $plns AND " .
126  "{$this->prefix}_title $op= $pltitle)))"
127  );
128  }
129 
130  $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
131  // Here's some MySQL craziness going on: if you use WHERE foo='bar'
132  // and later ORDER BY foo MySQL doesn't notice the ORDER BY is pointless
133  // but instead goes and filesorts, because the index for foo was used
134  // already. To work around this, we drop constant fields in the WHERE
135  // clause from the ORDER BY clause
136  $order = array();
137  if ( count( $this->getPageSet()->getGoodTitles() ) != 1 ) {
138  $order[] = $this->prefix . '_from' . $sort;
139  }
140  if ( count( $params['namespace'] ) != 1 ) {
141  $order[] = $this->prefix . '_namespace' . $sort;
142  }
143 
144  $order[] = $this->prefix . '_title' . $sort;
145  $this->addOption( 'ORDER BY', $order );
146  $this->addOption( 'USE INDEX', $this->prefix . '_from' );
147  $this->addOption( 'LIMIT', $params['limit'] + 1 );
148 
149  $res = $this->select( __METHOD__ );
150 
151  if ( is_null( $resultPageSet ) ) {
152  $count = 0;
153  foreach ( $res as $row ) {
154  if ( ++$count > $params['limit'] ) {
155  // We've reached the one extra which shows that
156  // there are additional pages to be had. Stop here...
157  $this->setContinueEnumParameter( 'continue',
158  "{$row->pl_from}|{$row->pl_namespace}|{$row->pl_title}" );
159  break;
160  }
161  $vals = array();
162  ApiQueryBase::addTitleInfo( $vals, Title::makeTitle( $row->pl_namespace, $row->pl_title ) );
163  $fit = $this->addPageSubItem( $row->pl_from, $vals );
164  if ( !$fit ) {
165  $this->setContinueEnumParameter( 'continue',
166  "{$row->pl_from}|{$row->pl_namespace}|{$row->pl_title}" );
167  break;
168  }
169  }
170  } else {
171  $titles = array();
172  $count = 0;
173  foreach ( $res as $row ) {
174  if ( ++$count > $params['limit'] ) {
175  // We've reached the one extra which shows that
176  // there are additional pages to be had. Stop here...
177  $this->setContinueEnumParameter( 'continue',
178  "{$row->pl_from}|{$row->pl_namespace}|{$row->pl_title}" );
179  break;
180  }
181  $titles[] = Title::makeTitle( $row->pl_namespace, $row->pl_title );
182  }
183  $resultPageSet->populateFromTitles( $titles );
184  }
185  }
186 
187  public function getAllowedParams() {
188  return array(
189  'namespace' => array(
190  ApiBase::PARAM_TYPE => 'namespace',
191  ApiBase::PARAM_ISMULTI => true
192  ),
193  'limit' => array(
194  ApiBase::PARAM_DFLT => 10,
195  ApiBase::PARAM_TYPE => 'limit',
196  ApiBase::PARAM_MIN => 1,
199  ),
200  'continue' => null,
201  $this->titlesParam => array(
202  ApiBase::PARAM_ISMULTI => true,
203  ),
204  'dir' => array(
205  ApiBase::PARAM_DFLT => 'ascending',
207  'ascending',
208  'descending'
209  )
210  ),
211  );
212  }
213 
214  public function getParamDescription() {
215  $desc = $this->description;
216 
217  return array(
218  'namespace' => "Show {$desc}s in this namespace(s) only",
219  'limit' => "How many {$desc}s to return",
220  'continue' => 'When more results are available, use this to continue',
221  $this->titlesParam => $this->titlesParamDescription,
222  'dir' => 'The direction in which to list',
223  );
224  }
225 
226  public function getResultProperties() {
227  return array(
228  '' => array(
229  'ns' => 'namespace',
230  'title' => 'string'
231  )
232  );
233  }
234 
235  public function getDescription() {
236  return "Returns all {$this->description}s from the given page(s).";
237  }
238 
239  public function getExamples() {
240  $desc = $this->description;
241  $name = $this->getModuleName();
242 
243  return array(
244  "api.php?action=query&prop={$name}&titles=Main%20Page" => "Get {$desc}s from the [[Main Page]]",
245  "api.php?action=query&generator={$name}&titles=Main%20Page&prop=info"
246  => "Get information about the {$desc} pages in the [[Main Page]]",
247  "api.php?action=query&prop={$name}&titles=Main%20Page&{$this->prefix}namespace=2|10"
248  => "Get {$desc}s from the Main Page in the User and Template namespaces",
249  );
250  }
251 
252  public function getHelpUrls() {
253  return $this->helpUrl;
254  }
255 }
Title\makeTitle
static & makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:398
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:189
ApiQueryBase\addFields
addFields( $value)
Add a set of fields to select to the internal array.
Definition: ApiQueryBase.php:117
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
LinkBatch
Class representing a list of titles The execute() method checks them all for existence and adds them ...
Definition: LinkBatch.php:30
ApiBase\PARAM_TYPE
const PARAM_TYPE
Definition: ApiBase.php:50
ApiQueryBase\select
select( $method, $extraQuery=array())
Execute a SELECT query based on the values in the internal arrays.
Definition: ApiQueryBase.php:274
$params
$params
Definition: styleTest.css.php:40
ApiQueryBase\addOption
addOption( $name, $value=null)
Add an option such as LIMIT or USE INDEX.
Definition: ApiQueryBase.php:252
ApiQueryGeneratorBase\setContinueEnumParameter
setContinueEnumParameter( $paramName, $paramValue)
Overrides base in case of generator & smart continue to notify ApiQueryMain instead of adding them to...
Definition: ApiQueryBase.php:676
ApiBase\PARAM_MIN
const PARAM_MIN
Definition: ApiBase.php:56
$lb
if( $wgAPIRequestLog) $lb
Definition: api.php:126
ApiQueryGeneratorBase\getPageSet
getPageSet()
Get the PageSet object to work on.
Definition: ApiQueryBase.php:649
$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\LIMIT_BIG1
const LIMIT_BIG1
Definition: ApiBase.php:78
ApiQueryBase\getDB
getDB()
Get the Query database connection (read-only)
Definition: ApiQueryBase.php:417
ApiBase\PARAM_MAX
const PARAM_MAX
Definition: ApiBase.php:52
ApiQueryBase\addTables
addTables( $tables, $alias=null)
Add a set of tables to the internal array.
Definition: ApiQueryBase.php:82
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
$sort
$sort
Definition: profileinfo.php:301
ApiBase\extractRequestParams
extractRequestParams( $parseLimit=true)
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:687
$title
presenting them properly to the user as errors is done by the caller $title
Definition: hooks.txt:1324
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:336
ApiBase\dieContinueUsageIf
dieContinueUsageIf( $condition)
Die with the $prefix.
Definition: ApiBase.php:1969
ApiQueryBase\addWhereFld
addWhereFld( $field, $value)
Equivalent to addWhere(array($field => $value))
Definition: ApiQueryBase.php:185
$count
$count
Definition: UtfNormalTest2.php:96
ApiBase\setWarning
setWarning( $warning)
Set warning section for this module.
Definition: ApiBase.php:245
ApiQueryGeneratorBase
Definition: ApiQueryBase.php:626
ApiBase\LIMIT_BIG2
const LIMIT_BIG2
Definition: ApiBase.php:79
ApiBase\PARAM_DFLT
const PARAM_DFLT
Definition: ApiBase.php:46
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:148
ApiBase\PARAM_ISMULTI
const PARAM_ISMULTI
Definition: ApiBase.php:48
ApiBase\PARAM_MAX2
const PARAM_MAX2
Definition: ApiBase.php:54
ApiQueryBase\addWhere
addWhere( $value)
Add a set of WHERE clauses to the internal array.
Definition: ApiQueryBase.php:152
$t
$t
Definition: testCompression.php:65
$query
return true to allow those checks to and false if checking is done use this to change the tables headers temp or archived zone change it to an object instance and return false override the list derivative used the name of the old file when set the default code will be skipped add a value to it if you want to add a cookie that have to vary cache options can modify $query
Definition: hooks.txt:1105
$res
$res
Definition: database.txt:21
ApiQueryBase\addPageSubItem
addPageSubItem( $pageId, $item, $elemname=null)
Same as addPageSubItems(), but one element of $data at a time.
Definition: ApiQueryBase.php:383
ApiBase\dieDebug
static dieDebug( $method, $message)
Internal code errors should be reported with this method.
Definition: ApiBase.php:2010
ApiQueryBase\addTitleInfo
static addTitleInfo(&$arr, $title, $prefix='')
Add information (title and namespace) about a Title object to a result array.
Definition: ApiQueryBase.php:339