diff options
author | Luke Shumaker <lukeshu@sbcglobal.net> | 2016-05-01 15:12:12 -0400 |
---|---|---|
committer | Luke Shumaker <lukeshu@sbcglobal.net> | 2016-05-01 15:12:12 -0400 |
commit | c9aa36da061816dee256a979c2ff8d2ee41824d9 (patch) | |
tree | 29f7002b80ee984b488bd047dbbd80b36bf892e9 /includes/api/ApiPageSet.php | |
parent | b4274e0e33eafb5e9ead9d949ebf031a9fb8363b (diff) | |
parent | d1ba966140d7a60cd5ae4e8667ceb27c1a138592 (diff) |
Merge branch 'archwiki'
# Conflicts:
# skins/ArchLinux.php
# skins/ArchLinux/archlogo.gif
Diffstat (limited to 'includes/api/ApiPageSet.php')
-rw-r--r-- | includes/api/ApiPageSet.php | 207 |
1 files changed, 142 insertions, 65 deletions
diff --git a/includes/api/ApiPageSet.php b/includes/api/ApiPageSet.php index b05cb2b6..0f264675 100644 --- a/includes/api/ApiPageSet.php +++ b/includes/api/ApiPageSet.php @@ -39,7 +39,6 @@ * @since 1.21 derives from ApiBase instead of ApiQueryBase */ class ApiPageSet extends ApiBase { - /** * Constructor flag: The new instance of ApiPageSet will ignore the 'generator=' parameter * @since 1.21 @@ -62,6 +61,7 @@ class ApiPageSet extends ApiBase { private $mSpecialTitles = array(); private $mNormalizedTitles = array(); private $mInterwikiTitles = array(); + /** @var Title[] */ private $mPendingRedirectIDs = array(); private $mConvertedTitles = array(); private $mGoodRevIDs = array(); @@ -69,17 +69,38 @@ class ApiPageSet extends ApiBase { private $mFakePageId = -1; private $mCacheMode = 'public'; private $mRequestedPageFields = array(); + /** @var int */ + private $mDefaultNamespace = NS_MAIN; + /** - * @var int + * Add all items from $values into the result + * @param array $result Output + * @param array $values Values to add + * @param string $flag The name of the boolean flag to mark this element + * @param string $name If given, name of the value */ - private $mDefaultNamespace = NS_MAIN; + private static function addValues( array &$result, $values, $flag = null, $name = null ) { + foreach ( $values as $val ) { + if ( $val instanceof Title ) { + $v = array(); + ApiQueryBase::addTitleInfo( $v, $val ); + } elseif ( $name !== null ) { + $v = array( $name => $val ); + } else { + $v = $val; + } + if ( $flag !== null ) { + $v[$flag] = ''; + } + $result[] = $v; + } + } /** - * Constructor - * @param $dbSource ApiBase Module implementing getDB(). + * @param ApiBase $dbSource Module implementing getDB(). * Allows PageSet to reuse existing db connection from the shared state like ApiQuery. * @param int $flags Zero or more flags like DISABLE_GENERATORS - * @param int $defaultNamespace the namespace to use if none is specified by a prefix. + * @param int $defaultNamespace The namespace to use if none is specified by a prefix. * @since 1.21 accepts $flags instead of two boolean values */ public function __construct( ApiBase $dbSource, $flags = 0, $defaultNamespace = NS_MAIN ) { @@ -112,7 +133,8 @@ class ApiPageSet extends ApiBase { /** * Populate the PageSet from the request parameters. - * @param bool $isDryRun If true, instantiates generator, but only to mark relevant parameters as used + * @param bool $isDryRun If true, instantiates generator, but only to mark + * relevant parameters as used */ private function executeInternal( $isDryRun ) { $this->profileIn(); @@ -200,8 +222,9 @@ class ApiPageSet extends ApiBase { 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.' ); + $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( $this->mParams['revids'] ); @@ -244,6 +267,7 @@ class ApiPageSet extends ApiBase { if ( isset( $this->mParams['revids'] ) ) { return 'revids'; } + return null; } @@ -270,7 +294,7 @@ class ApiPageSet extends ApiBase { * Get the fields that have to be queried from the page table: * the ones requested through requestField() and a few basic ones * we always need - * @return array of field names + * @return array Array of field names */ public function getPageTableFields() { // Ensure we get minimum required fields @@ -289,6 +313,7 @@ class ApiPageSet extends ApiBase { $this->mRequestedPageFields = array_diff_key( $this->mRequestedPageFields, $pageFlds ); $pageFlds = array_merge( $pageFlds, $this->mRequestedPageFields ); + return array_keys( $pageFlds ); } @@ -304,7 +329,7 @@ class ApiPageSet extends ApiBase { /** * All Title objects provided. - * @return array of Title objects + * @return Title[] */ public function getTitles() { return $this->mTitles; @@ -320,7 +345,7 @@ class ApiPageSet extends ApiBase { /** * Title objects that were found in the database. - * @return array page_id (int) => Title (obj) + * @return Title[] Array page_id (int) => Title (obj) */ public function getGoodTitles() { return $this->mGoodTitles; @@ -337,7 +362,7 @@ class ApiPageSet extends ApiBase { /** * Title objects that were NOT found in the database. * The array's index will be negative for each item - * @return array of Title objects + * @return Title[] */ public function getMissingTitles() { return $this->mMissingTitles; @@ -346,7 +371,7 @@ class ApiPageSet extends ApiBase { /** * 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) + * @return string[] Array of strings (not Title objects) */ public function getInvalidTitles() { return $this->mInvalidTitles; @@ -354,7 +379,7 @@ class ApiPageSet extends ApiBase { /** * Page IDs that were not found in the database - * @return array of page IDs + * @return array Array of page IDs */ public function getMissingPageIDs() { return $this->mMissingPageIDs; @@ -363,7 +388,7 @@ class ApiPageSet extends ApiBase { /** * Get a list of redirect resolutions - maps a title to its redirect * target, as an array of output-ready arrays - * @return array + * @return Title[] */ public function getRedirectTitles() { return $this->mRedirectTitles; @@ -372,8 +397,8 @@ class ApiPageSet extends ApiBase { /** * Get a list of redirect resolutions - maps a title to its redirect * target. - * @param $result ApiResult - * @return array of prefixed_title (string) => Title object + * @param ApiResult $result + * @return array Array of prefixed_title (string) => Title object * @since 1.21 */ public function getRedirectTitlesAsResult( $result = null ) { @@ -383,7 +408,7 @@ class ApiPageSet extends ApiBase { 'from' => strval( $titleStrFrom ), 'to' => $titleTo->getPrefixedText(), ); - if ( $titleTo->getFragment() !== '' ) { + if ( $titleTo->hasFragment() ) { $r['tofragment'] = $titleTo->getFragment(); } $values[] = $r; @@ -391,13 +416,14 @@ class ApiPageSet extends ApiBase { if ( !empty( $values ) && $result ) { $result->setIndexedTagName( $values, 'r' ); } + return $values; } /** * Get a list of title normalizations - maps a title to its normalized * version. - * @return array raw_prefixed_title (string) => prefixed_title (string) + * @return array Array of raw_prefixed_title (string) => prefixed_title (string) */ public function getNormalizedTitles() { return $this->mNormalizedTitles; @@ -406,8 +432,8 @@ class ApiPageSet extends ApiBase { /** * Get a list of title normalizations - maps a title to its normalized * version in the form of result array. - * @param $result ApiResult - * @return array of raw_prefixed_title (string) => prefixed_title (string) + * @param ApiResult $result + * @return array Array of raw_prefixed_title (string) => prefixed_title (string) * @since 1.21 */ public function getNormalizedTitlesAsResult( $result = null ) { @@ -421,13 +447,14 @@ class ApiPageSet extends ApiBase { if ( !empty( $values ) && $result ) { $result->setIndexedTagName( $values, 'n' ); } + return $values; } /** * Get a list of title conversions - maps a title to its converted * version. - * @return array raw_prefixed_title (string) => prefixed_title (string) + * @return array Array of raw_prefixed_title (string) => prefixed_title (string) */ public function getConvertedTitles() { return $this->mConvertedTitles; @@ -436,8 +463,8 @@ class ApiPageSet extends ApiBase { /** * Get a list of title conversions - maps a title to its converted * version as a result array. - * @param $result ApiResult - * @return array of (from, to) strings + * @param ApiResult $result + * @return array Array of (from, to) strings * @since 1.21 */ public function getConvertedTitlesAsResult( $result = null ) { @@ -451,13 +478,14 @@ class ApiPageSet extends ApiBase { if ( !empty( $values ) && $result ) { $result->setIndexedTagName( $values, 'c' ); } + return $values; } /** * Get a list of interwiki titles - maps a title to its interwiki * prefix. - * @return array raw_prefixed_title (string) => interwiki_prefix (string) + * @return array Array of raw_prefixed_title (string) => interwiki_prefix (string) */ public function getInterwikiTitles() { return $this->mInterwikiTitles; @@ -466,9 +494,9 @@ class ApiPageSet extends ApiBase { /** * Get a list of interwiki titles - maps a title to its interwiki * prefix as result. - * @param $result ApiResult - * @param $iwUrl boolean - * @return array raw_prefixed_title (string) => interwiki_prefix (string) + * @param ApiResult $result + * @param bool $iwUrl + * @return array Array of raw_prefixed_title (string) => interwiki_prefix (string) * @since 1.21 */ public function getInterwikiTitlesAsResult( $result = null, $iwUrl = false ) { @@ -487,12 +515,53 @@ class ApiPageSet extends ApiBase { if ( !empty( $values ) && $result ) { $result->setIndexedTagName( $values, 'i' ); } + return $values; } /** + * Get an array of invalid/special/missing titles. + * + * @param array $invalidChecks List of types of invalid titles to include. + * Recognized values are: + * - invalidTitles: Titles from $this->getInvalidTitles() + * - special: Titles from $this->getSpecialTitles() + * - missingIds: ids from $this->getMissingPageIDs() + * - missingRevIds: ids from $this->getMissingRevisionIDs() + * - missingTitles: Titles from $this->getMissingTitles() + * - interwikiTitles: Titles from $this->getInterwikiTitlesAsResult() + * @return array Array suitable for inclusion in the response + * @since 1.23 + */ + public function getInvalidTitlesAndRevisions( $invalidChecks = array( 'invalidTitles', + 'special', 'missingIds', 'missingRevIds', 'missingTitles', 'interwikiTitles' ) + ) { + $result = array(); + if ( in_array( "invalidTitles", $invalidChecks ) ) { + self::addValues( $result, $this->getInvalidTitles(), 'invalid', 'title' ); + } + if ( in_array( "special", $invalidChecks ) ) { + self::addValues( $result, $this->getSpecialTitles(), 'special', 'title' ); + } + if ( in_array( "missingIds", $invalidChecks ) ) { + self::addValues( $result, $this->getMissingPageIDs(), 'missing', 'pageid' ); + } + if ( in_array( "missingRevIds", $invalidChecks ) ) { + self::addValues( $result, $this->getMissingRevisionIDs(), 'missing', 'revid' ); + } + if ( in_array( "missingTitles", $invalidChecks ) ) { + self::addValues( $result, $this->getMissingTitles(), 'missing' ); + } + if ( in_array( "interwikiTitles", $invalidChecks ) ) { + self::addValues( $result, $this->getInterwikiTitlesAsResult() ); + } + + return $result; + } + + /** * Get the list of revision IDs (requested with the revids= parameter) - * @return array revID (int) => pageID (int) + * @return array Array of revID (int) => pageID (int) */ public function getRevisionIDs() { return $this->mGoodRevIDs; @@ -500,7 +569,7 @@ class ApiPageSet extends ApiBase { /** * Revision IDs that were not found in the database - * @return array of revision IDs + * @return array Array of revision IDs */ public function getMissingRevisionIDs() { return $this->mMissingRevIDs; @@ -508,8 +577,8 @@ class ApiPageSet extends ApiBase { /** * Revision IDs that were not found in the database as result array. - * @param $result ApiResult - * @return array of revision IDs + * @param ApiResult $result + * @return array Array of revision IDs * @since 1.21 */ public function getMissingRevisionIDsAsResult( $result = null ) { @@ -522,12 +591,13 @@ class ApiPageSet extends ApiBase { if ( !empty( $values ) && $result ) { $result->setIndexedTagName( $values, 'rev' ); } + return $values; } /** * Get the list of titles with negative namespace - * @return array Title + * @return Title[] */ public function getSpecialTitles() { return $this->mSpecialTitles; @@ -543,7 +613,7 @@ class ApiPageSet extends ApiBase { /** * Populate this PageSet from a list of Titles - * @param array $titles of Title objects + * @param array $titles Array of Title objects */ public function populateFromTitles( $titles ) { $this->profileIn(); @@ -553,7 +623,7 @@ class ApiPageSet extends ApiBase { /** * Populate this PageSet from a list of page IDs - * @param array $pageIDs of page IDs + * @param array $pageIDs Array of page IDs */ public function populateFromPageIDs( $pageIDs ) { $this->profileIn(); @@ -563,8 +633,8 @@ class ApiPageSet extends ApiBase { /** * Populate this PageSet from a rowset returned from the database - * @param $db DatabaseBase object - * @param $queryResult ResultWrapper Query result object + * @param DatabaseBase $db + * @param ResultWrapper $queryResult Query result object */ public function populateFromQueryResult( $db, $queryResult ) { $this->profileIn(); @@ -574,7 +644,7 @@ class ApiPageSet extends ApiBase { /** * Populate this PageSet from a list of revision IDs - * @param array $revIDs of revision IDs + * @param array $revIDs Array of revision IDs */ public function populateFromRevisionIDs( $revIDs ) { $this->profileIn(); @@ -584,7 +654,7 @@ class ApiPageSet extends ApiBase { /** * Extract all requested fields from the row received from the database - * @param $row Result row + * @param stdClass $row Result row */ public function processDbRow( $row ) { // Store Title object in various data structures @@ -627,7 +697,7 @@ class ApiPageSet extends ApiBase { * #5 Substitute the original LinkBatch object with the new list * #6 Repeat from step #1 * - * @param array $titles of Title objects or strings + * @param array $titles Array of Title objects or strings */ private function initFromTitles( $titles ) { // Get validated and normalized title objects @@ -642,7 +712,7 @@ class ApiPageSet extends ApiBase { // Get pageIDs data from the `page` table $this->profileDBIn(); $res = $db->select( 'page', $this->getPageTableFields(), $set, - __METHOD__ ); + __METHOD__ ); $this->profileDBOut(); // Hack: get the ns:titles stored in array(ns => array(titles)) format @@ -654,7 +724,7 @@ class ApiPageSet extends ApiBase { /** * Does the same as initFromTitles(), but is based on page IDs instead - * @param array $pageids of page IDs + * @param array $pageids Array of page IDs */ private function initFromPageIds( $pageids ) { if ( !$pageids ) { @@ -676,7 +746,7 @@ class ApiPageSet extends ApiBase { // Get pageIDs data from the `page` table $this->profileDBIn(); $res = $db->select( 'page', $this->getPageTableFields(), $set, - __METHOD__ ); + __METHOD__ ); $this->profileDBOut(); } @@ -689,8 +759,8 @@ class ApiPageSet extends ApiBase { /** * 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 $res ResultWrapper DB Query result - * @param array $remaining of either pageID or ns/title elements (optional). + * @param ResultWrapper $res DB Query result + * @param array $remaining Array of either pageID or ns/title elements (optional). * If given, any missing items will go to $mMissingPageIDs and $mMissingTitles * @param bool $processTitles Must be provided together with $remaining. * If true, treat $remaining as an array of [ns][title] @@ -761,7 +831,7 @@ class ApiPageSet extends ApiBase { /** * Does the same as initFromTitles(), but is based on revision IDs * instead - * @param array $revids of revision IDs + * @param array $revids Array of revision IDs */ private function initFromRevIDs( $revids ) { if ( !$revids ) { @@ -863,7 +933,12 @@ class ApiPageSet extends ApiBase { foreach ( $res as $row ) { $rdfrom = intval( $row->rd_from ); $from = $this->mPendingRedirectIDs[$rdfrom]->getPrefixedText(); - $to = Title::makeTitle( $row->rd_namespace, $row->rd_title, $row->rd_fragment, $row->rd_interwiki ); + $to = Title::makeTitle( + $row->rd_namespace, + $row->rd_title, + $row->rd_fragment, + $row->rd_interwiki + ); unset( $this->mPendingRedirectIDs[$rdfrom] ); if ( !$to->isExternal() && !isset( $this->mAllPages[$row->rd_namespace][$row->rd_title] ) ) { $lb->add( $row->rd_namespace, $row->rd_title ); @@ -886,6 +961,7 @@ class ApiPageSet extends ApiBase { unset( $this->mPendingRedirectIDs[$id] ); } } + return $lb; } @@ -898,7 +974,7 @@ class ApiPageSet extends ApiBase { * Public caching will only be allowed if *all* the modules that supply * data for a given request return a cache mode of public. * - * @param $params + * @param array|null $params * @return string * @since 1.21 */ @@ -912,7 +988,7 @@ class ApiPageSet extends ApiBase { * This method validates access rights for the title, * and appends normalization values to the output. * - * @param array $titles of Title objects or strings + * @param array $titles Array of Title objects or strings * @return LinkBatch */ private function processTitlesArray( $titles ) { @@ -941,8 +1017,9 @@ class ApiPageSet extends ApiBase { // Variants checking global $wgContLang; if ( $this->mConvertTitles && - count( $wgContLang->getVariants() ) > 1 && - !$titleObj->exists() ) { + count( $wgContLang->getVariants() ) > 1 && + !$titleObj->exists() + ) { // Language::findVariantLink will modify titleText and titleObj into // the canonical variant if possible $titleText = is_string( $title ) ? $title : $titleObj->getPrefixedText(); @@ -999,7 +1076,7 @@ class ApiPageSet extends ApiBase { /** * Returns the input array of integers with all values < 0 removed * - * @param $array array + * @param array $array * @return array */ private static function getPositiveIntegers( $array ) { @@ -1040,6 +1117,7 @@ class ApiPageSet extends ApiBase { $result['generator'] = null; } } + return $result; } @@ -1067,6 +1145,7 @@ class ApiPageSet extends ApiBase { sort( $gens ); self::$generators = $gens; } + return self::$generators; } @@ -1075,19 +1154,17 @@ class ApiPageSet extends ApiBase { 'titles' => 'A list of titles to work on', 'pageids' => 'A list of page IDs to work on', 'revids' => 'A list of revision IDs to work on', - 'generator' => array( 'Get the list of pages to work on by executing the specified query module.', - 'NOTE: generator parameter names must be prefixed with a \'g\', see examples' ), + 'generator' => array( + 'Get the list of pages to work on by executing the specified query module.', + 'NOTE: generator parameter names must be prefixed with a \'g\', see examples' + ), 'redirects' => 'Automatically resolve redirects', - 'converttitles' => array( 'Convert titles to other variants if necessary. Only works if the wiki\'s content language supports variant conversion.', - 'Languages that support variant conversion include ' . implode( ', ', LanguageConverter::$languagesWithVariants ) ), + 'converttitles' => array( + 'Convert titles to other variants if necessary. Only works if ' . + 'the wiki\'s content language supports variant conversion.', + 'Languages that support variant conversion include ' . + implode( ', ', LanguageConverter::$languagesWithVariants ) + ), ); } - - 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'" ), - array( 'code' => 'badgenerator', 'info' => 'Module $generatorName cannot be used as a generator' ), - ) ); - } } |