r66440 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r66439‎ | r66440 | r66441 >
Date:20:54, 14 May 2010
Author:reedy
Status:ok
Tags:
Comment:
Part 1 of Bug 23524 - Api Modules as followup to bug 14473 (Add iwlinks table to track inline interwiki link usage)

Addition of ApiQueryIWLinks (prop=iwlinks)
Modified paths:
  • /trunk/phase3/includes/AutoLoader.php (modified) (history)
  • /trunk/phase3/includes/api/ApiQuery.php (modified) (history)
  • /trunk/phase3/includes/api/ApiQueryIWLinks.php (added) (history)

Diff [purge]

Index: trunk/phase3/includes/api/ApiQuery.php
@@ -49,6 +49,7 @@
5050 'info' => 'ApiQueryInfo',
5151 'revisions' => 'ApiQueryRevisions',
5252 'links' => 'ApiQueryLinks',
 53+ 'iwlinks' => 'ApiQueryIWLinks',
5354 'langlinks' => 'ApiQueryLangLinks',
5455 'images' => 'ApiQueryImages',
5556 'imageinfo' => 'ApiQueryImageInfo',
Index: trunk/phase3/includes/api/ApiQueryIWLinks.php
@@ -0,0 +1,144 @@
 2+<?php
 3+
 4+/**
 5+ * Created on May 14, 2010
 6+ *
 7+ * API for MediaWiki 1.17+
 8+ *
 9+ * Copyright © 2010 Sam Reed
 10+ * Copyright © 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+ * A query module to list all interwiki links
 35+ *
 36+ * @ingroup API
 37+ */
 38+class ApiQueryIWLinks extends ApiQueryBase {
 39+
 40+ public function __construct( $query, $moduleName ) {
 41+ parent::__construct( $query, $moduleName, 'iw' );
 42+ }
 43+
 44+ public function execute() {
 45+ if ( $this->getPageSet()->getGoodTitleCount() == 0 ) {
 46+ return;
 47+ }
 48+
 49+ $params = $this->extractRequestParams();
 50+ $this->addFields( array(
 51+ 'iwl_from',
 52+ 'iwl_prefix',
 53+ 'iwl_title'
 54+ ) );
 55+
 56+ $this->addTables( 'iwlinks' );
 57+ $this->addWhereFld( 'iwl_from', array_keys( $this->getPageSet()->getGoodTitles() ) );
 58+ if ( !is_null( $params['continue'] ) ) {
 59+ $cont = explode( '|', $params['continue'] );
 60+ if ( count( $cont ) != 3 ) {
 61+ $this->dieUsage( 'Invalid continue param. You should pass the ' .
 62+ 'original value returned by the previous query', '_badcontinue' );
 63+ }
 64+ $iwlfrom = intval( $cont[0] );
 65+ $iwlprefix = $this->getDB()->strencode( $cont[1] );
 66+ $iwltitle = $this->getDB()->strencode( $this->titleToKey( $cont[2] ) );
 67+ $this->addWhere(
 68+ "iwl_from > $iwlfrom OR " .
 69+ "(iwl_from = $iwlfrom AND " .
 70+ "(iwl_prefix > '$iwlprefix' OR " .
 71+ "(iwl_prefix = '$iwlprefix' AND " .
 72+ "iwl_title >= '$iwltitle')))"
 73+ );
 74+ }
 75+
 76+ // Don't order by iwl_from if it's constant in the WHERE clause
 77+ if ( count( $this->getPageSet()->getGoodTitles() ) == 1 ) {
 78+ $this->addOption( 'ORDER BY', 'iwl_prefix' );
 79+ } else {
 80+ $this->addOption( 'ORDER BY', 'iwl_from, iwl_prefix' );
 81+ }
 82+ $this->addOption( 'LIMIT', $params['limit'] + 1 );
 83+ $res = $this->select( __METHOD__ );
 84+
 85+ $count = 0;
 86+ $db = $this->getDB();
 87+ while ( $row = $db->fetchObject( $res ) ) {
 88+ if ( ++$count > $params['limit'] ) {
 89+ // We've reached the one extra which shows that
 90+ // there are additional pages to be had. Stop here...
 91+ $this->setContinueEnumParameter( 'continue', "{$row->iwl_from}|{$row->iwl_prefix}|{$row->iwl_title}" );
 92+ break;
 93+ }
 94+ $entry = array( 'prefix' => $row->iwl_prefix );
 95+ ApiResult::setContent( $entry, $row->iwl_title );
 96+ $fit = $this->addPageSubItem( $row->iwl_from, $entry );
 97+ if ( !$fit ) {
 98+ $this->setContinueEnumParameter( 'continue', "{$row->iwl_from}|{$row->iwl_prefix}|{$row->iwl_title}" );
 99+ break;
 100+ }
 101+ }
 102+ $db->freeResult( $res );
 103+ }
 104+
 105+ public function getAllowedParams() {
 106+ return array(
 107+ 'limit' => array(
 108+ ApiBase::PARAM_DFLT => 10,
 109+ ApiBase::PARAM_TYPE => 'limit',
 110+ ApiBase::PARAM_MIN => 1,
 111+ ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
 112+ ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
 113+ ),
 114+ 'continue' => null,
 115+ );
 116+ }
 117+
 118+ public function getParamDescription() {
 119+ return array(
 120+ 'limit' => 'How many interwiki links to return',
 121+ 'continue' => 'When more results are available, use this to continue',
 122+ );
 123+ }
 124+
 125+ public function getDescription() {
 126+ return 'Returns all interwiki links from the given page(s)';
 127+ }
 128+
 129+ public function getPossibleErrors() {
 130+ return array_merge( parent::getPossibleErrors(), array(
 131+ array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
 132+ ) );
 133+ }
 134+
 135+ protected function getExamples() {
 136+ return array(
 137+ 'Get interwiki links from the [[Main Page]]:',
 138+ ' api.php?action=query&prop=iwlinks&titles=Main%20Page&redirects',
 139+ );
 140+ }
 141+
 142+ public function getVersion() {
 143+ return __CLASS__ . ': $Id$';
 144+ }
 145+}
\ No newline at end of file
Property changes on: trunk/phase3/includes/api/ApiQueryIWLinks.php
___________________________________________________________________
Name: svn:eol-style
1146 + native
Name: svn:keywords
2147 + Id
Index: trunk/phase3/includes/AutoLoader.php
@@ -320,6 +320,7 @@
321321 'ApiQueryImageInfo' => 'includes/api/ApiQueryImageInfo.php',
322322 'ApiQueryImages' => 'includes/api/ApiQueryImages.php',
323323 'ApiQueryInfo' => 'includes/api/ApiQueryInfo.php',
 324+ 'ApiQueryIWLinks' => 'includes/api/ApiQueryIWLinks.php',
324325 'ApiQueryLangLinks' => 'includes/api/ApiQueryLangLinks.php',
325326 'ApiQueryLinks' => 'includes/api/ApiQueryLinks.php',
326327 'ApiQueryLogEvents' => 'includes/api/ApiQueryLogEvents.php',

Follow-up revisions

RevisionCommit summaryAuthorDate
r66891* (bug 23524) Api Modules as followup to bug 14473 (Add iwlinks table to trac...reedy19:50, 25 May 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

Status & tagging log