MediaWiki
REL1_20
|
00001 <?php 00032 class ApiQueryIWBacklinks extends ApiQueryGeneratorBase { 00033 00034 public function __construct( $query, $moduleName ) { 00035 parent::__construct( $query, $moduleName, 'iwbl' ); 00036 } 00037 00038 public function execute() { 00039 $this->run(); 00040 } 00041 00042 public function executeGenerator( $resultPageSet ) { 00043 $this->run( $resultPageSet ); 00044 } 00045 00050 public function run( $resultPageSet = null ) { 00051 $params = $this->extractRequestParams(); 00052 00053 if ( isset( $params['title'] ) && !isset( $params['prefix'] ) ) { 00054 $this->dieUsageMsg( array( 'missingparam', 'prefix' ) ); 00055 } 00056 00057 if ( !is_null( $params['continue'] ) ) { 00058 $cont = explode( '|', $params['continue'] ); 00059 if ( count( $cont ) != 3 ) { 00060 $this->dieUsage( 'Invalid continue param. You should pass the ' . 00061 'original value returned by the previous query', '_badcontinue' ); 00062 } 00063 00064 $db = $this->getDB(); 00065 $op = $params['dir'] == 'descending' ? '<' : '>'; 00066 $prefix = $db->addQuotes( $cont[0] ); 00067 $title = $db->addQuotes( $cont[1] ); 00068 $from = intval( $cont[2] ); 00069 $this->addWhere( 00070 "iwl_prefix $op $prefix OR " . 00071 "(iwl_prefix = $prefix AND " . 00072 "(iwl_title $op $title OR " . 00073 "(iwl_title = $title AND " . 00074 "iwl_from $op= $from)))" 00075 ); 00076 } 00077 00078 $prop = array_flip( $params['prop'] ); 00079 $iwprefix = isset( $prop['iwprefix'] ); 00080 $iwtitle = isset( $prop['iwtitle'] ); 00081 00082 $this->addTables( array( 'iwlinks', 'page' ) ); 00083 $this->addWhere( 'iwl_from = page_id' ); 00084 00085 $this->addFields( array( 'page_id', 'page_title', 'page_namespace', 'page_is_redirect', 00086 'iwl_from', 'iwl_prefix', 'iwl_title' ) ); 00087 00088 $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' ); 00089 if ( isset( $params['prefix'] ) ) { 00090 $this->addWhereFld( 'iwl_prefix', $params['prefix'] ); 00091 if ( isset( $params['title'] ) ) { 00092 $this->addWhereFld( 'iwl_title', $params['title'] ); 00093 $this->addOption( 'ORDER BY', 'iwl_from' . $sort ); 00094 } else { 00095 $this->addOption( 'ORDER BY', array( 00096 'iwl_title' . $sort, 00097 'iwl_from' . $sort 00098 )); 00099 } 00100 } else { 00101 $this->addOption( 'ORDER BY', array( 00102 'iwl_prefix' . $sort, 00103 'iwl_title' . $sort, 00104 'iwl_from' . $sort 00105 )); 00106 } 00107 00108 $this->addOption( 'LIMIT', $params['limit'] + 1 ); 00109 00110 $res = $this->select( __METHOD__ ); 00111 00112 $pages = array(); 00113 00114 $count = 0; 00115 $result = $this->getResult(); 00116 foreach ( $res as $row ) { 00117 if ( ++ $count > $params['limit'] ) { 00118 // We've reached the one extra which shows that there are additional pages to be had. Stop here... 00119 // Continue string preserved in case the redirect query doesn't pass the limit 00120 $this->setContinueEnumParameter( 'continue', "{$row->iwl_prefix}|{$row->iwl_title}|{$row->iwl_from}" ); 00121 break; 00122 } 00123 00124 if ( !is_null( $resultPageSet ) ) { 00125 $pages[] = Title::newFromRow( $row ); 00126 } else { 00127 $entry = array( 'pageid' => $row->page_id ); 00128 00129 $title = Title::makeTitle( $row->page_namespace, $row->page_title ); 00130 ApiQueryBase::addTitleInfo( $entry, $title ); 00131 00132 if ( $row->page_is_redirect ) { 00133 $entry['redirect'] = ''; 00134 } 00135 00136 if ( $iwprefix ) { 00137 $entry['iwprefix'] = $row->iwl_prefix; 00138 } 00139 00140 if ( $iwtitle ) { 00141 $entry['iwtitle'] = $row->iwl_title; 00142 } 00143 00144 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $entry ); 00145 if ( !$fit ) { 00146 $this->setContinueEnumParameter( 'continue', "{$row->iwl_prefix}|{$row->iwl_title}|{$row->iwl_from}" ); 00147 break; 00148 } 00149 } 00150 } 00151 00152 if ( is_null( $resultPageSet ) ) { 00153 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'iw' ); 00154 } else { 00155 $resultPageSet->populateFromTitles( $pages ); 00156 } 00157 } 00158 00159 public function getCacheMode( $params ) { 00160 return 'public'; 00161 } 00162 00163 public function getAllowedParams() { 00164 return array( 00165 'prefix' => null, 00166 'title' => null, 00167 'continue' => null, 00168 'limit' => array( 00169 ApiBase::PARAM_DFLT => 10, 00170 ApiBase::PARAM_TYPE => 'limit', 00171 ApiBase::PARAM_MIN => 1, 00172 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1, 00173 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2 00174 ), 00175 'prop' => array( 00176 ApiBase::PARAM_ISMULTI => true, 00177 ApiBase::PARAM_DFLT => '', 00178 ApiBase::PARAM_TYPE => array( 00179 'iwprefix', 00180 'iwtitle', 00181 ), 00182 ), 00183 'dir' => array( 00184 ApiBase::PARAM_DFLT => 'ascending', 00185 ApiBase::PARAM_TYPE => array( 00186 'ascending', 00187 'descending' 00188 ) 00189 ), 00190 ); 00191 } 00192 00193 public function getParamDescription() { 00194 return array( 00195 'prefix' => 'Prefix for the interwiki', 00196 'title' => "Interwiki link to search for. Must be used with {$this->getModulePrefix()}prefix", 00197 'continue' => 'When more results are available, use this to continue', 00198 'prop' => array( 00199 'Which properties to get', 00200 ' iwprefix - Adds the prefix of the interwiki', 00201 ' iwtitle - Adds the title of the interwiki', 00202 ), 00203 'limit' => 'How many total pages to return', 00204 'dir' => 'The direction in which to list', 00205 ); 00206 } 00207 00208 public function getResultProperties() { 00209 return array( 00210 '' => array( 00211 'pageid' => 'integer', 00212 'ns' => 'namespace', 00213 'title' => 'string', 00214 'redirect' => 'boolean' 00215 ), 00216 'iwprefix' => array( 00217 'iwprefix' => 'string' 00218 ), 00219 'iwtitle' => array( 00220 'iwtitle' => 'string' 00221 ) 00222 ); 00223 } 00224 00225 public function getDescription() { 00226 return array( 'Find all pages that link to the given interwiki link.', 00227 'Can be used to find all links with a prefix, or', 00228 'all links to a title (with a given prefix).', 00229 'Using neither parameter is effectively "All IW Links"', 00230 ); 00231 } 00232 00233 public function getPossibleErrors() { 00234 return array_merge( parent::getPossibleErrors(), array( 00235 array( 'missingparam', 'prefix' ), 00236 array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ), 00237 ) ); 00238 } 00239 00240 public function getExamples() { 00241 return array( 00242 'api.php?action=query&list=iwbacklinks&iwbltitle=Test&iwblprefix=wikibooks', 00243 'api.php?action=query&generator=iwbacklinks&giwbltitle=Test&giwblprefix=wikibooks&prop=info' 00244 ); 00245 } 00246 00247 public function getVersion() { 00248 return __CLASS__ . ': $Id$'; 00249 } 00250 }