Merge "Show no size links on image pages with errors"
[lhc/web/wiklou.git] / includes / db / Database.php
index 62a3d87..7e865c1 100644 (file)
@@ -112,8 +112,8 @@ interface DatabaseType {
         * The value inserted should be fetched from nextSequenceValue()
         *
         * Example:
-        * $id = $dbw->nextSequenceValue('page_page_id_seq');
-        * $dbw->insert('page',array('page_id' => $id));
+        * $id = $dbw->nextSequenceValue( 'page_page_id_seq' );
+        * $dbw->insert( 'page', array( 'page_id' => $id ) );
         * $id = $dbw->insertId();
         *
         * @return int
@@ -393,7 +393,7 @@ abstract class DatabaseBase implements DatabaseType {
                return wfSetVar( $this->mTablePrefix, $prefix );
        }
 
-       /**
+       /**
         * Set the filehandle to copy write statements to.
         *
         * @param $fh filehandle
@@ -584,7 +584,7 @@ abstract class DatabaseBase implements DatabaseType {
                global $wgDebugDBTransactions;
                $this->mFlags |= $flag;
                if ( ( $flag & DBO_TRX) & $wgDebugDBTransactions ) {
-                       wfDebug("Implicit transactions are now  disabled.\n");
+                       wfDebug( "Implicit transactions are now  disabled.\n" );
                }
        }
 
@@ -597,7 +597,7 @@ abstract class DatabaseBase implements DatabaseType {
                global $wgDebugDBTransactions;
                $this->mFlags &= ~$flag;
                if ( ( $flag & DBO_TRX ) && $wgDebugDBTransactions ) {
-                       wfDebug("Implicit transactions are now disabled.\n");
+                       wfDebug( "Implicit transactions are now disabled.\n" );
                }
        }
 
@@ -671,12 +671,12 @@ abstract class DatabaseBase implements DatabaseType {
                        if ( $wgCommandLineMode ) {
                                $this->mFlags &= ~DBO_TRX;
                                if ( $wgDebugDBTransactions ) {
-                                       wfDebug("Implicit transaction open disabled.\n");
+                                       wfDebug( "Implicit transaction open disabled.\n" );
                                }
                        } else {
                                $this->mFlags |= DBO_TRX;
                                if ( $wgDebugDBTransactions ) {
-                                       wfDebug("Implicit transaction open enabled.\n");
+                                       wfDebug( "Implicit transaction open enabled.\n" );
                                }
                        }
                }
@@ -724,7 +724,7 @@ abstract class DatabaseBase implements DatabaseType {
         *    Valid options are: host, user, password, dbname, flags, tablePrefix
         * @return DatabaseBase subclass or null
         */
-       public final static function factory( $dbType, $p = array() ) {
+       final public static function factory( $dbType, $p = array() ) {
                $canonicalDBTypes = array(
                        'mysql', 'postgres', 'sqlite', 'oracle', 'mssql', 'ibm_db2'
                );
@@ -772,7 +772,7 @@ abstract class DatabaseBase implements DatabaseType {
         * @param $errno
         * @param $errstr
         */
-       protected function connectionErrorHandler( $errno,  $errstr ) {
+       protected function connectionErrorHandler( $errno, $errstr ) {
                $this->mPHPError = $errstr;
        }
 
@@ -811,7 +811,7 @@ abstract class DatabaseBase implements DatabaseType {
         * @since 1.20
         * @return bool: Whether connection was closed successfully
         */
-       protected abstract function closeConnection();
+       abstract protected function closeConnection();
 
        /**
         * @param $error String: fallback error message, used if none is given by DB
@@ -833,7 +833,7 @@ abstract class DatabaseBase implements DatabaseType {
         * @param  $sql String: SQL query.
         * @return ResultWrapper Result object to feed to fetchObject, fetchRow, ...; or false on failure
         */
-       protected abstract function doQuery( $sql );
+       abstract protected function doQuery( $sql );
 
        /**
         * Determine whether a query writes to the DB.
@@ -921,7 +921,7 @@ abstract class DatabaseBase implements DatabaseType {
                        if ( strpos( $sqlstart, "SHOW " ) !== 0 && strpos( $sqlstart, "SET " ) !== 0 ) {
                                global $wgDebugDBTransactions;
                                if ( $wgDebugDBTransactions ) {
-                                       wfDebug("Implicit transaction start.\n");
+                                       wfDebug( "Implicit transaction start.\n" );
                                }
                                $this->begin( __METHOD__ . " ($fname)" );
                                $this->mTrxAutomatic = true;
@@ -1096,7 +1096,7 @@ abstract class DatabaseBase implements DatabaseType {
                        case '\\&': return '&';
                }
 
-               list( /* $n */ , $arg ) = each( $this->preparedArgs );
+               list( /* $n */, $arg ) = each( $this->preparedArgs );
 
                switch( $matches[1] ) {
                        case '?': return $this->addQuotes( $arg );
@@ -1180,26 +1180,9 @@ abstract class DatabaseBase implements DatabaseType {
                        }
                }
 
-               if ( isset( $options['GROUP BY'] ) ) {
-                       $gb = is_array( $options['GROUP BY'] )
-                               ? implode( ',', $options['GROUP BY'] )
-                               : $options['GROUP BY'];
-                       $preLimitTail .= " GROUP BY {$gb}";
-               }
+               $preLimitTail .= $this->makeGroupByWithHaving( $options );
 
-               if ( isset( $options['HAVING'] ) ) {
-                       $having = is_array( $options['HAVING'] )
-                               ? $this->makeList( $options['HAVING'], LIST_AND )
-                               : $options['HAVING'];
-                       $preLimitTail .= " HAVING {$having}";
-               }
-
-               if ( isset( $options['ORDER BY'] ) ) {
-                       $ob = is_array( $options['ORDER BY'] )
-                               ? implode( ',', $options['ORDER BY'] )
-                               : $options['ORDER BY'];
-                       $preLimitTail .= " ORDER BY {$ob}";
-               }
+               $preLimitTail .= $this->makeOrderBy( $options );
 
                // if (isset($options['LIMIT'])) {
                //      $tailOpts .= $this->limitResult('', $options['LIMIT'],
@@ -1261,6 +1244,49 @@ abstract class DatabaseBase implements DatabaseType {
                return array( $startOpts, $useIndex, $preLimitTail, $postLimitTail );
        }
 
+       /**
+        * Returns an optional GROUP BY with an optional HAVING
+        *
+        * @param $options Array: associative array of options
+        * @return string
+        * @see DatabaseBase::select()
+        * @since 1.21
+        */
+       public function makeGroupByWithHaving( $options ) {
+               $sql = '';
+               if ( isset( $options['GROUP BY'] ) ) {
+                       $gb = is_array( $options['GROUP BY'] )
+                               ? implode( ',', $options['GROUP BY'] )
+                               : $options['GROUP BY'];
+                       $sql .= ' GROUP BY ' . $gb;
+               }
+               if ( isset( $options['HAVING'] ) ) {
+                       $having = is_array( $options['HAVING'] )
+                               ? $this->makeList( $options['HAVING'], LIST_AND )
+                               : $options['HAVING'];
+                       $sql .= ' HAVING ' . $having;
+               }
+               return $sql;
+       }
+
+       /**
+        * Returns an optional ORDER BY
+        *
+        * @param $options Array: associative array of options
+        * @return string
+        * @see DatabaseBase::select()
+        * @since 1.21
+        */
+       public function makeOrderBy( $options ) {
+               if ( isset( $options['ORDER BY'] ) ) {
+                       $ob = is_array( $options['ORDER BY'] )
+                               ? implode( ',', $options['ORDER BY'] )
+                               : $options['ORDER BY'];
+                       return ' ORDER BY ' . $ob;
+               }
+               return '';
+       }
+
        /**
         * Execute a SELECT query constructed using the various parameters provided.
         * See below for full details of the parameters.
@@ -1393,7 +1419,7 @@ abstract class DatabaseBase implements DatabaseType {
         * join, the second is an SQL fragment giving the join condition for that
         * table. For example:
         *
-        *    array( 'page' => array('LEFT JOIN','page_latest=rev_id') )
+        *    array( 'page' => array( 'LEFT JOIN', 'page_latest=rev_id' ) )
         *
         * @return ResultWrapper. If the query returned no rows, a ResultWrapper
         *   with no rows in it will be returned. If there was a query error, a
@@ -2071,7 +2097,7 @@ abstract class DatabaseBase implements DatabaseType {
         * This is handy when you need to construct SQL for joins
         *
         * Example:
-        * extract($dbr->tableNames('user','watchlist'));
+        * extract( $dbr->tableNames( 'user', 'watchlist' ) );
         * $sql = "SELECT wl_namespace,wl_title FROM $watchlist,$user
         *         WHERE wl_user=user_id AND wl_user=$nameWithQuotes";
         *
@@ -2093,7 +2119,7 @@ abstract class DatabaseBase implements DatabaseType {
         * This is handy when you need to construct SQL for joins
         *
         * Example:
-        * list( $user, $watchlist ) = $dbr->tableNamesN('user','watchlist');
+        * list( $user, $watchlist ) = $dbr->tableNamesN( 'user', 'watchlist' );
         * $sql = "SELECT wl_namespace,wl_title FROM $watchlist,$user
         *         WHERE wl_user=user_id AND wl_user=$nameWithQuotes";
         *
@@ -2645,7 +2671,7 @@ abstract class DatabaseBase implements DatabaseType {
                list( $startOpts, $useIndex, $tailOpts ) = $this->makeSelectOptions( $selectOptions );
 
                if ( is_array( $srcTable ) ) {
-                       $srcTable =  implode( ',', array_map( array( &$this, 'tableName' ), $srcTable ) );
+                       $srcTable = implode( ',', array_map( array( &$this, 'tableName' ), $srcTable ) );
                } else {
                        $srcTable = $this->tableName( $srcTable );
                }
@@ -3440,7 +3466,7 @@ abstract class DatabaseBase implements DatabaseType {
                        // replace `{$var}`
                        $ins = str_replace( '`{$' . $var . '}`', $this->addIdentifierQuotes( $value ), $ins );
                        // replace /*$var*/
-                       $ins = str_replace( '/*$' . $var . '*/', $this->strencode( $value ) , $ins );
+                       $ins = str_replace( '/*$' . $var . '*/', $this->strencode( $value ), $ins );
                }
                return $ins;
        }