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