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