r88047 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r88046‎ | r88047 | r88048 >
Date:11:29, 14 May 2011
Author:reedy
Status:resolved (Comments)
Tags:
Comment:
* (bug 28963) add langbacklinks module to api

Completely untested at this point, that's being done next
Modified paths:
  • /trunk/phase3/RELEASE-NOTES-1.19 (modified) (history)
  • /trunk/phase3/includes/AutoLoader.php (modified) (history)
  • /trunk/phase3/includes/api/ApiQuery.php (modified) (history)
  • /trunk/phase3/includes/api/ApiQueryLangBacklinks.php (added) (history)

Diff [purge]

Index: trunk/phase3/RELEASE-NOTES-1.19
@@ -36,6 +36,7 @@
3737
3838 === API changes in 1.19 ===
3939 * (bug 27790) add query type for querymodules to action=paraminfo
 40+* (bug 28963) add langbacklinks module to api
4041
4142 === Languages updated in 1.19 ===
4243
Index: trunk/phase3/includes/api/ApiQuery.php
@@ -82,6 +82,7 @@
8383 'filearchive' => 'ApiQueryFilearchive',
8484 'imageusage' => 'ApiQueryBacklinks',
8585 'iwbacklinks' => 'ApiQueryIWBacklinks',
 86+ 'iwlanglinks' => 'ApiQueryLangLinks',
8687 'logevents' => 'ApiQueryLogEvents',
8788 'recentchanges' => 'ApiQueryRecentChanges',
8889 'search' => 'ApiQuerySearch',
Index: trunk/phase3/includes/api/ApiQueryLangBacklinks.php
@@ -0,0 +1,220 @@
 2+<?php
 3+/**
 4+ * API for MediaWiki 1.17+
 5+ *
 6+ * Created on May 14, 2011
 7+ *
 8+ * Copyright © 2011 Sam Reed
 9+ * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
 10+ *
 11+ * This program is free software; you can redistribute it and/or modify
 12+ * it under the terms of the GNU General Public License as published by
 13+ * the Free Software Foundation; either version 2 of the License, or
 14+ * (at your option) any later version.
 15+ *
 16+ * This program is distributed in the hope that it will be useful,
 17+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
 18+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 19+ * GNU General Public License for more details.
 20+ *
 21+ * You should have received a copy of the GNU General Public License along
 22+ * with this program; if not, write to the Free Software Foundation, Inc.,
 23+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 24+ * http://www.gnu.org/copyleft/gpl.html
 25+ *
 26+ * @file
 27+ */
 28+
 29+if ( !defined( 'MEDIAWIKI' ) ) {
 30+ // Eclipse helper - will be ignored in production
 31+ require_once( "ApiQueryBase.php" );
 32+}
 33+
 34+/**
 35+ * This gives links pointing to the given interwiki
 36+ * @ingroup API
 37+ */
 38+class ApiQueryLangBacklinks extends ApiQueryGeneratorBase {
 39+
 40+ public function __construct( $query, $moduleName ) {
 41+ parent::__construct( $query, $moduleName, 'lbl' );
 42+ }
 43+
 44+ public function execute() {
 45+ $this->run();
 46+ }
 47+
 48+ public function executeGenerator( $resultPageSet ) {
 49+ $this->run( $resultPageSet );
 50+ }
 51+
 52+ /**
 53+ * @param $resultPageSet ApiPageSet
 54+ * @return void
 55+ */
 56+ public function run( $resultPageSet = null ) {
 57+ $params = $this->extractRequestParams();
 58+
 59+ if ( isset( $params['title'] ) && !isset( $params['lang'] ) ) {
 60+ $this->dieUsageMsg( array( 'missingparam', 'lang' ) );
 61+ }
 62+
 63+ if ( !is_null( $params['continue'] ) ) {
 64+ $cont = explode( '|', $params['continue'] );
 65+ if ( count( $cont ) != 3 ) {
 66+ $this->dieUsage( 'Invalid continue param. You should pass the ' .
 67+ 'original value returned by the previous query', '_badcontinue' );
 68+ }
 69+
 70+ $prefix = $this->getDB()->strencode( $cont[0] );
 71+ $title = $this->getDB()->strencode( $this->titleToKey( $cont[1] ) );
 72+ $from = intval( $cont[2] );
 73+ $this->addWhere(
 74+ "ll_lang > '$prefix' OR " .
 75+ "(ll_lang = '$prefix' AND " .
 76+ "(ll_title > '$title' OR " .
 77+ "(ll_title = '$title' AND " .
 78+ "ll_from >= $from)))"
 79+ );
 80+ }
 81+
 82+ $prop = array_flip( $params['prop'] );
 83+ $lllang = isset( $prop['llang'] );
 84+ $lltitle = isset( $prop['lltitle'] );
 85+
 86+ $this->addTables( array( 'langlinks', 'page' ) );
 87+ $this->addWhere( 'll_from = page_id' );
 88+
 89+ $this->addFields( array( 'page_id', 'page_title', 'page_namespace', 'page_is_redirect',
 90+ 'll_from', 'iwl_lang', 'll_title' ) );
 91+
 92+ if ( isset( $params['lang'] ) ) {
 93+ $this->addWhereFld( 'll_lang', $params['lang'] );
 94+ if ( isset( $params['title'] ) ) {
 95+ $this->addWhereFld( 'll_title', $params['title'] );
 96+ $this->addOption( 'ORDER BY', 'll_from' );
 97+ } else {
 98+ $this->addOption( 'ORDER BY', 'll_title, ll_from' );
 99+ }
 100+ } else {
 101+ $this->addOption( 'ORDER BY', 'll_lang, ll_title, ll_from' );
 102+ }
 103+
 104+ $this->addOption( 'LIMIT', $params['limit'] + 1 );
 105+
 106+ $res = $this->select( __METHOD__ );
 107+
 108+ $pages = array();
 109+
 110+ $count = 0;
 111+ $result = $this->getResult();
 112+ foreach ( $res as $row ) {
 113+ if ( ++ $count > $params['limit'] ) {
 114+ // We've reached the one extra which shows that there are additional pages to be had. Stop here...
 115+ // Continue string preserved in case the redirect query doesn't pass the limit
 116+ $this->setContinueEnumParameter( 'continue', "{$row->ll_lang}|{$row->ll_title}|{$row->ll_from}" );
 117+ break;
 118+ }
 119+
 120+ if ( !is_null( $resultPageSet ) ) {
 121+ $pages[] = Title::newFromRow( $row );
 122+ } else {
 123+ $entry = array( 'pageid' => $row->page_id );
 124+
 125+ $title = Title::makeTitle( $row->page_namespace, $row->page_title );
 126+ ApiQueryBase::addTitleInfo( $entry, $title );
 127+
 128+ if ( $row->page_is_redirect ) {
 129+ $entry['redirect'] = '';
 130+ }
 131+
 132+ if ( $lllang ) {
 133+ $entry['iwprefix'] = $row->ll_lang;
 134+ }
 135+
 136+ if ( $lltitle ) {
 137+ $entry['iwtitle'] = $row->ll_title;
 138+ }
 139+
 140+ $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $entry );
 141+ if ( !$fit ) {
 142+ $this->setContinueEnumParameter( 'continue', "{$row->ll_lang}|{$row->ll_title}|{$row->ll_from}" );
 143+ break;
 144+ }
 145+ }
 146+ }
 147+
 148+ if ( is_null( $resultPageSet ) ) {
 149+ $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'll' );
 150+ } else {
 151+ $resultPageSet->populateFromTitles( $pages );
 152+ }
 153+ }
 154+
 155+ public function getCacheMode( $params ) {
 156+ return 'public';
 157+ }
 158+
 159+ public function getAllowedParams() {
 160+ return array(
 161+ 'prefix' => null,
 162+ 'title' => null,
 163+ 'continue' => null,
 164+ 'limit' => array(
 165+ ApiBase::PARAM_DFLT => 10,
 166+ ApiBase::PARAM_TYPE => 'limit',
 167+ ApiBase::PARAM_MIN => 1,
 168+ ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
 169+ ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
 170+ ),
 171+ 'prop' => array(
 172+ ApiBase::PARAM_ISMULTI => true,
 173+ ApiBase::PARAM_DFLT => '',
 174+ ApiBase::PARAM_TYPE => array(
 175+ 'lllang',
 176+ 'lltitle',
 177+ ),
 178+ ),
 179+ );
 180+ }
 181+
 182+ public function getParamDescription() {
 183+ return array(
 184+ 'lang' => 'Language for the language link',
 185+ 'title' => "Language link to search for. Must be used with {$this->getModulePrefix()}prefix",
 186+ 'continue' => 'When more results are available, use this to continue',
 187+ 'prop' => array(
 188+ 'Which properties to get',
 189+ ' lllang - Adds the language code of the language link',
 190+ ' lltitle - Adds the title of the language ink',
 191+ ),
 192+ 'limit' => 'How many total pages to return',
 193+ );
 194+ }
 195+
 196+ public function getDescription() {
 197+ return array( 'Find all pages that link to the given language link.',
 198+ 'Can be used to find all links with a language code, or',
 199+ 'all links to a title (with a given language).',
 200+ 'Using neither parameter is effectively "All Language Links"',
 201+ );
 202+ }
 203+
 204+ public function getPossibleErrors() {
 205+ return array_merge( parent::getPossibleErrors(), array(
 206+ array( 'missingparam', 'lang' ),
 207+ array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
 208+ ) );
 209+ }
 210+
 211+ protected function getExamples() {
 212+ return array(
 213+ 'api.php?action=query&list=llbacklinks&lbltitle=Test&lbllang=fr',
 214+ 'api.php?action=query&generator=llbacklinks&glbltitle=Test&lbllang=fr&prop=info'
 215+ );
 216+ }
 217+
 218+ public function getVersion() {
 219+ return __CLASS__ . ': $Id$';
 220+ }
 221+}
Property changes on: trunk/phase3/includes/api/ApiQueryLangBacklinks.php
___________________________________________________________________
Added: svn:eol-style
1222 + native
Added: svn:keywords
2223 + Id
Index: trunk/phase3/includes/AutoLoader.php
@@ -317,6 +317,7 @@
318318 'ApiQueryInfo' => 'includes/api/ApiQueryInfo.php',
319319 'ApiQueryIWBacklinks' => 'includes/api/ApiQueryIWBacklinks.php',
320320 'ApiQueryIWLinks' => 'includes/api/ApiQueryIWLinks.php',
 321+ 'ApiQueryLangBacklinks' => 'includes/api/ApiQueryLangBacklinks.php',
321322 'ApiQueryLangLinks' => 'includes/api/ApiQueryLangLinks.php',
322323 'ApiQueryLinks' => 'includes/api/ApiQueryLinks.php',
323324 'ApiQueryLogEvents' => 'includes/api/ApiQueryLogEvents.php',

Follow-up revisions

RevisionCommit summaryAuthorDate
r88049Fix daft loading error I added in r88047reedy12:00, 14 May 2011
r88052Fix up last errors from r88047reedy12:14, 14 May 2011
r88429Fix 2 things from CR in r88047, and one not noticedreedy21:13, 19 May 2011

Comments

#Comment by Duplicatebug (talk | contribs)   18:49, 14 May 2011

Why this (and iwbacklinks) is adding "redirect"? Is that not the job of prop=info?

mistakes:

  • getAllowedParams: "prefix" => "lang"
  • $lllang is always false
#Comment by Catrope (talk | contribs)   19:41, 19 May 2011

$lllang is always false because of a missing l in + $lllang = isset( $prop['llang'] );.

Also:

+			'title' => "Language link to search for. Must be used with {$this->getModulePrefix()}prefix",

should be lang instead of prefix.

Both of these issues are still present post r88052, so marking fixme.

Status & tagging log