$this->run( $resultPageSet );
}
+ /**
+ * @para $resultPageSet ApiPageSet
+ * @return void
+ */
private function run( $resultPageSet = null ) {
$params = $this->extractRequestParams();
- $protocol = $params['protocol'];
$query = $params['query'];
-
- // Find the right prefix
- global $wgUrlProtocols;
- if ( $protocol && !in_array( $protocol, $wgUrlProtocols ) ) {
- foreach ( $wgUrlProtocols as $p ) {
- if ( substr( $p, 0, strlen( $protocol ) ) === $protocol ) {
- $protocol = $p;
- break;
- }
- }
- } else {
- $protocol = null;
- }
+ $protocol = self::getProtocolPrefix( $params['protocol'] );
$db = $this->getDB();
$this->addTables( array( 'page', 'externallinks' ) ); // must be in this order for 'USE INDEX'
return $protocols;
}
+ public static function getProtocolPrefix( $protocol ) {
+ // Find the right prefix
+ global $wgUrlProtocols;
+ if ( $protocol && !in_array( $protocol, $wgUrlProtocols ) ) {
+ foreach ( $wgUrlProtocols as $p ) {
+ if ( substr( $p, 0, strlen( $protocol ) ) === $protocol ) {
+ $protocol = $p;
+ break;
+ }
+ }
+
+ return $protocol;
+ } else {
+ return null;
+ }
+ }
+
public function getParamDescription() {
$p = $this->getModulePrefix();
return array(
}
$params = $this->extractRequestParams();
+ $db = $this->getDB();
+
+ $query = $params['query'];
+ $protocol = ApiQueryExtLinksUsage::getProtocolPrefix( $params['protocol'] );
+
$this->addFields( array(
'el_from',
'el_to'
$this->addTables( 'externallinks' );
$this->addWhereFld( 'el_from', array_keys( $this->getPageSet()->getGoodTitles() ) );
+ //TODO: Refactor out,duplicated from ApiQueryExtLinksUsage
+ if ( !is_null( $query ) || $query != '' ) {
+ if ( is_null( $protocol ) ) {
+ $protocol = 'http://';
+ }
+
+ $likeQuery = LinkFilter::makeLikeArray( $query, $protocol );
+ if ( !$likeQuery ) {
+ $this->dieUsage( 'Invalid query', 'bad_query' );
+ }
+
+ $likeQuery = LinkFilter::keepOneWildcard( $likeQuery );
+ $this->addWhere( 'el_index ' . $db->buildLike( $likeQuery ) );
+ } elseif ( !is_null( $protocol ) ) {
+ $this->addWhere( 'el_index ' . $db->buildLike( "$protocol", $db->anyString() ) );
+ }
+
// Don't order by el_from if it's constant in the WHERE clause
if ( count( $this->getPageSet()->getGoodTitles() ) != 1 ) {
$this->addOption( 'ORDER BY', 'el_from' );
ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
),
'offset' => null,
+ 'protocol' => array(
+ ApiBase::PARAM_TYPE => ApiQueryExtLinksUsage::prepareProtocols(),
+ ApiBase::PARAM_DFLT => '',
+ ),
+ 'query' => null,
);
}
public function getParamDescription() {
+ $p = $this->getModulePrefix();
return array(
'limit' => 'How many links to return',
'offset' => 'When more results are available, use this to continue',
+ 'protocol' => array(
+ "Protocol of the url. If empty and {$p}query set, the protocol is http.",
+ "Leave both this and {$p}query empty to list all external links"
+ ),
+ 'query' => 'Search string without protocol. Useful for checking whether a certain page contains a certain external url',
);
}
return 'Returns all external urls (not interwikies) from the given page(s)';
}
+ public function getPossibleErrors() {
+ return array_merge( parent::getPossibleErrors(), array(
+ array( 'code' => 'bad_query', 'info' => 'Invalid query' ),
+ ) );
+ }
+
protected function getExamples() {
return array(
'Get a list of external links on the [[Main Page]]:',