MediaWiki  REL1_22
ApiQueryImages.php
Go to the documentation of this file.
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             $this->dieContinueUsageIf( count( $cont ) != 2 );
00066             $op = $params['dir'] == 'descending' ? '<' : '>';
00067             $ilfrom = intval( $cont[0] );
00068             $ilto = $this->getDB()->addQuotes( $cont[1] );
00069             $this->addWhere(
00070                 "il_from $op $ilfrom OR " .
00071                 "(il_from = $ilfrom AND " .
00072                 "il_to $op= $ilto)"
00073             );
00074         }
00075 
00076         $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
00077         // Don't order by il_from if it's constant in the WHERE clause
00078         if ( count( $this->getPageSet()->getGoodTitles() ) == 1 ) {
00079             $this->addOption( 'ORDER BY', 'il_to' . $sort );
00080         } else {
00081             $this->addOption( 'ORDER BY', array(
00082                         'il_from' . $sort,
00083                         'il_to' . $sort
00084             ));
00085         }
00086         $this->addOption( 'LIMIT', $params['limit'] + 1 );
00087 
00088         if ( !is_null( $params['images'] ) ) {
00089             $images = array();
00090             foreach ( $params['images'] as $img ) {
00091                 $title = Title::newFromText( $img );
00092                 if ( !$title || $title->getNamespace() != NS_FILE ) {
00093                     $this->setWarning( "\"$img\" is not a file" );
00094                 } else {
00095                     $images[] = $title->getDBkey();
00096                 }
00097             }
00098             $this->addWhereFld( 'il_to', $images );
00099         }
00100 
00101         $res = $this->select( __METHOD__ );
00102 
00103         if ( is_null( $resultPageSet ) ) {
00104             $count = 0;
00105             foreach ( $res as $row ) {
00106                 if ( ++$count > $params['limit'] ) {
00107                     // We've reached the one extra which shows that
00108                     // there are additional pages to be had. Stop here...
00109                     $this->setContinueEnumParameter( 'continue', $row->il_from . '|' . $row->il_to );
00110                     break;
00111                 }
00112                 $vals = array();
00113                 ApiQueryBase::addTitleInfo( $vals, Title::makeTitle( NS_FILE, $row->il_to ) );
00114                 $fit = $this->addPageSubItem( $row->il_from, $vals );
00115                 if ( !$fit ) {
00116                     $this->setContinueEnumParameter( 'continue', $row->il_from . '|' . $row->il_to );
00117                     break;
00118                 }
00119             }
00120         } else {
00121             $titles = array();
00122             $count = 0;
00123             foreach ( $res as $row ) {
00124                 if ( ++$count > $params['limit'] ) {
00125                     // We've reached the one extra which shows that
00126                     // there are additional pages to be had. Stop here...
00127                     $this->setContinueEnumParameter( 'continue', $row->il_from . '|' . $row->il_to );
00128                     break;
00129                 }
00130                 $titles[] = Title::makeTitle( NS_FILE, $row->il_to );
00131             }
00132             $resultPageSet->populateFromTitles( $titles );
00133         }
00134     }
00135 
00136     public function getCacheMode( $params ) {
00137         return 'public';
00138     }
00139 
00140     public function getAllowedParams() {
00141         return array(
00142             'limit' => array(
00143                 ApiBase::PARAM_DFLT => 10,
00144                 ApiBase::PARAM_TYPE => 'limit',
00145                 ApiBase::PARAM_MIN => 1,
00146                 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
00147                 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
00148             ),
00149             'continue' => null,
00150             'images' => array(
00151                 ApiBase::PARAM_ISMULTI => true,
00152             ),
00153             'dir' => array(
00154                 ApiBase::PARAM_DFLT => 'ascending',
00155                 ApiBase::PARAM_TYPE => array(
00156                     'ascending',
00157                     'descending'
00158                 )
00159             ),
00160         );
00161     }
00162 
00163     public function getParamDescription() {
00164         return array(
00165             'limit' => 'How many images to return',
00166             'continue' => 'When more results are available, use this to continue',
00167             'images' => 'Only list these images. Useful for checking whether a certain page has a certain Image.',
00168             'dir' => 'The direction in which to list',
00169         );
00170     }
00171 
00172     public function getResultProperties() {
00173         return array(
00174             '' => array(
00175                 'ns' => 'namespace',
00176                 'title' => 'string'
00177             )
00178         );
00179     }
00180 
00181     public function getDescription() {
00182         return 'Returns all images contained on the given page(s)';
00183     }
00184 
00185     public function getExamples() {
00186         return array(
00187             'api.php?action=query&prop=images&titles=Main%20Page' => 'Get a list of images used in the [[Main Page]]',
00188             'api.php?action=query&generator=images&titles=Main%20Page&prop=info' => 'Get information about all images used in the [[Main Page]]',
00189         );
00190     }
00191 
00192     public function getHelpUrls() {
00193         return 'https://www.mediawiki.org/wiki/API:Properties#images_.2F_im';
00194     }
00195 }