Docs and code convention cleanups. see #11644 props hakre

git-svn-id: http://svn.automattic.com/wordpress/trunk@13314 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
nacin 2010-02-22 22:53:54 +00:00
parent e692640c11
commit 9a11f30898

View File

@ -65,7 +65,7 @@ class wpdb {
* Whether to suppress errors during the DB bootstrapping.
*
* @access private
* @since {@internal Version Unknown}}
* @since 2.5
* @var bool
*/
var $suppress_errors = false;
@ -73,7 +73,9 @@ class wpdb {
/**
* The last error during query.
*
* @since {@internal Version Unknown}}
* @see get_last_error()
* @since 2.5
* @access private
* @var string
*/
var $last_error = '';
@ -87,6 +89,22 @@ class wpdb {
*/
var $num_queries = 0;
/**
* Count of rows returned by previous query
*
* @since 1.2
* @var int
*/
var $num_rows = 0;
/**
* Count of affected rows by previous query
*
* @since 0.71
* @var int
*/
var $rows_affected = 0;
/**
* Saved result of the last query made
*
@ -96,6 +114,14 @@ class wpdb {
*/
var $last_query;
/**
* Results of the last query made
*
* @since {@internal Version Unknown}}
* @var mixed
*/
var $last_result;
/**
* Saved info on the table column
*
@ -135,7 +161,21 @@ class wpdb {
* @var bool
*/
var $ready = false;
/**
* {@internal Missing Description}}
*
* @since 3.0.0
* @var int
*/
var $blogid = 0;
/**
* {@internal Missing Description}}
*
* @since 3.0.0
* @var int
*/
var $siteid = 0;
/**
@ -251,6 +291,7 @@ class wpdb {
/**
* List of deprecated WordPress tables
*
* @deprecated
* @since 2.9.0
* @access private
* @see wpdb::tables()
@ -343,9 +384,9 @@ class wpdb {
var $global_tables = array( 'users', 'usermeta' );
/**
* Format specifiers for DB columns. Columns not listed here default to %s. Initialized in wp-settings.php.
* Format specifiers for DB columns. Columns not listed here default to %s. Initialized during WP load.
*
* Keys are colmn names, values are format types: 'ID' => '%d'
* Keys are column names, values are format types: 'ID' => '%d'
*
* @since 2.8.0
* @see wpdb:prepare()
@ -427,7 +468,7 @@ class wpdb {
* @param string $dbhost MySQL database host
*/
function __construct( $dbuser, $dbpassword, $dbname, $dbhost ) {
register_shutdown_function(array(&$this, "__destruct"));
register_shutdown_function( array( &$this, '__destruct' ) );
if ( WP_DEBUG )
$this->show_errors();
@ -469,10 +510,10 @@ class wpdb {
mysql_set_charset( $this->charset, $this->dbh );
$this->real_escape = true;
} else {
$collation_query = "SET NAMES '{$this->charset}'";
$query = $this->prepare( 'SET NAMES %s', $this->charset );
if ( ! empty( $this->collate ) )
$collation_query .= " COLLATE '{$this->collate}'";
$this->query($collation_query);
$query .= $this->prepare( ' COLLATE %s', $this->collate );
$this->query( $query );
}
}
@ -483,8 +524,7 @@ class wpdb {
* PHP5 style destructor and will run when database object is destroyed.
*
* @since 2.0.8
*
* @return bool Always true
* @return bool true
*/
function __destruct() {
return true;
@ -506,10 +546,7 @@ class wpdb {
if ( preg_match( '|[^a-z0-9_]|i', $prefix ) )
return new WP_Error('invalid_db_prefix', /*WP_I18N_DB_BAD_PREFIX*/'Invalid database prefix'/*/WP_I18N_DB_BAD_PREFIX*/);
if ( is_multisite() )
$old_prefix = '';
else
$old_prefix = $prefix;
$old_prefix = is_multisite() ? '' : $prefix;
if ( isset( $this->base_prefix ) )
$old_prefix = $this->base_prefix;
@ -537,6 +574,15 @@ class wpdb {
return $old_prefix;
}
/**
* Sets blog id.
*
* @since 3.0.0
* @access public
* @param string $blog_id
* @param string $site_id. Optional.
* @return string previous blog id
*/
function set_blog_id( $blog_id, $site_id = '' ) {
if ( ! empty( $site_id ) )
$this->siteid = $site_id;
@ -555,7 +601,15 @@ class wpdb {
return $old_blog_id;
}
function get_blog_prefix( $blog_id = '' ) {
/**
* Gets blog prefix.
*
* @uses is_multisite()
* @since 3.0.0
* @param int $blog_id. Optional.
* @return string Blog prefix.
*/
function get_blog_prefix( $blog_id = 0 ) {
if ( is_multisite() && $blog_id ) {
if ( defined('MULTISITE') && ( $blog_id == 0 || $blog_id == 1 ) )
return $this->base_prefix;
@ -636,10 +690,31 @@ class wpdb {
}
}
/**
* Weak escape
*
* @see addslashes()
* @since unknown
* @access private
*
* @param string $string
* @return string
*/
function _weak_escape( $string ) {
return addslashes( $string );
}
/**
* Real escape
*
* @see mysql_real_escape_string()
* @see addslashes()
* @since 2.8
* @access private
*
* @param string $string to escape
* @return string escaped
*/
function _real_escape( $string ) {
if ( $this->dbh && $this->real_escape )
return mysql_real_escape_string( $string, $this->dbh );
@ -647,6 +722,16 @@ class wpdb {
return addslashes( $string );
}
/**
* Escape data.
*
* @see esc_sql()
* @since 2.8
* @access private
*
* @param string|array $data
* @return string|array escaped
*/
function _escape( $data ) {
if ( is_array( $data ) ) {
foreach ( (array) $data as $k => $v ) {
@ -667,8 +752,8 @@ class wpdb {
*
* @since 0.71
*
* @param string|array $data
* @return string query safe string
* @param string|array $data to escape
* @return string|array escaped as query safe string
*/
function escape( $data ) {
if ( is_array( $data ) ) {
@ -690,7 +775,8 @@ class wpdb {
*
* @since 2.3.0
*
* @param string $s
* @param string $string to escape
* @return void
*/
function escape_by_ref( &$string ) {
$string = $this->_real_escape( $string );
@ -699,6 +785,15 @@ class wpdb {
/**
* Prepares a SQL query for safe execution. Uses sprintf()-like syntax.
*
* The following directives can be used in the query format string:
* %d (decimal number)
* %s (string)
* %% (literal percentage sign - no argument needed)
*
* Both %d and %s are to be left unquoted in the query string and
* they need an argument passed for them. Literals (%) as parts of
* the query must be properly written as %%.
*
* This function only supports a small subset of the sprintf syntax; it only supports %d (decimal number), %s (string).
* Does not support sign, padding, alignment, width or precision specifiers.
* Does not support argument numbering/swapping.
@ -715,9 +810,13 @@ class wpdb {
* @since 2.3.0
*
* @param string $query Query statement with sprintf()-like placeholders
* @param array|mixed $args The array of variables to substitute into the query's placeholders if being called like {@link http://php.net/vsprintf vsprintf()}, or the first variable to substitute into the query's placeholders if being called like {@link http://php.net/sprintf sprintf()}.
* @param mixed $args,... further variables to substitute into the query's placeholders if being called like {@link http://php.net/sprintf sprintf()}.
* @return null|string Sanitized query string
* @param array|mixed $args The array of variables to substitute into the query's placeholders if being called like
* {@link http://php.net/vsprintf vsprintf()}, or the first variable to substitute into the query's placeholders if
* being called like {@link http://php.net/sprintf sprintf()}.
* @param mixed $args,... further variables to substitute into the query's placeholders if being called like
* {@link http://php.net/sprintf sprintf()}.
* @return null|false|string Sanitized query string, null if there is no query, false if there is an error and string
* if there was something to prepare
*/
function prepare( $query = null ) { // ( $query, *$args )
if ( is_null( $query ) )
@ -746,7 +845,8 @@ class wpdb {
function print_error( $str = '' ) {
global $EZSQL_ERROR;
if (!$str) $str = mysql_error($this->dbh);
if ( !$str )
$str = mysql_error( $this->dbh );
$EZSQL_ERROR[] = array( 'query' => $this->last_query, 'error_str' => $str );
if ( $this->suppress_errors )
@ -799,6 +899,7 @@ class wpdb {
* errors.
*
* @since 0.71
* @see wpdb::hide_errors()
*
* @param bool $show Whether to show or hide errors
* @return bool Old value for showing errors.
@ -812,7 +913,10 @@ class wpdb {
/**
* Disables showing of database errors.
*
* By default database errors are not shown.
*
* @since 0.71
* @see wpdb::show_errors()
*
* @return bool Whether showing of errors was active or not
*/
@ -825,12 +929,17 @@ class wpdb {
/**
* Whether to suppress database errors.
*
* @param unknown_type $suppress
* @return unknown
* By default database errors are suppressed, with a simple
* call to this function they can be enabled.
*
* @since 2.5
* @see wpdb::hide_errors()
* @param bool $suppress Optional. New value. Defaults to true.
* @return bool Old value
*/
function suppress_errors( $suppress = true ) {
$errors = $this->suppress_errors;
$this->suppress_errors = $suppress;
$this->suppress_errors = (bool) $suppress;
return $errors;
}
@ -838,6 +947,7 @@ class wpdb {
* Kill cached query results.
*
* @since 0.71
* @return void
*/
function flush() {
$this->last_result = array();
@ -847,7 +957,7 @@ class wpdb {
function db_connect( $query = "SELECT" ) {
global $db_list, $global_db_list;
if ( is_array( $db_list ) == false )
if ( ! is_array( $db_list ) )
return true;
if ( $this->blogs != '' && preg_match("/(" . $this->blogs . "|" . $this->users . "|" . $this->usermeta . "|" . $this->site . "|" . $this->sitemeta . "|" . $this->sitecategories . ")/i",$query) ) {
@ -867,7 +977,7 @@ class wpdb {
$dbhname = "dbh" . $action;
$this->$dbhname = @mysql_connect( $details[ 'db_host' ], $details[ 'db_user' ], $details[ 'db_password' ] );
if (!$this->$dbhname ) {
$this->bail("
$this->bail(/*WP_I18N_DB_CONNECT_DB*/"
<h1>Error establishing a database connection</h1>
<p>This either means that the username and password information in your <code>wp-config.php</code> file is incorrect or we can't contact the database server at <code>{$details['db_host']}</code>. This could mean your host's database server is down.</p>
<ul>
@ -876,7 +986,7 @@ class wpdb {
<li>Are you sure that the database server is running?</li>
</ul>
<p>If you're unsure what these terms mean you should probably contact your host. If you still need help you can always visit the <a href='http://wordpress.org/support/'>WordPress Support Forums</a>.</p>
");
"/*/WP_I18N_DB_CONNECT_DB*/);
}
$this->select( $details[ 'db_name' ], $this->$dbhname );
}
@ -888,7 +998,7 @@ class wpdb {
*
* @since 0.71
*
* @param string $query
* @param string $query Database query
* @return int|false Number of rows affected/selected or false on error
*/
function query( $query ) {
@ -900,7 +1010,7 @@ class wpdb {
if ( function_exists( 'apply_filters' ) )
$query = apply_filters( 'query', $query );
// initialise return
// initialize return
$return_val = 0;
$this->flush();
@ -910,7 +1020,6 @@ class wpdb {
// Keep track of the last query for debug..
$this->last_query = $query;
// Perform the query via std mysql_query function..
if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES )
$this->timer_start();
@ -974,10 +1083,9 @@ class wpdb {
@mysql_free_result( $this->result );
// Log number of rows the query returned
// and return number of rows selected
$this->num_rows = $num_rows;
// Return number of rows selected
$return_val = $this->num_rows;
$return_val = $num_rows;
}
return $return_val;
@ -1073,10 +1181,10 @@ class wpdb {
*
* @since 0.71
*
* @param string|null $query SQL query. If null, use the result from the previous query.
* @param string|null $query Optional. SQL query. If null (default), uses the result from the previous query.
* @param int $x (optional) Column of value to return. Indexed from 0.
* @param int $y (optional) Row of value to return. Indexed from 0.
* @return string Database query result
* @return string|null Database query result, or null on failure
*/
function get_var( $query = null, $x = 0, $y = 0 ) {
$this->func_call = "\$db->get_var(\"$query\", $x, $y)";
@ -1102,7 +1210,7 @@ class wpdb {
* @param string|null $query SQL query.
* @param string $output (optional) one of ARRAY_A | ARRAY_N | OBJECT constants. Return an associative array (column => value, ...), a numerically indexed array (0 => value, ...) or an object ( ->column = value ), respectively.
* @param int $y (optional) Row to return. Indexed from 0.
* @return mixed Database query result in format specifed by $output
* @return mixed Database query result in format specifed by $output or null on failure
*/
function get_row( $query = null, $output = OBJECT, $y = 0 ) {
$this->func_call = "\$db->get_row(\"$query\",$output,$y)";
@ -1134,8 +1242,8 @@ class wpdb {
*
* @since 0.71
*
* @param string|null $query SQL query. If null, use the result from the previous query.
* @param int $x Column to return. Indexed from 0.
* @param string|null $query Optional. SQL query. If null (default), use the result from the previous query.
* @param int $x Optional. Column to return. Indexed from 0.
* @return array Database query result. Array indexed from 0 by SQL result row number.
*/
function get_col( $query = null , $x = 0 ) {
@ -1206,8 +1314,8 @@ class wpdb {
*
* @since 0.71
*
* @param string $info_type one of name, table, def, max_length, not_null, primary_key, multiple_key, unique_key, numeric, blob, type, unsigned, zerofill
* @param int $col_offset 0: col name. 1: which table the col's in. 2: col's max length. 3: if the col is numeric. 4: col's type
* @param string $info_type Optional. Type one of name, table, def, max_length, not_null, primary_key, multiple_key, unique_key, numeric, blob, type, unsigned, zerofill
* @param int $col_offset Optional. 0: col name. 1: which table the col's in. 2: col's max length. 3: if the col is numeric. 4: col's type
* @return mixed Column Results
*/
function get_col_info( $info_type = 'name', $col_offset = -1 ) {
@ -1234,8 +1342,7 @@ class wpdb {
* @return true
*/
function timer_start() {
$mtime = microtime();
$mtime = explode(' ', $mtime);
$mtime = explode( ' ', microtime() );
$this->time_start = $mtime[1] + $mtime[0];
return true;
}
@ -1248,8 +1355,7 @@ class wpdb {
* @return int Total time spent on the query, in milliseconds
*/
function timer_stop() {
$mtime = microtime();
$mtime = explode(' ', $mtime);
$mtime = explode( ' ', microtime() );
$time_end = $mtime[1] + $mtime[0];
$time_total = $time_end - $this->time_start;
return $time_total;
@ -1282,6 +1388,7 @@ class wpdb {
*
* @since 2.5.0
* @uses $wp_version
* @uses $required_mysql_version
*
* @return WP_Error
*/
@ -1306,21 +1413,25 @@ class wpdb {
}
/**
* Generic function to determine if a database supports a particular feature
* Determine if a database supports a particular feature
*
* @since 2.7
* @see wpdb::db_version()
*
* @param string $db_cap the feature
* @param false|string|resource $dbh_or_table (not implemented) Which database to test. False = the currently selected database, string = the database containing the specified table, resource = the database corresponding to the specified mysql resource.
* @param false|string|resource $dbh_or_table. Not implemented.
* Which database to test. False = the currently selected database, string = the database containing the specified table, resource = the database corresponding to the specified mysql resource.
* @return bool
*/
function has_cap( $db_cap ) {
$version = $this->db_version();
switch ( strtolower( $db_cap ) ) :
switch ( strtolower( $db_cap ) ) {
case 'collation' : // @since 2.5.0
case 'group_concat' : // @since 2.7
case 'subqueries' : // @since 2.7
return version_compare( $version, '4.1', '>=' );
break;
endswitch;
};
return false;
}
@ -1355,7 +1466,8 @@ class wpdb {
/**
* The database version number
* @param false|string|resource $dbh_or_table (not implemented) Which database to test. False = the currently selected database, string = the database containing the specified table, resource = the database corresponding to the specified mysql resource.
* @param false|string|resource $dbh_or_table. Not implemented.
* Which database to test. False = the currently selected database, string = the database containing the specified table, resource = the database corresponding to the specified mysql resource.
* @return false|string false on failure, version number on success
*/
function db_version() {