Ricordisamoa has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/179402

Change subject: Basic compatibility interface between Revision and File classes
......................................................................

Basic compatibility interface between Revision and File classes

New interface IBasicRevision, contains all common constants
and method definitions
 --> implemented by new class BasicRevision, that adds
     the static userCanBitfield() method taken from Revision
  --> extended by Revision and File

New methods File->isCurrent() and File->getComment()

As a first sample, Linker::revComment() has been tweaked to use
IBasicRevision instead of Revision.
This prepares the ground for a future patch that will enable
diffs between file contents.

Change-Id: Ic2b75e5fbd76ad71fa48252609f2087e666372f7
---
M autoload.php
A includes/BasicRevision.php
A includes/IBasicRevision.php
M includes/Linker.php
M includes/Revision.php
M includes/filerepo/file/File.php
M includes/filerepo/file/OldLocalFile.php
7 files changed, 214 insertions(+), 79 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/02/179402/1

diff --git a/autoload.php b/autoload.php
index 175ab42..980780f 100644
--- a/autoload.php
+++ b/autoload.php
@@ -147,6 +147,7 @@
        'BagOStuff' => __DIR__ . '/includes/objectcache/BagOStuff.php',
        'BaseDump' => __DIR__ . '/maintenance/backupPrefetch.inc',
        'BaseTemplate' => __DIR__ . '/includes/skins/BaseTemplate.php',
+       'BasicRevision' => __DIR__ . '/includes/BasicRevision.php',
        'BatchedQueryRunner' => __DIR__ . '/maintenance/runBatchedQuery.php',
        'BcryptPassword' => __DIR__ . '/includes/password/BcryptPassword.php',
        'BenchHttpHttps' => __DIR__ . 
'/maintenance/benchmarks/bench_HTTP_HTTPS.php',
@@ -497,6 +498,7 @@
        'Http' => __DIR__ . '/includes/HttpFunctions.php',
        'HttpError' => __DIR__ . '/includes/exception/HttpError.php',
        'HttpStatus' => __DIR__ . '/includes/libs/HttpStatus.php',
+       'IBasicRevision' => __DIR__ . '/includes/IBasicRevision.php',
        'ICacheHelper' => __DIR__ . '/includes/cache/CacheHelper.php',
        'IContextSource' => __DIR__ . '/includes/context/IContextSource.php',
        'IDBAccessObject' => __DIR__ . '/includes/dao/IDBAccessObject.php',
diff --git a/includes/BasicRevision.php b/includes/BasicRevision.php
new file mode 100644
index 0000000..4aa8028
--- /dev/null
+++ b/includes/BasicRevision.php
@@ -0,0 +1,72 @@
+<?php
+/**
+ * Representation of a page or file version.
+ *
+ * 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
+ */
+
+abstract class BasicRevision implements IBasicRevision {
+
+       /**
+        * Determine if the current user is allowed to view a particular
+        * field of this revision, if it's marked as deleted. This is used
+        * by various classes to avoid duplication.
+        *
+        * @param int $bitfield Current field
+        * @param int $field One of self::DELETED_CONTENT,
+        *                               self::DELETED_COMMENT,
+        *                               self::DELETED_USER
+        * @param User|null $user User object to check, or null to use $wgUser
+        * @param Title|null $title A Title object to check for per-page 
restrictions on,
+        *                          instead of just plain userrights
+        * @return bool
+        */
+       public static function userCanBitfield( $bitfield, $field, User $user = 
null,
+               Title $title = null
+       ) {
+               if ( $bitfield & $field ) { // aspect is deleted
+                       if ( $user === null ) {
+                               global $wgUser;
+                               $user = $wgUser;
+                       }
+                       if ( $bitfield & self::DELETED_RESTRICTED ) {
+                               $permissions = array( 'suppressrevision', 
'viewsuppressed' );
+                       } elseif ( $field & self::DELETED_CONTENT ) {
+                               $permissions = array( 'deletedtext' );
+                       } else {
+                               $permissions = array( 'deletedhistory' );
+                       }
+                       $permissionlist = implode( ', ', $permissions );
+                       if ( $title === null ) {
+                               wfDebug( "Checking for $permissionlist due to 
$field match on $bitfield\n" );
+                               return call_user_func_array( array( $user, 
'isAllowedAny' ), $permissions );
+                       } else {
+                               $text = $title->getPrefixedText();
+                               wfDebug( "Checking for $permissionlist on $text 
due to $field match on $bitfield\n" );
+                               foreach ( $permissions as $perm ) {
+                                       if ( $title->userCan( $perm, $user ) ) {
+                                               return true;
+                                       }
+                               }
+                               return false;
+                       }
+               } else {
+                       return true;
+               }
+       }
+}
diff --git a/includes/IBasicRevision.php b/includes/IBasicRevision.php
new file mode 100644
index 0000000..d660d5e
--- /dev/null
+++ b/includes/IBasicRevision.php
@@ -0,0 +1,112 @@
+<?php
+/**
+ * Interface for revisions consisting of content, user, comment and timestamp.
+ *
+ * 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
+ */
+
+interface IBasicRevision {
+
+       // Revision deletion constants
+       const DELETED_CONTENT = 1;
+       const DELETED_COMMENT = 2;
+       const DELETED_USER = 4;
+       const DELETED_RESTRICTED = 8;
+       const SUPPRESSED_USER = 12; // convenience
+
+       // Audience options for accessors
+       const FOR_PUBLIC = 1;
+       const FOR_THIS_USER = 2;
+       const RAW = 3;
+
+       public static function selectFields();
+
+       /**
+        * Returns the length or size of the content in this revision, or null 
if unknown.
+        *
+        * @return int|null
+        */
+       public function getSize();
+
+       /**
+        * Returns the base36 sha1 of the content in this revision, or null if 
unknown.
+        *
+        * @return string|null
+        */
+       public function getSha1();
+
+       /**
+        * Returns the title of the page associated with this entry or null.
+        *
+        * Will do a query, when title is not set and id is given.
+        *
+        * @return Title|null
+        */
+       public function getTitle();
+
+       /**
+        * Fetch revision comment if it's available to the specified audience.
+        * If the specified audience does not have access to the comment, an
+        * empty string will be returned.
+        *
+        * @param int $audience One of:
+        *   IBasicRevision::FOR_PUBLIC       to be displayed to all users
+        *   IBasicRevision::FOR_THIS_USER    to be displayed to the given user
+        *   IBasicRevision::RAW              get the text regardless of 
permissions
+        * @param User $user User object to check for, only if FOR_THIS_USER is 
passed
+        *   to the $audience parameter
+        * @return string
+        */
+       public function getComment( $audience = self::FOR_PUBLIC, User $user = 
null );
+
+       /**
+        * @param int $field One of DELETED_* bitfield constants
+        *
+        * @return bool
+        */
+       public function isDeleted( $field );
+
+       /**
+        * Get the deletion bitfield of the revision
+        *
+        * @return int
+        */
+       public function getVisibility();
+
+       /**
+        * @return string
+        */
+       public function getTimestamp();
+
+       /**
+        * @return bool
+        */
+       public function isCurrent();
+
+       /**
+        * Determine if the current user is allowed to view a particular
+        * field of this revision, if it's marked as deleted.
+        *
+        * @param int $field One of self::DELETED_CONTENT,
+        *                              self::DELETED_COMMENT,
+        *                              self::DELETED_USER
+        * @param User|null $user User object to check, or null to use $wgUser
+        * @return bool
+        */
+       public function userCan( $field, User $user = null );
+}
diff --git a/includes/Linker.php b/includes/Linker.php
index bfb8937..e3e3efa 100644
--- a/includes/Linker.php
+++ b/includes/Linker.php
@@ -1588,24 +1588,24 @@
         * Wrap and format the given revision's comment block, if the current
         * user is allowed to view it.
         *
-        * @param Revision $rev
+        * @param IBasicRevision $rev
         * @param bool $local Whether section links should refer to local page
         * @param bool $isPublic Show only if all users can see it
         * @return string HTML fragment
         */
-       public static function revComment( Revision $rev, $local = false, 
$isPublic = false ) {
-               if ( $rev->getRawComment() == "" ) {
+       public static function revComment( IBasicRevision $rev, $local = false, 
$isPublic = false ) {
+               if ( $rev->getComment( IBasicRevision::RAW ) == "" ) {
                        return "";
                }
-               if ( $rev->isDeleted( Revision::DELETED_COMMENT ) && $isPublic 
) {
+               if ( $rev->isDeleted( IBasicRevision::DELETED_COMMENT ) && 
$isPublic ) {
                        $block = " <span class=\"comment\">" . wfMessage( 
'rev-deleted-comment' )->escaped() . "</span>";
-               } elseif ( $rev->userCan( Revision::DELETED_COMMENT ) ) {
-                       $block = self::commentBlock( $rev->getComment( 
Revision::FOR_THIS_USER ),
+               } elseif ( $rev->userCan( IBasicRevision::DELETED_COMMENT ) ) {
+                       $block = self::commentBlock( $rev->getComment( 
IBasicRevision::FOR_THIS_USER ),
                                $rev->getTitle(), $local );
                } else {
                        $block = " <span class=\"comment\">" . wfMessage( 
'rev-deleted-comment' )->escaped() . "</span>";
                }
-               if ( $rev->isDeleted( Revision::DELETED_COMMENT ) ) {
+               if ( $rev->isDeleted( IBasicRevision::DELETED_COMMENT ) ) {
                        return " <span class=\"history-deleted\">$block</span>";
                }
                return $block;
diff --git a/includes/Revision.php b/includes/Revision.php
index 8ba79df..1810b09 100644
--- a/includes/Revision.php
+++ b/includes/Revision.php
@@ -23,7 +23,7 @@
 /**
  * @todo document
  */
-class Revision implements IDBAccessObject {
+class Revision extends BasicRevision implements IDBAccessObject {
        protected $mId;
 
        /**
@@ -71,17 +71,8 @@
         */
        protected $mQueryFlags = 0;
 
-       // Revision deletion constants
-       const DELETED_TEXT = 1;
-       const DELETED_COMMENT = 2;
-       const DELETED_USER = 4;
-       const DELETED_RESTRICTED = 8;
-       const SUPPRESSED_USER = 12; // convenience
-
-       // Audience options for accessors
-       const FOR_PUBLIC = 1;
-       const FOR_THIS_USER = 2;
-       const RAW = 3;
+       // Alias for DELETED_CONTENT
+       const DELETED_TEXT = parent::DELETED_CONTENT;
 
        /**
         * Load a page revision from a given revision ID number.
@@ -1635,54 +1626,6 @@
         */
        public function userCan( $field, User $user = null ) {
                return self::userCanBitfield( $this->mDeleted, $field, $user );
-       }
-
-       /**
-        * Determine if the current user is allowed to view a particular
-        * field of this revision, if it's marked as deleted. This is used
-        * by various classes to avoid duplication.
-        *
-        * @param int $bitfield Current field
-        * @param int $field One of self::DELETED_TEXT = File::DELETED_FILE,
-        *                               self::DELETED_COMMENT = 
File::DELETED_COMMENT,
-        *                               self::DELETED_USER = File::DELETED_USER
-        * @param User|null $user User object to check, or null to use $wgUser
-        * @param Title|null $title A Title object to check for per-page 
restrictions on,
-        *                          instead of just plain userrights
-        * @return bool
-        */
-       public static function userCanBitfield( $bitfield, $field, User $user = 
null,
-               Title $title = null
-       ) {
-               if ( $bitfield & $field ) { // aspect is deleted
-                       if ( $user === null ) {
-                               global $wgUser;
-                               $user = $wgUser;
-                       }
-                       if ( $bitfield & self::DELETED_RESTRICTED ) {
-                               $permissions = array( 'suppressrevision', 
'viewsuppressed' );
-                       } elseif ( $field & self::DELETED_TEXT ) {
-                               $permissions = array( 'deletedtext' );
-                       } else {
-                               $permissions = array( 'deletedhistory' );
-                       }
-                       $permissionlist = implode( ', ', $permissions );
-                       if ( $title === null ) {
-                               wfDebug( "Checking for $permissionlist due to 
$field match on $bitfield\n" );
-                               return call_user_func_array( array( $user, 
'isAllowedAny' ), $permissions );
-                       } else {
-                               $text = $title->getPrefixedText();
-                               wfDebug( "Checking for $permissionlist on $text 
due to $field match on $bitfield\n" );
-                               foreach ( $permissions as $perm ) {
-                                       if ( $title->userCan( $perm, $user ) ) {
-                                               return true;
-                                       }
-                               }
-                               return false;
-                       }
-               } else {
-                       return true;
-               }
        }
 
        /**
diff --git a/includes/filerepo/file/File.php b/includes/filerepo/file/File.php
index 58311d3..5fbf6c2 100644
--- a/includes/filerepo/file/File.php
+++ b/includes/filerepo/file/File.php
@@ -47,12 +47,9 @@
  *
  * @ingroup FileAbstraction
  */
-abstract class File {
-       // Bitfield values akin to the Revision deletion constants
-       const DELETED_FILE = 1;
-       const DELETED_COMMENT = 2;
-       const DELETED_USER = 4;
-       const DELETED_RESTRICTED = 8;
+abstract class File extends BasicRevision {
+       // Alias for DELETED_CONTENT
+       const DELETED_FILE = parent::DELETED_CONTENT;
 
        /** Force rendering in the current process */
        const RENDER_NOW = 1;
@@ -63,11 +60,6 @@
        const RENDER_FORCE = 2;
 
        const DELETE_SOURCE = 1;
-
-       // Audience options for File::getDescription()
-       const FOR_PUBLIC = 1;
-       const FOR_THIS_USER = 2;
-       const RAW = 3;
 
        // Options for File::thumbName()
        const THUMB_FULL_NAME = 1;
@@ -1820,6 +1812,13 @@
        }
 
        /**
+        * Inverse of isOld(), for compatibility with BasicRevision
+        */
+       public function isCurrent() {
+               return !$this->isOld();
+       }
+
+       /**
         * Is this file a "deleted" file in a private archive?
         * STUB
         *
@@ -2043,6 +2042,13 @@
        }
 
        /**
+        * Alias for getDescription(), for compatibility with BasicRevision
+        */
+       public function getComment( $audience = self::FOR_PUBLIC, User $user = 
null ) {
+               return $this->getDescription( $audience, $user );
+       }
+
+       /**
         * Get the 14-character timestamp of the file upload
         *
         * @return string|bool TS_MW timestamp or false on failure
diff --git a/includes/filerepo/file/OldLocalFile.php 
b/includes/filerepo/file/OldLocalFile.php
index 710058f..1f5d7e9 100644
--- a/includes/filerepo/file/OldLocalFile.php
+++ b/includes/filerepo/file/OldLocalFile.php
@@ -326,7 +326,7 @@
        function userCan( $field, User $user = null ) {
                $this->load();
 
-               return Revision::userCanBitfield( $this->deleted, $field, $user 
);
+               return self::userCanBitfield( $this->deleted, $field, $user );
        }
 
        /**

-- 
To view, visit https://gerrit.wikimedia.org/r/179402
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ic2b75e5fbd76ad71fa48252609f2087e666372f7
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Ricordisamoa <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to