MediaWiki  master
ApiQueryImages.php
Go to the documentation of this file.
1 <?php
30 
35  public function __construct( ApiQuery $query, $moduleName ) {
36  parent::__construct( $query, $moduleName, 'im' );
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( $resultPageSet = null ) {
51  if ( $this->getPageSet()->getGoodTitleCount() == 0 ) {
52  return; // nothing to do
53  }
54 
55  $params = $this->extractRequestParams();
56  $this->addFields( [
57  'il_from',
58  'il_to'
59  ] );
60 
61  $this->addTables( 'imagelinks' );
62  $this->addWhereFld( 'il_from', array_keys( $this->getPageSet()->getGoodTitles() ) );
63  if ( $params['continue'] !== null ) {
64  $cont = explode( '|', $params['continue'] );
65  $this->dieContinueUsageIf( count( $cont ) != 2 );
66  $op = $params['dir'] == 'descending' ? '<' : '>';
67  $ilfrom = (int)$cont[0];
68  $ilto = $this->getDB()->addQuotes( $cont[1] );
69  $this->addWhere(
70  "il_from $op $ilfrom OR " .
71  "(il_from = $ilfrom AND " .
72  "il_to $op= $ilto)"
73  );
74  }
75 
76  $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
77  // Don't order by il_from if it's constant in the WHERE clause
78  if ( count( $this->getPageSet()->getGoodTitles() ) == 1 ) {
79  $this->addOption( 'ORDER BY', 'il_to' . $sort );
80  } else {
81  $this->addOption( 'ORDER BY', [
82  'il_from' . $sort,
83  'il_to' . $sort
84  ] );
85  }
86  $this->addOption( 'LIMIT', $params['limit'] + 1 );
87 
88  if ( $params['images'] ) {
89  $images = [];
90  foreach ( $params['images'] as $img ) {
91  $title = Title::newFromText( $img );
92  if ( !$title || $title->getNamespace() !== NS_FILE ) {
93  $this->addWarning( [ 'apiwarn-notfile', wfEscapeWikiText( $img ) ] );
94  } else {
95  $images[] = $title->getDBkey();
96  }
97  }
98  if ( !$images ) {
99  // No titles so no results
100  return;
101  }
102  $this->addWhereFld( 'il_to', $images );
103  }
104 
105  $res = $this->select( __METHOD__ );
106 
107  if ( $resultPageSet === null ) {
108  $count = 0;
109  foreach ( $res as $row ) {
110  if ( ++$count > $params['limit'] ) {
111  // We've reached the one extra which shows that
112  // there are additional pages to be had. Stop here...
113  $this->setContinueEnumParameter( 'continue', $row->il_from . '|' . $row->il_to );
114  break;
115  }
116  $vals = [];
117  ApiQueryBase::addTitleInfo( $vals, Title::makeTitle( NS_FILE, $row->il_to ) );
118  $fit = $this->addPageSubItem( $row->il_from, $vals );
119  if ( !$fit ) {
120  $this->setContinueEnumParameter( 'continue', $row->il_from . '|' . $row->il_to );
121  break;
122  }
123  }
124  } else {
125  $titles = [];
126  $count = 0;
127  foreach ( $res as $row ) {
128  if ( ++$count > $params['limit'] ) {
129  // We've reached the one extra which shows that
130  // there are additional pages to be had. Stop here...
131  $this->setContinueEnumParameter( 'continue', $row->il_from . '|' . $row->il_to );
132  break;
133  }
134  $titles[] = Title::makeTitle( NS_FILE, $row->il_to );
135  }
136  $resultPageSet->populateFromTitles( $titles );
137  }
138  }
139 
140  public function getCacheMode( $params ) {
141  return 'public';
142  }
143 
144  public function getAllowedParams() {
145  return [
146  'limit' => [
147  ApiBase::PARAM_DFLT => 10,
148  ApiBase::PARAM_TYPE => 'limit',
149  ApiBase::PARAM_MIN => 1,
152  ],
153  'continue' => [
154  ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
155  ],
156  'images' => [
157  ApiBase::PARAM_ISMULTI => true,
158  ],
159  'dir' => [
160  ApiBase::PARAM_DFLT => 'ascending',
162  'ascending',
163  'descending'
164  ]
165  ],
166  ];
167  }
168 
169  protected function getExamplesMessages() {
170  return [
171  'action=query&prop=images&titles=Main%20Page'
172  => 'apihelp-query+images-example-simple',
173  'action=query&generator=images&titles=Main%20Page&prop=info'
174  => 'apihelp-query+images-example-generator',
175  ];
176  }
177 
178  public function getHelpUrls() {
179  return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Images';
180  }
181 }
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:363
ApiQueryBase\addFields
addFields( $value)
Add a set of fields to select to the internal array.
Definition: ApiQueryBase.php:212
ApiQuery
This is the main query class.
Definition: ApiQuery.php:37
ApiBase\addWarning
addWarning( $msg, $code=null, $data=null)
Add a warning for this module.
Definition: ApiBase.php:1303
ApiQueryImages
This query adds an "<images>" subelement to all pages with the list of images embedded into those pag...
Definition: ApiQueryImages.php:29
ApiBase\PARAM_HELP_MSG
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition: ApiBase.php:105
ApiQueryImages\executeGenerator
executeGenerator( $resultPageSet)
Execute this module as a generator.
Definition: ApiQueryImages.php:43
ApiBase\PARAM_TYPE
const PARAM_TYPE
Definition: ApiBase.php:72
ApiQueryImages\execute
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
Definition: ApiQueryImages.php:39
ApiQueryBase\addOption
addOption( $name, $value=null)
Add an option such as LIMIT or USE INDEX.
Definition: ApiQueryBase.php:378
$res
$res
Definition: testCompression.php:57
ApiQueryGeneratorBase\setContinueEnumParameter
setContinueEnumParameter( $paramName, $paramValue)
Overridden to set the generator param if in generator mode.
Definition: ApiQueryGeneratorBase.php:83
ApiBase\PARAM_MIN
const PARAM_MIN
Definition: ApiBase.php:75
ApiQueryGeneratorBase\getPageSet
getPageSet()
Get the PageSet object to work on.
Definition: ApiQueryGeneratorBase.php:57
ApiBase\LIMIT_BIG1
const LIMIT_BIG1
Fast query, standard limit.
Definition: ApiBase.php:163
ApiQueryBase\getDB
getDB()
Get the Query database connection (read-only) Stable to override.
Definition: ApiQueryBase.php:117
ApiBase\PARAM_MAX
const PARAM_MAX
Definition: ApiBase.php:73
ApiQueryBase\addTables
addTables( $tables, $alias=null)
Add a set of tables to the internal array.
Definition: ApiQueryBase.php:182
ApiQueryBase\select
select( $method, $extraQuery=[], array &$hookData=null)
Execute a SELECT query based on the values in the internal arrays.
Definition: ApiQueryBase.php:399
ApiQueryImages\getExamplesMessages
getExamplesMessages()
Returns usage examples for this module.
Definition: ApiQueryImages.php:169
ApiBase\extractRequestParams
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:720
ApiQueryImages\__construct
__construct(ApiQuery $query, $moduleName)
Definition: ApiQueryImages.php:35
$title
$title
Definition: testCompression.php:38
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:626
ApiBase\dieContinueUsageIf
dieContinueUsageIf( $condition)
Die with the 'badcontinue' error.
Definition: ApiBase.php:1567
ApiQueryImages\run
run( $resultPageSet=null)
Definition: ApiQueryImages.php:50
ApiQueryImages\getCacheMode
getCacheMode( $params)
Get the cache mode for the data generated by this module.
Definition: ApiQueryImages.php:140
wfEscapeWikiText
wfEscapeWikiText( $text)
Escapes the given text so that it may be output using addWikiText() without any linking,...
Definition: GlobalFunctions.php:1505
ApiQueryBase\addWhereFld
addWhereFld( $field, $value)
Equivalent to addWhere( [ $field => $value ] )
Definition: ApiQueryBase.php:282
ApiQueryGeneratorBase
Stable to extend.
Definition: ApiQueryGeneratorBase.php:28
ApiBase\LIMIT_BIG2
const LIMIT_BIG2
Fast query, apihighlimits limit.
Definition: ApiBase.php:165
ApiBase\PARAM_DFLT
const PARAM_DFLT
Definition: ApiBase.php:70
ApiBase\PARAM_ISMULTI
const PARAM_ISMULTI
Definition: ApiBase.php:71
ApiBase\PARAM_MAX2
const PARAM_MAX2
Definition: ApiBase.php:74
ApiQueryImages\getAllowedParams
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiQueryImages.php:144
ApiQueryBase\addWhere
addWhere( $value)
Add a set of WHERE clauses to the internal array.
Definition: ApiQueryBase.php:245
NS_FILE
const NS_FILE
Definition: Defines.php:70
ApiQueryBase\addPageSubItem
addPageSubItem( $pageId, $item, $elemname=null)
Same as addPageSubItems(), but one element of $data at a time.
Definition: ApiQueryBase.php:494
ApiQueryImages\getHelpUrls
getHelpUrls()
Return links to more detailed help pages about the module.
Definition: ApiQueryImages.php:178
ApiQueryBase\addTitleInfo
static addTitleInfo(&$arr, $title, $prefix='')
Add information (title and namespace) about a Title object to a result array.
Definition: ApiQueryBase.php:466