2006-09-25 04:12:07 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Created on Sep 24, 2006
|
|
|
|
|
*
|
|
|
|
|
* API for MediaWiki 1.8+
|
|
|
|
|
*
|
2007-05-20 23:31:44 +00:00
|
|
|
* Copyright (C) 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
|
2006-09-25 04:12:07 +00:00
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
if (!defined('MEDIAWIKI')) {
|
|
|
|
|
// Eclipse helper - will be ignored in production
|
2006-10-01 02:02:13 +00:00
|
|
|
require_once ('ApiQueryBase.php');
|
2006-09-25 04:12:07 +00:00
|
|
|
}
|
|
|
|
|
|
2007-04-20 08:55:14 +00:00
|
|
|
/**
|
2007-05-20 23:31:44 +00:00
|
|
|
* This class contains a list of pages that the client has requested.
|
|
|
|
|
* Initially, when the client passes in titles=, pageids=, or revisions= parameter,
|
|
|
|
|
* an instance of the ApiPageSet class will normalize titles,
|
|
|
|
|
* determine if the pages/revisions exist, and prefetch any additional data page data requested.
|
|
|
|
|
*
|
|
|
|
|
* When generator is used, the result of the generator will become the input for the
|
|
|
|
|
* second instance of this class, and all subsequent actions will go use the second instance
|
|
|
|
|
* for all their work.
|
|
|
|
|
*
|
2007-04-20 08:55:14 +00:00
|
|
|
* @addtogroup API
|
|
|
|
|
*/
|
2006-09-26 06:37:26 +00:00
|
|
|
class ApiPageSet extends ApiQueryBase {
|
2006-09-25 04:12:07 +00:00
|
|
|
|
2008-03-18 15:17:24 +00:00
|
|
|
private $mAllPages; // [ns][dbkey] => page_id or negative when missing
|
|
|
|
|
private $mTitles, $mGoodTitles, $mMissingTitles, $mInvalidTitles;
|
|
|
|
|
private $mMissingPageIDs, $mRedirectTitles;
|
2007-07-07 03:05:09 +00:00
|
|
|
private $mNormalizedTitles, $mInterwikiTitles;
|
2006-10-06 01:02:14 +00:00
|
|
|
private $mResolveRedirects, $mPendingRedirectIDs;
|
2006-10-13 04:59:14 +00:00
|
|
|
private $mGoodRevIDs, $mMissingRevIDs;
|
2007-07-07 03:05:09 +00:00
|
|
|
private $mFakePageId;
|
2006-09-25 04:12:07 +00:00
|
|
|
|
2006-10-06 01:02:14 +00:00
|
|
|
private $mRequestedPageFields;
|
2006-10-01 04:38:31 +00:00
|
|
|
|
2006-10-06 01:02:14 +00:00
|
|
|
public function __construct($query, $resolveRedirects = false) {
|
2006-09-26 06:37:26 +00:00
|
|
|
parent :: __construct($query, __CLASS__);
|
2006-09-25 04:12:07 +00:00
|
|
|
|
2006-09-27 05:13:48 +00:00
|
|
|
$this->mAllPages = array ();
|
2006-10-30 00:18:05 +00:00
|
|
|
$this->mTitles = array();
|
2006-09-27 05:13:48 +00:00
|
|
|
$this->mGoodTitles = array ();
|
|
|
|
|
$this->mMissingTitles = array ();
|
2008-03-18 15:17:24 +00:00
|
|
|
$this->mInvalidTitles = array ();
|
2006-10-01 20:17:16 +00:00
|
|
|
$this->mMissingPageIDs = array ();
|
2006-09-27 05:13:48 +00:00
|
|
|
$this->mRedirectTitles = array ();
|
|
|
|
|
$this->mNormalizedTitles = array ();
|
2007-06-14 03:14:44 +00:00
|
|
|
$this->mInterwikiTitles = array ();
|
2006-10-13 04:59:14 +00:00
|
|
|
$this->mGoodRevIDs = array();
|
|
|
|
|
$this->mMissingRevIDs = array();
|
2006-10-01 04:38:31 +00:00
|
|
|
|
2006-10-06 01:02:14 +00:00
|
|
|
$this->mRequestedPageFields = array ();
|
|
|
|
|
$this->mResolveRedirects = $resolveRedirects;
|
|
|
|
|
if($resolveRedirects)
|
|
|
|
|
$this->mPendingRedirectIDs = array();
|
2007-07-07 03:05:09 +00:00
|
|
|
|
|
|
|
|
$this->mFakePageId = -1;
|
2006-10-06 01:02:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function isResolvingRedirects() {
|
|
|
|
|
return $this->mResolveRedirects;
|
2006-10-01 04:38:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function requestField($fieldName) {
|
2006-10-06 01:02:14 +00:00
|
|
|
$this->mRequestedPageFields[$fieldName] = null;
|
2006-09-25 04:12:07 +00:00
|
|
|
}
|
|
|
|
|
|
2006-10-01 20:17:16 +00:00
|
|
|
public function getCustomField($fieldName) {
|
2006-10-06 01:02:14 +00:00
|
|
|
return $this->mRequestedPageFields[$fieldName];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get fields that modules have requested from the page table
|
|
|
|
|
*/
|
|
|
|
|
public function getPageTableFields() {
|
|
|
|
|
// Ensure we get minimum required fields
|
|
|
|
|
$pageFlds = array (
|
|
|
|
|
'page_id' => null,
|
|
|
|
|
'page_namespace' => null,
|
|
|
|
|
'page_title' => null
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// only store non-default fields
|
|
|
|
|
$this->mRequestedPageFields = array_diff_key($this->mRequestedPageFields, $pageFlds);
|
|
|
|
|
|
|
|
|
|
if ($this->mResolveRedirects)
|
|
|
|
|
$pageFlds['page_is_redirect'] = null;
|
|
|
|
|
|
2007-05-19 23:54:22 +00:00
|
|
|
$pageFlds = array_merge($pageFlds, $this->mRequestedPageFields);
|
|
|
|
|
return array_keys($pageFlds);
|
2006-10-01 20:17:16 +00:00
|
|
|
}
|
|
|
|
|
|
2007-07-07 03:05:09 +00:00
|
|
|
/**
|
2008-03-18 15:17:24 +00:00
|
|
|
* Returns an array [ns][dbkey] => page_id for all requested titles.
|
|
|
|
|
* page_id is a unique negative number in case title was not found.
|
|
|
|
|
* Invalid titles will also have negative page IDs and will be in namespace 0
|
2007-07-07 03:05:09 +00:00
|
|
|
*/
|
|
|
|
|
public function getAllTitlesByNamespace() {
|
|
|
|
|
return $this->mAllPages;
|
|
|
|
|
}
|
|
|
|
|
|
2006-10-30 00:18:05 +00:00
|
|
|
/**
|
|
|
|
|
* All Title objects provided.
|
|
|
|
|
* @return array of Title objects
|
|
|
|
|
*/
|
|
|
|
|
public function getTitles() {
|
|
|
|
|
return $this->mTitles;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the number of unique pages (not revisions) in the set.
|
|
|
|
|
*/
|
|
|
|
|
public function getTitleCount() {
|
|
|
|
|
return count($this->mTitles);
|
|
|
|
|
}
|
|
|
|
|
|
2006-09-25 04:12:07 +00:00
|
|
|
/**
|
|
|
|
|
* Title objects that were found in the database.
|
|
|
|
|
* @return array page_id (int) => Title (obj)
|
|
|
|
|
*/
|
2006-09-27 05:13:48 +00:00
|
|
|
public function getGoodTitles() {
|
|
|
|
|
return $this->mGoodTitles;
|
2006-09-25 04:12:07 +00:00
|
|
|
}
|
|
|
|
|
|
2006-10-06 01:02:14 +00:00
|
|
|
/**
|
2006-10-30 00:18:05 +00:00
|
|
|
* Returns the number of found unique pages (not revisions) in the set.
|
2006-10-06 01:02:14 +00:00
|
|
|
*/
|
|
|
|
|
public function getGoodTitleCount() {
|
2006-10-30 00:18:05 +00:00
|
|
|
return count($this->mGoodTitles);
|
2006-10-06 01:02:14 +00:00
|
|
|
}
|
|
|
|
|
|
2006-09-25 04:12:07 +00:00
|
|
|
/**
|
|
|
|
|
* Title objects that were NOT found in the database.
|
2007-07-07 03:05:09 +00:00
|
|
|
* The array's index will be negative for each item
|
2006-09-25 04:12:07 +00:00
|
|
|
* @return array of Title objects
|
|
|
|
|
*/
|
2006-09-27 05:13:48 +00:00
|
|
|
public function getMissingTitles() {
|
|
|
|
|
return $this->mMissingTitles;
|
2006-09-25 04:12:07 +00:00
|
|
|
}
|
2008-03-18 15:17:24 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Titles that were deemed invalid by Title::newFromText()
|
|
|
|
|
* The array's index will be unique and negative for each item
|
|
|
|
|
* @return array of strings (not Title objects)
|
|
|
|
|
*/
|
|
|
|
|
public function getInvalidTitles() {
|
|
|
|
|
return $this->mInvalidTitles;
|
|
|
|
|
}
|
2006-09-25 04:12:07 +00:00
|
|
|
|
2006-10-01 20:17:16 +00:00
|
|
|
/**
|
|
|
|
|
* Page IDs that were not found in the database
|
|
|
|
|
* @return array of page IDs
|
|
|
|
|
*/
|
|
|
|
|
public function getMissingPageIDs() {
|
|
|
|
|
return $this->mMissingPageIDs;
|
|
|
|
|
}
|
|
|
|
|
|
2006-09-25 04:12:07 +00:00
|
|
|
/**
|
|
|
|
|
* Get a list of redirects when doing redirect resolution
|
|
|
|
|
* @return array prefixed_title (string) => prefixed_title (string)
|
|
|
|
|
*/
|
2006-09-27 05:13:48 +00:00
|
|
|
public function getRedirectTitles() {
|
|
|
|
|
return $this->mRedirectTitles;
|
2006-09-25 04:12:07 +00:00
|
|
|
}
|
|
|
|
|
|
2006-09-25 06:10:16 +00:00
|
|
|
/**
|
|
|
|
|
* Get a list of title normalizations - maps the title given
|
|
|
|
|
* with its normalized version.
|
|
|
|
|
* @return array raw_prefixed_title (string) => prefixed_title (string)
|
|
|
|
|
*/
|
2006-09-27 05:13:48 +00:00
|
|
|
public function getNormalizedTitles() {
|
|
|
|
|
return $this->mNormalizedTitles;
|
2006-09-25 06:10:16 +00:00
|
|
|
}
|
2006-09-26 06:37:26 +00:00
|
|
|
|
2007-06-14 03:14:44 +00:00
|
|
|
/**
|
|
|
|
|
* Get a list of interwiki titles - maps the title given
|
|
|
|
|
* with to the interwiki prefix.
|
|
|
|
|
* @return array raw_prefixed_title (string) => interwiki_prefix (string)
|
|
|
|
|
*/
|
|
|
|
|
public function getInterwikiTitles() {
|
|
|
|
|
return $this->mInterwikiTitles;
|
|
|
|
|
}
|
|
|
|
|
|
2006-09-30 08:06:27 +00:00
|
|
|
/**
|
|
|
|
|
* Get the list of revision IDs (requested with revids= parameter)
|
2006-10-13 04:59:14 +00:00
|
|
|
* @return array revID (int) => pageID (int)
|
2006-09-30 08:06:27 +00:00
|
|
|
*/
|
|
|
|
|
public function getRevisionIDs() {
|
2006-10-13 04:59:14 +00:00
|
|
|
return $this->mGoodRevIDs;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Revision IDs that were not found in the database
|
|
|
|
|
* @return array of revision IDs
|
|
|
|
|
*/
|
|
|
|
|
public function getMissingRevisionIDs() {
|
|
|
|
|
return $this->mMissingRevIDs;
|
2006-09-30 08:06:27 +00:00
|
|
|
}
|
2006-10-01 02:02:13 +00:00
|
|
|
|
2006-09-30 08:06:27 +00:00
|
|
|
/**
|
|
|
|
|
* Returns the number of revisions (requested with revids= parameter)
|
|
|
|
|
*/
|
|
|
|
|
public function getRevisionCount() {
|
2006-10-13 04:59:14 +00:00
|
|
|
return count($this->getRevisionIDs());
|
2006-09-30 08:06:27 +00:00
|
|
|
}
|
2006-09-25 06:10:16 +00:00
|
|
|
|
2006-10-06 01:02:14 +00:00
|
|
|
/**
|
|
|
|
|
* Populate from the request parameters
|
|
|
|
|
*/
|
|
|
|
|
public function execute() {
|
|
|
|
|
$this->profileIn();
|
|
|
|
|
$titles = $pageids = $revids = null;
|
|
|
|
|
extract($this->extractRequestParams());
|
|
|
|
|
|
|
|
|
|
// Only one of the titles/pageids/revids is allowed at the same time
|
|
|
|
|
$dataSource = null;
|
|
|
|
|
if (isset ($titles))
|
|
|
|
|
$dataSource = 'titles';
|
|
|
|
|
if (isset ($pageids)) {
|
|
|
|
|
if (isset ($dataSource))
|
|
|
|
|
$this->dieUsage("Cannot use 'pageids' at the same time as '$dataSource'", 'multisource');
|
|
|
|
|
$dataSource = 'pageids';
|
|
|
|
|
}
|
|
|
|
|
if (isset ($revids)) {
|
|
|
|
|
if (isset ($dataSource))
|
|
|
|
|
$this->dieUsage("Cannot use 'revids' at the same time as '$dataSource'", 'multisource');
|
|
|
|
|
$dataSource = 'revids';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch ($dataSource) {
|
|
|
|
|
case 'titles' :
|
|
|
|
|
$this->initFromTitles($titles);
|
|
|
|
|
break;
|
|
|
|
|
case 'pageids' :
|
|
|
|
|
$this->initFromPageIds($pageids);
|
|
|
|
|
break;
|
|
|
|
|
case 'revids' :
|
2006-10-13 04:59:14 +00:00
|
|
|
if($this->mResolveRedirects)
|
|
|
|
|
$this->dieUsage('revids may not be used with redirect resolution', 'params');
|
2006-10-06 01:02:14 +00:00
|
|
|
$this->initFromRevIDs($revids);
|
|
|
|
|
break;
|
|
|
|
|
default :
|
|
|
|
|
// Do nothing - some queries do not need any of the data sources.
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
$this->profileOut();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Initialize PageSet from a list of Titles
|
|
|
|
|
*/
|
|
|
|
|
public function populateFromTitles($titles) {
|
|
|
|
|
$this->profileIn();
|
|
|
|
|
$this->initFromTitles($titles);
|
|
|
|
|
$this->profileOut();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Initialize PageSet from a list of Page IDs
|
|
|
|
|
*/
|
|
|
|
|
public function populateFromPageIDs($pageIDs) {
|
|
|
|
|
$this->profileIn();
|
|
|
|
|
$this->initFromPageIds($pageIDs);
|
|
|
|
|
$this->profileOut();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Initialize PageSet from a rowset returned from the database
|
|
|
|
|
*/
|
|
|
|
|
public function populateFromQueryResult($db, $queryResult) {
|
|
|
|
|
$this->profileIn();
|
|
|
|
|
$this->initFromQueryResult($db, $queryResult);
|
|
|
|
|
$this->profileOut();
|
|
|
|
|
}
|
|
|
|
|
|
2006-10-14 16:02:42 +00:00
|
|
|
/**
|
|
|
|
|
* Initialize PageSet from a list of Revision IDs
|
|
|
|
|
*/
|
|
|
|
|
public function populateFromRevisionIDs($revIDs) {
|
|
|
|
|
$this->profileIn();
|
2006-10-17 09:27:39 +00:00
|
|
|
$revIDs = array_map('intval', $revIDs); // paranoia
|
2006-10-14 16:02:42 +00:00
|
|
|
$this->initFromRevIDs($revIDs);
|
|
|
|
|
$this->profileOut();
|
|
|
|
|
}
|
|
|
|
|
|
2006-10-06 01:02:14 +00:00
|
|
|
/**
|
|
|
|
|
* Extract all requested fields from the row received from the database
|
|
|
|
|
*/
|
|
|
|
|
public function processDbRow($row) {
|
2006-10-25 03:54:56 +00:00
|
|
|
|
2006-10-06 01:02:14 +00:00
|
|
|
// Store Title object in various data structures
|
|
|
|
|
$title = Title :: makeTitle($row->page_namespace, $row->page_title);
|
2006-10-25 03:54:56 +00:00
|
|
|
|
2007-07-14 19:04:31 +00:00
|
|
|
$pageId = intval($row->page_id);
|
|
|
|
|
$this->mAllPages[$row->page_namespace][$row->page_title] = $pageId;
|
|
|
|
|
$this->mTitles[] = $title;
|
|
|
|
|
|
|
|
|
|
if ($this->mResolveRedirects && $row->page_is_redirect == '1') {
|
|
|
|
|
$this->mPendingRedirectIDs[$pageId] = $title;
|
|
|
|
|
} else {
|
|
|
|
|
$this->mGoodTitles[$pageId] = $title;
|
2006-10-06 01:02:14 +00:00
|
|
|
}
|
2007-07-14 19:04:31 +00:00
|
|
|
|
|
|
|
|
foreach ($this->mRequestedPageFields as $fieldName => & $fieldValues)
|
|
|
|
|
$fieldValues[$pageId] = $row-> $fieldName;
|
2006-10-06 01:02:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function finishPageSetGeneration() {
|
|
|
|
|
$this->profileIn();
|
|
|
|
|
$this->resolvePendingRedirects();
|
|
|
|
|
$this->profileOut();
|
|
|
|
|
}
|
|
|
|
|
|
2006-09-25 04:12:07 +00:00
|
|
|
/**
|
|
|
|
|
* This method populates internal variables with page information
|
2006-09-25 06:10:16 +00:00
|
|
|
* based on the given array of title strings.
|
2006-09-25 04:12:07 +00:00
|
|
|
*
|
|
|
|
|
* Steps:
|
|
|
|
|
* #1 For each title, get data from `page` table
|
|
|
|
|
* #2 If page was not found in the DB, store it as missing
|
|
|
|
|
*
|
|
|
|
|
* Additionally, when resolving redirects:
|
|
|
|
|
* #3 If no more redirects left, stop.
|
|
|
|
|
* #4 For each redirect, get its links from `pagelinks` table.
|
|
|
|
|
* #5 Substitute the original LinkBatch object with the new list
|
|
|
|
|
* #6 Repeat from step #1
|
|
|
|
|
*/
|
2006-10-06 01:02:14 +00:00
|
|
|
private function initFromTitles($titles) {
|
2006-10-01 02:02:13 +00:00
|
|
|
|
2006-10-06 01:02:14 +00:00
|
|
|
// Get validated and normalized title objects
|
2007-05-14 05:28:06 +00:00
|
|
|
$linkBatch = $this->processTitlesArray($titles);
|
2006-10-13 06:13:13 +00:00
|
|
|
if($linkBatch->isEmpty())
|
|
|
|
|
return;
|
|
|
|
|
|
2007-01-22 23:50:42 +00:00
|
|
|
$db = $this->getDB();
|
2006-10-06 01:02:14 +00:00
|
|
|
$set = $linkBatch->constructSet('page', $db);
|
2006-10-01 04:38:31 +00:00
|
|
|
|
2006-10-06 01:02:14 +00:00
|
|
|
// Get pageIDs data from the `page` table
|
|
|
|
|
$this->profileDBIn();
|
|
|
|
|
$res = $db->select('page', $this->getPageTableFields(), $set, __METHOD__);
|
|
|
|
|
$this->profileDBOut();
|
2006-10-01 04:38:31 +00:00
|
|
|
|
2006-10-06 01:02:14 +00:00
|
|
|
// Hack: get the ns:titles stored in array(ns => array(titles)) format
|
|
|
|
|
$this->initFromQueryResult($db, $res, $linkBatch->data, true); // process Titles
|
2006-10-01 04:38:31 +00:00
|
|
|
|
2006-10-06 01:02:14 +00:00
|
|
|
// Resolve any found redirects
|
|
|
|
|
$this->resolvePendingRedirects();
|
|
|
|
|
}
|
2006-09-26 06:37:26 +00:00
|
|
|
|
2006-10-06 01:02:14 +00:00
|
|
|
private function initFromPageIds($pageids) {
|
2006-10-13 06:13:13 +00:00
|
|
|
if(empty($pageids))
|
|
|
|
|
return;
|
2007-07-08 03:35:37 +00:00
|
|
|
|
|
|
|
|
$pageids = array_map('intval', $pageids); // paranoia
|
2006-10-06 01:02:14 +00:00
|
|
|
$set = array (
|
|
|
|
|
'page_id' => $pageids
|
|
|
|
|
);
|
2006-09-25 04:12:07 +00:00
|
|
|
|
2007-01-22 23:50:42 +00:00
|
|
|
$db = $this->getDB();
|
2006-10-25 03:54:56 +00:00
|
|
|
|
2006-10-06 01:02:14 +00:00
|
|
|
// Get pageIDs data from the `page` table
|
|
|
|
|
$this->profileDBIn();
|
|
|
|
|
$res = $db->select('page', $this->getPageTableFields(), $set, __METHOD__);
|
|
|
|
|
$this->profileDBOut();
|
|
|
|
|
|
|
|
|
|
$this->initFromQueryResult($db, $res, array_flip($pageids), false); // process PageIDs
|
2006-09-25 04:12:07 +00:00
|
|
|
|
2006-10-06 01:02:14 +00:00
|
|
|
// Resolve any found redirects
|
|
|
|
|
$this->resolvePendingRedirects();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Iterate through the result of the query on 'page' table,
|
|
|
|
|
* and for each row create and store title object and save any extra fields requested.
|
|
|
|
|
* @param $db Database
|
|
|
|
|
* @param $res DB Query result
|
|
|
|
|
* @param $remaining Array of either pageID or ns/title elements (optional).
|
|
|
|
|
* If given, any missing items will go to $mMissingPageIDs and $mMissingTitles
|
|
|
|
|
* @param $processTitles bool Must be provided together with $remaining.
|
|
|
|
|
* If true, treat $remaining as an array of [ns][title]
|
|
|
|
|
* If false, treat it as an array of [pageIDs]
|
|
|
|
|
* @return Array of redirect IDs (only when resolving redirects)
|
|
|
|
|
*/
|
|
|
|
|
private function initFromQueryResult($db, $res, &$remaining = null, $processTitles = null) {
|
|
|
|
|
if (!is_null($remaining) && is_null($processTitles))
|
2006-11-04 05:24:59 +00:00
|
|
|
ApiBase :: dieDebug(__METHOD__, 'Missing $processTitles parameter when $remaining is provided');
|
2006-10-06 01:02:14 +00:00
|
|
|
|
|
|
|
|
while ($row = $db->fetchObject($res)) {
|
2006-09-25 04:12:07 +00:00
|
|
|
|
2006-10-06 01:02:14 +00:00
|
|
|
$pageId = intval($row->page_id);
|
2006-10-01 20:17:16 +00:00
|
|
|
|
2006-10-06 01:02:14 +00:00
|
|
|
// Remove found page from the list of remaining items
|
|
|
|
|
if (isset($remaining)) {
|
2006-10-01 20:17:16 +00:00
|
|
|
if ($processTitles)
|
|
|
|
|
unset ($remaining[$row->page_namespace][$row->page_title]);
|
|
|
|
|
else
|
|
|
|
|
unset ($remaining[$pageId]);
|
2006-09-25 04:12:07 +00:00
|
|
|
}
|
2006-10-06 01:02:14 +00:00
|
|
|
|
|
|
|
|
// Store any extra fields requested by modules
|
|
|
|
|
$this->processDbRow($row);
|
|
|
|
|
}
|
|
|
|
|
$db->freeResult($res);
|
|
|
|
|
|
|
|
|
|
if(isset($remaining)) {
|
|
|
|
|
// Any items left in the $remaining list are added as missing
|
|
|
|
|
if($processTitles) {
|
2006-10-01 20:17:16 +00:00
|
|
|
// The remaining titles in $remaining are non-existant pages
|
|
|
|
|
foreach ($remaining as $ns => $dbkeys) {
|
2006-11-25 17:11:58 +00:00
|
|
|
foreach ( $dbkeys as $dbkey => $unused ) {
|
2006-10-30 00:18:05 +00:00
|
|
|
$title = Title :: makeTitle($ns, $dbkey);
|
2007-07-07 03:05:09 +00:00
|
|
|
$this->mAllPages[$ns][$dbkey] = $this->mFakePageId;
|
|
|
|
|
$this->mMissingTitles[$this->mFakePageId] = $title;
|
|
|
|
|
$this->mFakePageId--;
|
2006-10-30 00:18:05 +00:00
|
|
|
$this->mTitles[] = $title;
|
2006-10-01 20:17:16 +00:00
|
|
|
}
|
|
|
|
|
}
|
2006-09-25 04:12:07 +00:00
|
|
|
}
|
2006-10-06 01:02:14 +00:00
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// The remaining pageids do not exist
|
|
|
|
|
if(empty($this->mMissingPageIDs))
|
|
|
|
|
$this->mMissingPageIDs = array_keys($remaining);
|
|
|
|
|
else
|
|
|
|
|
$this->mMissingPageIDs = array_merge($this->mMissingPageIDs, array_keys($remaining));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2006-09-25 04:12:07 +00:00
|
|
|
|
2006-10-06 01:02:14 +00:00
|
|
|
private function initFromRevIDs($revids) {
|
2006-10-13 04:59:14 +00:00
|
|
|
|
2006-10-13 06:13:13 +00:00
|
|
|
if(empty($revids))
|
|
|
|
|
return;
|
|
|
|
|
|
2007-01-22 23:50:42 +00:00
|
|
|
$db = $this->getDB();
|
2006-10-13 04:59:14 +00:00
|
|
|
$pageids = array();
|
|
|
|
|
$remaining = array_flip($revids);
|
|
|
|
|
|
2006-10-15 07:43:52 +00:00
|
|
|
$tables = array('revision');
|
2006-10-13 04:59:14 +00:00
|
|
|
$fields = array('rev_id','rev_page');
|
2006-10-15 07:43:52 +00:00
|
|
|
$where = array('rev_deleted' => 0, 'rev_id' => $revids);
|
2006-10-13 04:59:14 +00:00
|
|
|
|
|
|
|
|
// Get pageIDs data from the `page` table
|
|
|
|
|
$this->profileDBIn();
|
|
|
|
|
$res = $db->select( $tables, $fields, $where, __METHOD__ );
|
|
|
|
|
while ( $row = $db->fetchObject( $res ) ) {
|
|
|
|
|
$revid = intval($row->rev_id);
|
|
|
|
|
$pageid = intval($row->rev_page);
|
|
|
|
|
$this->mGoodRevIDs[$revid] = $pageid;
|
|
|
|
|
$pageids[$pageid] = '';
|
|
|
|
|
unset($remaining[$revid]);
|
|
|
|
|
}
|
|
|
|
|
$db->freeResult( $res );
|
|
|
|
|
$this->profileDBOut();
|
|
|
|
|
|
|
|
|
|
$this->mMissingRevIDs = array_keys($remaining);
|
|
|
|
|
|
|
|
|
|
// Populate all the page information
|
|
|
|
|
if($this->mResolveRedirects)
|
2006-11-04 05:24:59 +00:00
|
|
|
ApiBase :: dieDebug(__METHOD__, 'revids may not be used with redirect resolution');
|
2006-10-13 06:13:13 +00:00
|
|
|
$this->initFromPageIds(array_keys($pageids));
|
2006-10-06 01:02:14 +00:00
|
|
|
}
|
2006-09-25 04:12:07 +00:00
|
|
|
|
2006-10-06 01:02:14 +00:00
|
|
|
private function resolvePendingRedirects() {
|
2006-09-25 04:12:07 +00:00
|
|
|
|
2006-10-06 01:02:14 +00:00
|
|
|
if($this->mResolveRedirects) {
|
2007-01-22 23:50:42 +00:00
|
|
|
$db = $this->getDB();
|
2006-10-06 01:02:14 +00:00
|
|
|
$pageFlds = $this->getPageTableFields();
|
|
|
|
|
|
|
|
|
|
// Repeat until all redirects have been resolved
|
|
|
|
|
// The infinite loop is prevented by keeping all known pages in $this->mAllPages
|
|
|
|
|
while (!empty ($this->mPendingRedirectIDs)) {
|
|
|
|
|
|
|
|
|
|
// Resolve redirects by querying the pagelinks table, and repeat the process
|
|
|
|
|
// Create a new linkBatch object for the next pass
|
|
|
|
|
$linkBatch = $this->getRedirectTargets();
|
|
|
|
|
|
|
|
|
|
if ($linkBatch->isEmpty())
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
$set = $linkBatch->constructSet('page', $db);
|
|
|
|
|
if(false === $set)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
// Get pageIDs data from the `page` table
|
|
|
|
|
$this->profileDBIn();
|
|
|
|
|
$res = $db->select('page', $pageFlds, $set, __METHOD__);
|
|
|
|
|
$this->profileDBOut();
|
|
|
|
|
|
|
|
|
|
// Hack: get the ns:titles stored in array(ns => array(titles)) format
|
|
|
|
|
$this->initFromQueryResult($db, $res, $linkBatch->data, true);
|
|
|
|
|
}
|
2006-10-01 20:17:16 +00:00
|
|
|
}
|
|
|
|
|
}
|
2006-09-25 04:12:07 +00:00
|
|
|
|
2006-10-06 01:02:14 +00:00
|
|
|
private function getRedirectTargets() {
|
2006-09-25 04:12:07 +00:00
|
|
|
|
2006-10-01 20:17:16 +00:00
|
|
|
$linkBatch = new LinkBatch();
|
2007-01-22 23:50:42 +00:00
|
|
|
$db = $this->getDB();
|
2006-09-25 04:12:07 +00:00
|
|
|
|
2006-10-01 20:17:16 +00:00
|
|
|
// find redirect targets for all redirect pages
|
|
|
|
|
$this->profileDBIn();
|
|
|
|
|
$res = $db->select('pagelinks', array (
|
|
|
|
|
'pl_from',
|
|
|
|
|
'pl_namespace',
|
|
|
|
|
'pl_title'
|
|
|
|
|
), array (
|
2006-10-06 01:02:14 +00:00
|
|
|
'pl_from' => array_keys($this->mPendingRedirectIDs
|
2006-10-01 20:17:16 +00:00
|
|
|
)), __METHOD__);
|
|
|
|
|
$this->profileDBOut();
|
|
|
|
|
|
|
|
|
|
while ($row = $db->fetchObject($res)) {
|
|
|
|
|
|
|
|
|
|
$plfrom = intval($row->pl_from);
|
|
|
|
|
|
|
|
|
|
// Bug 7304 workaround
|
|
|
|
|
// ( http://bugzilla.wikipedia.org/show_bug.cgi?id=7304 )
|
|
|
|
|
// A redirect page may have more than one link.
|
|
|
|
|
// This code will only use the first link returned.
|
2006-10-06 01:02:14 +00:00
|
|
|
if (isset ($this->mPendingRedirectIDs[$plfrom])) { // remove line when bug 7304 is fixed
|
2006-10-01 20:17:16 +00:00
|
|
|
|
2006-10-06 01:02:14 +00:00
|
|
|
$titleStrFrom = $this->mPendingRedirectIDs[$plfrom]->getPrefixedText();
|
2006-10-01 20:17:16 +00:00
|
|
|
$titleStrTo = Title :: makeTitle($row->pl_namespace, $row->pl_title)->getPrefixedText();
|
2006-10-06 01:02:14 +00:00
|
|
|
unset ($this->mPendingRedirectIDs[$plfrom]); // remove line when bug 7304 is fixed
|
2006-10-01 20:17:16 +00:00
|
|
|
|
|
|
|
|
// Avoid an infinite loop by checking if we have already processed this target
|
|
|
|
|
if (!isset ($this->mAllPages[$row->pl_namespace][$row->pl_title])) {
|
|
|
|
|
$linkBatch->add($row->pl_namespace, $row->pl_title);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// This redirect page has more than one link.
|
|
|
|
|
// This is very slow, but safer until bug 7304 is resolved
|
|
|
|
|
$title = Title :: newFromID($plfrom);
|
|
|
|
|
$titleStrFrom = $title->getPrefixedText();
|
|
|
|
|
|
|
|
|
|
$article = new Article($title);
|
|
|
|
|
$text = $article->getContent();
|
|
|
|
|
$titleTo = Title :: newFromRedirect($text);
|
|
|
|
|
$titleStrTo = $titleTo->getPrefixedText();
|
|
|
|
|
|
|
|
|
|
if (is_null($titleStrTo))
|
|
|
|
|
ApiBase :: dieDebug(__METHOD__, 'Bug7304 workaround: redir target from {$title->getPrefixedText()} not found');
|
|
|
|
|
|
|
|
|
|
// Avoid an infinite loop by checking if we have already processed this target
|
|
|
|
|
if (!isset ($this->mAllPages[$titleTo->getNamespace()][$titleTo->getDBkey()])) {
|
|
|
|
|
$linkBatch->addObj($titleTo);
|
2006-09-25 04:12:07 +00:00
|
|
|
}
|
|
|
|
|
}
|
2006-10-01 20:17:16 +00:00
|
|
|
|
|
|
|
|
$this->mRedirectTitles[$titleStrFrom] = $titleStrTo;
|
2006-09-25 04:12:07 +00:00
|
|
|
}
|
2006-10-01 20:17:16 +00:00
|
|
|
$db->freeResult($res);
|
|
|
|
|
|
2006-10-06 01:02:14 +00:00
|
|
|
// All IDs must exist in the page table
|
|
|
|
|
if (!empty($this->mPendingRedirectIDs[$plfrom]))
|
2006-11-04 05:24:59 +00:00
|
|
|
ApiBase :: dieDebug(__METHOD__, 'Invalid redirect IDs were found');
|
2006-10-06 01:02:14 +00:00
|
|
|
|
2006-10-01 20:17:16 +00:00
|
|
|
return $linkBatch;
|
2006-09-27 05:13:48 +00:00
|
|
|
}
|
2006-09-29 07:29:13 +00:00
|
|
|
|
2006-09-27 05:13:48 +00:00
|
|
|
/**
|
|
|
|
|
* Given an array of title strings, convert them into Title objects.
|
2007-06-14 03:14:44 +00:00
|
|
|
* Alternativelly, an array of Title objects may be given.
|
2006-09-27 05:13:48 +00:00
|
|
|
* This method validates access rights for the title,
|
|
|
|
|
* and appends normalization values to the output.
|
|
|
|
|
*
|
|
|
|
|
* @return LinkBatch of title objects.
|
|
|
|
|
*/
|
2007-05-14 05:28:06 +00:00
|
|
|
private function processTitlesArray($titles) {
|
2006-09-27 05:13:48 +00:00
|
|
|
|
|
|
|
|
$linkBatch = new LinkBatch();
|
|
|
|
|
|
2007-05-14 05:28:06 +00:00
|
|
|
foreach ($titles as $title) {
|
|
|
|
|
|
|
|
|
|
$titleObj = is_string($title) ? Title :: newFromText($title) : $title;
|
2006-09-27 05:13:48 +00:00
|
|
|
if (!$titleObj)
|
2008-03-18 15:17:24 +00:00
|
|
|
{
|
|
|
|
|
# Handle invalid titles gracefully
|
|
|
|
|
$this->mAllpages[0][$title] = $this->mFakePageId;
|
|
|
|
|
$this->mInvalidTitles[$this->mFakePageId] = $title;
|
|
|
|
|
$this->mFakePageId--;
|
|
|
|
|
continue; // There's nothing else we can do
|
|
|
|
|
}
|
2007-06-14 03:14:44 +00:00
|
|
|
$iw = $titleObj->getInterwiki();
|
|
|
|
|
if (!empty($iw)) {
|
|
|
|
|
// This title is an interwiki link.
|
|
|
|
|
$this->mInterwikiTitles[$titleObj->getPrefixedText()] = $iw;
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
// Validation
|
|
|
|
|
if ($titleObj->getNamespace() < 0)
|
2007-08-20 08:13:16 +00:00
|
|
|
$this->dieUsage("No support for special pages has been implemented", 'unsupportednamespace');
|
2006-09-27 05:13:48 +00:00
|
|
|
|
2007-06-14 03:14:44 +00:00
|
|
|
$linkBatch->addObj($titleObj);
|
|
|
|
|
}
|
|
|
|
|
|
2006-09-27 05:13:48 +00:00
|
|
|
// Make sure we remember the original title that was given to us
|
|
|
|
|
// This way the caller can correlate new titles with the originally requested,
|
|
|
|
|
// i.e. namespace is localized or capitalization is different
|
2007-05-14 05:28:06 +00:00
|
|
|
if (is_string($title) && $title !== $titleObj->getPrefixedText()) {
|
|
|
|
|
$this->mNormalizedTitles[$title] = $titleObj->getPrefixedText();
|
2006-09-27 05:13:48 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $linkBatch;
|
2006-09-25 04:12:07 +00:00
|
|
|
}
|
2007-05-14 05:28:06 +00:00
|
|
|
|
2006-09-30 08:06:27 +00:00
|
|
|
protected function getAllowedParams() {
|
|
|
|
|
return array (
|
|
|
|
|
'titles' => array (
|
2006-10-01 20:17:16 +00:00
|
|
|
ApiBase :: PARAM_ISMULTI => true
|
2006-09-30 08:06:27 +00:00
|
|
|
),
|
|
|
|
|
'pageids' => array (
|
2006-10-01 20:17:16 +00:00
|
|
|
ApiBase :: PARAM_TYPE => 'integer',
|
|
|
|
|
ApiBase :: PARAM_ISMULTI => true
|
2006-09-30 08:06:27 +00:00
|
|
|
),
|
|
|
|
|
'revids' => array (
|
2006-10-01 20:17:16 +00:00
|
|
|
ApiBase :: PARAM_TYPE => 'integer',
|
|
|
|
|
ApiBase :: PARAM_ISMULTI => true
|
2006-10-06 01:02:14 +00:00
|
|
|
)
|
2006-09-30 08:06:27 +00:00
|
|
|
);
|
|
|
|
|
}
|
2006-10-01 02:02:13 +00:00
|
|
|
|
2006-09-30 08:06:27 +00:00
|
|
|
protected function getParamDescription() {
|
|
|
|
|
return array (
|
|
|
|
|
'titles' => 'A list of titles to work on',
|
|
|
|
|
'pageids' => 'A list of page IDs to work on',
|
2006-10-06 01:02:14 +00:00
|
|
|
'revids' => 'A list of revision IDs to work on'
|
2006-09-30 08:06:27 +00:00
|
|
|
);
|
2006-09-25 06:10:16 +00:00
|
|
|
}
|
2006-10-01 21:20:55 +00:00
|
|
|
|
|
|
|
|
public function getVersion() {
|
2006-10-02 18:27:06 +00:00
|
|
|
return __CLASS__ . ': $Id$';
|
2006-10-01 21:20:55 +00:00
|
|
|
}
|
2006-09-25 04:12:07 +00:00
|
|
|
}
|
2007-06-29 01:19:14 +00:00
|
|
|
|