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 ApiQueryAllLinks extends ApiQueryGeneratorBase {
00037
00038 public function __construct( $query, $moduleName ) {
00039 parent :: __construct( $query, $moduleName, 'al' );
00040 }
00041
00042 public function execute() {
00043 $this->run();
00044 }
00045
00046 public function getCacheMode( $params ) {
00047 return 'public';
00048 }
00049
00050 public function executeGenerator( $resultPageSet ) {
00051 $this->run( $resultPageSet );
00052 }
00053
00054 private function run( $resultPageSet = null ) {
00055
00056 $db = $this->getDB();
00057 $params = $this->extractRequestParams();
00058
00059 $prop = array_flip( $params['prop'] );
00060 $fld_ids = isset( $prop['ids'] );
00061 $fld_title = isset( $prop['title'] );
00062
00063 if ( $params['unique'] ) {
00064 if ( !is_null( $resultPageSet ) )
00065 $this->dieUsage( $this->getModuleName() . ' cannot be used as a generator in unique links mode', 'params' );
00066 if ( $fld_ids )
00067 $this->dieUsage( $this->getModuleName() . ' cannot return corresponding page ids in unique links mode', 'params' );
00068 $this->addOption( 'DISTINCT' );
00069 }
00070
00071 $this->addTables( 'pagelinks' );
00072 $this->addWhereFld( 'pl_namespace', $params['namespace'] );
00073
00074 if ( !is_null( $params['from'] ) && !is_null( $params['continue'] ) )
00075 $this->dieUsage( 'alcontinue and alfrom cannot be used together', 'params' );
00076 if ( !is_null( $params['continue'] ) )
00077 {
00078 $arr = explode( '|', $params['continue'] );
00079 if ( count( $arr ) != 2 )
00080 $this->dieUsage( "Invalid continue parameter", 'badcontinue' );
00081 $from = $this->getDB()->strencode( $this->titleToKey( $arr[0] ) );
00082 $id = intval( $arr[1] );
00083 $this->addWhere( "pl_title > '$from' OR " .
00084 "(pl_title = '$from' AND " .
00085 "pl_from > $id)" );
00086 }
00087
00088 if ( !is_null( $params['from'] ) )
00089 $this->addWhere( 'pl_title>=' . $db->addQuotes( $this->titlePartToKey( $params['from'] ) ) );
00090 if ( isset ( $params['prefix'] ) )
00091 $this->addWhere( 'pl_title' . $db->buildLike( $this->titlePartToKey( $params['prefix'] ), $db->anyString() ) );
00092
00093 $this->addFields( array (
00094 'pl_title',
00095 ) );
00096 $this->addFieldsIf( 'pl_from', !$params['unique'] );
00097
00098 $this->addOption( 'USE INDEX', 'pl_namespace' );
00099 $limit = $params['limit'];
00100 $this->addOption( 'LIMIT', $limit + 1 );
00101 if ( $params['unique'] )
00102 $this->addOption( 'ORDER BY', 'pl_title' );
00103 else
00104 $this->addOption( 'ORDER BY', 'pl_title, pl_from' );
00105
00106 $res = $this->select( __METHOD__ );
00107
00108 $pageids = array ();
00109 $count = 0;
00110 $result = $this->getResult();
00111 while ( $row = $db->fetchObject( $res ) ) {
00112 if ( ++ $count > $limit ) {
00113
00114
00115 if ( $params['unique'] )
00116 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->pl_title ) );
00117 else
00118 $this->setContinueEnumParameter( 'continue', $this->keyToTitle( $row->pl_title ) . "|" . $row->pl_from );
00119 break;
00120 }
00121
00122 if ( is_null( $resultPageSet ) ) {
00123 $vals = array();
00124 if ( $fld_ids )
00125 $vals['fromid'] = intval( $row->pl_from );
00126 if ( $fld_title ) {
00127 $title = Title :: makeTitle( $params['namespace'], $row->pl_title );
00128 ApiQueryBase::addTitleInfo( $vals, $title );
00129 }
00130 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
00131 if ( !$fit )
00132 {
00133 if ( $params['unique'] )
00134 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->pl_title ) );
00135 else
00136 $this->setContinueEnumParameter( 'continue', $this->keyToTitle( $row->pl_title ) . "|" . $row->pl_from );
00137 break;
00138 }
00139 } else {
00140 $pageids[] = $row->pl_from;
00141 }
00142 }
00143 $db->freeResult( $res );
00144
00145 if ( is_null( $resultPageSet ) ) {
00146 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'l' );
00147 } else {
00148 $resultPageSet->populateFromPageIDs( $pageids );
00149 }
00150 }
00151
00152 public function getAllowedParams() {
00153 return array (
00154 'continue' => null,
00155 'from' => null,
00156 'prefix' => null,
00157 'unique' => false,
00158 'prop' => array (
00159 ApiBase :: PARAM_ISMULTI => true,
00160 ApiBase :: PARAM_DFLT => 'title',
00161 ApiBase :: PARAM_TYPE => array (
00162 'ids',
00163 'title'
00164 )
00165 ),
00166 'namespace' => array (
00167 ApiBase :: PARAM_DFLT => 0,
00168 ApiBase :: PARAM_TYPE => 'namespace'
00169 ),
00170 'limit' => array (
00171 ApiBase :: PARAM_DFLT => 10,
00172 ApiBase :: PARAM_TYPE => 'limit',
00173 ApiBase :: PARAM_MIN => 1,
00174 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
00175 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
00176 )
00177 );
00178 }
00179
00180 public function getParamDescription() {
00181 return array (
00182 'from' => 'The page title to start enumerating from.',
00183 'prefix' => 'Search for all page titles that begin with this value.',
00184 'unique' => 'Only show unique links. Cannot be used with generator or prop=ids',
00185 'prop' => 'What pieces of information to include',
00186 'namespace' => 'The namespace to enumerate.',
00187 'limit' => 'How many total links to return.',
00188 'continue' => 'When more results are available, use this to continue.',
00189 );
00190 }
00191
00192 public function getDescription() {
00193 return 'Enumerate all links that point to a given namespace';
00194 }
00195
00196 public function getPossibleErrors() {
00197 return array_merge( parent::getPossibleErrors(), array(
00198 array( 'code' => 'params', 'info' => $this->getModuleName() . ' cannot be used as a generator in unique links mode' ),
00199 array( 'code' => 'params', 'info' => $this->getModuleName() . ' cannot return corresponding page ids in unique links mode' ),
00200 array( 'code' => 'params', 'info' => 'alcontinue and alfrom cannot be used together' ),
00201 array( 'code' => 'badcontinue', 'info' => 'Invalid continue parameter' ),
00202 ) );
00203 }
00204
00205 protected function getExamples() {
00206 return array (
00207 'api.php?action=query&list=alllinks&alunique&alfrom=B',
00208 );
00209 }
00210
00211 public function getVersion() {
00212 return __CLASS__ . ': $Id: ApiQueryAllLinks.php 69932 2010-07-26 08:03:21Z tstarling $';
00213 }
00214 }