MediaWiki  REL1_19
ApiQueryExternalLinks.php
Go to the documentation of this file.
00001 <?php
00032 class ApiQueryExternalLinks extends ApiQueryBase {
00033 
00034         public function __construct( $query, $moduleName ) {
00035                 parent::__construct( $query, $moduleName, 'el' );
00036         }
00037 
00038         public function execute() {
00039                 if ( $this->getPageSet()->getGoodTitleCount() == 0 ) {
00040                         return;
00041                 }
00042 
00043                 $params = $this->extractRequestParams();
00044 
00045                 $query = $params['query'];
00046                 $protocol = ApiQueryExtLinksUsage::getProtocolPrefix( $params['protocol'] );
00047 
00048                 $this->addFields( array(
00049                         'el_from',
00050                         'el_to'
00051                 ) );
00052 
00053                 $this->addTables( 'externallinks' );
00054                 $this->addWhereFld( 'el_from', array_keys( $this->getPageSet()->getGoodTitles() ) );
00055 
00056                 $whereQuery = $this->prepareUrlQuerySearchString( $query, $protocol );
00057 
00058                 if ( $whereQuery !== null ) {
00059                         $this->addWhere( $whereQuery );
00060                 }
00061 
00062                 // Don't order by el_from if it's constant in the WHERE clause
00063                 if ( count( $this->getPageSet()->getGoodTitles() ) != 1 ) {
00064                         $this->addOption( 'ORDER BY', 'el_from' );
00065                 }
00066 
00067                 // If we're querying all protocols, use DISTINCT to avoid repeating protocol-relative links twice
00068                 if ( $protocol === null ) {
00069                         $this->addOption( 'DISTINCT' );
00070                 }
00071 
00072                 $this->addOption( 'LIMIT', $params['limit'] + 1 );
00073                 $offset = isset( $params['offset'] ) ? $params['offset'] : 0;
00074                 if ( $offset ) {
00075                         $this->addOption( 'OFFSET', $params['offset'] );
00076                 }
00077 
00078                 $res = $this->select( __METHOD__ );
00079 
00080                 $count = 0;
00081                 foreach ( $res as $row ) {
00082                         if ( ++$count > $params['limit'] ) {
00083                                 // We've reached the one extra which shows that
00084                                 // there are additional pages to be had. Stop here...
00085                                 $this->setContinueEnumParameter( 'offset', $offset + $params['limit'] );
00086                                 break;
00087                         }
00088                         $entry = array();
00089                         // We *could* run this through wfExpandUrl() but I think it's better to output the link verbatim, even if it's protocol-relative --Roan
00090                         ApiResult::setContent( $entry, $row->el_to );
00091                         $fit = $this->addPageSubItem( $row->el_from, $entry );
00092                         if ( !$fit ) {
00093                                 $this->setContinueEnumParameter( 'offset', $offset + $count - 1 );
00094                                 break;
00095                         }
00096                 }
00097         }
00098 
00099         public function getCacheMode( $params ) {
00100                 return 'public';
00101         }
00102 
00103         public function getAllowedParams() {
00104                 return array(
00105                         'limit' => array(
00106                                 ApiBase::PARAM_DFLT => 10,
00107                                 ApiBase::PARAM_TYPE => 'limit',
00108                                 ApiBase::PARAM_MIN => 1,
00109                                 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
00110                                 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
00111                         ),
00112                         'offset' => array(
00113                                 ApiBase::PARAM_TYPE => 'integer'
00114                         ),
00115                         'protocol' => array(
00116                                 ApiBase::PARAM_TYPE => ApiQueryExtLinksUsage::prepareProtocols(),
00117                                 ApiBase::PARAM_DFLT => '',
00118                         ),
00119                         'query' => null,
00120                 );
00121         }
00122 
00123         public function getParamDescription() {
00124                 $p = $this->getModulePrefix();
00125                 return array(
00126                         'limit' => 'How many links to return',
00127                         'offset' => 'When more results are available, use this to continue',
00128                         'protocol' => array(
00129                                 "Protocol of the url. If empty and {$p}query set, the protocol is http.",
00130                                 "Leave both this and {$p}query empty to list all external links"
00131                         ),
00132                         'query' => 'Search string without protocol. Useful for checking whether a certain page contains a certain external url',
00133                 );
00134         }
00135 
00136         public function getDescription() {
00137                 return 'Returns all external urls (not interwikies) from the given page(s)';
00138         }
00139 
00140         public function getPossibleErrors() {
00141                 return array_merge( parent::getPossibleErrors(), array(
00142                         array( 'code' => 'bad_query', 'info' => 'Invalid query' ),
00143                 ) );
00144         }
00145 
00146         public function getExamples() {
00147                 return array(
00148                         'api.php?action=query&prop=extlinks&titles=Main%20Page' => 'Get a list of external links on the [[Main Page]]',
00149                 );
00150         }
00151 
00152         public function getHelpUrls() {
00153                 return 'https://www.mediawiki.org/wiki/API:Properties#extlinks_.2F_el';
00154         }
00155 
00156         public function getVersion() {
00157                 return __CLASS__ . ': $Id$';
00158         }
00159 }