r66891 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r66890‎ | r66891 | r66892 >
Date:19:50, 25 May 2010
Author:reedy
Status:resolved (Comments)
Tags:
Comment:
* (bug 23524) Api Modules as followup to bug 14473 (Add iwlinks table to track inline interwiki link usage

Addition of IWBacklinks... That should be the bug done
Modified paths:
  • /trunk/phase3/RELEASE-NOTES (modified) (history)
  • /trunk/phase3/includes/AutoLoader.php (modified) (history)
  • /trunk/phase3/includes/api/ApiQuery.php (modified) (history)
  • /trunk/phase3/includes/api/ApiQueryIWBacklinks.php (added) (history)

Diff [purge]

Index: trunk/phase3/includes/api/ApiQuery.php
@@ -73,6 +73,7 @@
7474 'embeddedin' => 'ApiQueryBacklinks',
7575 'filearchive' => 'ApiQueryFilearchive',
7676 'imageusage' => 'ApiQueryBacklinks',
 77+ 'iwbacklinks' => 'ApiQueryIWBacklinks',
7778 'logevents' => 'ApiQueryLogEvents',
7879 'recentchanges' => 'ApiQueryRecentChanges',
7980 'search' => 'ApiQuerySearch',
Index: trunk/phase3/includes/api/ApiQueryIWBacklinks.php
@@ -0,0 +1,180 @@
 2+<?php
 3+
 4+/*
 5+ * Created on May 14, 2010
 6+ *
 7+ * API for MediaWiki 1.17+
 8+ *
 9+ * Copyright (C) 2010 Sam Reed
 10+ * Copyright (C) 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
 11+ *
 12+ * This program is free software; you can redistribute it and/or modify
 13+ * it under the terms of the GNU General Public License as published by
 14+ * the Free Software Foundation; either version 2 of the License, or
 15+ * (at your option) any later version.
 16+ *
 17+ * This program is distributed in the hope that it will be useful,
 18+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
 19+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 20+ * GNU General Public License for more details.
 21+ *
 22+ * You should have received a copy of the GNU General Public License along
 23+ * with this program; if not, write to the Free Software Foundation, Inc.,
 24+ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 25+ * http://www.gnu.org/copyleft/gpl.html
 26+ */
 27+
 28+if ( !defined( 'MEDIAWIKI' ) ) {
 29+ // Eclipse helper - will be ignored in production
 30+ require_once( "ApiQueryBase.php" );
 31+}
 32+
 33+/**
 34+ * This gives links pointing to the given interwiki
 35+ * @ingroup API
 36+ */
 37+class ApiQueryIWBacklinks extends ApiQueryBase {
 38+
 39+ public function __construct( $query, $moduleName ) {
 40+ parent::__construct( $query, $moduleName, 'iwbl' );
 41+ }
 42+
 43+ public function execute() {
 44+ $params = $this->extractRequestParams( false );
 45+
 46+ $userMax = ( $this->redirect ? ApiBase::LIMIT_BIG1 / 2 : ApiBase::LIMIT_BIG1 );
 47+ $botMax = ( $this->redirect ? ApiBase::LIMIT_BIG2 / 2 : ApiBase::LIMIT_BIG2 );
 48+
 49+ if ( $params['limit'] == 'max' ) {
 50+ $params['limit'] = $this->getMain()->canApiHighLimits() ? $botMax : $userMax;
 51+ $this->getResult()->addValue( 'limits', $this->getModuleName(), $params['limit'] );
 52+ }
 53+
 54+ if ( !is_null( $params['continue'] ) ) {
 55+ $cont = explode( '|', $params['continue'] );
 56+ if ( count( $cont ) != 3 ) {
 57+ $this->dieUsage( 'Invalid continue param. You should pass the ' .
 58+ 'original value returned by the previous query', '_badcontinue' );
 59+ }
 60+
 61+ $from = intval( $cont[0] );
 62+ $prefix = $this->getDB()->strencode( $cont[1] );
 63+ $title = $this->getDB()->strencode( $this->titleToKey( $cont[2] ) );
 64+ $this->addWhere(
 65+ "iwl_from > $from OR " .
 66+ "(iwl_from = $from AND " .
 67+ "(iwl_prefix > '$prefix' OR " .
 68+ "(iwl_prefix = '$prefix' AND " .
 69+ "iwl_title >= '$title')))"
 70+ );
 71+ }
 72+
 73+ $this->addTables( array( 'iwlinks', 'page' ) );
 74+ $this->addWhere( 'iwl_from = page_id' );
 75+
 76+ $this->addFields( array( 'page_id', 'page_title', 'page_namespace', 'page_is_redirect',
 77+ 'iwl_from', 'iwl_prefix', 'iwl_title' ) );
 78+
 79+ if ( $params['prefix'] !== '' ) {
 80+ $this->addWhereFld( 'iwl_prefix', $params['prefix'] );
 81+ }
 82+
 83+ if ( $params['title'] !== '' ) {
 84+ $this->addWhereFld( 'iwl_title', $params['title'] );
 85+ }
 86+
 87+ $this->addWhereFld( 'page_namespace', $params['namespace'] );
 88+
 89+ $this->addOption( 'LIMIT', $params['limit'] + 1 );
 90+ $this->addOption( 'ORDER BY', 'iwl_from' );
 91+ $this->addOption( 'STRAIGHT_JOIN' );
 92+
 93+ $db = $this->getDB();
 94+ $res = $this->select( __METHOD__ . '::firstQuery' );
 95+
 96+ $count = 0;
 97+ $result = $this->getResult();
 98+ while ( $row = $db->fetchObject( $res ) ) {
 99+ if ( ++ $count > $params['limit'] ) {
 100+ // We've reached the one extra which shows that there are additional pages to be had. Stop here...
 101+ // Continue string preserved in case the redirect query doesn't pass the limit
 102+ $this->setContinueEnumParameter( 'continue', "{$row->iwl_from}|{$row->iwl_prefix}|{$row->iwl_title}" );
 103+ break;
 104+ }
 105+
 106+ $entry = array();
 107+
 108+ $entry['pageid'] = intval( $row->page_id );
 109+ $entry['ns'] = $row->page_namespace;
 110+ $entry['title'] = $row->page_title;
 111+
 112+ if ( $row->page_is_redirect ) {
 113+ $entry['redirect'] = '';
 114+ }
 115+
 116+ $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $entry );
 117+ if ( !$fit ) {
 118+ $this->setContinueEnumParameter( 'continue', "{$row->iwl_from}|{$row->iwl_prefix}|{$row->iwl_title}" );
 119+ break;
 120+ }
 121+ }
 122+ $db->freeResult( $res );
 123+
 124+ $this->getResult()->setIndexedTagName_internal(
 125+ array( 'query', $this->getModuleName() ),
 126+ 'iw'
 127+ );
 128+ }
 129+
 130+ public function getAllowedParams() {
 131+ return array(
 132+ 'prefix' => null,
 133+ 'title' => null,
 134+ 'continue' => null,
 135+ 'namespace' => array(
 136+ ApiBase::PARAM_ISMULTI => true,
 137+ ApiBase::PARAM_TYPE => 'namespace'
 138+ ),
 139+ 'limit' => array(
 140+ ApiBase::PARAM_DFLT => 10,
 141+ ApiBase::PARAM_TYPE => 'limit',
 142+ ApiBase::PARAM_MIN => 1,
 143+ ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
 144+ ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
 145+ )
 146+ );
 147+ }
 148+
 149+ public function getParamDescription() {
 150+ return array(
 151+ 'prefix' => 'Prefix for the interwiki',
 152+ 'title' => 'Interwiki link to search for',
 153+ 'continue' => 'When more results are available, use this to continue',
 154+ 'namespace' => 'The namespace to enumerate',
 155+ 'limit' => 'How many total pages to return',
 156+ );
 157+ }
 158+
 159+ public function getDescription() {
 160+ return 'Find all pages that link to the given interwiki link';
 161+ }
 162+
 163+ public function getPossibleErrors() {
 164+ return array_merge( parent::getPossibleErrors(), array(
 165+ array( 'invalidtitle', 'title' ),
 166+ array( 'missingparam', 'title' ),
 167+ array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
 168+ ) );
 169+ }
 170+
 171+ protected function getExamples() {
 172+ return array(
 173+ 'api.php?action=query&list=iwbacklinks&iwbltitle=Test&iwblprefix=wikibooks',
 174+ 'api.php?action=query&generator=iwbacklinks&giwbltitle=Test&iwblprefix=wikibooks&prop=info'
 175+ );
 176+ }
 177+
 178+ public function getVersion() {
 179+ return __CLASS__ . ': $Id$';
 180+ }
 181+}
Property changes on: trunk/phase3/includes/api/ApiQueryIWBacklinks.php
___________________________________________________________________
Added: svn:eol-style
1182 + native
Added: svn:keywords
2183 + Id
Index: trunk/phase3/includes/AutoLoader.php
@@ -322,6 +322,7 @@
323323 'ApiQueryImages' => 'includes/api/ApiQueryImages.php',
324324 'ApiQueryInfo' => 'includes/api/ApiQueryInfo.php',
325325 'ApiQueryIWLinks' => 'includes/api/ApiQueryIWLinks.php',
 326+ 'ApiQueryIWBacklinks' => 'includes/api/ApiQueryIWBacklinks.php',
326327 'ApiQueryLangLinks' => 'includes/api/ApiQueryLangLinks.php',
327328 'ApiQueryLinks' => 'includes/api/ApiQueryLinks.php',
328329 'ApiQueryLogEvents' => 'includes/api/ApiQueryLogEvents.php',
Index: trunk/phase3/RELEASE-NOTES
@@ -187,6 +187,7 @@
188188 * (bug 21346) Make deleted images searchable by hash
189189 * (bug 23461) Normalise usage of parameter names in parameter descriptions
190190 * (bug 23548) Allow access of another users watchlist through watchlistraw using token and username
 191+* (bug 23524) Api Modules as followup to bug 14473 (Add iwlinks table to track inline interwiki link usage
191192
192193 === Languages updated in 1.17 ===
193194

Follow-up revisions

RevisionCommit summaryAuthorDate
r66899Update description of ApiQueryIWBacklinks as per IRC with Roan...reedy21:48, 25 May 2010
r67669Fixup mixture of copy pasta fail, and some oversights from r66891 due to comm...reedy20:50, 8 June 2010

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r65104* (bug 14473) Add iwlinks table to track inline interwiki link usage...brion01:40, 16 April 2010
r66440Part 1 of Bug 23524 - Api Modules as followup to bug 14473 (Add iwlinks table...reedy20:54, 14 May 2010
r66872bug 23524 - &url for ApiQueryIWLinks to expand full URLreedy13:33, 25 May 2010

Comments

#Comment by Catrope (talk | contribs)   19:48, 8 June 2010
+		$userMax = ( $this->redirect ? ApiBase::LIMIT_BIG1 / 2 : ApiBase::LIMIT_BIG1 );
+		$botMax  = ( $this->redirect ? ApiBase::LIMIT_BIG2 / 2 : ApiBase::LIMIT_BIG2 );

$this->redirect is not set or read anywhere else. Looks like this is a copypaste fail from ApiQueryBacklinks

+		if ( $params['limit'] == 'max' ) {
+			$params['limit'] = $this->getMain()->canApiHighLimits() ? $botMax : $userMax;
+			$this->getResult()->addValue( 'limits', $this->getModuleName(), $params['limit'] );
+		}

...which means you can also ditch this code...

+		$params = $this->extractRequestParams( false );

...and remove the parameter here.

+		if ( $params['prefix'] !== '' ) {
+			$this->addWhereFld( 'iwl_prefix', $params['prefix'] );
+		}
+		
+		if ( $params['title'] !== '' ) {
+			$this->addWhereFld( 'iwl_title', $params['title'] );
+		}

Two problems here:

  1. There is no index on (prefix, title, from), although IMO such an index makes a lot of sense and should be added
  2. Even with reasonable indexing, setting title but not setting prefix will result in an inefficient query and should be disallowed
+		$this->addWhereFld( 'page_namespace', $params['namespace'] );

This is very inefficient, similar features have been disabled in the past. Just say no ;)

+		$this->addOption( 'STRAIGHT_JOIN' );

Are you sure you need this?

+		$res = $this->select( __METHOD__ . '::firstQuery' );

Another ApiQueryBacklinks remnant.

+			$entry['pageid'] = intval( $row->page_id );
+			$entry['ns'] = $row->page_namespace;

Might as well intval() the latter too.

+	protected function getExamples() {
+		return array(
+				'api.php?action=query&list=iwbacklinks&iwbltitle=Test&iwblprefix=wikibooks',
+				'api.php?action=query&generator=iwbacklinks&giwbltitle=Test&iwblprefix=wikibooks&prop=info'
+			);
+	}

The indentation is messed up here.

#Comment by Reedy (talk | contribs)   11:16, 9 June 2010

r67669 fixes up the code issues

bug 22744 repurposed to include this missing index

Status & tagging log