MediaWiki  REL1_24
ApiQueryExtLinksUsage.php
Go to the documentation of this file.
00001 <?php
00030 class ApiQueryExtLinksUsage extends ApiQueryGeneratorBase {
00031 
00032     public function __construct( ApiQuery $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         $miser_ns = array();
00063         if ( $this->getConfig()->get( 'MiserMode' ) ) {
00064             $miser_ns = $params['namespace'];
00065         } else {
00066             $this->addWhereFld( 'page_namespace', $params['namespace'] );
00067         }
00068 
00069         $whereQuery = $this->prepareUrlQuerySearchString( $query, $protocol );
00070 
00071         if ( $whereQuery !== null ) {
00072             $this->addWhere( $whereQuery );
00073         }
00074 
00075         $prop = array_flip( $params['prop'] );
00076         $fld_ids = isset( $prop['ids'] );
00077         $fld_title = isset( $prop['title'] );
00078         $fld_url = isset( $prop['url'] );
00079 
00080         if ( is_null( $resultPageSet ) ) {
00081             $this->addFields( array(
00082                 'page_id',
00083                 'page_namespace',
00084                 'page_title'
00085             ) );
00086             $this->addFieldsIf( 'el_to', $fld_url );
00087         } else {
00088             $this->addFields( $resultPageSet->getPageTableFields() );
00089         }
00090 
00091         $limit = $params['limit'];
00092         $offset = $params['offset'];
00093         $this->addOption( 'LIMIT', $limit + 1 );
00094         if ( isset( $offset ) ) {
00095             $this->addOption( 'OFFSET', $offset );
00096         }
00097 
00098         $res = $this->select( __METHOD__ );
00099 
00100         $result = $this->getResult();
00101         $count = 0;
00102         foreach ( $res as $row ) {
00103             if ( ++$count > $limit ) {
00104                 // We've reached the one extra which shows that there are
00105                 // 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                     $to = $row->el_to;
00125                     // expand protocol-relative urls
00126                     if ( $params['expandurl'] ) {
00127                         $to = wfExpandUrl( $to, PROTO_CANONICAL );
00128                     }
00129                     $vals['url'] = $to;
00130                 }
00131                 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
00132                 if ( !$fit ) {
00133                     $this->setContinueEnumParameter( 'offset', $offset + $count - 1 );
00134                     break;
00135                 }
00136             } else {
00137                 $resultPageSet->processDbRow( $row );
00138             }
00139         }
00140 
00141         if ( is_null( $resultPageSet ) ) {
00142             $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ),
00143                 $this->getModulePrefix() );
00144         }
00145     }
00146 
00147     public function getAllowedParams() {
00148         return array(
00149             'prop' => array(
00150                 ApiBase::PARAM_ISMULTI => true,
00151                 ApiBase::PARAM_DFLT => 'ids|title|url',
00152                 ApiBase::PARAM_TYPE => array(
00153                     'ids',
00154                     'title',
00155                     'url'
00156                 )
00157             ),
00158             'offset' => array(
00159                 ApiBase::PARAM_TYPE => 'integer'
00160             ),
00161             'protocol' => array(
00162                 ApiBase::PARAM_TYPE => self::prepareProtocols(),
00163                 ApiBase::PARAM_DFLT => '',
00164             ),
00165             'query' => null,
00166             'namespace' => array(
00167                 ApiBase::PARAM_ISMULTI => true,
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             'expandurl' => false,
00178         );
00179     }
00180 
00181     public static function prepareProtocols() {
00182         global $wgUrlProtocols;
00183         $protocols = array( '' );
00184         foreach ( $wgUrlProtocols as $p ) {
00185             if ( $p !== '//' ) {
00186                 $protocols[] = substr( $p, 0, strpos( $p, ':' ) );
00187             }
00188         }
00189 
00190         return $protocols;
00191     }
00192 
00193     public static function getProtocolPrefix( $protocol ) {
00194         // Find the right prefix
00195         global $wgUrlProtocols;
00196         if ( $protocol && !in_array( $protocol, $wgUrlProtocols ) ) {
00197             foreach ( $wgUrlProtocols as $p ) {
00198                 if ( substr( $p, 0, strlen( $protocol ) ) === $protocol ) {
00199                     $protocol = $p;
00200                     break;
00201                 }
00202             }
00203 
00204             return $protocol;
00205         } else {
00206             return null;
00207         }
00208     }
00209 
00210     public function getParamDescription() {
00211         $p = $this->getModulePrefix();
00212         $desc = array(
00213             'prop' => array(
00214                 'What pieces of information to include',
00215                 ' ids    - Adds the ID of page',
00216                 ' title  - Adds the title and namespace ID of the page',
00217                 ' url    - Adds the URL used in the page',
00218             ),
00219             'offset' => 'Used for paging. Use the value returned for "continue"',
00220             'protocol' => array(
00221                 "Protocol of the URL. If empty and {$p}query set, the protocol is http.",
00222                 "Leave both this and {$p}query empty to list all external links"
00223             ),
00224             'query' => 'Search string without protocol. See [[Special:LinkSearch]]. ' .
00225                 'Leave empty to list all external links',
00226             'namespace' => 'The page namespace(s) to enumerate.',
00227             'limit' => 'How many pages to return.',
00228             'expandurl' => 'Expand protocol-relative URLs with the canonical protocol',
00229         );
00230 
00231         if ( $this->getConfig()->get( 'MiserMode' ) ) {
00232             $desc['namespace'] = array(
00233                 $desc['namespace'],
00234                 "NOTE: Due to \$wgMiserMode, using this may result in fewer than \"{$p}limit\" results",
00235                 'returned before continuing; in extreme cases, zero results may be returned',
00236             );
00237         }
00238 
00239         return $desc;
00240     }
00241 
00242     public function getDescription() {
00243         return 'Enumerate pages that contain a given URL.';
00244     }
00245 
00246     public function getExamples() {
00247         return array(
00248             'api.php?action=query&list=exturlusage&euquery=www.mediawiki.org'
00249         );
00250     }
00251 
00252     public function getHelpUrls() {
00253         return 'https://www.mediawiki.org/wiki/API:Exturlusage';
00254     }
00255 }