MediaWiki  master
ApiQueryIWLinks.php
Go to the documentation of this file.
1 <?php
32 
33  public function __construct( ApiQuery $query, $moduleName ) {
34  parent::__construct( $query, $moduleName, 'iw' );
35  }
36 
37  public function execute() {
38  $pages = $this->getPageSet()->getGoodPages();
39  if ( $pages === [] ) {
40  return;
41  }
42 
43  $params = $this->extractRequestParams();
44  $prop = array_fill_keys( (array)$params['prop'], true );
45 
46  if ( isset( $params['title'] ) && !isset( $params['prefix'] ) ) {
47  $this->dieWithError(
48  [
49  'apierror-invalidparammix-mustusewith',
50  $this->encodeParamName( 'title' ),
51  $this->encodeParamName( 'prefix' ),
52  ],
53  'invalidparammix'
54  );
55  }
56 
57  // Handle deprecated param
58  $this->requireMaxOneParameter( $params, 'url', 'prop' );
59  if ( $params['url'] ) {
60  $prop = [ 'url' => 1 ];
61  }
62 
63  $this->addFields( [
64  'iwl_from',
65  'iwl_prefix',
66  'iwl_title'
67  ] );
68 
69  $this->addTables( 'iwlinks' );
70  $this->addWhereFld( 'iwl_from', array_keys( $pages ) );
71 
72  if ( $params['continue'] !== null ) {
73  $cont = explode( '|', $params['continue'] );
74  $this->dieContinueUsageIf( count( $cont ) != 3 );
75  $op = $params['dir'] == 'descending' ? '<' : '>';
76  $db = $this->getDB();
77  $iwlfrom = (int)$cont[0];
78  $iwlprefix = $db->addQuotes( $cont[1] );
79  $iwltitle = $db->addQuotes( $cont[2] );
80  $this->addWhere(
81  "iwl_from $op $iwlfrom OR " .
82  "(iwl_from = $iwlfrom AND " .
83  "(iwl_prefix $op $iwlprefix OR " .
84  "(iwl_prefix = $iwlprefix AND " .
85  "iwl_title $op= $iwltitle)))"
86  );
87  }
88 
89  $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
90  if ( isset( $params['prefix'] ) ) {
91  $this->addWhereFld( 'iwl_prefix', $params['prefix'] );
92  if ( isset( $params['title'] ) ) {
93  $this->addWhereFld( 'iwl_title', $params['title'] );
94  $this->addOption( 'ORDER BY', 'iwl_from' . $sort );
95  } else {
96  $this->addOption( 'ORDER BY', [
97  'iwl_from' . $sort,
98  'iwl_title' . $sort
99  ] );
100  }
101  } else {
102  // Don't order by iwl_from if it's constant in the WHERE clause
103  if ( count( $pages ) === 1 ) {
104  $this->addOption( 'ORDER BY', 'iwl_prefix' . $sort );
105  } else {
106  $this->addOption( 'ORDER BY', [
107  'iwl_from' . $sort,
108  'iwl_prefix' . $sort,
109  'iwl_title' . $sort
110  ] );
111  }
112  }
113 
114  $this->addOption( 'LIMIT', $params['limit'] + 1 );
115  $res = $this->select( __METHOD__ );
116 
117  $count = 0;
118  foreach ( $res as $row ) {
119  if ( ++$count > $params['limit'] ) {
120  // We've reached the one extra which shows that
121  // there are additional pages to be had. Stop here...
123  'continue',
124  "{$row->iwl_from}|{$row->iwl_prefix}|{$row->iwl_title}"
125  );
126  break;
127  }
128  $entry = [ 'prefix' => $row->iwl_prefix ];
129 
130  if ( isset( $prop['url'] ) ) {
131  $title = Title::newFromText( "{$row->iwl_prefix}:{$row->iwl_title}" );
132  if ( $title ) {
133  $entry['url'] = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT );
134  }
135  }
136 
137  ApiResult::setContentValue( $entry, 'title', $row->iwl_title );
138  $fit = $this->addPageSubItem( $row->iwl_from, $entry );
139  if ( !$fit ) {
141  'continue',
142  "{$row->iwl_from}|{$row->iwl_prefix}|{$row->iwl_title}"
143  );
144  break;
145  }
146  }
147  }
148 
149  public function getCacheMode( $params ) {
150  return 'public';
151  }
152 
153  public function getAllowedParams() {
154  return [
155  'prop' => [
156  ApiBase::PARAM_ISMULTI => true,
158  'url',
159  ],
161  ],
162  'prefix' => null,
163  'title' => null,
164  'dir' => [
165  ApiBase::PARAM_DFLT => 'ascending',
167  'ascending',
168  'descending'
169  ]
170  ],
171  'limit' => [
172  ApiBase::PARAM_DFLT => 10,
173  ApiBase::PARAM_TYPE => 'limit',
174  ApiBase::PARAM_MIN => 1,
177  ],
178  'continue' => [
179  ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
180  ],
181  'url' => [
182  ApiBase::PARAM_DFLT => false,
184  ],
185  ];
186  }
187 
188  protected function getExamplesMessages() {
189  return [
190  'action=query&prop=iwlinks&titles=Main%20Page'
191  => 'apihelp-query+iwlinks-example-simple',
192  ];
193  }
194 
195  public function getHelpUrls() {
196  return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Iwlinks';
197  }
198 }
const PROTO_CURRENT
Definition: Defines.php:197
wfExpandUrl( $url, $defaultProto=PROTO_CURRENT)
Expand a potentially local URL to a fully-qualified URL.
const PARAM_MAX2
Definition: ApiBase.php:90
const PARAM_DEPRECATED
Definition: ApiBase.php:102
encodeParamName( $paramName)
This method mangles parameter name based on the prefix supplied to the constructor.
Definition: ApiBase.php:743
const PARAM_MAX
Definition: ApiBase.php:86
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition: ApiBase.php:1446
dieContinueUsageIf( $condition)
Die with the 'badcontinue' error.
Definition: ApiBase.php:1638
const PARAM_TYPE
Definition: ApiBase.php:82
const PARAM_DFLT
Definition: ApiBase.php:74
const PARAM_HELP_MSG_PER_VALUE
((string|array|Message)[]) When PARAM_TYPE is an array, this is an array mapping those values to $msg...
Definition: ApiBase.php:196
const PARAM_MIN
Definition: ApiBase.php:94
const LIMIT_BIG1
Fast query, standard limit.
Definition: ApiBase.php:221
requireMaxOneParameter( $params,... $required)
Die if more than one of a certain set of parameters is set and not false.
Definition: ApiBase.php:938
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:765
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition: ApiBase.php:163
const LIMIT_BIG2
Fast query, apihighlimits limit.
Definition: ApiBase.php:223
const PARAM_ISMULTI
Definition: ApiBase.php:78
This is a base class for all Query modules.
setContinueEnumParameter( $paramName, $paramValue)
Set a query-continue value.
addFields( $value)
Add a set of fields to select to the internal array.
addPageSubItem( $pageId, $item, $elemname=null)
Same as addPageSubItems(), but one element of $data at a time.
addOption( $name, $value=null)
Add an option such as LIMIT or USE INDEX.
addTables( $tables, $alias=null)
Add a set of tables to the internal array.
getDB()
Get the Query database connection (read-only)
select( $method, $extraQuery=[], array &$hookData=null)
Execute a SELECT query based on the values in the internal arrays.
addWhereFld( $field, $value)
Equivalent to addWhere( [ $field => $value ] )
getPageSet()
Get the PageSet object to work on.
addWhere( $value)
Add a set of WHERE clauses to the internal array.
This is the main query class.
Definition: ApiQuery.php:40
static setContentValue(array &$arr, $name, $value, $flags=0)
Add an output value to the array by name and mark as META_CONTENT.
Definition: ApiResult.php:467
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:369