Add File::getDescriptionTouched() method
authorGergő Tisza <tgr.huwiki@gmail.com>
Thu, 5 Feb 2015 03:43:22 +0000 (03:43 +0000)
committerGergő Tisza <tgr.huwiki@gmail.com>
Sat, 14 Feb 2015 00:49:26 +0000 (00:49 +0000)
Fetch page_touched timestamp of file description page via a DB query
(possibly to a foreign DB).

Not sure about the performance implications; could add a memcached
layer on top, but the same DB lookup was already done for local
files.

Bug: T88648
Change-Id: I891c806aab235ff2c0e73c98b06b64fbe78e1517

includes/filerepo/file/File.php
includes/filerepo/file/LocalFile.php

index df85f9c..6ca61b2 100644 (file)
@@ -2046,6 +2046,17 @@ abstract class File {
                return $this->repo->getFileTimestamp( $this->getPath() );
        }
 
+       /**
+        * Returns the timestamp (in TS_MW format) of the last change of the description page.
+        * Returns false if the file does not have a description page, or retrieving the timestamp
+        * would be expensive.
+        * @since 1.25
+        * @return string|bool
+        */
+       public function getDescriptionTouched() {
+               return false;
+       }
+
        /**
         * Get the SHA-1 base 36 hash of the file
         *
index b2e5b00..699c915 100644 (file)
@@ -109,6 +109,9 @@ class LocalFile extends File {
        /** @var string Description of current revision of the file */
        private $description;
 
+       /** @var string TS_MW timestamp of the last change of the file description */
+       private $descriptionTouched;
+
        /** @var bool Whether the row was upgraded on load */
        private $upgraded;
 
@@ -1777,6 +1780,22 @@ class LocalFile extends File {
                return $this->timestamp;
        }
 
+       /**
+        * @return bool|string
+        */
+       public function getDescriptionTouched() {
+               // The DB lookup might return false, e.g. if the file was just deleted, or the shared DB repo
+               // itself gets it from elsewhere. To avoid repeating the DB lookups in such a case, we
+               // need to differentiate between null (uninitialized) and false (failed to load).
+               if ( $this->descriptionTouched === null ) {
+                       $cond = array( 'page_namespace' => $this->title->getNamespace(), 'page_title' => $this->title->getDBkey() );
+                       $touched = $this->repo->getSlaveDB()->selectField( 'page', 'page_touched', $cond, __METHOD__ );
+                       $this->descriptionTouched = $touched ? wfTimestamp( TS_MW, $touched ) : false;
+               }
+
+               return $this->descriptionTouched;
+       }
+
        /**
         * @return string
         */