From: Erik Bernhardson Date: Fri, 31 Oct 2014 20:16:54 +0000 (-0700) Subject: Always decode Blob objects from Database::addQuotes X-Git-Tag: 1.31.0-rc.0~12272^2 X-Git-Url: https://git.cyclocoop.org/%7B%24www_url%7Dadmin/compta/banques/ajouter.php?a=commitdiff_plain;h=7fd481c77077ef3d2dc03ee40278e277f2a211e5;p=lhc%2Fweb%2Fwiklou.git Always decode Blob objects from Database::addQuotes The current API for Database::encodeBlob/Database::addQuotes requires the code that is outputting binary data to have a database handle, so that it may call Database::encodeBlob to get either a plain string or a Blob object back. All database implementations other than MySQL return a Blob object from Database::encodeBlob. This is a rather inconvenient API, it tightly couples the creation of binary data with the Database object unnecessarily. If all database objects accept a Blob via Database::addQuotes then code can simply wrap its arguments in Blob and know that any database it ends up at will be properly handled. This patch changes the default implementation of Database::addQuotes to recognize a Blob object was passed in, and use Blob::fetch to turn it back into a string. Database implementations other than MySQL all handle this Blob object already. The postgresql implementation had to be adjusted slightly. Now when it sees a Blob object that it did not create it will encode that appropriately. Bug: 72367 Change-Id: I12fb4bd339be19137fffba2e47a70741382f6a8c --- diff --git a/autoload.php b/autoload.php index 90cd074d7d..32c9934fd1 100644 --- a/autoload.php +++ b/autoload.php @@ -881,6 +881,7 @@ $wgAutoloadLocalClasses = array( 'PopulateRevisionLength' => __DIR__ . '/maintenance/populateRevisionLength.php', 'PopulateRevisionSha1' => __DIR__ . '/maintenance/populateRevisionSha1.php', 'PostgreSqlLockManager' => __DIR__ . '/includes/filebackend/lockmanager/DBLockManager.php', + 'PostgresBlob' => __DIR__ . '/includes/db/DatabasePostgres.php', 'PostgresField' => __DIR__ . '/includes/db/DatabasePostgres.php', 'PostgresInstaller' => __DIR__ . '/includes/installer/PostgresInstaller.php', 'PostgresTransactionState' => __DIR__ . '/includes/db/DatabasePostgres.php', diff --git a/includes/db/Database.php b/includes/db/Database.php index 054f27a909..8e7623926d 100644 --- a/includes/db/Database.php +++ b/includes/db/Database.php @@ -2642,10 +2642,13 @@ abstract class DatabaseBase implements IDatabase { /** * Adds quotes and backslashes. * - * @param string $s + * @param string|Blob $s * @return string */ public function addQuotes( $s ) { + if ( $s instanceof Blob ) { + $s = $s->fetch(); + } if ( $s === null ) { return 'NULL'; } else { @@ -3909,10 +3912,13 @@ abstract class DatabaseBase implements IDatabase { * in result objects. Pass the object through this function to return the * original string. * - * @param string $b + * @param string|Blob $b * @return string */ public function decodeBlob( $b ) { + if ( $b instanceof Blob ) { + $b = $b->fetch(); + } return $b; } diff --git a/includes/db/DatabaseMssql.php b/includes/db/DatabaseMssql.php index a7bc6dd621..e5ae766143 100644 --- a/includes/db/DatabaseMssql.php +++ b/includes/db/DatabaseMssql.php @@ -1131,7 +1131,7 @@ class DatabaseMssql extends DatabaseBase { } /** - * @param string $s + * @param string|Blob $s * @return string */ public function addQuotes( $s ) { diff --git a/includes/db/DatabasePostgres.php b/includes/db/DatabasePostgres.php index ce14d7a969..0487b79f78 100644 --- a/includes/db/DatabasePostgres.php +++ b/includes/db/DatabasePostgres.php @@ -1495,12 +1495,14 @@ SQL; * @return Blob */ function encodeBlob( $b ) { - return new Blob( pg_escape_bytea( $this->mConn, $b ) ); + return new PostgresBlob( pg_escape_bytea( $b ) ); } function decodeBlob( $b ) { - if ( $b instanceof Blob ) { + if ( $b instanceof PostgresBlob ) { $b = $b->fetch(); + } elseif ( $b instanceof Blob ) { + return $b->fetch(); } return pg_unescape_bytea( $b ); @@ -1520,7 +1522,12 @@ SQL; } elseif ( is_bool( $s ) ) { return intval( $s ); } elseif ( $s instanceof Blob ) { - return "'" . $s->fetch( $s ) . "'"; + if ( $s instanceof PostgresBlob ) { + $s = $s->fetch(); + } else { + $s = pg_escape_bytea( $this->mConn, $s->fetch() ); + } + return "'$s'"; } return "'" . pg_escape_string( $this->mConn, $s ) . "'"; @@ -1692,3 +1699,5 @@ SQL; return wfBaseConvert( substr( sha1( $lockName ), 0, 15 ), 16, 10 ); } } // end DatabasePostgres class + +class PostgresBlob extends Blob {}