From 222b01f5169f1c7e69762e0e8904c24f78f71882 Mon Sep 17 00:00:00 2001 From: Pierre Schmitz Date: Wed, 28 Jul 2010 11:52:48 +0200 Subject: update to MediaWiki 1.16.0 --- includes/api/ApiPageSet.php | 257 +++++++++++++++++++++++--------------------- 1 file changed, 132 insertions(+), 125 deletions(-) (limited to 'includes/api/ApiPageSet.php') diff --git a/includes/api/ApiPageSet.php b/includes/api/ApiPageSet.php index 6b9e90b8..361f1d8b 100644 --- a/includes/api/ApiPageSet.php +++ b/includes/api/ApiPageSet.php @@ -23,9 +23,9 @@ * http://www.gnu.org/copyleft/gpl.html */ -if (!defined('MEDIAWIKI')) { +if ( !defined( 'MEDIAWIKI' ) ) { // Eclipse helper - will be ignored in production - require_once ('ApiQueryBase.php'); + require_once ( 'ApiQueryBase.php' ); } /** @@ -58,8 +58,8 @@ class ApiPageSet extends ApiQueryBase { * @param $query ApiQuery * @param $resolveRedirects bool Whether redirects should be resolved */ - public function __construct($query, $resolveRedirects = false) { - parent :: __construct($query, 'query'); + public function __construct( $query, $resolveRedirects = false ) { + parent :: __construct( $query, 'query' ); $this->mAllPages = array (); $this->mTitles = array(); @@ -75,10 +75,10 @@ class ApiPageSet extends ApiQueryBase { $this->mRequestedPageFields = array (); $this->mResolveRedirects = $resolveRedirects; - if($resolveRedirects) + if ( $resolveRedirects ) $this->mPendingRedirectIDs = array(); - $this->mFakePageId = -1; + $this->mFakePageId = - 1; } /** @@ -94,7 +94,7 @@ class ApiPageSet extends ApiQueryBase { * before execute() * @param $fieldName string Field name */ - public function requestField($fieldName) { + public function requestField( $fieldName ) { $this->mRequestedPageFields[$fieldName] = null; } @@ -104,7 +104,7 @@ class ApiPageSet extends ApiQueryBase { * @param $fieldName string Field name * @return mixed Field value */ - public function getCustomField($fieldName) { + public function getCustomField( $fieldName ) { return $this->mRequestedPageFields[$fieldName]; } @@ -123,14 +123,14 @@ class ApiPageSet extends ApiQueryBase { 'page_id' => null, ); - if ($this->mResolveRedirects) + if ( $this->mResolveRedirects ) $pageFlds['page_is_redirect'] = null; // only store non-default fields - $this->mRequestedPageFields = array_diff_key($this->mRequestedPageFields, $pageFlds); + $this->mRequestedPageFields = array_diff_key( $this->mRequestedPageFields, $pageFlds ); - $pageFlds = array_merge($pageFlds, $this->mRequestedPageFields); - return array_keys($pageFlds); + $pageFlds = array_merge( $pageFlds, $this->mRequestedPageFields ); + return array_keys( $pageFlds ); } /** @@ -156,7 +156,7 @@ class ApiPageSet extends ApiQueryBase { * @return int */ public function getTitleCount() { - return count($this->mTitles); + return count( $this->mTitles ); } /** @@ -172,7 +172,7 @@ class ApiPageSet extends ApiQueryBase { * @return int */ public function getGoodTitleCount() { - return count($this->mGoodTitles); + return count( $this->mGoodTitles ); } /** @@ -249,7 +249,7 @@ class ApiPageSet extends ApiQueryBase { * @return int */ public function getRevisionCount() { - return count($this->getRevisionIDs()); + return count( $this->getRevisionIDs() ); } /** @@ -261,32 +261,32 @@ class ApiPageSet extends ApiQueryBase { // Only one of the titles/pageids/revids is allowed at the same time $dataSource = null; - if (isset ($params['titles'])) + if ( isset ( $params['titles'] ) ) $dataSource = 'titles'; - if (isset ($params['pageids'])) { - if (isset ($dataSource)) - $this->dieUsage("Cannot use 'pageids' at the same time as '$dataSource'", 'multisource'); + if ( isset ( $params['pageids'] ) ) { + if ( isset ( $dataSource ) ) + $this->dieUsage( "Cannot use 'pageids' at the same time as '$dataSource'", 'multisource' ); $dataSource = 'pageids'; } - if (isset ($params['revids'])) { - if (isset ($dataSource)) - $this->dieUsage("Cannot use 'revids' at the same time as '$dataSource'", 'multisource'); + if ( isset ( $params['revids'] ) ) { + if ( isset ( $dataSource ) ) + $this->dieUsage( "Cannot use 'revids' at the same time as '$dataSource'", 'multisource' ); $dataSource = 'revids'; } - switch ($dataSource) { + switch ( $dataSource ) { case 'titles' : - $this->initFromTitles($params['titles']); + $this->initFromTitles( $params['titles'] ); break; case 'pageids' : - $this->initFromPageIds($params['pageids']); + $this->initFromPageIds( $params['pageids'] ); break; case 'revids' : - if($this->mResolveRedirects) - $this->setWarning('Redirect resolution cannot be used together with the revids= parameter. '. - 'Any redirects the revids= point to have not been resolved.'); + if ( $this->mResolveRedirects ) + $this->setWarning( 'Redirect resolution cannot be used together with the revids= parameter. ' . + 'Any redirects the revids= point to have not been resolved.' ); $this->mResolveRedirects = false; - $this->initFromRevIDs($params['revids']); + $this->initFromRevIDs( $params['revids'] ); break; default : // Do nothing - some queries do not need any of the data sources. @@ -299,9 +299,9 @@ class ApiPageSet extends ApiQueryBase { * Populate this PageSet from a list of Titles * @param $titles array of Title objects */ - public function populateFromTitles($titles) { + public function populateFromTitles( $titles ) { $this->profileIn(); - $this->initFromTitles($titles); + $this->initFromTitles( $titles ); $this->profileOut(); } @@ -309,9 +309,9 @@ class ApiPageSet extends ApiQueryBase { * Populate this PageSet from a list of page IDs * @param $pageIDs array of page IDs */ - public function populateFromPageIDs($pageIDs) { + public function populateFromPageIDs( $pageIDs ) { $this->profileIn(); - $this->initFromPageIds($pageIDs); + $this->initFromPageIds( $pageIDs ); $this->profileOut(); } @@ -320,9 +320,9 @@ class ApiPageSet extends ApiQueryBase { * @param $db Database object * @param $queryResult Query result object */ - public function populateFromQueryResult($db, $queryResult) { + public function populateFromQueryResult( $db, $queryResult ) { $this->profileIn(); - $this->initFromQueryResult($db, $queryResult); + $this->initFromQueryResult( $db, $queryResult ); $this->profileOut(); } @@ -330,9 +330,9 @@ class ApiPageSet extends ApiQueryBase { * Populate this PageSet from a list of revision IDs * @param $revIDs array of revision IDs */ - public function populateFromRevisionIDs($revIDs) { + public function populateFromRevisionIDs( $revIDs ) { $this->profileIn(); - $this->initFromRevIDs($revIDs); + $this->initFromRevIDs( $revIDs ); $this->profileOut(); } @@ -340,22 +340,22 @@ class ApiPageSet extends ApiQueryBase { * Extract all requested fields from the row received from the database * @param $row Result row */ - public function processDbRow($row) { + public function processDbRow( $row ) { // Store Title object in various data structures - $title = Title :: makeTitle($row->page_namespace, $row->page_title); + $title = Title :: makeTitle( $row->page_namespace, $row->page_title ); - $pageId = intval($row->page_id); + $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') { + if ( $this->mResolveRedirects && $row->page_is_redirect == '1' ) { $this->mPendingRedirectIDs[$pageId] = $title; } else { $this->mGoodTitles[$pageId] = $title; } - foreach ($this->mRequestedPageFields as $fieldName => & $fieldValues) + foreach ( $this->mRequestedPageFields as $fieldName => & $fieldValues ) $fieldValues[$pageId] = $row-> $fieldName; } @@ -384,24 +384,24 @@ class ApiPageSet extends ApiQueryBase { * * @param $titles array of Title objects or strings */ - private function initFromTitles($titles) { + private function initFromTitles( $titles ) { // Get validated and normalized title objects - $linkBatch = $this->processTitlesArray($titles); - if($linkBatch->isEmpty()) + $linkBatch = $this->processTitlesArray( $titles ); + if ( $linkBatch->isEmpty() ) return; $db = $this->getDB(); - $set = $linkBatch->constructSet('page', $db); + $set = $linkBatch->constructSet( 'page', $db ); // Get pageIDs data from the `page` table $this->profileDBIn(); - $res = $db->select('page', $this->getPageTableFields(), $set, - __METHOD__); + $res = $db->select( 'page', $this->getPageTableFields(), $set, + __METHOD__ ); $this->profileDBOut(); // Hack: get the ns:titles stored in array(ns => array(titles)) format - $this->initFromQueryResult($db, $res, $linkBatch->data, true); // process Titles + $this->initFromQueryResult( $db, $res, $linkBatch->data, true ); // process Titles // Resolve any found redirects $this->resolvePendingRedirects(); @@ -411,11 +411,11 @@ class ApiPageSet extends ApiQueryBase { * Does the same as initFromTitles(), but is based on page IDs instead * @param $pageids array of page IDs */ - private function initFromPageIds($pageids) { - if(!count($pageids)) + private function initFromPageIds( $pageids ) { + if ( !count( $pageids ) ) return; - $pageids = array_map('intval', $pageids); // paranoia + $pageids = array_map( 'intval', $pageids ); // paranoia $set = array ( 'page_id' => $pageids ); @@ -423,12 +423,12 @@ class ApiPageSet extends ApiQueryBase { // Get pageIDs data from the `page` table $this->profileDBIn(); - $res = $db->select('page', $this->getPageTableFields(), $set, - __METHOD__); + $res = $db->select( 'page', $this->getPageTableFields(), $set, + __METHOD__ ); $this->profileDBOut(); - $remaining = array_flip($pageids); - $this->initFromQueryResult($db, $res, $remaining, false); // process PageIDs + $remaining = array_flip( $pageids ); + $this->initFromQueryResult( $db, $res, $remaining, false ); // process PageIDs // Resolve any found redirects $this->resolvePendingRedirects(); @@ -445,34 +445,34 @@ class ApiPageSet extends ApiQueryBase { * If true, treat $remaining as an array of [ns][title] * If false, treat it as an array of [pageIDs] */ - private function initFromQueryResult($db, $res, &$remaining = null, $processTitles = null) { - if (!is_null($remaining) && is_null($processTitles)) - ApiBase :: dieDebug(__METHOD__, 'Missing $processTitles parameter when $remaining is provided'); + private function initFromQueryResult( $db, $res, &$remaining = null, $processTitles = null ) { + if ( !is_null( $remaining ) && is_null( $processTitles ) ) + ApiBase :: dieDebug( __METHOD__, 'Missing $processTitles parameter when $remaining is provided' ); - while ($row = $db->fetchObject($res)) { + while ( $row = $db->fetchObject( $res ) ) { - $pageId = intval($row->page_id); + $pageId = intval( $row->page_id ); // Remove found page from the list of remaining items - if (isset($remaining)) { - if ($processTitles) - unset ($remaining[$row->page_namespace][$row->page_title]); + if ( isset( $remaining ) ) { + if ( $processTitles ) + unset ( $remaining[$row->page_namespace][$row->page_title] ); else - unset ($remaining[$pageId]); + unset ( $remaining[$pageId] ); } // Store any extra fields requested by modules - $this->processDbRow($row); + $this->processDbRow( $row ); } - $db->freeResult($res); + $db->freeResult( $res ); - if(isset($remaining)) { + if ( isset( $remaining ) ) { // Any items left in the $remaining list are added as missing - if($processTitles) { + if ( $processTitles ) { // The remaining titles in $remaining are non-existent pages - foreach ($remaining as $ns => $dbkeys) { + foreach ( $remaining as $ns => $dbkeys ) { foreach ( $dbkeys as $dbkey => $unused ) { - $title = Title :: makeTitle($ns, $dbkey); + $title = Title :: makeTitle( $ns, $dbkey ); $this->mAllPages[$ns][$dbkey] = $this->mFakePageId; $this->mMissingTitles[$this->mFakePageId] = $title; $this->mFakePageId--; @@ -483,10 +483,10 @@ class ApiPageSet extends ApiQueryBase { else { // The remaining pageids do not exist - if(!$this->mMissingPageIDs) - $this->mMissingPageIDs = array_keys($remaining); + if ( !$this->mMissingPageIDs ) + $this->mMissingPageIDs = array_keys( $remaining ); else - $this->mMissingPageIDs = array_merge($this->mMissingPageIDs, array_keys($remaining)); + $this->mMissingPageIDs = array_merge( $this->mMissingPageIDs, array_keys( $remaining ) ); } } } @@ -496,37 +496,37 @@ class ApiPageSet extends ApiQueryBase { * instead * @param $revids array of revision IDs */ - private function initFromRevIDs($revids) { + private function initFromRevIDs( $revids ) { - if(!count($revids)) + if ( !count( $revids ) ) return; - $revids = array_map('intval', $revids); // paranoia + $revids = array_map( 'intval', $revids ); // paranoia $db = $this->getDB(); $pageids = array(); - $remaining = array_flip($revids); + $remaining = array_flip( $revids ); - $tables = array('revision', 'page'); - $fields = array('rev_id', 'rev_page'); - $where = array('rev_id' => $revids, 'rev_page = page_id'); + $tables = array( 'revision', 'page' ); + $fields = array( 'rev_id', 'rev_page' ); + $where = array( 'rev_id' => $revids, 'rev_page = page_id' ); // 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); + $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]); + unset( $remaining[$revid] ); } - $db->freeResult($res); + $db->freeResult( $res ); $this->profileDBOut(); - $this->mMissingRevIDs = array_keys($remaining); + $this->mMissingRevIDs = array_keys( $remaining ); // Populate all the page information - $this->initFromPageIds(array_keys($pageids)); + $this->initFromPageIds( array_keys( $pageids ) ); } /** @@ -536,32 +536,32 @@ class ApiPageSet extends ApiQueryBase { */ private function resolvePendingRedirects() { - if($this->mResolveRedirects) { + if ( $this->mResolveRedirects ) { $db = $this->getDB(); $pageFlds = $this->getPageTableFields(); // Repeat until all redirects have been resolved // The infinite loop is prevented by keeping all known pages in $this->mAllPages - while ($this->mPendingRedirectIDs) { + while ( $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()) + if ( $linkBatch->isEmpty() ) break; - $set = $linkBatch->constructSet('page', $db); - if($set === false) + $set = $linkBatch->constructSet( 'page', $db ); + if ( $set === false ) break; // Get pageIDs data from the `page` table $this->profileDBIn(); - $res = $db->select('page', $pageFlds, $set, __METHOD__); + $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); + $this->initFromQueryResult( $db, $res, $linkBatch->data, true ); } } } @@ -578,40 +578,40 @@ class ApiPageSet extends ApiQueryBase { $db = $this->getDB(); $this->profileDBIn(); - $res = $db->select('redirect', array( + $res = $db->select( 'redirect', array( 'rd_from', 'rd_namespace', 'rd_title' - ), array('rd_from' => array_keys($this->mPendingRedirectIDs)), + ), array( 'rd_from' => array_keys( $this->mPendingRedirectIDs ) ), __METHOD__ ); $this->profileDBOut(); - while($row = $db->fetchObject($res)) + while ( $row = $db->fetchObject( $res ) ) { - $rdfrom = intval($row->rd_from); + $rdfrom = intval( $row->rd_from ); $from = $this->mPendingRedirectIDs[$rdfrom]->getPrefixedText(); - $to = Title::makeTitle($row->rd_namespace, $row->rd_title)->getPrefixedText(); - unset($this->mPendingRedirectIDs[$rdfrom]); - if(!isset($this->mAllPages[$row->rd_namespace][$row->rd_title])) - $lb->add($row->rd_namespace, $row->rd_title); + $to = Title::makeTitle( $row->rd_namespace, $row->rd_title )->getPrefixedText(); + unset( $this->mPendingRedirectIDs[$rdfrom] ); + if ( !isset( $this->mAllPages[$row->rd_namespace][$row->rd_title] ) ) + $lb->add( $row->rd_namespace, $row->rd_title ); $this->mRedirectTitles[$from] = $to; } - $db->freeResult($res); - if($this->mPendingRedirectIDs) + $db->freeResult( $res ); + if ( $this->mPendingRedirectIDs ) { - # We found pages that aren't in the redirect table - # Add them - foreach($this->mPendingRedirectIDs as $id => $title) + // We found pages that aren't in the redirect table + // Add them + foreach ( $this->mPendingRedirectIDs as $id => $title ) { - $article = new Article($title); + $article = new Article( $title ); $rt = $article->insertRedirect(); - if(!$rt) - # What the hell. Let's just ignore this + if ( !$rt ) + // What the hell. Let's just ignore this continue; - $lb->addObj($rt); + $lb->addObj( $rt ); $this->mRedirectTitles[$title->getPrefixedText()] = $rt->getPrefixedText(); - unset($this->mPendingRedirectIDs[$id]); + unset( $this->mPendingRedirectIDs[$id] ); } } return $lb; @@ -626,32 +626,32 @@ class ApiPageSet extends ApiQueryBase { * @param $titles array of Title objects or strings * @return LinkBatch */ - private function processTitlesArray($titles) { + private function processTitlesArray( $titles ) { $linkBatch = new LinkBatch(); - foreach ($titles as $title) { + foreach ( $titles as $title ) { - $titleObj = is_string($title) ? Title :: newFromText($title) : $title; - if (!$titleObj) + $titleObj = is_string( $title ) ? Title :: newFromText( $title ) : $title; + if ( !$titleObj ) { - # Handle invalid titles gracefully + // 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 } $iw = $titleObj->getInterwiki(); - if (strval($iw) !== '') { + if ( strval( $iw ) !== '' ) { // This title is an interwiki link. $this->mInterwikiTitles[$titleObj->getPrefixedText()] = $iw; } else { // Validation - if ($titleObj->getNamespace() < 0) - $this->setWarning("No support for special pages has been implemented"); + if ( $titleObj->getNamespace() < 0 ) + $this->setWarning( "No support for special pages has been implemented" ); else - $linkBatch->addObj($titleObj); + $linkBatch->addObj( $titleObj ); } // Make sure we remember the original title that was @@ -659,7 +659,7 @@ class ApiPageSet extends ApiQueryBase { // titles with the originally requested when e.g. the // namespace is localized or the capitalization is // different - if (is_string($title) && $title !== $titleObj->getPrefixedText()) { + if ( is_string( $title ) && $title !== $titleObj->getPrefixedText() ) { $this->mNormalizedTitles[$title] = $titleObj->getPrefixedText(); } } @@ -691,7 +691,14 @@ class ApiPageSet extends ApiQueryBase { ); } + public function getPossibleErrors() { + return array_merge( parent::getPossibleErrors(), array( + array( 'code' => 'multisource', 'info' => "Cannot use 'pageids' at the same time as 'dataSource'" ), + array( 'code' => 'multisource', 'info' => "Cannot use 'revids' at the same time as 'dataSource'" ), + ) ); + } + public function getVersion() { - return __CLASS__ . ': $Id: ApiPageSet.php 47424 2009-02-18 05:29:11Z werdna $'; + return __CLASS__ . ': $Id: ApiPageSet.php 62410 2010-02-13 01:21:52Z reedy $'; } } -- cgit v1.2.3-54-g00ecf