Merge "Moved RecentChange::purgeExpiredChanges to a job"
[lhc/web/wiklou.git] / includes / db / Database.php
index 0b022d1..9df96f1 100644 (file)
@@ -46,9 +46,6 @@ abstract class DatabaseBase implements IDatabase {
        /** Maximum time to wait before retry */
        const DEADLOCK_DELAY_MAX = 1500000;
 
-       /** How many row changes in a write query trigger a log entry */
-       const LOG_WRITE_THRESHOLD = 300;
-
        protected $mLastQuery = '';
        protected $mDoneWrites = false;
        protected $mPHPError = false;
@@ -1160,11 +1157,13 @@ abstract class DatabaseBase implements IDatabase {
 
                # Log the query time and feed it into the DB trx profiler
                if ( $queryProf != '' ) {
+                       $that = $this;
                        $queryStartTime = microtime( true );
                        $queryProfile = new ScopedCallback(
-                               function () use ( $queryStartTime, $queryProf, $isMaster ) {
-                                       $trxProfiler = Profiler::instance()->getTransactionProfiler();
-                                       $trxProfiler->recordQueryCompletion( $queryProf, $queryStartTime, $isMaster );
+                               function () use ( $that, $queryStartTime, $queryProf, $isMaster ) {
+                                       $n = $that->affectedRows();
+                                       $trxProf = Profiler::instance()->getTransactionProfiler();
+                                       $trxProf->recordQueryCompletion( $queryProf, $queryStartTime, $isMaster, $n );
                                }
                        );
                }
@@ -1206,13 +1205,6 @@ abstract class DatabaseBase implements IDatabase {
 
                if ( false === $ret ) {
                        $this->reportQueryError( $this->lastError(), $this->lastErrno(), $sql, $fname, $tempIgnore );
-               } else {
-                       $n = $this->affectedRows();
-                       if ( $isWriteQuery && $n > self::LOG_WRITE_THRESHOLD && PHP_SAPI !== 'cli' ) {
-                               wfDebugLog( 'DBPerformance',
-                                       "Query affected $n rows:\n" .
-                                       DatabaseBase::generalizeSQL( $sql ) . "\n" . wfBacktrace( true ) );
-                       }
                }
 
                $res = $this->resultObject( $ret );
@@ -1391,9 +1383,13 @@ abstract class DatabaseBase implements IDatabase {
         *
         * @return bool|mixed The value from the field, or false on failure.
         */
-       public function selectField( $table, $var, $cond = '', $fname = __METHOD__,
-               $options = array()
+       public function selectField(
+               $table, $var, $cond = '', $fname = __METHOD__, $options = array()
        ) {
+               if ( $var === '*' ) { // sanity
+                       throw new DBUnexpectedError( $this, "Cannot use a * field: got '$var'" );
+               }
+
                if ( !is_array( $options ) ) {
                        $options = array( $options );
                }
@@ -1401,7 +1397,6 @@ abstract class DatabaseBase implements IDatabase {
                $options['LIMIT'] = 1;
 
                $res = $this->select( $table, $var, $cond, $fname, $options );
-
                if ( $res === false || !$this->numRows( $res ) ) {
                        return false;
                }
@@ -1415,6 +1410,48 @@ abstract class DatabaseBase implements IDatabase {
                }
        }
 
+       /**
+        * A SELECT wrapper which returns a list of single field values from result rows.
+        *
+        * Usually throws a DBQueryError on failure. If errors are explicitly
+        * ignored, returns false on failure.
+        *
+        * If no result rows are returned from the query, false is returned.
+        *
+        * @param string|array $table Table name. See DatabaseBase::select() for details.
+        * @param string $var The field name to select. This must be a valid SQL
+        *   fragment: do not use unvalidated user input.
+        * @param string|array $cond The condition array. See DatabaseBase::select() for details.
+        * @param string $fname The function name of the caller.
+        * @param string|array $options The query options. See DatabaseBase::select() for details.
+        *
+        * @return bool|array The values from the field, or false on failure
+        * @since 1.25
+        */
+       public function selectFieldValues(
+               $table, $var, $cond = '', $fname = __METHOD__, $options = array()
+       ) {
+               if ( $var === '*' ) { // sanity
+                       throw new DBUnexpectedError( $this, "Cannot use a * field: got '$var'" );
+               }
+
+               if ( !is_array( $options ) ) {
+                       $options = array( $options );
+               }
+
+               $res = $this->select( $table, $var, $cond, $fname, $options );
+               if ( $res === false ) {
+                       return false;
+               }
+
+               $values = array();
+               foreach ( $res as $row ) {
+                       $values[] = $row->$var;
+               }
+
+               return $values;
+       }
+
        /**
         * Returns an optional USE INDEX clause to go after the table, and a
         * string to go at the end of the query.
@@ -1822,7 +1859,7 @@ abstract class DatabaseBase implements IDatabase {
 
                if ( $res ) {
                        $row = $this->fetchRow( $res );
-                       $rows = ( isset( $row['rowcount'] ) ) ? $row['rowcount'] : 0;
+                       $rows = ( isset( $row['rowcount'] ) ) ? (int)$row['rowcount'] : 0;
                }
 
                return $rows;
@@ -1852,7 +1889,7 @@ abstract class DatabaseBase implements IDatabase {
 
                if ( $res ) {
                        $row = $this->fetchRow( $res );
-                       $rows = ( isset( $row['rowcount'] ) ) ? $row['rowcount'] : 0;
+                       $rows = ( isset( $row['rowcount'] ) ) ? (int)$row['rowcount'] : 0;
                }
 
                return $rows;
@@ -3962,7 +3999,7 @@ abstract class DatabaseBase implements IDatabase {
 
                try {
                        $error = $this->sourceStream( $fp, $lineCallback, $resultCallback, $fname, $inputCallback );
-               } catch ( MWException $e ) {
+               } catch ( Exception $e ) {
                        fclose( $fp );
                        throw $e;
                }