From df622c4195cc5a9f1b9c2d4197e24f8b852c2861 Mon Sep 17 00:00:00 2001 From: victorbarbu Date: Tue, 5 Jan 2016 01:40:01 +0200 Subject: [PATCH] Provide short URL to file description page in imageinfo API Bug: T122439 Change-Id: I0f00b986e6095bdb9b8d6af6fbc5b01995227e02 --- includes/api/ApiQueryImageInfo.php | 5 +++++ includes/filerepo/ForeignAPIRepo.php | 6 +++++- includes/filerepo/file/File.php | 10 ++++++++++ includes/filerepo/file/ForeignAPIFile.php | 19 ++++++++++++++++++ includes/filerepo/file/ForeignDBFile.php | 24 +++++++++++++++++++++++ includes/filerepo/file/LocalFile.php | 19 ++++++++++++++++++ 6 files changed, 82 insertions(+), 1 deletion(-) diff --git a/includes/api/ApiQueryImageInfo.php b/includes/api/ApiQueryImageInfo.php index 8dbd812c78..267cc6d997 100644 --- a/includes/api/ApiQueryImageInfo.php +++ b/includes/api/ApiQueryImageInfo.php @@ -515,6 +515,11 @@ class ApiQueryImageInfo extends ApiQueryBase { } $vals['url'] = wfExpandUrl( $file->getFullUrl(), PROTO_CURRENT ); $vals['descriptionurl'] = wfExpandUrl( $file->getDescriptionUrl(), PROTO_CURRENT ); + + $shortDescriptionUrl = $file->getDescriptionShortUrl(); + if ( $shortDescriptionUrl !== null ) { + $vals['descriptionshorturl'] = wfExpandUrl( $shortDescriptionUrl, PROTO_CURRENT ); + } } if ( $sha1 ) { diff --git a/includes/filerepo/ForeignAPIRepo.php b/includes/filerepo/ForeignAPIRepo.php index debdeb5c8b..a8d37a1569 100644 --- a/includes/filerepo/ForeignAPIRepo.php +++ b/includes/filerepo/ForeignAPIRepo.php @@ -218,7 +218,11 @@ class ForeignAPIRepo extends FileRepo { if ( $data && isset( $data['query']['pages'] ) ) { foreach ( $data['query']['pages'] as $info ) { if ( isset( $info['imageinfo'][0] ) ) { - return $info['imageinfo'][0]; + $return = $info['imageinfo'][0]; + if ( isset( $info['pageid'] ) ) { + $return['pageid'] = $info['pageid']; + } + return $return; } } } diff --git a/includes/filerepo/file/File.php b/includes/filerepo/file/File.php index 72f12d19f2..8a3900ee3d 100644 --- a/includes/filerepo/file/File.php +++ b/includes/filerepo/file/File.php @@ -354,6 +354,16 @@ abstract class File implements IDBAccessObject { return $this->url; } + /* + * Get short description URL for a files based on the page ID + * + * @return string|null + * @since 1.27 + */ + public function getDescriptionShortUrl() { + return null; + } + /** * Return a fully-qualified URL to the file. * Upload URL paths _may or may not_ be fully qualified, so diff --git a/includes/filerepo/file/ForeignAPIFile.php b/includes/filerepo/file/ForeignAPIFile.php index 26ea38b223..43cb5a5232 100644 --- a/includes/filerepo/file/ForeignAPIFile.php +++ b/includes/filerepo/file/ForeignAPIFile.php @@ -218,6 +218,25 @@ class ForeignAPIFile extends File { return isset( $this->mInfo['url'] ) ? strval( $this->mInfo['url'] ) : null; } + /** + * Get short description URL for a file based on the foreign API response, + * or if unavailable, the short URL is constructed from the foreign page ID. + * + * @return null|string + * @since 1.27 + */ + public function getDescriptionShortUrl() { + if ( isset( $this->mInfo['descriptionshorturl'] ) ) { + return $this->mInfo['descriptionshorturl']; + } elseif ( isset( $this->mInfo['pageid'] ) ) { + $url = $this->repo->makeUrl( array( 'curid' => $this->mInfo['pageid'] ) ); + if ( $url !== false ) { + return $url; + } + } + return null; + } + /** * @param string $type * @return int|null|string diff --git a/includes/filerepo/file/ForeignDBFile.php b/includes/filerepo/file/ForeignDBFile.php index 561ead755d..611ae10b80 100644 --- a/includes/filerepo/file/ForeignDBFile.php +++ b/includes/filerepo/file/ForeignDBFile.php @@ -127,4 +127,28 @@ class ForeignDBFile extends LocalFile { // Restore remote behavior return File::getDescriptionText( $lang ); } + + /** + * Get short description URL for a file based on the page ID. + * + * @return string + * @throws DBUnexpectedError + * @since 1.27 + */ + public function getDescriptionShortUrl() { + $dbr = $this->repo->getSlaveDB(); + $pageId = $dbr->selectField( 'page', 'page_id', array( + 'page_namespace' => NS_FILE, + 'page_title' => $this->title->getDBkey() + ) ); + + if ( $pageId !== false ) { + $url = $this->repo->makeUrl( array( 'curid' => $pageId ) ); + if ( $url !== false ) { + return $url; + } + } + return null; + } + } diff --git a/includes/filerepo/file/LocalFile.php b/includes/filerepo/file/LocalFile.php index b7d6f98478..9e214f6e20 100644 --- a/includes/filerepo/file/LocalFile.php +++ b/includes/filerepo/file/LocalFile.php @@ -757,6 +757,25 @@ class LocalFile extends File { } } + /** + * Get short description URL for a file based on the page ID. + * + * @return string|null + * @throws MWException + * @since 1.27 + */ + public function getDescriptionShortUrl() { + $pageId = $this->title->getArticleID(); + + if ( $pageId !== null ) { + $url = $this->repo->makeUrl( array( 'curid' => $pageId ) ); + if ( $url !== false ) { + return $url; + } + } + return null; + } + /** * Get handler-specific metadata * @return string -- 2.20.1