databaseb1.php

来自「php 开发的内容管理系统」· PHP 代码 · 共 2,038 行 · 第 1/4 页

PHP
2,038
字号
<?php
// Modified for mediawiki for XOOPS - by D.J.

if(defined("MEDIAWIKI_DATABASE")):
return;
else:
define("MEDIAWIKI_DATABASE", 1);

/**
 * This file deals with MySQL interface functions
 * and query specifics/optimisations
 * @package MediaWiki
 */

/** See Database::makeList() */
define( 'LIST_COMMA', 0 );
define( 'LIST_AND', 1 );
define( 'LIST_SET', 2 );
define( 'LIST_NAMES', 3);
define( 'LIST_OR', 4);

/** Number of times to re-try an operation in case of deadlock */
define( 'DEADLOCK_TRIES', 4 );
/** Minimum time to wait before retry, in microseconds */
define( 'DEADLOCK_DELAY_MIN', 500000 );
/** Maximum time to wait before retry */
define( 'DEADLOCK_DELAY_MAX', 1500000 );

class DBObject {
	var $mData;

	function DBObject($data) {
		$this->mData = $data;
	}

	function isLOB() {
		return false;
	}

	function data() {
		return $this->mData;
	}
};

/**
 * Database error base class
 */
class DBError extends MWException {
	public $db;

	/**
	 * Construct a database error
	 * @param Database $db The database object which threw the error
	 * @param string $error A simple error message to be used for debugging
	 */
	function __construct( mwDatabase &$db, $error ) {
		$this->db =& $db;
		parent::__construct( $error );
	}
}

class DBConnectionError extends DBError {
	public $error;
	
	function __construct( mwDatabase &$db, $error = 'unknown error' ) {
		$msg = 'DB connection error';
		if ( trim( $error ) != '' ) {
			$msg .= ": $error";
		}
		$this->error = $error;
		parent::__construct( $db, $msg );
	}

	function useOutputPage() {
		// Not likely to work
		return false;
	}

	function useMessageCache() {
		// Not likely to work
		return false;
	}
	
	function getText() {
		return $this->getMessage() . "\n";
	}

	function getPageTitle() {
		global $wgSitename;
		return "$wgSitename has a problem";
	}

	function getHTML() {
		global $wgTitle, $wgUseFileCache, $title, $wgInputEncoding, $wgOutputEncoding;
		global $wgSitename, $wgServer, $wgMessageCache, $wgLogo;

		# I give up, Brion is right. Getting the message cache to work when there is no DB is tricky.
		# Hard coding strings instead.

		$noconnect = "<p><strong>Sorry! This site is experiencing technical difficulties.</strong></p><p>Try waiting a few minutes and reloading.</p><p><small>(Can't contact the database server: $1)</small></p>";
		$mainpage = 'Main Page';
		$searchdisabled = <<<EOT
<p style="margin: 1.5em 2em 1em">$wgSitename search is disabled for performance reasons. You can search via Google in the meantime.
<span style="font-size: 89%; display: block; margin-left: .2em">Note that their indexes of $wgSitename content may be out of date.</span></p>',
EOT;

		$googlesearch = "
<!-- SiteSearch Google -->
<FORM method=GET action=\"http://www.google.com/search\">
<TABLE bgcolor=\"#FFFFFF\"><tr><td>
<A HREF=\"http://www.google.com/\">
<IMG SRC=\"http://www.google.com/logos/Logo_40wht.gif\"
border=\"0\" ALT=\"Google\"></A>
</td>
<td>
<INPUT TYPE=text name=q size=31 maxlength=255 value=\"$1\">
<INPUT type=submit name=btnG VALUE=\"Google Search\">
<font size=-1>
<input type=hidden name=domains value=\"$wgServer\"><br /><input type=radio name=sitesearch value=\"\"> WWW <input type=radio name=sitesearch value=\"$wgServer\" checked> $wgServer <br />
<input type='hidden' name='ie' value='$2'>
<input type='hidden' name='oe' value='$2'>
</font>
</td></tr></TABLE>
</FORM>
<!-- SiteSearch Google -->";
		$cachederror = "The following is a cached copy of the requested page, and may not be up to date. ";

		# No database access
		if ( is_object( $wgMessageCache ) ) {
			$wgMessageCache->disable();
		}

		if ( trim( $this->error ) == '' ) {
			$this->error = $this->db->getProperty('mServer');
		}

		$text = str_replace( '$1', $this->error, $noconnect );
		$text .= wfGetSiteNotice();

		if($wgUseFileCache) {
			if($wgTitle) {
				$t =& $wgTitle;
			} else {
				if($title) {
					$t = Title::newFromURL( $title );
				} elseif (@/**/$_REQUEST['search']) {
					$search = $_REQUEST['search'];
					return $searchdisabled .
					  str_replace( array( '$1', '$2' ), array( htmlspecialchars( $search ),
					  $wgInputEncoding ), $googlesearch );
				} else {
					$t = Title::newFromText( $mainpage );
				}
			}

			$cache = new CacheManager( $t );
			if( $cache->isFileCached() ) {
				$msg = '<p style="color: red"><b>'.$msg."<br />\n" .
					$cachederror . "</b></p>\n";

				$tag = '<div id="article">';
				$text = str_replace(
					$tag,
					$tag . $msg,
					$cache->fetchPageText() );
			}
		}

		return $text;
	}
}

class DBQueryError extends DBError {
	public $error, $errno, $sql, $fname;
	
	function __construct( mwDatabase &$db, $error, $errno, $sql, $fname ) {
		$message = "A database error has occurred\n" .
		  "Query: $sql\n" .
		  "Function: $fname\n" .
		  "Error: $errno $error\n";

		parent::__construct( $db, $message );
		$this->error = $error;
		$this->errno = $errno;
		$this->sql = $sql;
		$this->fname = $fname;
	}

	function getText() {
		if ( $this->useMessageCache() ) {
			return wfMsg( 'dberrortextcl', htmlspecialchars( $this->getSQL() ),
			  htmlspecialchars( $this->fname ), $this->errno, htmlspecialchars( $this->error ) ) . "\n";
		} else {
			return $this->getMessage();
		}
	}
	
	function getSQL() {
		global $wgShowSQLErrors;
		if( !$wgShowSQLErrors ) {
			return $this->msg( 'sqlhidden', 'SQL hidden' );
		} else {
			return $this->sql;
		}
	}
	
	function getPageTitle() {
		return $this->msg( 'databaseerror', 'Database error' );
	}

	function getHTML() {
		if ( $this->useMessageCache() ) {
			return wfMsgNoDB( 'dberrortext', htmlspecialchars( $this->getSQL() ),
			  htmlspecialchars( $this->fname ), $this->errno, htmlspecialchars( $this->error ) );
		} else {
			return nl2br( htmlspecialchars( $this->getMessage() ) );
		}
	}
}

class DBUnexpectedError extends DBError {}

/**
 * Database abstraction object
 * @package MediaWiki
 */
class mwDatabase {

#------------------------------------------------------------------------------
# Variables
#------------------------------------------------------------------------------
	/**#@+
	 * @access private
	 */
	var $mLastQuery = '';

	var $mServer, $mUser, $mPassword, $mConn = null, $mDBname;
	var $mOut, $mOpened = false;

	var $mFailFunction;
	var $mTablePrefix;
	var $mFlags;
	var $mTrxLevel = 0;
	var $mErrorCount = 0;
	var $mLBInfo = array();
	/**#@-*/

#------------------------------------------------------------------------------
# Accessors
#------------------------------------------------------------------------------
	# These optionally set a variable and return the previous state

	/**
	 * Fail function, takes a Database as a parameter
	 * Set to false for default, 1 for ignore errors
	 */
	function failFunction( $function = NULL ) {
		return wfSetVar( $this->mFailFunction, $function );
	}

	/**
	 * Output page, used for reporting errors
	 * FALSE means discard output
	 */
	function setOutputPage( $out ) {
		$this->mOut = $out;
	}

	/**
	 * Boolean, controls output of large amounts of debug information
	 */
	function debug( $debug = NULL ) {
		return wfSetBit( $this->mFlags, DBO_DEBUG, $debug );
	}

	/**
	 * Turns buffering of SQL result sets on (true) or off (false).
	 * Default is "on" and it should not be changed without good reasons.
	 */
	function bufferResults( $buffer = NULL ) {
		if ( is_null( $buffer ) ) {
			return !(bool)( $this->mFlags & DBO_NOBUFFER );
		} else {
			return !wfSetBit( $this->mFlags, DBO_NOBUFFER, !$buffer );
		}
	}

	/**
	 * Turns on (false) or off (true) the automatic generation and sending
	 * of a "we're sorry, but there has been a database error" page on
	 * database errors. Default is on (false). When turned off, the
	 * code should use wfLastErrno() and wfLastError() to handle the
	 * situation as appropriate.
	 */
	function ignoreErrors( $ignoreErrors = NULL ) {
		return wfSetBit( $this->mFlags, DBO_IGNORE, $ignoreErrors );
	}

	/**
	 * The current depth of nested transactions
	 * @param $level Integer: , default NULL.
	 */
	function trxLevel( $level = NULL ) {
		return wfSetVar( $this->mTrxLevel, $level );
	}

	/**
	 * Number of errors logged, only useful when errors are ignored
	 */
	function errorCount( $count = NULL ) {
		return wfSetVar( $this->mErrorCount, $count );
	}

	/**
	 * Properties passed down from the server info array of the load balancer
	 */
	function getLBInfo( $name = NULL ) {
		if ( is_null( $name ) ) {
			return $this->mLBInfo;
		} else {
			if ( array_key_exists( $name, $this->mLBInfo ) ) {
				return $this->mLBInfo[$name];
			} else {
				return NULL;
			}
		}
	}

	function setLBInfo( $name, $value = NULL ) {
		if ( is_null( $value ) ) {
			$this->mLBInfo = $name;
		} else {
			$this->mLBInfo[$name] = $value;
		}
	}

	/**#@+
	 * Get function
	 */
	function lastQuery() { return $this->mLastQuery; }
	function isOpen() { return $this->mOpened; }
	/**#@-*/

	function setFlag( $flag ) {
		$this->mFlags |= $flag;
	}

	function clearFlag( $flag ) {
		$this->mFlags &= ~$flag;
	}

	function getFlag( $flag ) {
		return !!($this->mFlags & $flag);
	}

	/**
	 * General read-only accessor
	 */
	function getProperty( $name ) {
		return $this->$name;
	}

// Modified for mediawiki for XOOPS - by D.J.
	/* compatible with XOOPS */
	function Database( $server = false, $user = false, $password = false, $dbName = false, 
		$failFunction = false, $flags = 0, $tablePrefix = 'get from global' ) {
		$this->mwDatabase( $server, $user, $password, $dbName, $failFunction, $flags, $tablePrefix);
	}
	
#------------------------------------------------------------------------------
# Other functions
#------------------------------------------------------------------------------

	/**#@+
	 * @param string $server database server host
	 * @param string $user database user name
	 * @param string $password database user password
	 * @param string $dbname database name
	 */

	/**
	 * @param failFunction
	 * @param $flags
	 * @param string $tablePrefix Database table prefixes. By default use the prefix gave in LocalSettings.php
	 */
// Modified for mediawiki for XOOPS - by D.J.
	function mwDatabase( $server = false, $user = false, $password = false, $dbName = false, 
		$failFunction = false, $flags = 0, $tablePrefix = 'get from global' ) {
			$this->_construct( $server, $user, $password, $dbName, $failFunction, $flags, $tablePrefix );
	}
		
	function _construct( $server = false, $user = false, $password = false, $dbName = false, 
		$failFunction = false, $flags = 0, $tablePrefix = 'get from global' ) {

		global $wgOut, $wgDBprefix, $wgCommandLineMode;
		# Can't get a reference if it hasn't been set yet
		if ( !isset( $wgOut ) ) {
			$wgOut = NULL;
		}
		$this->mOut =& $wgOut;

		$this->mFailFunction = $failFunction;
		$this->mFlags = $flags;

		if ( $this->mFlags & DBO_DEFAULT ) {
			if ( $wgCommandLineMode ) {
				$this->mFlags &= ~DBO_TRX;
			} else {
				$this->mFlags |= DBO_TRX;
			}
		}

		/*
		// Faster read-only access
		if ( wfReadOnly() ) {
			$this->mFlags |= DBO_PERSISTENT;
			$this->mFlags &= ~DBO_TRX;
		}*/

		/** Get the default table prefix*/
		if ( $tablePrefix == 'get from global' ) {
			$this->mTablePrefix = $wgDBprefix;
		} else {
			$this->mTablePrefix = $tablePrefix;
		}

		if ( $server ) {
			$this->open( $server, $user, $password, $dbName );
		}
	}

	/**
	 * @static
	 * @param failFunction
	 * @param $flags
	 */
	static function newFromParams( $server, $user, $password, $dbName,
		$failFunction = false, $flags = 0 )
	{
		return new mwDatabase( $server, $user, $password, $dbName, $failFunction, $flags );
	}

	/**
	 * Usually aborts on failure
	 * If the failFunction is set to a non-zero integer, returns success
	 */
	function open( $server, $user, $password, $dbName ) {
		global $wguname;

		# Test for missing mysql.so
		# First try to load it
		if (!@extension_loaded('mysql')) {
			@dl('mysql.so');
		}

		# Fail now
		# Otherwise we get a suppressed fatal error, which is very hard to track down
		if ( !function_exists( 'mysql_connect' ) ) {
			throw new DBConnectionError( $this, "MySQL functions missing, have you compiled PHP with the --with-mysql option?\n" );
		}

		$this->close();
		$this->mServer = $server;
		$this->mUser = $user;
		$this->mPassword = $password;
		$this->mDBname = $dbName;

		$success = false;

		# Force connecting to XOOPS DB
		if($GLOBALS["xoopsDB"]->conn){
			$this->mConn =& $GLOBALS["xoopsDB"]->conn;
		}else
		
		if ( $this->mFlags & DBO_PERSISTENT ) {
			@/**/$this->mConn = mysql_pconnect( $server, $user, $password );
		} else {
			# Create a new connection...
			@/**/$this->mConn = mysql_connect( $server, $user, $password, true );
		}

		if ( $dbName != '' ) {
			if ( $this->mConn !== false ) {
				$success = @/**/mysql_select_db( $dbName, $this->mConn );
				if ( !$success ) {
					$error = "Error selecting database $dbName on server {$this->mServer} " .
						"from client host {$wguname['nodename']}\n";
					wfDebug( $error );
				}
			} else {
				wfDebug( "DB connection error\n" );
				wfDebug( "Server: $server, User: $user, Password: " .
					substr( $password, 0, 3 ) . "..., error: " . mysql_error() . "\n" );
				$success = false;
			}
		} else {
			# Delay USE query
			$success = (bool)$this->mConn;
		}

		if ( !$success ) {
			$this->reportConnectionError();
		}

		global $wgDBmysql5;
		if( $wgDBmysql5 ) {
			// Tell the server we're communicating with it in UTF-8.
			// This may engage various charset conversions.
			$this->query( 'SET NAMES utf8' );
		}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?