MediaWiki
REL1_21
|
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 $to = $row->el_to; 00090 // expand protocol-relative urls 00091 if( $params['expandurl'] ) { 00092 $to = wfExpandUrl( $to, PROTO_CANONICAL ); 00093 } 00094 ApiResult::setContent( $entry, $to ); 00095 $fit = $this->addPageSubItem( $row->el_from, $entry ); 00096 if ( !$fit ) { 00097 $this->setContinueEnumParameter( 'offset', $offset + $count - 1 ); 00098 break; 00099 } 00100 } 00101 } 00102 00103 public function getCacheMode( $params ) { 00104 return 'public'; 00105 } 00106 00107 public function getAllowedParams() { 00108 return array( 00109 'limit' => array( 00110 ApiBase::PARAM_DFLT => 10, 00111 ApiBase::PARAM_TYPE => 'limit', 00112 ApiBase::PARAM_MIN => 1, 00113 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1, 00114 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2 00115 ), 00116 'offset' => array( 00117 ApiBase::PARAM_TYPE => 'integer' 00118 ), 00119 'protocol' => array( 00120 ApiBase::PARAM_TYPE => ApiQueryExtLinksUsage::prepareProtocols(), 00121 ApiBase::PARAM_DFLT => '', 00122 ), 00123 'query' => null, 00124 'expandurl' => false, 00125 ); 00126 } 00127 00128 public function getParamDescription() { 00129 $p = $this->getModulePrefix(); 00130 return array( 00131 'limit' => 'How many links to return', 00132 'offset' => 'When more results are available, use this to continue', 00133 'protocol' => array( 00134 "Protocol of the url. If empty and {$p}query set, the protocol is http.", 00135 "Leave both this and {$p}query empty to list all external links" 00136 ), 00137 'query' => 'Search string without protocol. Useful for checking whether a certain page contains a certain external url', 00138 'expandurl' => 'Expand protocol-relative urls with the canonical protocol', 00139 ); 00140 } 00141 00142 public function getResultProperties() { 00143 return array( 00144 '' => array( 00145 '*' => 'string' 00146 ) 00147 ); 00148 } 00149 00150 public function getDescription() { 00151 return 'Returns all external urls (not interwikis) from the given page(s)'; 00152 } 00153 00154 public function getPossibleErrors() { 00155 return array_merge( parent::getPossibleErrors(), array( 00156 array( 'code' => 'bad_query', 'info' => 'Invalid query' ), 00157 ) ); 00158 } 00159 00160 public function getExamples() { 00161 return array( 00162 'api.php?action=query&prop=extlinks&titles=Main%20Page' => 'Get a list of external links on the [[Main Page]]', 00163 ); 00164 } 00165 00166 public function getHelpUrls() { 00167 return 'https://www.mediawiki.org/wiki/API:Properties#extlinks_.2F_el'; 00168 } 00169 }