From 47e2bec3611dd78fe8e4b47d2b69452c7ea77f96 Mon Sep 17 00:00:00 2001 From: Brad Jorsch Date: Fri, 6 Jan 2017 12:49:27 -0500 Subject: [PATCH] API: Allow finding log events and links to special pages Log events are sometimes attributed to a special page; it should be allowed to use rcnamespace or lenamespace to filter for these. It's also possible for special pages to be the targets of redirects, so list=allredirects and prop=redirects should find them. Maybe someday we'll record links to and transclusions of special pages too (see T19597), so we may as well make it possible to query for those as well via list=alllinks, list=alltransclusions, list=backlinks, list=embeddedin, prop=linkshere, prop=transcludedin, prop=links, and prop=templates. NS_MEDIA has similar considerations: although we currently "normalize" page links to the corresponding File and I don't think anything logs the Media title rather than the File, transclusions and redirects do show up in those tables. Bug: T154319 Change-Id: I00348f83855c6c703b6bd6015f6d3bedc5bfd1c5 --- includes/api/ApiBase.php | 110 ++++++++++++++++--------- includes/api/ApiHelp.php | 6 ++ includes/api/ApiParamInfo.php | 9 ++ includes/api/ApiQueryAllLinks.php | 3 +- includes/api/ApiQueryBacklinks.php | 2 +- includes/api/ApiQueryBacklinksprop.php | 12 ++- includes/api/ApiQueryLinks.php | 3 +- includes/api/ApiQueryLogEvents.php | 3 +- includes/api/ApiQueryRecentChanges.php | 3 +- 9 files changed, 107 insertions(+), 44 deletions(-) diff --git a/includes/api/ApiBase.php b/includes/api/ApiBase.php index 063d6613ce..b8dd4641d9 100644 --- a/includes/api/ApiBase.php +++ b/includes/api/ApiBase.php @@ -180,6 +180,12 @@ abstract class ApiBase extends ContextSource { */ const PARAM_ALL = 17; + /** + * (int[]) When PARAM_TYPE is 'namespace', include these as additional possible values. + * @since 1.29 + */ + const PARAM_EXTRA_NAMESPACES = 18; + /**@}*/ const ALL_DEFAULT_STRING = '*'; @@ -899,6 +905,34 @@ abstract class ApiBase extends ContextSource { return $pageObj; } + /** + * Get a Title object from a title or pageid param, if possible. + * Can die, if no param is set or if the title or page id is not valid. + * + * @since 1.29 + * @param array $params + * @return Title + */ + public function getTitleFromTitleOrPageId( $params ) { + $this->requireOnlyOneParameter( $params, 'title', 'pageid' ); + + $titleObj = null; + if ( isset( $params['title'] ) ) { + $titleObj = Title::newFromText( $params['title'] ); + if ( !$titleObj || $titleObj->isExternal() ) { + $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['title'] ) ] ); + } + return $titleObj; + } elseif ( isset( $params['pageid'] ) ) { + $titleObj = Title::newFromID( $params['pageid'] ); + if ( !$titleObj ) { + $this->dieWithError( [ 'apierror-nosuchpageid', $params['pageid'] ] ); + } + } + + return $titleObj; + } + /** * Return true if we're to watch the page, false if not, null if no change. * @param string $watchlist Valid values: 'watch', 'unwatch', 'preferences', 'nochange' @@ -953,44 +987,41 @@ abstract class ApiBase extends ContextSource { // Some classes may decide to change parameter names $encParamName = $this->encodeParamName( $paramName ); + // Shorthand if ( !is_array( $paramSettings ) ) { - $default = $paramSettings; - $multi = false; - $type = gettype( $paramSettings ); - $dupes = false; - $deprecated = false; - $required = false; - $allowAll = false; - } else { - $default = isset( $paramSettings[self::PARAM_DFLT] ) - ? $paramSettings[self::PARAM_DFLT] - : null; - $multi = isset( $paramSettings[self::PARAM_ISMULTI] ) - ? $paramSettings[self::PARAM_ISMULTI] - : false; - $type = isset( $paramSettings[self::PARAM_TYPE] ) - ? $paramSettings[self::PARAM_TYPE] - : null; - $dupes = isset( $paramSettings[self::PARAM_ALLOW_DUPLICATES] ) - ? $paramSettings[self::PARAM_ALLOW_DUPLICATES] - : false; - $deprecated = isset( $paramSettings[self::PARAM_DEPRECATED] ) - ? $paramSettings[self::PARAM_DEPRECATED] - : false; - $required = isset( $paramSettings[self::PARAM_REQUIRED] ) - ? $paramSettings[self::PARAM_REQUIRED] - : false; - $allowAll = isset( $paramSettings[self::PARAM_ALL] ) - ? $paramSettings[self::PARAM_ALL] - : false; - - // When type is not given, and no choices, the type is the same as $default - if ( !isset( $type ) ) { - if ( isset( $default ) ) { - $type = gettype( $default ); - } else { - $type = 'NULL'; // allow everything - } + $paramSettings = [ + self::PARAM_DFLT => $paramSettings, + ]; + } + + $default = isset( $paramSettings[self::PARAM_DFLT] ) + ? $paramSettings[self::PARAM_DFLT] + : null; + $multi = isset( $paramSettings[self::PARAM_ISMULTI] ) + ? $paramSettings[self::PARAM_ISMULTI] + : false; + $type = isset( $paramSettings[self::PARAM_TYPE] ) + ? $paramSettings[self::PARAM_TYPE] + : null; + $dupes = isset( $paramSettings[self::PARAM_ALLOW_DUPLICATES] ) + ? $paramSettings[self::PARAM_ALLOW_DUPLICATES] + : false; + $deprecated = isset( $paramSettings[self::PARAM_DEPRECATED] ) + ? $paramSettings[self::PARAM_DEPRECATED] + : false; + $required = isset( $paramSettings[self::PARAM_REQUIRED] ) + ? $paramSettings[self::PARAM_REQUIRED] + : false; + $allowAll = isset( $paramSettings[self::PARAM_ALL] ) + ? $paramSettings[self::PARAM_ALL] + : false; + + // When type is not given, and no choices, the type is the same as $default + if ( !isset( $type ) ) { + if ( isset( $default ) ) { + $type = gettype( $default ); + } else { + $type = 'NULL'; // allow everything } } @@ -1034,6 +1065,11 @@ abstract class ApiBase extends ContextSource { if ( isset( $value ) && $type == 'namespace' ) { $type = MWNamespace::getValidNamespaces(); + if ( isset( $paramSettings[self::PARAM_EXTRA_NAMESPACES] ) && + is_array( $paramSettings[self::PARAM_EXTRA_NAMESPACES] ) + ) { + $type = array_merge( $type, $paramSettings[self::PARAM_EXTRA_NAMESPACES] ); + } // By default, namespace parameters allow ALL_DEFAULT_STRING to be used to specify // all namespaces. $allowAll = true; diff --git a/includes/api/ApiHelp.php b/includes/api/ApiHelp.php index 9a0d3ffd4c..e347a9f2c5 100644 --- a/includes/api/ApiHelp.php +++ b/includes/api/ApiHelp.php @@ -547,6 +547,12 @@ class ApiHelp extends ApiBase { case 'namespace': $namespaces = MWNamespace::getValidNamespaces(); + if ( isset( $settings[ApiBase::PARAM_EXTRA_NAMESPACES] ) && + is_array( $settings[ApiBase::PARAM_EXTRA_NAMESPACES] ) + ) { + $namespaces = array_merge( $namespaces, $settings[ApiBase::PARAM_EXTRA_NAMESPACES] ); + } + sort( $namespaces ); $count = count( $namespaces ); $info[] = $context->msg( 'api-help-param-list' ) ->params( $multi ? 2 : 1 ) diff --git a/includes/api/ApiParamInfo.php b/includes/api/ApiParamInfo.php index 46ba34b440..67983e7a3a 100644 --- a/includes/api/ApiParamInfo.php +++ b/includes/api/ApiParamInfo.php @@ -426,6 +426,15 @@ class ApiParamInfo extends ApiBase { ( is_array( $item['type'] ) || $item['type'] === 'namespace' ) ) { $item['allspecifier'] = $allSpecifier; } + + if ( $item['type'] === 'namespace' && + isset( $settings[ApiBase::PARAM_EXTRA_NAMESPACES] ) && + is_array( $settings[ApiBase::PARAM_EXTRA_NAMESPACES] ) + ) { + $item['extranamespaces'] = $settings[ApiBase::PARAM_EXTRA_NAMESPACES]; + ApiResult::setArrayType( $item['extranamespaces'], 'array' ); + ApiResult::setIndexedTagName( $item['extranamespaces'], 'ns' ); + } } if ( isset( $settings[ApiBase::PARAM_MAX] ) ) { $item['max'] = $settings[ApiBase::PARAM_MAX]; diff --git a/includes/api/ApiQueryAllLinks.php b/includes/api/ApiQueryAllLinks.php index c3636c6bcf..3b24e37409 100644 --- a/includes/api/ApiQueryAllLinks.php +++ b/includes/api/ApiQueryAllLinks.php @@ -263,7 +263,8 @@ class ApiQueryAllLinks extends ApiQueryGeneratorBase { ], 'namespace' => [ ApiBase::PARAM_DFLT => $this->dfltNamespace, - ApiBase::PARAM_TYPE => 'namespace' + ApiBase::PARAM_TYPE => 'namespace', + ApiBase::PARAM_EXTRA_NAMESPACES => [ NS_MEDIA, NS_SPECIAL ], ], 'limit' => [ ApiBase::PARAM_DFLT => 10, diff --git a/includes/api/ApiQueryBacklinks.php b/includes/api/ApiQueryBacklinks.php index 4c323206ef..613589e4d8 100644 --- a/includes/api/ApiQueryBacklinks.php +++ b/includes/api/ApiQueryBacklinks.php @@ -344,7 +344,7 @@ class ApiQueryBacklinks extends ApiQueryGeneratorBase { $this->validateLimit( 'limit', $this->params['limit'], 1, $userMax, $botMax ); } - $this->rootTitle = $this->getTitleOrPageId( $this->params )->getTitle(); + $this->rootTitle = $this->getTitleFromTitleOrPageId( $this->params ); // only image titles are allowed for the root in imageinfo mode if ( !$this->hasNS && $this->rootTitle->getNamespace() !== NS_FILE ) { diff --git a/includes/api/ApiQueryBacklinksprop.php b/includes/api/ApiQueryBacklinksprop.php index ef7b9af986..4ed7f52b26 100644 --- a/includes/api/ApiQueryBacklinksprop.php +++ b/includes/api/ApiQueryBacklinksprop.php @@ -104,6 +104,13 @@ class ApiQueryBacklinksprop extends ApiQueryGeneratorBase { $titles = $pageSet->getGoodAndMissingTitles(); $map = $pageSet->getGoodAndMissingTitlesByNamespace(); + // Add in special pages, they can theoretically have backlinks too. + // (although currently they only do for prop=redirects) + foreach ( $pageSet->getSpecialTitles() as $id => $title ) { + $titles[] = $title; + $map[$title->getNamespace()][$title->getDBkey()] = $id; + } + // Determine our fields to query on $p = $settings['prefix']; $hasNS = !isset( $settings['to_namespace'] ); @@ -220,8 +227,9 @@ class ApiQueryBacklinksprop extends ApiQueryGeneratorBase { $this->addFieldsIf( 'page_namespace', $miser_ns !== null ); if ( $hasNS ) { - $lb = new LinkBatch( $titles ); - $this->addWhere( $lb->constructSet( $p, $db ) ); + // Can't use LinkBatch because it throws away Special titles. + // And we already have the needed data structure anyway. + $this->addWhere( $db->makeWhereFrom2d( $map, $bl_namespace, $bl_title ) ); } else { $where = []; foreach ( $titles as $t ) { diff --git a/includes/api/ApiQueryLinks.php b/includes/api/ApiQueryLinks.php index e9ae132df7..4556e29062 100644 --- a/includes/api/ApiQueryLinks.php +++ b/includes/api/ApiQueryLinks.php @@ -182,7 +182,8 @@ class ApiQueryLinks extends ApiQueryGeneratorBase { return [ 'namespace' => [ ApiBase::PARAM_TYPE => 'namespace', - ApiBase::PARAM_ISMULTI => true + ApiBase::PARAM_ISMULTI => true, + ApiBase::PARAM_EXTRA_NAMESPACES => [ NS_MEDIA, NS_SPECIAL ], ], 'limit' => [ ApiBase::PARAM_DFLT => 10, diff --git a/includes/api/ApiQueryLogEvents.php b/includes/api/ApiQueryLogEvents.php index 2dcd0b4f88..4d84aade2c 100644 --- a/includes/api/ApiQueryLogEvents.php +++ b/includes/api/ApiQueryLogEvents.php @@ -435,7 +435,8 @@ class ApiQueryLogEvents extends ApiQueryBase { ], 'title' => null, 'namespace' => [ - ApiBase::PARAM_TYPE => 'namespace' + ApiBase::PARAM_TYPE => 'namespace', + ApiBase::PARAM_EXTRA_NAMESPACES => [ NS_MEDIA, NS_SPECIAL ], ], 'prefix' => [], 'tag' => null, diff --git a/includes/api/ApiQueryRecentChanges.php b/includes/api/ApiQueryRecentChanges.php index 8d149274fd..2c76e97105 100644 --- a/includes/api/ApiQueryRecentChanges.php +++ b/includes/api/ApiQueryRecentChanges.php @@ -617,7 +617,8 @@ class ApiQueryRecentChanges extends ApiQueryGeneratorBase { ], 'namespace' => [ ApiBase::PARAM_ISMULTI => true, - ApiBase::PARAM_TYPE => 'namespace' + ApiBase::PARAM_TYPE => 'namespace', + ApiBase::PARAM_EXTRA_NAMESPACES => [ NS_MEDIA, NS_SPECIAL ], ], 'user' => [ ApiBase::PARAM_TYPE => 'user' -- 2.20.1