From ca32f08966f1b51fcb19460f0996bb0c4048e6fe Mon Sep 17 00:00:00 2001 From: Pierre Schmitz Date: Sat, 3 Dec 2011 13:29:22 +0100 Subject: Update to MediaWiki 1.18.0 * also update ArchLinux skin to chagnes in MonoBook * Use only css to hide our menu bar when printing --- includes/db/DatabaseSqlite.php | 299 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 267 insertions(+), 32 deletions(-) (limited to 'includes/db/DatabaseSqlite.php') diff --git a/includes/db/DatabaseSqlite.php b/includes/db/DatabaseSqlite.php index 503ebdf6..e298175d 100644 --- a/includes/db/DatabaseSqlite.php +++ b/includes/db/DatabaseSqlite.php @@ -19,9 +19,19 @@ class DatabaseSqlite extends DatabaseBase { var $mDatabaseFile; var $mName; + /** + * @var PDO + */ + protected $mConn; + /** * Constructor. * Parameters $server, $user and $password are not used. + * @param $server string + * @param $user string + * @param $password string + * @param $dbName string + * @param $flags int */ function __construct( $server = false, $user = false, $password = false, $dbName = false, $flags = 0 ) { $this->mName = $dbName; @@ -35,21 +45,31 @@ class DatabaseSqlite extends DatabaseBase { } } + /** + * @return string + */ function getType() { return 'sqlite'; } /** * @todo: check if it should be true like parent class + * + * @return bool */ - function implicitGroupby() { return false; } - - static function newFromParams( $server, $user, $password, $dbName, $flags = 0 ) { - return new DatabaseSqlite( $server, $user, $password, $dbName, $flags ); + function implicitGroupby() { + return false; } /** Open an SQLite database and return a resource handle to it * NOTE: only $dbName is used, the other parameters are irrelevant for SQLite databases + * + * @param $server + * @param $user + * @param $pass + * @param $dbName + * + * @return PDO */ function open( $server, $user, $pass, $dbName ) { global $wgSQLiteDataDir; @@ -65,7 +85,10 @@ class DatabaseSqlite extends DatabaseBase { /** * Opens a database file - * @return SQL connection or false if failed + * + * @param $fileName string + * + * @return PDO|false SQL connection or false if failed */ function openFile( $fileName ) { $this->mDatabaseFile = $fileName; @@ -93,6 +116,8 @@ class DatabaseSqlite extends DatabaseBase { /** * Close an SQLite database + * + * @return bool */ function close() { $this->mOpened = false; @@ -115,7 +140,7 @@ class DatabaseSqlite extends DatabaseBase { /** * Check if the searchindext table is FTS enabled. - * @returns false if not enabled. + * @return false if not enabled. */ function checkForEnabledSearch() { if ( self::$fulltextEnabled === null ) { @@ -154,9 +179,12 @@ class DatabaseSqlite extends DatabaseBase { /** * Attaches external database to our connection, see http://sqlite.org/lang_attach.html * for details. + * * @param $name String: database name to be used in queries like SELECT foo FROM dbname.table * @param $file String: database file name. If omitted, will be generated using $name and $wgSQLiteDataDir * @param $fname String: calling function name + * + * @return ResultWrapper */ function attachDatabase( $name, $file = false, $fname = 'DatabaseSqlite::attachDatabase' ) { global $wgSQLiteDataDir; @@ -169,6 +197,10 @@ class DatabaseSqlite extends DatabaseBase { /** * @see DatabaseBase::isWriteQuery() + * + * @param $sql string + * + * @return bool */ function isWriteQuery( $sql ) { return parent::isWriteQuery( $sql ) && !preg_match( '/^ATTACH\b/i', $sql ); @@ -176,8 +208,12 @@ class DatabaseSqlite extends DatabaseBase { /** * SQLite doesn't allow buffered results or data seeking etc, so we'll use fetchAll as the result + * + * @param $sql string + * + * @return ResultWrapper */ - function doQuery( $sql ) { + protected function doQuery( $sql ) { $res = $this->mConn->query( $sql ); if ( $res === false ) { return false; @@ -189,6 +225,9 @@ class DatabaseSqlite extends DatabaseBase { return $res; } + /** + * @param $res ResultWrapper + */ function freeResult( $res ) { if ( $res instanceof ResultWrapper ) { $res->result = null; @@ -197,6 +236,10 @@ class DatabaseSqlite extends DatabaseBase { } } + /** + * @param $res ResultWrapper + * @return + */ function fetchObject( $res ) { if ( $res instanceof ResultWrapper ) { $r =& $res->result; @@ -219,6 +262,10 @@ class DatabaseSqlite extends DatabaseBase { return false; } + /** + * @param $res ResultWrapper + * @return bool|mixed + */ function fetchRow( $res ) { if ( $res instanceof ResultWrapper ) { $r =& $res->result; @@ -235,37 +282,60 @@ class DatabaseSqlite extends DatabaseBase { /** * The PDO::Statement class implements the array interface so count() will work + * + * @param $res ResultWrapper + * + * @return int */ function numRows( $res ) { $r = $res instanceof ResultWrapper ? $res->result : $res; return count( $r ); } + /** + * @param $res ResultWrapper + * @return int + */ function numFields( $res ) { $r = $res instanceof ResultWrapper ? $res->result : $res; return is_array( $r ) ? count( $r[0] ) : 0; } + /** + * @param $res ResultWrapper + * @param $n + * @return bool + */ function fieldName( $res, $n ) { $r = $res instanceof ResultWrapper ? $res->result : $res; if ( is_array( $r ) ) { $keys = array_keys( $r[0] ); return $keys[$n]; } - return false; + return false; } /** * Use MySQL's naming (accounts for prefix etc) but remove surrounding backticks + * + * @param $name + * @param bool $quoted + * @return string */ - function tableName( $name ) { + function tableName( $name, $quoted = true ) { // table names starting with sqlite_ are reserved - if ( strpos( $name, 'sqlite_' ) === 0 ) return $name; - return str_replace( '`', '', parent::tableName( $name ) ); + if ( strpos( $name, 'sqlite_' ) === 0 ) { + return $name; + } + return str_replace( '"', '', parent::tableName( $name, $quoted ) ); } /** * Index names have DB scope + * + * @param $index string + * + * @return string */ function indexName( $index ) { return $index; @@ -273,11 +343,17 @@ class DatabaseSqlite extends DatabaseBase { /** * This must be called after nextSequenceVal + * + * @return int */ function insertId() { return $this->mConn->lastInsertId(); } + /** + * @param $res ResultWrapper + * @param $row + */ function dataSeek( $res, $row ) { if ( $res instanceof ResultWrapper ) { $r =& $res->result; @@ -292,6 +368,9 @@ class DatabaseSqlite extends DatabaseBase { } } + /** + * @return string + */ function lastError() { if ( !is_object( $this->mConn ) ) { return "Cannot return last error, no db connection"; @@ -300,6 +379,9 @@ class DatabaseSqlite extends DatabaseBase { return isset( $e[2] ) ? $e[2] : ''; } + /** + * @return string + */ function lastErrno() { if ( !is_object( $this->mConn ) ) { return "Cannot return last error, no db connection"; @@ -309,6 +391,9 @@ class DatabaseSqlite extends DatabaseBase { } } + /** + * @return int + */ function affectedRows() { return $this->mAffectedRows; } @@ -317,6 +402,8 @@ class DatabaseSqlite extends DatabaseBase { * Returns information about an index * Returns false if the index does not exist * - if errors are explicitly ignored, returns NULL on failure + * + * @return array */ function indexInfo( $table, $index, $fname = 'DatabaseSqlite::indexExists' ) { $sql = 'PRAGMA index_info(' . $this->addQuotes( $this->indexName( $index ) ) . ')'; @@ -334,6 +421,12 @@ class DatabaseSqlite extends DatabaseBase { return $info; } + /** + * @param $table + * @param $index + * @param $fname string + * @return bool|null + */ function indexUnique( $table, $index, $fname = 'DatabaseSqlite::indexUnique' ) { $row = $this->selectRow( 'sqlite_master', '*', array( @@ -356,6 +449,10 @@ class DatabaseSqlite extends DatabaseBase { /** * Filter the options used in SELECT statements + * + * @param $options array + * + * @return array */ function makeSelectOptions( $options ) { foreach ( $options as $k => $v ) { @@ -367,22 +464,44 @@ class DatabaseSqlite extends DatabaseBase { } /** - * Based on generic method (parent) with some prior SQLite-sepcific adjustments + * @param $options array + * @return string */ - function insert( $table, $a, $fname = 'DatabaseSqlite::insert', $options = array() ) { - if ( !count( $a ) ) { - return true; - } - if ( !is_array( $options ) ) { - $options = array( $options ); - } + function makeUpdateOptions( $options ) { + $options = self::fixIgnore( $options ); + return parent::makeUpdateOptions( $options ); + } + /** + * @param $options array + * @return array + */ + static function fixIgnore( $options ) { # SQLite uses OR IGNORE not just IGNORE foreach ( $options as $k => $v ) { if ( $v == 'IGNORE' ) { $options[$k] = 'OR IGNORE'; } } + return $options; + } + + /** + * @param $options array + * @return string + */ + function makeInsertOptions( $options ) { + $options = self::fixIgnore( $options ); + return parent::makeInsertOptions( $options ); + } + + /** + * Based on generic method (parent) with some prior SQLite-sepcific adjustments + */ + function insert( $table, $a, $fname = 'DatabaseSqlite::insert', $options = array() ) { + if ( !count( $a ) ) { + return true; + } # SQLite can't handle multi-row inserts, so divide up into multiple single-row inserts if ( isset( $a[0] ) && is_array( $a[0] ) ) { @@ -399,6 +518,13 @@ class DatabaseSqlite extends DatabaseBase { return $ret; } + /** + * @param $table + * @param $uniqueIndexes + * @param $rows + * @param $fname string + * @return bool|ResultWrapper + */ function replace( $table, $uniqueIndexes, $rows, $fname = 'DatabaseSqlite::replace' ) { if ( !count( $rows ) ) return true; @@ -406,12 +532,12 @@ class DatabaseSqlite extends DatabaseBase { if ( isset( $rows[0] ) && is_array( $rows[0] ) ) { $ret = true; foreach ( $rows as $v ) { - if ( !parent::replace( $table, $uniqueIndexes, $v, "$fname/multi-row" ) ) { + if ( !$this->nativeReplace( $table, $v, "$fname/multi-row" ) ) { $ret = false; } } } else { - $ret = parent::replace( $table, $uniqueIndexes, $rows, "$fname/single-row" ); + $ret = $this->nativeReplace( $table, $rows, "$fname/single-row" ); } return $ret; @@ -420,32 +546,47 @@ class DatabaseSqlite extends DatabaseBase { /** * Returns the size of a text field, or -1 for "unlimited" * In SQLite this is SQLITE_MAX_LENGTH, by default 1GB. No way to query it though. + * + * @return int */ function textFieldSize( $table, $field ) { - return - 1; + return -1; } + /** + * @return bool + */ function unionSupportsOrderAndLimit() { return false; } + /** + * @param $sqls + * @param $all + * @return string + */ function unionQueries( $sqls, $all ) { $glue = $all ? ' UNION ALL ' : ' UNION '; return implode( $glue, $sqls ); } - public function unixTimestamp( $field ) { - return $field; - } - + /** + * @return bool + */ function wasDeadlock() { return $this->lastErrno() == 5; // SQLITE_BUSY } + /** + * @return bool + */ function wasErrorReissuable() { return $this->lastErrno() == 17; // SQLITE_SCHEMA; } + /** + * @return bool + */ function wasReadOnlyError() { return $this->lastErrno() == 8; // SQLITE_READONLY; } @@ -475,6 +616,8 @@ class DatabaseSqlite extends DatabaseBase { /** * Get information about a given field * Returns false if the field does not exist. + * + * @return SQLiteField|false */ function fieldInfo( $table, $field ) { $tableName = $this->tableName( $table ); @@ -489,35 +632,58 @@ class DatabaseSqlite extends DatabaseBase { } function begin( $fname = '' ) { - if ( $this->mTrxLevel == 1 ) $this->commit(); + if ( $this->mTrxLevel == 1 ) { + $this->commit(); + } $this->mConn->beginTransaction(); $this->mTrxLevel = 1; } function commit( $fname = '' ) { - if ( $this->mTrxLevel == 0 ) return; + if ( $this->mTrxLevel == 0 ) { + return; + } $this->mConn->commit(); $this->mTrxLevel = 0; } function rollback( $fname = '' ) { - if ( $this->mTrxLevel == 0 ) return; + if ( $this->mTrxLevel == 0 ) { + return; + } $this->mConn->rollBack(); $this->mTrxLevel = 0; } + /** + * @param $sql + * @param $num + * @return string + */ function limitResultForUpdate( $sql, $num ) { return $this->limitResult( $sql, $num ); } + /** + * @param $s string + * @return string + */ function strencode( $s ) { return substr( $this->addQuotes( $s ), 1, - 1 ); } + /** + * @param $b + * @return Blob + */ function encodeBlob( $b ) { return new Blob( $b ); } + /** + * @param $b Blob|string + * @return string + */ function decodeBlob( $b ) { if ( $b instanceof Blob ) { $b = $b->fetch(); @@ -525,6 +691,10 @@ class DatabaseSqlite extends DatabaseBase { return $b; } + /** + * @param $s Blob|string + * @return string + */ function addQuotes( $s ) { if ( $s instanceof Blob ) { return "x'" . bin2hex( $s->fetch() ) . "'"; @@ -533,6 +703,9 @@ class DatabaseSqlite extends DatabaseBase { } } + /** + * @return string + */ function buildLike() { $params = func_get_args(); if ( count( $params ) > 0 && is_array( $params[0] ) ) { @@ -541,6 +714,9 @@ class DatabaseSqlite extends DatabaseBase { return parent::buildLike( $params ) . "ESCAPE '\' "; } + /** + * @return string + */ public function getSearchEngine() { return "SearchSqlite"; } @@ -554,6 +730,10 @@ class DatabaseSqlite extends DatabaseBase { return call_user_func_array( $function, $args ); } + /** + * @param $s string + * @return string + */ protected function replaceVars( $s ) { $s = parent::replaceVars( $s ); if ( preg_match( '/^\s*(CREATE|ALTER) TABLE/i', $s ) ) { @@ -596,23 +776,75 @@ class DatabaseSqlite extends DatabaseBase { return $s; } - /* + /** * Build a concatenation list to feed into a SQL query + * + * @param $stringList array + * + * @return string */ function buildConcat( $stringList ) { return '(' . implode( ') || (', $stringList ) . ')'; } + /** + * @throws MWException + * @param $oldName + * @param $newName + * @param $temporary bool + * @param $fname string + * @return bool|ResultWrapper + */ function duplicateTableStructure( $oldName, $newName, $temporary = false, $fname = 'DatabaseSqlite::duplicateTableStructure' ) { - $res = $this->query( "SELECT sql FROM sqlite_master WHERE tbl_name='$oldName' AND type='table'", $fname ); + $res = $this->query( "SELECT sql FROM sqlite_master WHERE tbl_name=" . $this->addQuotes( $oldName ) . " AND type='table'", $fname ); $obj = $this->fetchObject( $res ); if ( !$obj ) { throw new MWException( "Couldn't retrieve structure for table $oldName" ); } $sql = $obj->sql; - $sql = preg_replace( '/\b' . preg_quote( $oldName ) . '\b/', $newName, $sql, 1 ); + $sql = preg_replace( '/(?<=\W)"?' . preg_quote( trim( $this->addIdentifierQuotes( $oldName ), '"' ) ) . '"?(?=\W)/', $this->addIdentifierQuotes( $newName ), $sql, 1 ); + if ( $temporary ) { + if ( preg_match( '/^\\s*CREATE\\s+VIRTUAL\\s+TABLE\b/i', $sql ) ) { + wfDebug( "Table $oldName is virtual, can't create a temporary duplicate.\n" ); + } else { + $sql = str_replace( 'CREATE TABLE', 'CREATE TEMPORARY TABLE', $sql ); + } + } return $this->query( $sql, $fname ); } + + + /** + * List all tables on the database + * + * @param $prefix Only show tables with this prefix, e.g. mw_ + * @param $fname String: calling function name + * + * @return array + */ + function listTables( $prefix = null, $fname = 'DatabaseSqlite::listTables' ) { + $result = $this->select( + 'sqlite_master', + 'name', + "type='table'" + ); + + $endArray = array(); + + foreach( $result as $table ) { + $vars = get_object_vars($table); + $table = array_pop( $vars ); + + if( !$prefix || strpos( $table, $prefix ) === 0 ) { + if ( strpos( $table, 'sqlite_' ) !== 0 ) { + $endArray[] = $table; + } + + } + } + + return $endArray; + } } // end DatabaseSqlite class @@ -656,6 +888,9 @@ class SQLiteField implements Field { return $this->info->dflt_value; } + /** + * @return bool + */ function isNullable() { return !$this->info->notnull; } -- cgit v1.2.3-54-g00ecf