r79659 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r79658‎ | r79659 | r79660 >
Date:19:45, 5 January 2011
Author:reedy
Status:ok
Tags:
Comment:
* (bug 26485) add a elextlinks param to prop=extlinks

Refactored some code out of ApiQueryExtLinksUsage

Had to duplicate some code into ApiQueryExternalLinks (Boo). Marked with a TODO to fix it up
Modified paths:
  • /trunk/phase3/RELEASE-NOTES (modified) (history)
  • /trunk/phase3/includes/api/ApiQueryExtLinksUsage.php (modified) (history)
  • /trunk/phase3/includes/api/ApiQueryExternalLinks.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/api/ApiQueryExtLinksUsage.php
@@ -50,25 +50,16 @@
5151 $this->run( $resultPageSet );
5252 }
5353
 54+ /**
 55+ * @para $resultPageSet ApiPageSet
 56+ * @return void
 57+ */
5458 private function run( $resultPageSet = null ) {
5559 $params = $this->extractRequestParams();
5660
57 - $protocol = $params['protocol'];
5861 $query = $params['query'];
 62+ $protocol = self::getProtocolPrefix( $params['protocol'] );
5963
60 - // Find the right prefix
61 - global $wgUrlProtocols;
62 - if ( $protocol && !in_array( $protocol, $wgUrlProtocols ) ) {
63 - foreach ( $wgUrlProtocols as $p ) {
64 - if ( substr( $p, 0, strlen( $protocol ) ) === $protocol ) {
65 - $protocol = $p;
66 - break;
67 - }
68 - }
69 - } else {
70 - $protocol = null;
71 - }
72 -
7364 $db = $this->getDB();
7465 $this->addTables( array( 'page', 'externallinks' ) ); // must be in this order for 'USE INDEX'
7566 $this->addOption( 'USE INDEX', 'el_index' );
@@ -195,6 +186,23 @@
196187 return $protocols;
197188 }
198189
 190+ public static function getProtocolPrefix( $protocol ) {
 191+ // Find the right prefix
 192+ global $wgUrlProtocols;
 193+ if ( $protocol && !in_array( $protocol, $wgUrlProtocols ) ) {
 194+ foreach ( $wgUrlProtocols as $p ) {
 195+ if ( substr( $p, 0, strlen( $protocol ) ) === $protocol ) {
 196+ $protocol = $p;
 197+ break;
 198+ }
 199+ }
 200+
 201+ return $protocol;
 202+ } else {
 203+ return null;
 204+ }
 205+ }
 206+
199207 public function getParamDescription() {
200208 $p = $this->getModulePrefix();
201209 return array(
Index: trunk/phase3/includes/api/ApiQueryExternalLinks.php
@@ -46,6 +46,11 @@
4747 }
4848
4949 $params = $this->extractRequestParams();
 50+ $db = $this->getDB();
 51+
 52+ $query = $params['query'];
 53+ $protocol = ApiQueryExtLinksUsage::getProtocolPrefix( $params['protocol'] );
 54+
5055 $this->addFields( array(
5156 'el_from',
5257 'el_to'
@@ -54,6 +59,23 @@
5560 $this->addTables( 'externallinks' );
5661 $this->addWhereFld( 'el_from', array_keys( $this->getPageSet()->getGoodTitles() ) );
5762
 63+ //TODO: Refactor out,duplicated from ApiQueryExtLinksUsage
 64+ if ( !is_null( $query ) || $query != '' ) {
 65+ if ( is_null( $protocol ) ) {
 66+ $protocol = 'http://';
 67+ }
 68+
 69+ $likeQuery = LinkFilter::makeLikeArray( $query, $protocol );
 70+ if ( !$likeQuery ) {
 71+ $this->dieUsage( 'Invalid query', 'bad_query' );
 72+ }
 73+
 74+ $likeQuery = LinkFilter::keepOneWildcard( $likeQuery );
 75+ $this->addWhere( 'el_index ' . $db->buildLike( $likeQuery ) );
 76+ } elseif ( !is_null( $protocol ) ) {
 77+ $this->addWhere( 'el_index ' . $db->buildLike( "$protocol", $db->anyString() ) );
 78+ }
 79+
5880 // Don't order by el_from if it's constant in the WHERE clause
5981 if ( count( $this->getPageSet()->getGoodTitles() ) != 1 ) {
6082 $this->addOption( 'ORDER BY', 'el_from' );
@@ -98,13 +120,24 @@
99121 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
100122 ),
101123 'offset' => null,
 124+ 'protocol' => array(
 125+ ApiBase::PARAM_TYPE => ApiQueryExtLinksUsage::prepareProtocols(),
 126+ ApiBase::PARAM_DFLT => '',
 127+ ),
 128+ 'query' => null,
102129 );
103130 }
104131
105132 public function getParamDescription() {
 133+ $p = $this->getModulePrefix();
106134 return array(
107135 'limit' => 'How many links to return',
108136 'offset' => 'When more results are available, use this to continue',
 137+ 'protocol' => array(
 138+ "Protocol of the url. If empty and {$p}query set, the protocol is http.",
 139+ "Leave both this and {$p}query empty to list all external links"
 140+ ),
 141+ 'query' => 'Search string without protocol. Useful for checking whether a certain page contains a certain external url',
109142 );
110143 }
111144
@@ -112,6 +145,12 @@
113146 return 'Returns all external urls (not interwikies) from the given page(s)';
114147 }
115148
 149+ public function getPossibleErrors() {
 150+ return array_merge( parent::getPossibleErrors(), array(
 151+ array( 'code' => 'bad_query', 'info' => 'Invalid query' ),
 152+ ) );
 153+ }
 154+
116155 protected function getExamples() {
117156 return array(
118157 'Get a list of external links on the [[Main Page]]:',
Index: trunk/phase3/RELEASE-NOTES
@@ -77,6 +77,7 @@
7878 * (bug 26460) Add support for listing category members by category pageid
7979 * (bug 26482) add a imimages param to prop=images
8080 * (bug 26498) allow LinksUpdate with API
 81+* (bug 26485) add a elextlinks param to prop=extlinks
8182
8283 === Languages updated in 1.18 ===
8384

Follow-up revisions

RevisionCommit summaryAuthorDate
r82915Refactor out the duplication I left with a TODO in r79659reedy21:31, 27 February 2011

Status & tagging log