MediaWiki  REL1_21
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                                         $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                 return $protocols;
00190         }
00191 
00192         public static function getProtocolPrefix( $protocol ) {
00193                 // Find the right prefix
00194                 global $wgUrlProtocols;
00195                 if ( $protocol && !in_array( $protocol, $wgUrlProtocols ) ) {
00196                         foreach ( $wgUrlProtocols as $p ) {
00197                                 if ( substr( $p, 0, strlen( $protocol ) ) === $protocol ) {
00198                                         $protocol = $p;
00199                                         break;
00200                                 }
00201                         }
00202 
00203                         return $protocol;
00204                 } else {
00205                         return null;
00206                 }
00207         }
00208 
00209         public function getParamDescription() {
00210                 global $wgMiserMode;
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]]. Leave empty to list all external links',
00225                         'namespace' => 'The page namespace(s) to enumerate.',
00226                         'limit' => 'How many pages to return.',
00227                         'expandurl' => 'Expand protocol-relative urls with the canonical protocol',
00228                 );
00229 
00230                 if ( $wgMiserMode ) {
00231                         $desc['namespace'] = array(
00232                                 $desc['namespace'],
00233                                 "NOTE: Due to \$wgMiserMode, using this may result in fewer than \"{$p}limit\" results",
00234                                 'returned before continuing; in extreme cases, zero results may be returned',
00235                         );
00236                 }
00237 
00238                 return $desc;
00239         }
00240 
00241         public function getResultProperties() {
00242                 return array(
00243                         'ids' => array(
00244                                 'pageid' => 'integer'
00245                         ),
00246                         'title' => array(
00247                                 'ns' => 'namespace',
00248                                 'title' => 'string'
00249                         ),
00250                         'url' => array(
00251                                 'url' => 'string'
00252                         )
00253                 );
00254         }
00255 
00256         public function getDescription() {
00257                 return 'Enumerate pages that contain a given URL';
00258         }
00259 
00260         public function getPossibleErrors() {
00261                 return array_merge( parent::getPossibleErrors(), array(
00262                         array( 'code' => 'bad_query', 'info' => 'Invalid query' ),
00263                 ) );
00264         }
00265 
00266         public function getExamples() {
00267                 return array(
00268                         'api.php?action=query&list=exturlusage&euquery=www.mediawiki.org'
00269                 );
00270         }
00271 
00272         public function getHelpUrls() {
00273                 return 'https://www.mediawiki.org/wiki/API:Exturlusage';
00274         }
00275 }