00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 if ( !defined( 'MEDIAWIKI' ) ) {
00027
00028 require_once ( "ApiQueryBase.php" );
00029 }
00030
00036 class ApiQueryImages extends ApiQueryGeneratorBase {
00037
00038 public function __construct( $query, $moduleName ) {
00039 parent :: __construct( $query, $moduleName, 'im' );
00040 }
00041
00042 public function execute() {
00043 $this->run();
00044 }
00045
00046 public function executeGenerator( $resultPageSet ) {
00047 $this->run( $resultPageSet );
00048 }
00049
00050 private function run( $resultPageSet = null ) {
00051
00052 if ( $this->getPageSet()->getGoodTitleCount() == 0 )
00053 return;
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 $ilfrom = intval( $cont[0] );
00069 $ilto = $this->getDB()->strencode( $this->titleToKey( $cont[1] ) );
00070 $this->addWhere( "il_from > $ilfrom OR " .
00071 "(il_from = $ilfrom AND " .
00072 "il_to >= '$ilto')" );
00073 }
00074
00075
00076 if ( count( $this->getPageSet()->getGoodTitles() ) == 1 )
00077 $this->addOption( 'ORDER BY', 'il_to' );
00078 else
00079 $this->addOption( 'ORDER BY', 'il_from, il_to' );
00080 $this->addOption( 'LIMIT', $params['limit'] + 1 );
00081
00082 $db = $this->getDB();
00083 $res = $this->select( __METHOD__ );
00084
00085 if ( is_null( $resultPageSet ) ) {
00086 $count = 0;
00087 while ( $row = $db->fetchObject( $res ) ) {
00088 if ( ++$count > $params['limit'] ) {
00089
00090
00091 $this->setContinueEnumParameter( 'continue', $row->il_from .
00092 '|' . $this->keyToTitle( $row->il_to ) );
00093 break;
00094 }
00095 $vals = array();
00096 ApiQueryBase :: addTitleInfo( $vals, Title :: makeTitle( NS_FILE, $row->il_to ) );
00097 $fit = $this->addPageSubItem( $row->il_from, $vals );
00098 if ( !$fit )
00099 {
00100 $this->setContinueEnumParameter( 'continue', $row->il_from .
00101 '|' . $this->keyToTitle( $row->il_to ) );
00102 break;
00103 }
00104 }
00105 } else {
00106
00107 $titles = array();
00108 $count = 0;
00109 while ( $row = $db->fetchObject( $res ) ) {
00110 if ( ++$count > $params['limit'] ) {
00111
00112
00113 $this->setContinueEnumParameter( 'continue', $row->il_from .
00114 '|' . $this->keyToTitle( $row->il_to ) );
00115 break;
00116 }
00117 $titles[] = Title :: makeTitle( NS_FILE, $row->il_to );
00118 }
00119 $resultPageSet->populateFromTitles( $titles );
00120 }
00121
00122 $db->freeResult( $res );
00123 }
00124
00125 public function getCacheMode( $params ) {
00126 return 'public';
00127 }
00128
00129 public function getAllowedParams() {
00130 return array(
00131 'limit' => array(
00132 ApiBase :: PARAM_DFLT => 10,
00133 ApiBase :: PARAM_TYPE => 'limit',
00134 ApiBase :: PARAM_MIN => 1,
00135 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
00136 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
00137 ),
00138 'continue' => null,
00139 );
00140 }
00141
00142 public function getParamDescription () {
00143 return array(
00144 'limit' => 'How many images to return',
00145 'continue' => 'When more results are available, use this to continue',
00146 );
00147 }
00148
00149 public function getDescription() {
00150 return 'Returns all images contained on the given page(s)';
00151 }
00152
00153 public function getPossibleErrors() {
00154 return array_merge( parent::getPossibleErrors(), array(
00155 array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
00156 ) );
00157 }
00158
00159 protected function getExamples() {
00160 return array (
00161 "Get a list of images used in the [[Main Page]]:",
00162 " api.php?action=query&prop=images&titles=Main%20Page",
00163 "Get information about all images used in the [[Main Page]]:",
00164 " api.php?action=query&generator=images&titles=Main%20Page&prop=info"
00165 );
00166 }
00167
00168 public function getVersion() {
00169 return __CLASS__ . ': $Id: ApiQueryImages.php 69932 2010-07-26 08:03:21Z tstarling $';
00170 }
00171 }