Merge "fix sidebar HTML escaping in CologneBlue"
[lhc/web/wiklou.git] / includes / filerepo / file / ArchivedFile.php
index 34044ba..694623b 100644 (file)
@@ -1,15 +1,30 @@
 <?php
 /**
- * Deleted file in the 'filearchive' table
+ * Deleted file in the 'filearchive' table.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
  *
  * @file
- * @ingroup FileRepo
+ * @ingroup FileAbstraction
  */
 
 /**
  * Class representing a row of the 'filearchive' table
  *
- * @ingroup FileRepo
+ * @ingroup FileAbstraction
  */
 class ArchivedFile {
        /**#@+
@@ -32,6 +47,7 @@ class ArchivedFile {
                $timestamp, # time of upload
                $dataLoaded, # Whether or not all this has been loaded from the database (loadFromXxx)
                $deleted, # Bitfield akin to rev_deleted
+               $sha1, # sha1 hash of file content
                $pageCount,
                $archive_name;
 
@@ -72,6 +88,7 @@ class ArchivedFile {
                $this->deleted = 0;
                $this->dataLoaded = false;
                $this->exists = false;
+               $this->sha1 = '';
 
                if( $title instanceof Title ) {
                        $this->title = File::normalizeTitle( $title, 'exception' );
@@ -93,7 +110,8 @@ class ArchivedFile {
 
        /**
         * Loads a file object from the filearchive table
-        * @return true on success or null
+        * @throws MWException
+        * @return bool|null True on success or null
         */
        public function load() {
                if ( $this->dataLoaded ) {
@@ -137,35 +155,20 @@ class ArchivedFile {
                                        'fa_user',
                                        'fa_user_text',
                                        'fa_timestamp',
-                                       'fa_deleted' ),
+                                       'fa_deleted',
+                                       'fa_sha1' ),
                                $conds,
                                __METHOD__,
                                array( 'ORDER BY' => 'fa_timestamp DESC' ) );
                        if ( $res == false || $dbr->numRows( $res ) == 0 ) {
                        // this revision does not exist?
-                               return;
+                               return null;
                        }
                        $ret = $dbr->resultObject( $res );
                        $row = $ret->fetchObject();
 
                        // initialize fields for filestore image object
-                       $this->id = intval($row->fa_id);
-                       $this->name = $row->fa_name;
-                       $this->archive_name = $row->fa_archive_name;
-                       $this->group = $row->fa_storage_group;
-                       $this->key = $row->fa_storage_key;
-                       $this->size = $row->fa_size;
-                       $this->bits = $row->fa_bits;
-                       $this->width = $row->fa_width;
-                       $this->height = $row->fa_height;
-                       $this->metadata = $row->fa_metadata;
-                       $this->mime = "$row->fa_major_mime/$row->fa_minor_mime";
-                       $this->media_type = $row->fa_media_type;
-                       $this->description = $row->fa_description;
-                       $this->user = $row->fa_user;
-                       $this->user_text = $row->fa_user_text;
-                       $this->timestamp = $row->fa_timestamp;
-                       $this->deleted = $row->fa_deleted;
+                       $this->loadFromRow( $row );
                } else {
                        throw new MWException( 'This title does not correspond to an image page.' );
                }
@@ -184,28 +187,42 @@ class ArchivedFile {
         */
        public static function newFromRow( $row ) {
                $file = new ArchivedFile( Title::makeTitle( NS_FILE, $row->fa_name ) );
-
-               $file->id = intval($row->fa_id);
-               $file->name = $row->fa_name;
-               $file->archive_name = $row->fa_archive_name;
-               $file->group = $row->fa_storage_group;
-               $file->key = $row->fa_storage_key;
-               $file->size = $row->fa_size;
-               $file->bits = $row->fa_bits;
-               $file->width = $row->fa_width;
-               $file->height = $row->fa_height;
-               $file->metadata = $row->fa_metadata;
-               $file->mime = "$row->fa_major_mime/$row->fa_minor_mime";
-               $file->media_type = $row->fa_media_type;
-               $file->description = $row->fa_description;
-               $file->user = $row->fa_user;
-               $file->user_text = $row->fa_user_text;
-               $file->timestamp = $row->fa_timestamp;
-               $file->deleted = $row->fa_deleted;
-
+               $file->loadFromRow( $row );
                return $file;
        }
 
+       /**
+        * Load ArchivedFile object fields from a DB row.
+        *
+        * @param $row Object database row
+        * @since 1.21
+        */
+       public function loadFromRow( $row ) {
+               $this->id = intval($row->fa_id);
+               $this->name = $row->fa_name;
+               $this->archive_name = $row->fa_archive_name;
+               $this->group = $row->fa_storage_group;
+               $this->key = $row->fa_storage_key;
+               $this->size = $row->fa_size;
+               $this->bits = $row->fa_bits;
+               $this->width = $row->fa_width;
+               $this->height = $row->fa_height;
+               $this->metadata = $row->fa_metadata;
+               $this->mime = "$row->fa_major_mime/$row->fa_minor_mime";
+               $this->media_type = $row->fa_media_type;
+               $this->description = $row->fa_description;
+               $this->user = $row->fa_user;
+               $this->user_text = $row->fa_user_text;
+               $this->timestamp = $row->fa_timestamp;
+               $this->deleted = $row->fa_deleted;
+               if( isset( $row->fa_sha1 ) ) {
+                       $this->sha1 = $row->fa_sha1;
+               } else {
+                       // old row, populate from key
+                       $this->sha1 = LocalRepo::getHashFromKey( $this->key );
+               }
+       }
+
        /**
         * Return the associated title object
         *
@@ -365,6 +382,17 @@ class ArchivedFile {
                return wfTimestamp( TS_MW, $this->timestamp );
        }
 
+       /**
+        * Get the SHA-1 base 36 hash of the file
+        *
+        * @return string
+        * @since 1.21
+        */
+       function getSha1() {
+               $this->load();
+               return $this->sha1;
+       }
+
        /**
         * Return the user ID of the uploader.
         *