MediaWiki
REL1_20
|
00001 <?php 00033 class ApiQueryImages extends ApiQueryGeneratorBase { 00034 00035 public function __construct( $query, $moduleName ) { 00036 parent::__construct( $query, $moduleName, 'im' ); 00037 } 00038 00039 public function execute() { 00040 $this->run(); 00041 } 00042 00043 public function executeGenerator( $resultPageSet ) { 00044 $this->run( $resultPageSet ); 00045 } 00046 00050 private function run( $resultPageSet = null ) { 00051 if ( $this->getPageSet()->getGoodTitleCount() == 0 ) { 00052 return; // nothing to do 00053 } 00054 00055 $params = $this->extractRequestParams(); 00056 $this->addFields( array( 00057 'il_from', 00058 'il_to' 00059 ) ); 00060 00061 $this->addTables( 'imagelinks' ); 00062 $this->addWhereFld( 'il_from', array_keys( $this->getPageSet()->getGoodTitles() ) ); 00063 if ( !is_null( $params['continue'] ) ) { 00064 $cont = explode( '|', $params['continue'] ); 00065 if ( count( $cont ) != 2 ) { 00066 $this->dieUsage( 'Invalid continue param. You should pass the ' . 00067 'original value returned by the previous query', '_badcontinue' ); 00068 } 00069 $op = $params['dir'] == 'descending' ? '<' : '>'; 00070 $ilfrom = intval( $cont[0] ); 00071 $ilto = $this->getDB()->addQuotes( $cont[1] ); 00072 $this->addWhere( 00073 "il_from $op $ilfrom OR " . 00074 "(il_from = $ilfrom AND " . 00075 "il_to $op= $ilto)" 00076 ); 00077 } 00078 00079 $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' ); 00080 // Don't order by il_from if it's constant in the WHERE clause 00081 if ( count( $this->getPageSet()->getGoodTitles() ) == 1 ) { 00082 $this->addOption( 'ORDER BY', 'il_to' . $sort ); 00083 } else { 00084 $this->addOption( 'ORDER BY', array( 00085 'il_from' . $sort, 00086 'il_to' . $sort 00087 )); 00088 } 00089 $this->addOption( 'LIMIT', $params['limit'] + 1 ); 00090 00091 if ( !is_null( $params['images'] ) ) { 00092 $images = array(); 00093 foreach ( $params['images'] as $img ) { 00094 $title = Title::newFromText( $img ); 00095 if ( !$title || $title->getNamespace() != NS_FILE ) { 00096 $this->setWarning( "\"$img\" is not a file" ); 00097 } else { 00098 $images[] = $title->getDBkey(); 00099 } 00100 } 00101 $this->addWhereFld( 'il_to', $images ); 00102 } 00103 00104 $res = $this->select( __METHOD__ ); 00105 00106 if ( is_null( $resultPageSet ) ) { 00107 $count = 0; 00108 foreach ( $res as $row ) { 00109 if ( ++$count > $params['limit'] ) { 00110 // We've reached the one extra which shows that 00111 // there are additional pages to be had. Stop here... 00112 $this->setContinueEnumParameter( 'continue', $row->il_from . '|' . $row->il_to ); 00113 break; 00114 } 00115 $vals = array(); 00116 ApiQueryBase::addTitleInfo( $vals, Title::makeTitle( NS_FILE, $row->il_to ) ); 00117 $fit = $this->addPageSubItem( $row->il_from, $vals ); 00118 if ( !$fit ) { 00119 $this->setContinueEnumParameter( 'continue', $row->il_from . '|' . $row->il_to ); 00120 break; 00121 } 00122 } 00123 } else { 00124 $titles = array(); 00125 $count = 0; 00126 foreach ( $res as $row ) { 00127 if ( ++$count > $params['limit'] ) { 00128 // We've reached the one extra which shows that 00129 // there are additional pages to be had. Stop here... 00130 $this->setContinueEnumParameter( 'continue', $row->il_from . '|' . $row->il_to ); 00131 break; 00132 } 00133 $titles[] = Title::makeTitle( NS_FILE, $row->il_to ); 00134 } 00135 $resultPageSet->populateFromTitles( $titles ); 00136 } 00137 } 00138 00139 public function getCacheMode( $params ) { 00140 return 'public'; 00141 } 00142 00143 public function getAllowedParams() { 00144 return array( 00145 'limit' => array( 00146 ApiBase::PARAM_DFLT => 10, 00147 ApiBase::PARAM_TYPE => 'limit', 00148 ApiBase::PARAM_MIN => 1, 00149 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1, 00150 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2 00151 ), 00152 'continue' => null, 00153 'images' => array( 00154 ApiBase::PARAM_ISMULTI => true, 00155 ), 00156 'dir' => array( 00157 ApiBase::PARAM_DFLT => 'ascending', 00158 ApiBase::PARAM_TYPE => array( 00159 'ascending', 00160 'descending' 00161 ) 00162 ), 00163 ); 00164 } 00165 00166 public function getParamDescription() { 00167 return array( 00168 'limit' => 'How many images to return', 00169 'continue' => 'When more results are available, use this to continue', 00170 'images' => 'Only list these images. Useful for checking whether a certain page has a certain Image.', 00171 'dir' => 'The direction in which to list', 00172 ); 00173 } 00174 00175 public function getResultProperties() { 00176 return array( 00177 '' => array( 00178 'ns' => 'namespace', 00179 'title' => 'string' 00180 ) 00181 ); 00182 } 00183 00184 public function getDescription() { 00185 return 'Returns all images contained on the given page(s)'; 00186 } 00187 00188 public function getPossibleErrors() { 00189 return array_merge( parent::getPossibleErrors(), array( 00190 array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ), 00191 ) ); 00192 } 00193 00194 public function getExamples() { 00195 return array( 00196 'api.php?action=query&prop=images&titles=Main%20Page' => 'Get a list of images used in the [[Main Page]]', 00197 'api.php?action=query&generator=images&titles=Main%20Page&prop=info' => 'Get information about all images used in the [[Main Page]]', 00198 ); 00199 } 00200 00201 public function getHelpUrls() { 00202 return 'https://www.mediawiki.org/wiki/API:Properties#images_.2F_im'; 00203 } 00204 00205 public function getVersion() { 00206 return __CLASS__ . ': $Id$'; 00207 } 00208 }