MediaWiki  REL1_19
ApiQueryExtLinksUsage.php
Go to the documentation of this file.
00001 <?php
00030 class ApiQueryExtLinksUsage extends ApiQueryGeneratorBase {
00031 
00032         public function __construct( $query, $moduleName ) {
00033                 parent::__construct( $query, $moduleName, 'eu' );
00034         }
00035 
00036         public function execute() {
00037                 $this->run();
00038         }
00039 
00040         public function getCacheMode( $params ) {
00041                 return 'public';
00042         }
00043 
00044         public function executeGenerator( $resultPageSet ) {
00045                 $this->run( $resultPageSet );
00046         }
00047 
00052         private function run( $resultPageSet = null ) {
00053                 $params = $this->extractRequestParams();
00054 
00055                 $query = $params['query'];
00056                 $protocol = self::getProtocolPrefix( $params['protocol'] );
00057 
00058                 $this->addTables( array( 'page', 'externallinks' ) );   // must be in this order for 'USE INDEX'
00059                 $this->addOption( 'USE INDEX', 'el_index' );
00060                 $this->addWhere( 'page_id=el_from' );
00061 
00062                 global $wgMiserMode;
00063                 $miser_ns = array();
00064                 if ( $wgMiserMode ) {
00065                         $miser_ns = $params['namespace'];
00066                 } else {
00067                         $this->addWhereFld( 'page_namespace', $params['namespace'] );
00068                 }
00069 
00070                 $whereQuery = $this->prepareUrlQuerySearchString( $query, $protocol );
00071 
00072                 if ( $whereQuery !== null ) {
00073                         $this->addWhere( $whereQuery );
00074                 }
00075 
00076                 $prop = array_flip( $params['prop'] );
00077                 $fld_ids = isset( $prop['ids'] );
00078                 $fld_title = isset( $prop['title'] );
00079                 $fld_url = isset( $prop['url'] );
00080 
00081                 if ( is_null( $resultPageSet ) ) {
00082                         $this->addFields( array(
00083                                 'page_id',
00084                                 'page_namespace',
00085                                 'page_title'
00086                         ) );
00087                         $this->addFieldsIf( 'el_to', $fld_url );
00088                 } else {
00089                         $this->addFields( $resultPageSet->getPageTableFields() );
00090                 }
00091 
00092                 $limit = $params['limit'];
00093                 $offset = $params['offset'];
00094                 $this->addOption( 'LIMIT', $limit + 1 );
00095                 if ( isset( $offset ) ) {
00096                         $this->addOption( 'OFFSET', $offset );
00097                 }
00098 
00099                 $res = $this->select( __METHOD__ );
00100 
00101                 $result = $this->getResult();
00102                 $count = 0;
00103                 foreach ( $res as $row ) {
00104                         if ( ++ $count > $limit ) {
00105                                 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
00106                                 $this->setContinueEnumParameter( 'offset', $offset + $limit );
00107                                 break;
00108                         }
00109 
00110                         if ( count( $miser_ns ) && !in_array( $row->page_namespace, $miser_ns ) ) {
00111                                 continue;
00112                         }
00113 
00114                         if ( is_null( $resultPageSet ) ) {
00115                                 $vals = array();
00116                                 if ( $fld_ids ) {
00117                                         $vals['pageid'] = intval( $row->page_id );
00118                                 }
00119                                 if ( $fld_title ) {
00120                                         $title = Title::makeTitle( $row->page_namespace, $row->page_title );
00121                                         ApiQueryBase::addTitleInfo( $vals, $title );
00122                                 }
00123                                 if ( $fld_url ) {
00124                                         // We *could* run this through wfExpandUrl() but I think it's better to output the link verbatim, even if it's protocol-relative --Roan
00125                                         $vals['url'] = $row->el_to;
00126                                 }
00127                                 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
00128                                 if ( !$fit ) {
00129                                         $this->setContinueEnumParameter( 'offset', $offset + $count - 1 );
00130                                         break;
00131                                 }
00132                         } else {
00133                                 $resultPageSet->processDbRow( $row );
00134                         }
00135                 }
00136 
00137                 if ( is_null( $resultPageSet ) ) {
00138                         $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ),
00139                                         $this->getModulePrefix() );
00140                 }
00141         }
00142 
00143         public function getAllowedParams() {
00144                 return array(
00145                         'prop' => array(
00146                                 ApiBase::PARAM_ISMULTI => true,
00147                                 ApiBase::PARAM_DFLT => 'ids|title|url',
00148                                 ApiBase::PARAM_TYPE => array(
00149                                         'ids',
00150                                         'title',
00151                                         'url'
00152                                 )
00153                         ),
00154                         'offset' => array(
00155                                 ApiBase::PARAM_TYPE => 'integer'
00156                         ),
00157                         'protocol' => array(
00158                                 ApiBase::PARAM_TYPE => self::prepareProtocols(),
00159                                 ApiBase::PARAM_DFLT => '',
00160                         ),
00161                         'query' => null,
00162                         'namespace' => array(
00163                                 ApiBase::PARAM_ISMULTI => true,
00164                                 ApiBase::PARAM_TYPE => 'namespace'
00165                         ),
00166                         'limit' => array(
00167                                 ApiBase::PARAM_DFLT => 10,
00168                                 ApiBase::PARAM_TYPE => 'limit',
00169                                 ApiBase::PARAM_MIN => 1,
00170                                 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
00171                                 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
00172                         )
00173                 );
00174         }
00175 
00176         public static function prepareProtocols() {
00177                 global $wgUrlProtocols;
00178                 $protocols = array( '' );
00179                 foreach ( $wgUrlProtocols as $p ) {
00180                         if ( $p !== '//' ) {
00181                                 $protocols[] = substr( $p, 0, strpos( $p, ':' ) );
00182                         }
00183                 }
00184                 return $protocols;
00185         }
00186 
00187         public static function getProtocolPrefix( $protocol ) {
00188                 // Find the right prefix
00189                 global $wgUrlProtocols;
00190                 if ( $protocol && !in_array( $protocol, $wgUrlProtocols ) ) {
00191                         foreach ( $wgUrlProtocols as $p ) {
00192                                 if ( substr( $p, 0, strlen( $protocol ) ) === $protocol ) {
00193                                         $protocol = $p;
00194                                         break;
00195                                 }
00196                         }
00197 
00198                         return $protocol;
00199                 } else {
00200                         return null;
00201                 }
00202         }
00203 
00204         public function getParamDescription() {
00205                 global $wgMiserMode;
00206                 $p = $this->getModulePrefix();
00207                 $desc = array(
00208                         'prop' => array(
00209                                 'What pieces of information to include',
00210                                 ' ids    - Adds the ID of page',
00211                                 ' title  - Adds the title and namespace ID of the page',
00212                                 ' url    - Adds the URL used in the page',
00213                         ),
00214                         'offset' => 'Used for paging. Use the value returned for "continue"',
00215                         'protocol' => array(
00216                                 "Protocol of the url. If empty and {$p}query set, the protocol is http.",
00217                                 "Leave both this and {$p}query empty to list all external links"
00218                         ),
00219                         'query' => 'Search string without protocol. See [[Special:LinkSearch]]. Leave empty to list all external links',
00220                         'namespace' => 'The page namespace(s) to enumerate.',
00221                         'limit' => 'How many pages to return.'
00222                 );
00223 
00224                 if ( $wgMiserMode ) {
00225                         $desc['namespace'] = array(
00226                                 $desc['namespace'],
00227                                 "NOTE: Due to \$wgMiserMode, using this may result in fewer than \"{$p}limit\" results",
00228                                 'returned before continuing; in extreme cases, zero results may be returned',
00229                         );
00230                 }
00231 
00232                 return $desc;
00233         }
00234 
00235         public function getDescription() {
00236                 return 'Enumerate pages that contain a given URL';
00237         }
00238 
00239         public function getPossibleErrors() {
00240                 return array_merge( parent::getPossibleErrors(), array(
00241                         array( 'code' => 'bad_query', 'info' => 'Invalid query' ),
00242                 ) );
00243         }
00244 
00245         public function getExamples() {
00246                 return array(
00247                         'api.php?action=query&list=exturlusage&euquery=www.mediawiki.org'
00248                 );
00249         }
00250 
00251         public function getHelpUrls() {
00252                 return 'https://www.mediawiki.org/wiki/API:Exturlusage';
00253         }
00254 
00255         public function getVersion() {
00256                 return __CLASS__ . ': $Id$';
00257         }
00258 }