Index: MediaWiki/branches/1.11/includes/api/ApiQueryBacklinks.php
===================================================================
--- MediaWiki/branches/1.11/includes/api/ApiQueryBacklinks.php	(revision 1280)
+++ MediaWiki/branches/1.11/includes/api/ApiQueryBacklinks.php	(revision 1281)
@@ -6,5 +6,5 @@
  * API for MediaWiki 1.8+
  *
- * Copyright (C) 2006 Yuri Astrakhan <FirstnameLastname@gmail.com>
+ * Copyright (C) 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
  *
  * This program is free software; you can redistribute it and/or modify
@@ -30,9 +30,14 @@
 
 /**
+ * This is three-in-one module to query:
+ *   * backlinks  - links pointing to the given page,
+ *   * embeddedin - what pages transclude the given page within themselves,
+ *   * imageusage - what pages use the given image
+ * 
  * @addtogroup API
  */
 class ApiQueryBacklinks extends ApiQueryGeneratorBase {
 
-	private $rootTitle, $contRedirs, $contLevel, $contTitle, $contID;
+	private $params, $rootTitle, $contRedirs, $contLevel, $contTitle, $contID;
 
 	// output element name, database column field prefix, database table 
@@ -48,6 +53,6 @@
 			'linktbl' => 'templatelinks'
 		),
-		'imagelinks' => array (
-			'code' => 'il',
+		'imageusage' => array (
+			'code' => 'iu',
 			'prefix' => 'il',
 			'linktbl' => 'imagelinks'
@@ -68,5 +73,5 @@
 		$this->bl_code = $code;
 
-		$this->hasNS = $moduleName !== 'imagelinks';
+		$this->hasNS = $moduleName !== 'imageusage';
 		if ($this->hasNS) {
 			$this->bl_title = $prefix . '_title';
@@ -94,11 +99,11 @@
 
 	private function run($resultPageSet = null) {
-		$continue = $namespace = $redirect = $limit = null;
-		extract($this->extractRequestParams());
-
+		$this->params = $this->extractRequestParams();
+		
+		$redirect = $this->params['redirect'];
 		if ($redirect)
-			ApiBase :: dieDebug(__METHOD__, 'Redirect is not yet been implemented', 'notimplemented');
-
-		$this->processContinue($continue, $redirect);
+			$this->dieDebug('Redirect has not been implemented', 'notimplemented');
+
+		$this->processContinue();
 
 		$this->addFields($this->bl_fields);
@@ -118,13 +123,17 @@
 			$this->addWhereFld($this->bl_ns, $this->rootTitle->getNamespace());
 		$this->addWhereFld($this->bl_title, $this->rootTitle->getDBkey());
-		$this->addWhereFld('page_namespace', $namespace);
+		$this->addWhereFld('page_namespace', $this->params['namespace']);
+
+		if($this->params['filterredir'] == 'redirects')
+			$this->addWhereFld('page_is_redirect', 1);
+		if($this->params['filterredir'] == 'nonredirects')
+			$this->addWhereFld('page_is_redirect', 0);
+
+		$limit = $this->params['limit'];
 		$this->addOption('LIMIT', $limit +1);
 		$this->addOption('ORDER BY', $this->bl_sort);
 
-		if ($redirect)
-			$this->addWhereFld('page_is_redirect', 0);
-
 		$db = $this->getDB();
-		if (!is_null($continue)) {
+		if (!is_null($this->params['continue'])) {
 			$plfrm = intval($this->contID);
 			if ($this->contLevel == 0) {
@@ -151,11 +160,10 @@
 				// We've reached the one extra which shows that there are additional pages to be had. Stop here...
 				if ($redirect) {
-					$ns = $row-> {
-						$this->bl_ns };
-					$t = $row-> {
-						$this->bl_title };
+					$ns = $row-> { $this->bl_ns };
+					$t = $row-> { $this->bl_title };
 					$continue = $this->getContinueRedirStr(false, 0, $ns, $t, $row->page_id);
 				} else
 					$continue = $this->getContinueStr($row->page_id);
+				// TODO: Security issue - if the user has no right to view next title, it will still be shown
 				$this->setContinueEnumParameter('continue', $continue);
 				break;
@@ -163,7 +171,7 @@
 
 			if (is_null($resultPageSet)) {
-				$vals = $this->addRowInfo('page', $row);
+				$vals = $this->extractRowInfo($row);
 				if ($vals)
-					$data[intval($row->page_id)] = $vals;
+					$data[] = $vals;
 			} else {
 				$resultPageSet->processDbRow($row);
@@ -172,5 +180,5 @@
 		$db->freeResult($res);
 
-		if (is_null($resultPageSet)) {
+		if (is_null($resultPageSet) && !empty($data)) {
 			$result = $this->getResult();
 			$result->setIndexedTagName($data, $this->bl_code);
@@ -179,18 +187,32 @@
 	}
 
-	protected function processContinue($continue, $redirect) {
+	private function extractRowInfo($row) {
+
+		$vals = array();
+		$vals['pageid'] = intval($row->page_id);
+		ApiQueryBase :: addTitleInfo($vals, Title :: makeTitle($row->page_namespace, $row->page_title));
+
+		return $vals;
+	}
+
+	protected function processContinue() {
 		$pageSet = $this->getPageSet();
 		$count = $pageSet->getTitleCount();
-		if (!is_null($continue)) {
-			if ($count !== 0)
-				$this->dieUsage("When continuing the {$this->getModuleName()} query, no other titles may be provided", 'titles_on_continue');
-			$this->parseContinueParam($continue, $redirect);
+		
+		if (!is_null($this->params['continue'])) {
+			$this->parseContinueParam();
 
 			// Skip all completed links
 
 		} else {
-			if ($count !== 1)
-				$this->dieUsage("The {$this->getModuleName()} query requires one title to start", 'bad_title_count');
-			$this->rootTitle = current($pageSet->getTitles()); // only one title there
+			$title = $this->params['title'];
+			if (!is_null($title)) {
+				$this->rootTitle = Title :: newFromText($title);
+			} else {  // This case is obsolete. Will support this for a while
+				if ($count !== 1)
+					$this->dieUsage("The {$this->getModuleName()} query requires one title to start", 'bad_title_count');
+				$this->rootTitle = current($pageSet->getTitles()); // only one title there
+				$this->setWarning('Using titles parameter is obsolete for this list. Use ' . $this->encodeParamName('title') . ' instead.');
+			}
 		}
 
@@ -200,7 +222,7 @@
 	}
 
-	protected function parseContinueParam($continue, $redirect) {
-		$continueList = explode('|', $continue);
-		if ($redirect) {
+	protected function parseContinueParam() {
+		$continueList = explode('|', $this->params['continue']);
+		if ($this->params['redirect']) {
 			//
 			// expected redirect-mode parameter:
@@ -216,5 +238,5 @@
 				if (($rootNs !== 0 || $continueList[0] === '0') && !empty ($continueList[1])) {
 					$this->rootTitle = Title :: makeTitleSafe($rootNs, $continueList[1]);
-					if ($this->rootTitle && $this->rootTitle->userCanRead()) {
+					if ($this->rootTitle) {
 
 						$step = intval($continueList[2]);
@@ -264,5 +286,5 @@
 				if (($rootNs !== 0 || $continueList[0] === '0') && !empty ($continueList[1])) {
 					$this->rootTitle = Title :: makeTitleSafe($rootNs, $continueList[1]);
-					if ($this->rootTitle && $this->rootTitle->userCanRead()) {
+					if ($this->rootTitle) {
 
 						$contID = intval($continueList[2]);
@@ -297,8 +319,17 @@
 
 		return array (
+			'title' => null,
 			'continue' => null,
 			'namespace' => array (
 				ApiBase :: PARAM_ISMULTI => true,
 				ApiBase :: PARAM_TYPE => 'namespace'
+			),
+			'filterredir' => array(
+				ApiBase :: PARAM_DFLT => 'all',
+				ApiBase :: PARAM_TYPE => array(
+					'all',
+					'redirects',
+					'nonredirects'
+				)
 			),
 			'redirect' => false,
@@ -307,5 +338,5 @@
 				ApiBase :: PARAM_TYPE => 'limit',
 				ApiBase :: PARAM_MIN => 1,
-				ApiBase :: PARAM_MAX1 => ApiBase :: LIMIT_BIG1,
+				ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
 				ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
 			)
@@ -315,6 +346,8 @@
 	protected function getParamDescription() {
 		return array (
+			'title' => 'Title to search. If null, titles= parameter will be used instead, but will be obsolete soon.',
 			'continue' => 'When more results are available, use this to continue.',
 			'namespace' => 'The namespace to enumerate.',
+			'filterredir' => 'How to filter for redirects',
 			'redirect' => 'If linking page is a redirect, find all pages that link to that redirect (not implemented)',
 			'limit' => 'How many total pages to return.'
@@ -328,5 +361,5 @@
 			case 'embeddedin' :
 				return 'Find all pages that embed (transclude) the given title';
-			case 'imagelinks' :
+			case 'imageusage' :
 				return 'Find all pages that use the given image title.';
 			default :
@@ -338,14 +371,14 @@
 		static $examples = array (
 			'backlinks' => array (
-				"api.php?action=query&list=backlinks&titles=Main%20Page",
-				"api.php?action=query&generator=backlinks&titles=Main%20Page&prop=info"
+				"api.php?action=query&list=backlinks&bltitle=Main%20Page",
+				"api.php?action=query&generator=backlinks&gbltitle=Main%20Page&prop=info"
 			),
 			'embeddedin' => array (
-				"api.php?action=query&list=embeddedin&titles=Template:Stub",
-				"api.php?action=query&generator=embeddedin&titles=Template:Stub&prop=info"
+				"api.php?action=query&list=embeddedin&eititle=Template:Stub",
+				"api.php?action=query&generator=embeddedin&geititle=Template:Stub&prop=info"
 			),
-			'imagelinks' => array (
-				"api.php?action=query&list=imagelinks&titles=Image:Albert%20Einstein%20Head.jpg",
-				"api.php?action=query&generator=imagelinks&titles=Image:Albert%20Einstein%20Head.jpg&prop=info"
+			'imageusage' => array (
+				"api.php?action=query&list=imageusage&iutitle=Image:Albert%20Einstein%20Head.jpg",
+				"api.php?action=query&generator=imageusage&giutitle=Image:Albert%20Einstein%20Head.jpg&prop=info"
 			)
 		);
@@ -355,6 +388,6 @@
 
 	public function getVersion() {
-		return __CLASS__ . ': $Id: ApiQueryBacklinks.php 21402 2007-04-20 08:55:14Z nickj $';
+		return __CLASS__ . ': $Id: ApiQueryBacklinks.php 25476 2007-09-04 14:44:46Z catrope $';
 	}
 }
-?>
+
