Provide short URL to file description page in imageinfo API
authorvictorbarbu <victorbarbu08@gmail.com>
Mon, 4 Jan 2016 23:40:01 +0000 (01:40 +0200)
committervictorbarbu <victorbarbu08@gmail.com>
Tue, 12 Jan 2016 13:48:52 +0000 (15:48 +0200)
Bug: T122439
Change-Id: I0f00b986e6095bdb9b8d6af6fbc5b01995227e02

includes/api/ApiQueryImageInfo.php
includes/filerepo/ForeignAPIRepo.php
includes/filerepo/file/File.php
includes/filerepo/file/ForeignAPIFile.php
includes/filerepo/file/ForeignDBFile.php
includes/filerepo/file/LocalFile.php

index 8dbd812..267cc6d 100644 (file)
@@ -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 ) {
index debdeb5..a8d37a1 100644 (file)
@@ -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;
                                }
                        }
                }
index 72f12d1..8a3900e 100644 (file)
@@ -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
index 26ea38b..43cb5a5 100644 (file)
@@ -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
index 561ead7..611ae10 100644 (file)
@@ -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;
+       }
+
 }
index b7d6f98..9e214f6 100644 (file)
@@ -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