r56392 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r56391‎ | r56392 | r56393 >
Date:21:05, 15 September 2009
Author:ialex
Status:ok
Tags:
Comment:
whitespaces tweaks
Modified paths:
  • /trunk/phase3/includes/db/DatabaseSqlite.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/db/DatabaseSqlite.php
@@ -20,7 +20,7 @@
2121 /**
2222 * Constructor
2323 */
24 - function __construct($server = false, $user = false, $password = false, $dbName = false, $failFunction = false, $flags = 0) {
 24+ function __construct( $server = false, $user = false, $password = false, $dbName = false, $failFunction = false, $flags = 0 ) {
2525 global $wgSQLiteDataDir;
2626 $this->mFailFunction = $failFunction;
2727 $this->mFlags = $flags;
@@ -28,7 +28,7 @@
2929 if( !is_readable( $this->mDatabaseFile ) )
3030 throw new DBConnectionError( $this, "SQLite database not accessible" );
3131 $this->mName = $dbName;
32 - $this->open($server, $user, $password, $dbName);
 32+ $this->open( $server, $user, $password, $dbName );
3333 }
3434
3535 /**
@@ -37,20 +37,20 @@
3838 function implicitGroupby() { return false; }
3939 function implicitOrderby() { return false; }
4040
41 - static function newFromParams($server, $user, $password, $dbName, $failFunction = false, $flags = 0) {
42 - return new DatabaseSqlite($server, $user, $password, $dbName, $failFunction, $flags);
 41+ static function newFromParams( $server, $user, $password, $dbName, $failFunction = false, $flags = 0 ) {
 42+ return new DatabaseSqlite( $server, $user, $password, $dbName, $failFunction, $flags );
4343 }
4444
4545 /** Open an SQLite database and return a resource handle to it
4646 * NOTE: only $dbName is used, the other parameters are irrelevant for SQLite databases
4747 */
48 - function open($server,$user,$pass,$dbName) {
 48+ function open( $server, $user, $pass, $dbName ) {
4949 $this->mConn = false;
50 - if ($dbName) {
 50+ if ( $dbName ) {
5151 $file = $this->mDatabaseFile;
5252 try {
5353 if ( $this->mFlags & DBO_PERSISTENT ) {
54 - $this->mConn = new PDO( "sqlite:$file", $user, $pass,
 54+ $this->mConn = new PDO( "sqlite:$file", $user, $pass,
5555 array( PDO::ATTR_PERSISTENT => true ) );
5656 } else {
5757 $this->mConn = new PDO( "sqlite:$file", $user, $pass );
@@ -69,7 +69,7 @@
7070 }
7171 $this->mOpened = $this->mConn;
7272 # set error codes only, don't raise exceptions
73 - $this->mConn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT );
 73+ $this->mConn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT );
7474 }
7575 return $this->mConn;
7676 }
@@ -79,8 +79,8 @@
8080 */
8181 function close() {
8282 $this->mOpened = false;
83 - if (is_object($this->mConn)) {
84 - if ($this->trxLevel()) $this->immediateCommit();
 83+ if ( is_object( $this->mConn ) ) {
 84+ if ( $this->trxLevel() ) $this->immediateCommit();
8585 $this->mConn = null;
8686 }
8787 return true;
@@ -89,36 +89,50 @@
9090 /**
9191 * SQLite doesn't allow buffered results or data seeking etc, so we'll use fetchAll as the result
9292 */
93 - function doQuery($sql) {
94 - $res = $this->mConn->query($sql);
95 - if ($res === false) {
 93+ function doQuery( $sql ) {
 94+ $res = $this->mConn->query( $sql );
 95+ if ( $res === false ) {
9696 return false;
9797 } else {
9898 $r = $res instanceof ResultWrapper ? $res->result : $res;
9999 $this->mAffectedRows = $r->rowCount();
100 - $res = new ResultWrapper($this,$r->fetchAll());
 100+ $res = new ResultWrapper( $this, $r->fetchAll() );
101101 }
102102 return $res;
103103 }
104104
105 - function freeResult($res) {
106 - if ($res instanceof ResultWrapper) $res->result = NULL; else $res = NULL;
 105+ function freeResult( $res ) {
 106+ if ( $res instanceof ResultWrapper )
 107+ $res->result = NULL;
 108+ else
 109+ $res = NULL;
107110 }
108111
109112 function fetchObject($res) {
110 - if ($res instanceof ResultWrapper) $r =& $res->result; else $r =& $res;
111 - $cur = current($r);
112 - if (is_array($cur)) {
113 - next($r);
 113+ if ($res instanceof ResultWrapper)
 114+ $r =& $res->result;
 115+ else
 116+ $r =& $res;
 117+
 118+ $cur = current( $r );
 119+ if ( is_array( $cur ) ) {
 120+ next( $r );
114121 $obj = new stdClass;
115 - foreach ($cur as $k => $v) if (!is_numeric($k)) $obj->$k = $v;
 122+ foreach ( $cur as $k => $v )
 123+ if ( !is_numeric( $k ) )
 124+ $obj->$k = $v;
 125+
116126 return $obj;
117127 }
118128 return false;
119129 }
120130
121131 function fetchRow($res) {
122 - if ($res instanceof ResultWrapper) $r =& $res->result; else $r =& $res;
 132+ if ( $res instanceof ResultWrapper )
 133+ $r =& $res->result;
 134+ else
 135+ $r =& $res;
 136+
123137 $cur = current($r);
124138 if (is_array($cur)) {
125139 next($r);
@@ -130,20 +144,20 @@
131145 /**
132146 * The PDO::Statement class implements the array interface so count() will work
133147 */
134 - function numRows($res) {
 148+ function numRows( $res ) {
135149 $r = $res instanceof ResultWrapper ? $res->result : $res;
136 - return count($r);
 150+ return count( $r );
137151 }
138152
139 - function numFields($res) {
 153+ function numFields( $res ) {
140154 $r = $res instanceof ResultWrapper ? $res->result : $res;
141 - return is_array($r) ? count($r[0]) : 0;
 155+ return is_array( $r ) ? count( $r[0] ) : 0;
142156 }
143157
144 - function fieldName($res,$n) {
 158+ function fieldName( $res, $n ) {
145159 $r = $res instanceof ResultWrapper ? $res->result : $res;
146 - if (is_array($r)) {
147 - $keys = array_keys($r[0]);
 160+ if ( is_array( $r ) ) {
 161+ $keys = array_keys( $r[0] );
148162 return $keys[$n];
149163 }
150164 return false;
@@ -152,8 +166,8 @@
153167 /**
154168 * Use MySQL's naming (accounts for prefix etc) but remove surrounding backticks
155169 */
156 - function tableName($name) {
157 - return str_replace('`','',parent::tableName($name));
 170+ function tableName( $name ) {
 171+ return str_replace( '`', '', parent::tableName( $name ) );
158172 }
159173
160174 /**
@@ -170,20 +184,26 @@
171185 return $this->mConn->lastInsertId();
172186 }
173187
174 - function dataSeek($res,$row) {
175 - if ($res instanceof ResultWrapper) $r =& $res->result; else $r =& $res;
176 - reset($r);
177 - if ($row > 0) for ($i = 0; $i < $row; $i++) next($r);
 188+ function dataSeek( $res, $row ) {
 189+ if ( $res instanceof ResultWrapper )
 190+ $r =& $res->result;
 191+ else
 192+ $r =& $res;
 193+ reset( $r );
 194+ if ( $row > 0 )
 195+ for ( $i = 0; $i < $row; $i++ )
 196+ next( $r );
178197 }
179198
180199 function lastError() {
181 - if (!is_object($this->mConn)) return "Cannot return last error, no db connection";
 200+ if ( !is_object( $this->mConn ) )
 201+ return "Cannot return last error, no db connection";
182202 $e = $this->mConn->errorInfo();
183 - return isset($e[2]) ? $e[2] : '';
 203+ return isset( $e[2] ) ? $e[2] : '';
184204 }
185205
186206 function lastErrno() {
187 - if (!is_object($this->mConn)) {
 207+ if ( !is_object( $this->mConn ) ) {
188208 return "Cannot return last error, no db connection";
189209 } else {
190210 $info = $this->mConn->errorInfo();
@@ -200,7 +220,7 @@
201221 * Returns false if the index does not exist
202222 * - if errors are explicitly ignored, returns NULL on failure
203223 */
204 - function indexInfo($table, $index, $fname = 'Database::indexExists') {
 224+ function indexInfo( $table, $index, $fname = 'DatabaseSqlite::indexExists' ) {
205225 $sql = 'PRAGMA index_info(' . $this->addQuotes( $this->indexName( $index ) ) . ')';
206226 $res = $this->query( $sql, $fname );
207227 if ( !$res ) {
@@ -216,8 +236,8 @@
217237 return $info;
218238 }
219239
220 - function indexUnique($table, $index, $fname = 'Database::indexUnique') {
221 - $row = $this->selectRow( 'sqlite_master', '*',
 240+ function indexUnique( $table, $index, $fname = 'DatabaseSqlite::indexUnique' ) {
 241+ $row = $this->selectRow( 'sqlite_master', '*',
222242 array(
223243 'type' => 'index',
224244 'name' => $this->indexName( $index ),
@@ -239,27 +259,34 @@
240260 /**
241261 * Filter the options used in SELECT statements
242262 */
243 - function makeSelectOptions($options) {
244 - foreach ($options as $k => $v) if (is_numeric($k) && $v == 'FOR UPDATE') $options[$k] = '';
245 - return parent::makeSelectOptions($options);
 263+ function makeSelectOptions( $options ) {
 264+ foreach ( $options as $k => $v )
 265+ if ( is_numeric( $k ) && $v == 'FOR UPDATE' )
 266+ $options[$k] = '';
 267+ return parent::makeSelectOptions( $options );
246268 }
247269
248270 /**
249271 * Based on MySQL method (parent) with some prior SQLite-sepcific adjustments
250272 */
251 - function insert($table, $a, $fname = 'DatabaseSqlite::insert', $options = array()) {
252 - if (!count($a)) return true;
253 - if (!is_array($options)) $options = array($options);
 273+ function insert( $table, $a, $fname = 'DatabaseSqlite::insert', $options = array() ) {
 274+ if ( !count( $a ) ) return true;
 275+ if ( !is_array( $options ) ) $options = array( $options );
254276
255277 # SQLite uses OR IGNORE not just IGNORE
256 - foreach ($options as $k => $v) if ($v == 'IGNORE') $options[$k] = 'OR IGNORE';
 278+ foreach ( $options as $k => $v )
 279+ if ( $v == 'IGNORE' )
 280+ $options[$k] = 'OR IGNORE';
257281
258282 # SQLite can't handle multi-row inserts, so divide up into multiple single-row inserts
259 - if (isset($a[0]) && is_array($a[0])) {
 283+ if ( isset( $a[0] ) && is_array( $a[0] ) ) {
260284 $ret = true;
261 - foreach ($a as $k => $v) if (!parent::insert($table,$v,"$fname/multi-row",$options)) $ret = false;
 285+ foreach ( $a as $k => $v )
 286+ if ( !parent::insert( $table, $v, "$fname/multi-row", $options ) )
 287+ $ret = false;
 288+ } else {
 289+ $ret = parent::insert( $table, $a, "$fname/single-row", $options );
262290 }
263 - else $ret = parent::insert($table,$a,"$fname/single-row",$options);
264291
265292 return $ret;
266293 }
@@ -268,8 +295,8 @@
269296 * Returns the size of a text field, or -1 for "unlimited"
270297 * In SQLite this is SQLITE_MAX_LENGTH, by default 1GB. No way to query it though.
271298 */
272 - function textFieldSize($table, $field) {
273 - return -1;
 299+ function textFieldSize( $table, $field ) {
 300+ return - 1;
274301 }
275302
276303 function wasDeadlock() {
@@ -295,15 +322,14 @@
296323 * @return string Version information from the database
297324 */
298325 function getServerVersion() {
299 - global $wgContLang;
300 - $ver = $this->mConn->getAttribute(PDO::ATTR_SERVER_VERSION);
 326+ $ver = $this->mConn->getAttribute( PDO::ATTR_SERVER_VERSION );
301327 return $ver;
302328 }
303329
304330 /**
305331 * Query whether a given column exists in the mediawiki schema
306332 */
307 - function fieldExists($table, $field, $fname = '') {
 333+ function fieldExists( $table, $field, $fname = '' ) {
308334 $info = $this->fieldInfo( $table, $field );
309335 return (bool)$info;
310336 }
@@ -312,7 +338,7 @@
313339 * Get information about a given field
314340 * Returns false if the field does not exist.
315341 */
316 - function fieldInfo($table, $field) {
 342+ function fieldInfo( $table, $field ) {
317343 $tableName = $this->tableName( $table );
318344 $sql = 'PRAGMA table_info(' . $this->addQuotes( $tableName ) . ')';
319345 $res = $this->query( $sql, __METHOD__ );
@@ -325,51 +351,53 @@
326352 }
327353
328354 function begin( $fname = '' ) {
329 - if ($this->mTrxLevel == 1) $this->commit();
 355+ if ( $this->mTrxLevel == 1 ) $this->commit();
330356 $this->mConn->beginTransaction();
331357 $this->mTrxLevel = 1;
332358 }
333359
334360 function commit( $fname = '' ) {
335 - if ($this->mTrxLevel == 0) return;
 361+ if ( $this->mTrxLevel == 0 ) return;
336362 $this->mConn->commit();
337363 $this->mTrxLevel = 0;
338364 }
339365
340366 function rollback( $fname = '' ) {
341 - if ($this->mTrxLevel == 0) return;
 367+ if ( $this->mTrxLevel == 0 ) return;
342368 $this->mConn->rollBack();
343369 $this->mTrxLevel = 0;
344370 }
345371
346 - function limitResultForUpdate($sql, $num) {
 372+ function limitResultForUpdate( $sql, $num ) {
347373 return $this->limitResult( $sql, $num );
348374 }
349375
350 - function strencode($s) {
351 - return substr($this->addQuotes($s),1,-1);
 376+ function strencode( $s ) {
 377+ return substr( $this->addQuotes( $s ), 1, - 1 );
352378 }
353379
354 - function encodeBlob($b) {
 380+ function encodeBlob( $b ) {
355381 return new Blob( $b );
356382 }
357383
358 - function decodeBlob($b) {
359 - if ($b instanceof Blob) {
 384+ function decodeBlob( $b ) {
 385+ if ( $b instanceof Blob ) {
360386 $b = $b->fetch();
361387 }
362388 return $b;
363389 }
364390
365 - function addQuotes($s) {
 391+ function addQuotes( $s ) {
366392 if ( $s instanceof Blob ) {
367393 return "x'" . bin2hex( $s->fetch() ) . "'";
368394 } else {
369 - return $this->mConn->quote($s);
 395+ return $this->mConn->quote( $s );
370396 }
371397 }
372398
373 - function quote_ident($s) { return $s; }
 399+ function quote_ident( $s ) {
 400+ return $s;
 401+ }
374402
375403 /**
376404 * How lagged is this slave?
@@ -383,25 +411,25 @@
384412 * - this is the same way PostgreSQL works, MySQL reads in tables.sql and interwiki.sql using dbsource (which calls db->sourceFile)
385413 */
386414 public function setup_database() {
387 - global $IP,$wgSQLiteDataDir,$wgDBTableOptions;
 415+ global $IP, $wgSQLiteDataDir, $wgDBTableOptions;
388416 $wgDBTableOptions = '';
389417
390418 # Process common MySQL/SQLite table definitions
391419 $err = $this->sourceFile( "$IP/maintenance/tables.sql" );
392 - if ($err !== true) {
393 - $this->reportQueryError($err,0,$sql,__FUNCTION__);
 420+ if ( $err !== true ) {
 421+ $this->reportQueryError( $err, 0, $sql, __FUNCTION__ );
394422 exit( 1 );
395423 }
396424
397425 # Use DatabasePostgres's code to populate interwiki from MySQL template
398 - $f = fopen("$IP/maintenance/interwiki.sql",'r');
399 - if ($f == false) dieout("<li>Could not find the interwiki.sql file");
 426+ $f = fopen( "$IP/maintenance/interwiki.sql", 'r' );
 427+ if ( $f == false ) dieout( "<li>Could not find the interwiki.sql file" );
400428 $sql = "INSERT INTO interwiki(iw_prefix,iw_url,iw_local) VALUES ";
401 - while (!feof($f)) {
402 - $line = fgets($f,1024);
 429+ while ( !feof( $f ) ) {
 430+ $line = fgets( $f, 1024 );
403431 $matches = array();
404 - if (!preg_match('/^\s*(\(.+?),(\d)\)/', $line, $matches)) continue;
405 - $this->query("$sql $matches[1],$matches[2])");
 432+ if ( !preg_match( '/^\s*(\(.+?),(\d)\)/', $line, $matches ) ) continue;
 433+ $this->query( "$sql $matches[1],$matches[2])" );
406434 }
407435 }
408436
@@ -422,7 +450,7 @@
423451 $s = parent::replaceVars( $s );
424452 if ( preg_match( '/^\s*CREATE TABLE/i', $s ) ) {
425453 // CREATE TABLE hacks to allow schema file sharing with MySQL
426 -
 454+
427455 // binary/varbinary column type -> blob
428456 $s = preg_replace( '/\b(var)?binary(\(\d+\))/i', 'blob\1', $s );
429457 // no such thing as unsigned
@@ -494,10 +522,9 @@
495523
496524 # isKey(), isMultipleKey() not implemented, MySQL-specific concept.
497525 # Suggest removal from base class [TS]
498 -
 526+
499527 function type() {
500528 return $this->info->type;
501529 }
502530
503531 } // end SQLiteField
504 -

Status & tagging log