Merge "Implement static public Parser::getExternalLinkRel"
[lhc/web/wiklou.git] / includes / HistoryBlob.php
index 7535a0d..05c27fe 100644 (file)
@@ -1,8 +1,28 @@
 <?php
+/**
+ * Efficient concatenated text storage.
+ *
+ * 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
+ */
 
 /**
- * Base class for general text storage via the "object" flag in old_flags, or 
- * two-part external storage URLs. Used for represent efficient concatenated 
+ * Base class for general text storage via the "object" flag in old_flags, or
+ * two-part external storage URLs. Used for represent efficient concatenated
  * storage, and migration-related pointer objects.
  */
 interface HistoryBlob
@@ -158,7 +178,7 @@ class ConcatenatedGzipHistoryBlob implements HistoryBlob
         * @return bool
         */
        public function isHappy() {
-               return $this->mSize < $this->mMaxSize 
+               return $this->mSize < $this->mMaxSize
                        && count( $this->mItems ) < $this->mMaxCount;
        }
 }
@@ -179,8 +199,8 @@ class HistoryBlobStub {
        var $mOldId, $mHash, $mRef;
 
        /**
-        * @param $hash Strng: the content hash of the text
-        * @param $oldid Integer: the old_id for the CGZ object
+        * @param $hash string the content hash of the text
+        * @param $oldid Integer the old_id for the CGZ object
         */
        function __construct( $hash = '', $oldid = 0 ) {
                $this->mHash = $hash;
@@ -298,7 +318,7 @@ class HistoryBlobCurStub {
        }
 
        /**
-        * @return string|false
+        * @return string|bool
         */
        function getText() {
                $dbr = wfGetDB( DB_SLAVE );
@@ -321,12 +341,12 @@ class DiffHistoryBlob implements HistoryBlob {
        /** Total uncompressed size */
        var $mSize = 0;
 
-       /** 
-        * Array of diffs. If a diff D from A to B is notated D = B - A, and Z is 
+       /**
+        * Array of diffs. If a diff D from A to B is notated D = B - A, and Z is
         * an empty string:
         *
         *              { item[map[i]] - item[map[i-1]]   where i > 0
-        *    diff[i] = { 
+        *    diff[i] = {
         *              { item[map[i]] - Z                where i = 0
         */
        var $mDiffs;
@@ -359,7 +379,7 @@ class DiffHistoryBlob implements HistoryBlob {
         * The maximum number of text items before the object becomes sad
         */
        var $mMaxCount = 100;
-       
+
        /** Constants from xdiff.h */
        const XDL_BDOP_INS = 1;
        const XDL_BDOP_CPY = 2;
@@ -413,7 +433,7 @@ class DiffHistoryBlob implements HistoryBlob {
         * @throws MWException
         */
        function compress() {
-               if ( !function_exists( 'xdiff_string_rabdiff' ) ){ 
+               if ( !function_exists( 'xdiff_string_rabdiff' ) ){
                        throw new MWException( "Need xdiff 1.5+ support to write DiffHistoryBlob\n" );
                }
                if ( isset( $this->mDiffs ) ) {
@@ -514,20 +534,18 @@ class DiffHistoryBlob implements HistoryBlob {
                # Pure PHP implementation
 
                $header = unpack( 'Vofp/Vcsize', substr( $diff, 0, 8 ) );
-               
-               # Check the checksum if mhash is available
-               if ( function_exists( 'mhash' ) ) {
-                       $ofp = mhash( MHASH_ADLER32, $base );
-                       if ( $ofp !== substr( $diff, 0, 4 ) ) {
-                               wfDebug( __METHOD__. ": incorrect base checksum\n" );
-                               return false;
-                       }
+
+               # Check the checksum if hash/mhash is available
+               $ofp = $this->xdiffAdler32( $base );
+               if ( $ofp !== false && $ofp !== substr( $diff, 0, 4 ) ) {
+                       wfDebug( __METHOD__. ": incorrect base checksum\n" );
+                       return false;
                }
                if ( $header['csize'] != strlen( $base ) ) {
                        wfDebug( __METHOD__. ": incorrect base length\n" );
                        return false;
                }
-               
+
                $p = 8;
                $out = '';
                while ( $p < strlen( $diff ) ) {
@@ -560,6 +578,30 @@ class DiffHistoryBlob implements HistoryBlob {
                return $out;
        }
 
+       /**
+        * Compute a binary "Adler-32" checksum as defined by LibXDiff, i.e. with
+        * the bytes backwards and initialised with 0 instead of 1. See bug 34428.
+        *
+        * Returns false if no hashing library is available
+        */
+       function xdiffAdler32( $s ) {
+               static $init;
+               if ( $init === null ) {
+                       $init = str_repeat( "\xf0", 205 ) . "\xee" . str_repeat( "\xf0", 67 ) . "\x02";
+               }
+               // The real Adler-32 checksum of $init is zero, so it initialises the
+               // state to zero, as it is at the start of LibXDiff's checksum
+               // algorithm. Appending the subject string then simulates LibXDiff.
+               if ( function_exists( 'hash' ) ) {
+                       $hash = hash( 'adler32', $init . $s, true );
+               } elseif ( function_exists( 'mhash' ) ) {
+                       $hash = mhash( MHASH_ADLER32, $init . $s );
+               } else {
+                       return false;
+               }
+               return strrev( $hash );
+       }
+
        function uncompress() {
                if ( !$this->mDiffs ) {
                        return;
@@ -622,7 +664,7 @@ class DiffHistoryBlob implements HistoryBlob {
                if ( isset( $info['base'] ) ) {
                        // Old format
                        $this->mDiffMap = range( 0, count( $this->mDiffs ) - 1 );
-                       array_unshift( $this->mDiffs, 
+                       array_unshift( $this->mDiffs,
                                pack( 'VVCV', 0, 0, self::XDL_BDOP_INSB, strlen( $info['base'] ) ) .
                                $info['base'] );
                } else {
@@ -645,7 +687,7 @@ class DiffHistoryBlob implements HistoryBlob {
         * @return bool
         */
        function isHappy() {
-               return $this->mSize < $this->mMaxSize 
+               return $this->mSize < $this->mMaxSize
                        && count( $this->mItems ) < $this->mMaxCount;
        }