2012-03-01 17:37:16 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class ApiQueryPageImages extends ApiQueryBase {
|
2012-03-05 21:36:43 +00:00
|
|
|
private $count = 0, $limit;
|
|
|
|
|
2012-03-01 17:37:16 +00:00
|
|
|
public function __construct( $query, $moduleName ) {
|
|
|
|
parent::__construct( $query, $moduleName, 'pi' );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function execute() {
|
|
|
|
wfProfileIn( __METHOD__ );
|
|
|
|
$titles = $this->getPageSet()->getGoodTitles();
|
|
|
|
if ( count( $titles ) == 0 ) {
|
|
|
|
wfProfileOut( __METHOD__ );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$params = $this->extractRequestParams();
|
|
|
|
$prop = array_flip( $params['prop'] );
|
2012-03-05 21:36:43 +00:00
|
|
|
if ( !count( $prop ) ) {
|
|
|
|
$this->dieUsage( );
|
|
|
|
}
|
2012-03-01 17:37:16 +00:00
|
|
|
$size = $params['thumbsize'];
|
2012-03-05 21:36:43 +00:00
|
|
|
$this->limit = $params['limit'];
|
2012-03-01 17:37:16 +00:00
|
|
|
|
|
|
|
$this->addTables( 'page_props' );
|
|
|
|
$this->addFields( array( 'pp_page', 'pp_propname', 'pp_value' ) );
|
|
|
|
$propNames = array();
|
|
|
|
$propMapping = array( 'hasphotos' => 'has_photos', 'thumbnail' => 'page_image' );
|
|
|
|
foreach ( $propMapping as $apiName => $dbName ) {
|
|
|
|
if ( isset( $prop[$apiName] ) ) {
|
|
|
|
$propNames[] = $dbName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->addWhere( array( 'pp_page' => array_keys( $titles ), 'pp_propname' => $propNames ) );
|
2012-03-05 21:36:43 +00:00
|
|
|
if ( isset( $params['continue'] ) ) {
|
|
|
|
// is_numeric() accepts floats, so...
|
|
|
|
if ( preg_match( '/^\d+$/', $params['continue'] ) ) {
|
|
|
|
$this->addWhere( 'pp_page >= ' . intval( $params['continue'] ) );
|
|
|
|
} else {
|
|
|
|
$this->dieUsage( 'Invalid continue param. You should pass the original value returned by the previous query' , '_badcontinue' );
|
|
|
|
}
|
|
|
|
}
|
2012-03-01 17:37:16 +00:00
|
|
|
$this->addOption( 'ORDER BY', 'pp_page' );
|
2012-03-05 21:36:43 +00:00
|
|
|
$this->addOption( 'LIMIT', $this->limit * count( $prop ) + 1 );
|
2012-03-01 19:44:37 +00:00
|
|
|
|
2012-03-01 17:37:16 +00:00
|
|
|
$res = $this->select( __METHOD__ );
|
2012-03-01 19:44:37 +00:00
|
|
|
$lastId = 0;
|
|
|
|
$vals = array();
|
2012-03-01 17:37:16 +00:00
|
|
|
foreach ( $res as $row ) {
|
2012-03-01 19:44:37 +00:00
|
|
|
if ( $lastId && $lastId != $row->pp_page && !$this->addData( $lastId, $vals ) ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$lastId = $row->pp_page;
|
2012-03-01 17:37:16 +00:00
|
|
|
if ( $row->pp_propname == 'has_photos' ) {
|
|
|
|
$vals['hasphotos'] = '';
|
|
|
|
} elseif ( $row->pp_propname == 'page_image' ) {
|
|
|
|
$file = wfFindFile( $row->pp_value );
|
|
|
|
if ( $file ) {
|
|
|
|
$thumb = $file->transform( array( 'width' => $size, 'height' => $size ) );
|
|
|
|
if ( $thumb ) {
|
|
|
|
$vals['thumb'] = array(
|
2012-03-08 14:01:00 +00:00
|
|
|
'source' => wfExpandUrl( $thumb->getUrl(), PROTO_CURRENT ),
|
2012-03-01 17:37:16 +00:00
|
|
|
'width' => $thumb->getWidth(),
|
|
|
|
'height' => $thumb->getHeight(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-03-01 19:44:37 +00:00
|
|
|
$this->addData( $lastId, $vals );
|
|
|
|
}
|
|
|
|
|
|
|
|
private function addData( $pageId, array &$data ) {
|
|
|
|
$fit = true;
|
|
|
|
if ( count( $data ) ) {
|
2012-03-05 21:36:43 +00:00
|
|
|
$fit = ++$this->count <= $this->limit
|
|
|
|
&& $this->getResult()->addValue( array( 'query', 'pages' ), $pageId, $data );
|
2012-03-01 19:44:37 +00:00
|
|
|
$data = array();
|
|
|
|
}
|
|
|
|
if ( !$fit ) {
|
|
|
|
$this->setContinueEnumParameter( 'continue', $pageId );
|
|
|
|
}
|
|
|
|
return $fit;
|
2012-03-01 17:37:16 +00:00
|
|
|
}
|
|
|
|
|
2012-03-05 21:36:43 +00:00
|
|
|
public function getDescription() {
|
|
|
|
return 'Returns information about images on the page such as thumbnail and presence of photos.';
|
|
|
|
}
|
|
|
|
|
2012-03-01 17:37:16 +00:00
|
|
|
public function getAllowedParams() {
|
|
|
|
return array(
|
|
|
|
'prop' => array(
|
|
|
|
ApiBase::PARAM_TYPE => array( 'thumbnail', 'hasphotos' ),
|
|
|
|
ApiBase::PARAM_ISMULTI => true,
|
|
|
|
ApiBase::PARAM_DFLT => 'thumbnail|hasphotos',
|
|
|
|
),
|
|
|
|
'thumbsize' => array(
|
|
|
|
ApiBase::PARAM_TYPE => 'integer',
|
|
|
|
APiBase::PARAM_DFLT => 50,
|
|
|
|
),
|
2012-03-05 21:36:43 +00:00
|
|
|
'limit' => array(
|
|
|
|
ApiBase::PARAM_DFLT => 1,
|
|
|
|
ApiBase::PARAM_TYPE => 'limit',
|
|
|
|
ApiBase::PARAM_MIN => 1,
|
|
|
|
ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
|
|
|
|
ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2,
|
|
|
|
),
|
|
|
|
'continue' => array(
|
|
|
|
ApiBase::PARAM_TYPE => 'integer',
|
|
|
|
),
|
2012-03-01 17:37:16 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-03-01 19:55:02 +00:00
|
|
|
public function getParamDescription() {
|
|
|
|
return array(
|
|
|
|
'prop' => array( 'What information to return',
|
|
|
|
' thumbnail - URL and dimensions of image associated with page, if any',
|
|
|
|
' hasphotos - whether this page contains photos'
|
|
|
|
),
|
|
|
|
'thumbsize' => 'Width of thumbnail image',
|
2012-03-05 21:36:43 +00:00
|
|
|
'limit' => 'Properties of how many pages to return',
|
|
|
|
'continue' => 'When more results are available, use this to continue',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getPossibleErrors() {
|
|
|
|
return array_merge( parent::getPossibleErrors(), array(
|
|
|
|
array( 'code' => '_noprop', 'info' => 'At least one prop should be specified' ),
|
|
|
|
array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
|
|
|
|
)
|
2012-03-01 19:55:02 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-03-01 17:37:16 +00:00
|
|
|
public function getVersion() {
|
|
|
|
return '$Id$';
|
|
|
|
}
|
|
|
|
}
|