MediaWiki  1.23.1
ApiQueryRedirects.php
Go to the documentation of this file.
1 <?php
34 
35  public function __construct( $query, $moduleName ) {
36  parent::__construct( $query, $moduleName, 'rd' );
37  }
38 
39  public function execute() {
40  $this->run();
41  }
42 
43  public function executeGenerator( $resultPageSet ) {
44  $this->run( $resultPageSet );
45  }
46 
50  private function run( ApiPageSet $resultPageSet = null ) {
51  $db = $this->getDB();
52  $params = $this->extractRequestParams();
53  $emptyString = $db->addQuotes( '' );
54 
55  $pageSet = $this->getPageSet();
56  $titles = $pageSet->getGoodTitles() + $pageSet->getMissingTitles();
57 
58  if ( !is_null( $params['continue'] ) ) {
59  $cont = explode( '|', $params['continue'] );
60  $this->dieContinueUsageIf( count( $cont ) != 3 );
61  $rd_namespace = (int)$cont[0];
62  $this->dieContinueUsageIf( $rd_namespace != $cont[0] );
63  $rd_title = $db->addQuotes( $cont[1] );
64  $rd_from = (int)$cont[2];
65  $this->dieContinueUsageIf( $rd_from != $cont[2] );
66  $this->addWhere(
67  "rd_namespace > $rd_namespace OR " .
68  "(rd_namespace = $rd_namespace AND " .
69  "(rd_title > $rd_title OR " .
70  "(rd_title = $rd_title AND " .
71  "rd_from >= $rd_from)))"
72  );
73 
74  // Remove titles that we're past already
75  $titles = array_filter( $titles, function ( $t ) use ( $rd_namespace, $rd_title ) {
76  $ns = $t->getNamespace();
77  return ( $ns > $rd_namespace ||
78  $ns == $rd_namespace && $t->getDBKey() >= $rd_title
79  );
80  } );
81  }
82 
83  if ( !$titles ) {
84  return; // nothing to do
85  }
86 
87  $this->addTables( array( 'redirect', 'page' ) );
88  $this->addFields( array(
89  'rd_from',
90  'rd_namespace',
91  'rd_title',
92  ) );
93 
94  if ( is_null( $resultPageSet ) ) {
95  $prop = array_flip( $params['prop'] );
96  $fld_pageid = isset( $prop['pageid'] );
97  $fld_title = isset( $prop['title'] );
98  $fld_fragment = isset( $prop['fragment'] );
99 
100  $this->addFieldsIf( 'rd_fragment', $fld_fragment );
101  $this->addFieldsIf( array( 'page_namespace', 'page_title' ), $fld_title );
102  } else {
103  $this->addFields( array( 'page_namespace', 'page_title' ) );
104  }
105 
106  $lb = new LinkBatch( $titles );
107  $this->addWhere( array(
108  'rd_from = page_id',
109  "rd_interwiki = $emptyString OR rd_interwiki IS NULL",
110  $lb->constructSet( 'rd', $db ),
111  ) );
112 
113  if ( $params['show'] !== null ) {
114  $show = array_flip( $params['show'] );
115  if ( isset( $show['fragment'] ) && isset( $show['!fragment'] ) ) {
116  $this->dieUsageMsg( 'show' );
117  }
118  $this->addWhereIf( "rd_fragment != $emptyString", isset( $show['fragment'] ) );
119  $this->addWhereIf( "rd_fragment = $emptyString OR rd_fragment IS NULL", isset( $show['!fragment'] ) );
120  }
121 
122  $map = $pageSet->getAllTitlesByNamespace();
123 
124  // Why, MySQL? Why do you do this to us?
125  $sortby = array();
126  if ( count( $map ) > 1 ) {
127  $sortby[] = 'rd_namespace';
128  }
129  $theTitle = null;
130  foreach ( $map as $nsTitles ) {
131  reset( $nsTitles );
132  $key = key( $nsTitles );
133  if ( $theTitle === null ) {
134  $theTitle = $key;
135  }
136  if ( count( $nsTitles ) > 1 || $key !== $theTitle ) {
137  $sortby[] = 'rd_title';
138  break;
139  }
140  }
141  $sortby[] = 'rd_from';
142  $this->addOption( 'ORDER BY', $sortby );
143 
144  $this->addOption( 'LIMIT', $params['limit'] + 1 );
145 
146  $res = $this->select( __METHOD__ );
147 
148  if ( is_null( $resultPageSet ) ) {
149  $count = 0;
150  foreach ( $res as $row ) {
151  if ( ++$count > $params['limit'] ) {
152  // We've reached the one extra which shows that
153  // there are additional pages to be had. Stop here...
154  $this->setContinueEnumParameter( 'continue',
155  "$row->rd_namespace|$row->rd_title|$row->rd_from"
156  );
157  break;
158  }
159 
160  # Get the ID of the current page
161  $id = $map[$row->rd_namespace][$row->rd_title];
162 
163  $vals = array();
164  if ( $fld_pageid ) {
165  $vals['pageid'] = $row->rd_from;
166  }
167  if ( $fld_title ) {
169  Title::makeTitle( $row->page_namespace, $row->page_title )
170  );
171  }
172  if ( $fld_fragment && $row->rd_fragment !== null && $row->rd_fragment !== '' ) {
173  $vals['fragment'] = $row->rd_fragment;
174  }
175  $fit = $this->addPageSubItem( $id, $vals );
176  if ( !$fit ) {
177  $this->setContinueEnumParameter( 'continue',
178  "$row->rd_namespace|$row->rd_title|$row->rd_from"
179  );
180  break;
181  }
182  }
183  } else {
184  $titles = array();
185  $count = 0;
186  foreach ( $res as $row ) {
187  if ( ++$count > $params['limit'] ) {
188  // We've reached the one extra which shows that
189  // there are additional pages to be had. Stop here...
190  $this->setContinueEnumParameter( 'continue',
191  "$row->rd_namespace|$row->rd_title|$row->rd_from"
192  );
193  break;
194  }
195  $titles[] = Title::makeTitle( $row->page_namespace, $row->page_title );
196  }
197  $resultPageSet->populateFromTitles( $titles );
198  }
199  }
200 
201  public function getCacheMode( $params ) {
202  return 'public';
203  }
204 
205  public function getAllowedParams() {
206  return array(
207  'prop' => array(
209  'pageid',
210  'title',
211  'fragment',
212  ),
213  ApiBase::PARAM_ISMULTI => true,
214  ApiBase::PARAM_DFLT => 'pageid|title',
215  ),
216  'show' => array(
218  'fragment', '!fragment',
219  ),
220  ApiBase::PARAM_ISMULTI => true,
221  ),
222  'limit' => array(
223  ApiBase::PARAM_DFLT => 10,
224  ApiBase::PARAM_TYPE => 'limit',
225  ApiBase::PARAM_MIN => 1,
228  ),
229  'continue' => null,
230  );
231  }
232 
233  public function getParamDescription() {
234  return array(
235  'prop' => array(
236  'Which properties to get:',
237  ' pageid - Page id of each redirect',
238  ' title - Title of each redirect',
239  ' fragment - Fragment of each redirect, if any',
240  ),
241  'show' => array(
242  'Show only items that meet this criteria.',
243  ' fragment - Only show redirects with a fragment',
244  ' !fragment - Only show redirects without a fragment',
245  ),
246  'limit' => 'How many redirects to return',
247  'continue' => 'When more results are available, use this to continue',
248  );
249  }
250 
251  public function getDescription() {
252  return 'Returns all redirects to the given page(s).';
253  }
254 
255  public function getExamples() {
256  return array(
257  'api.php?action=query&prop=redirects&titles=Main%20Page'
258  => 'Get a list of redirects to the [[Main Page]]',
259  'api.php?action=query&generator=redirects&titles=Main%20Page&prop=info'
260  => 'Get information about all redirects to the [[Main Page]]',
261  );
262  }
263 
264  public function getHelpUrls() {
265  return 'https://www.mediawiki.org/wiki/API:Properties#redirects_.2F_rd';
266  }
267 }
Title\makeTitle
static & makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:398
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
ApiQueryRedirects\__construct
__construct( $query, $moduleName)
Definition: ApiQueryRedirects.php:35
ApiQueryRedirects\getParamDescription
getParamDescription()
Returns an array of parameter descriptions.
Definition: ApiQueryRedirects.php:233
ApiBase\dieUsageMsg
dieUsageMsg( $error)
Output the error message related to a certain array.
Definition: ApiBase.php:1929
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
ApiQueryRedirects\getHelpUrls
getHelpUrls()
Definition: ApiQueryRedirects.php:264
ApiQueryBase\addOption
addOption( $name, $value=null)
Add an option such as LIMIT or USE INDEX.
Definition: ApiQueryBase.php:252
ApiQueryRedirects\run
run(ApiPageSet $resultPageSet=null)
Definition: ApiQueryRedirects.php:50
ApiQueryBase\addFieldsIf
addFieldsIf( $value, $condition)
Same as addFields(), but add the fields only if a condition is met.
Definition: ApiQueryBase.php:131
ApiPageSet
This class contains a list of pages that the client has requested.
Definition: ApiPageSet.php:41
key
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at etc Handles the details of getting and saving to the user table of the and dealing with sessions and cookies OutputPage Encapsulates the entire HTML page that will be sent in response to any server request It is used by calling its functions to add in any and then calling but I prefer the flexibility This should also do the output encoding The system allocates a global one in $wgOut Title Represents the title of an and does all the work of translating among various forms such as plain database key
Definition: design.txt:25
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
ApiQueryRedirects\execute
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
Definition: ApiQueryRedirects.php:39
ApiBase\LIMIT_BIG1
const LIMIT_BIG1
Definition: ApiBase.php:78
ApiQueryRedirects\getDescription
getDescription()
Returns the description string for this module.
Definition: ApiQueryRedirects.php:251
ApiQueryRedirects\getAllowedParams
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiQueryRedirects.php:205
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
ApiQueryRedirects\getExamples
getExamples()
Returns usage examples for this module.
Definition: ApiQueryRedirects.php:255
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
ApiBase\extractRequestParams
extractRequestParams( $parseLimit=true)
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:687
ApiBase\dieContinueUsageIf
dieContinueUsageIf( $condition)
Die with the $prefix.
Definition: ApiBase.php:1965
$count
$count
Definition: UtfNormalTest2.php:96
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\PARAM_ISMULTI
const PARAM_ISMULTI
Definition: ApiBase.php:48
ApiBase\PARAM_MAX2
const PARAM_MAX2
Definition: ApiBase.php:54
ApiQueryRedirects\getCacheMode
getCacheMode( $params)
Get the cache mode for the data generated by this module.
Definition: ApiQueryRedirects.php:201
ApiQueryRedirects
This query lists redirects to the given pages.
Definition: ApiQueryRedirects.php:33
ApiQueryRedirects\executeGenerator
executeGenerator( $resultPageSet)
Execute this module as a generator.
Definition: ApiQueryRedirects.php:43
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
ApiQueryBase\addWhereIf
addWhereIf( $value, $condition)
Same as addWhere(), but add the WHERE clauses only if a condition is met.
Definition: ApiQueryBase.php:170
ApiQueryBase\addTitleInfo
static addTitleInfo(&$arr, $title, $prefix='')
Add information (title and namespace) about a Title object to a result array.
Definition: ApiQueryBase.php:339