cb.dbchecker.php
上传用户:stephen_wu
上传日期:2008-07-05
资源大小:1757k
文件大小:6k
- <?php
- /**
- * Joomla/Mambo Community Builder
- * @version $Id: install.comprofiler.sql.php 567 2006-11-19 10:05:00Z beat $
- * @package Community Builder
- * @subpackage install.comprofiler.sql.php
- * @author Beat
- * @copyright (C) 2008 Lightning MultiCom SA, www.joomlapolis.com
- * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU/GPL version 2
- */
- // ensure this file is being included by a parent file
- if ( ! ( defined( '_VALID_CB' ) || defined( '_JEXEC' ) || defined( '_VALID_MOS' ) ) ) { die( 'Direct Access to this location is not allowed.' ); }
- /**
- * CB SQL versioning / upgrading functions:
- *
- * WARNING:
- * This new library is experimental work in progress and should not be used directly by plugins and 3pds,
- * as it is subject to change without notice, and is not part of current CB API.
- *
- * @access private
- *
- */
- class CBdbChecker {
- var $_db = null;
- var $_sqlUpgrader = null;
- var $_silentWhenOK = true;
- function CBdbChecker( &$db ) {
- $this->_db =& $db;
- $this->_silentWhenOK = false;
- }
- /**
- * Returns the database CBSimpleXMLElement
- *
- * @return CBSimpleXMLElement
- */
- function & _getCbDbXml() {
- global $_CB_framework;
- static $_cb_db_xml = null;
- if ( $_cb_db_xml == null ) {
- $filename = $_CB_framework->getCfg( 'absolute_path' ) . '/administrator/components/com_comprofiler/database/database.cbcore.xml';
- if ( is_readable( $filename ) ) {
- cbimport( 'cb.xml.simplexml' );
- $_cb_db_xml = new CBSimpleXMLElement( file_get_contents( $filename ) );
- }
- }
- return $_cb_db_xml;
- }
- /**
- * Returns all errors logged
- *
- * @param string|boolean $implode False: returns full array, if string: imploding string
- * @param string|boolean $detailsImplode False: no details, otherwise imploding string
- * @return string|array
- */
- function getErrors( $implode = "n", $detailsImplode = false ) {
- return $this->_sqlUpgrader->getErrors( $implode, $detailsImplode );
- }
- /**
- * Returns all logs logged
- *
- * @param string|boolean $implode False: returns full array, if string: imploding string
- * @param string|boolean $detailsImplode False: no details, otherwise imploding string
- * @return string|array
- */
- function getLogs( $implode = "n", $detailsImplode = false ) {
- return $this->_sqlUpgrader->getLogs( $implode, $detailsImplode );
- }
- /**
- * Checks the comprofiler_fields table and upgrades if needed
- * Backend-use only.
- * @access private
- *
- * @param string $tableName
- * @param boolean $upgrade False: only check table, True: upgrades table (depending on $dryRun)
- * @param boolean $dryRun True: doesn't do the modifying queries, but lists them, False: does the job
- * @return string|boolean Message to display
- */
- function checkTable( $tableName, $upgrade = false, $dryRun = false ) {
- $xml = $this->_getCbDbXml();
- if ( $xml !== null ) {
- $db =& $xml->getElementByPath( 'database' );
- if ( $db !== false ) {
- $table = $db->getChildByNameAttr( 'table', 'name', $tableName );
- if ( $table !== false ) {
- cbimport( 'cb.sql.upgrader' );
- $this->_sqlUpgrader = new CBSQLupgrader( $this->_db, $this->_silentWhenOK );
- $this->_sqlUpgrader->setDryRun( $dryRun );
- $success = $this->_sqlUpgrader->checkXmlTableDescription( $table, '', $upgrade, null );
- /*
- var_dump( $success );
- echo "<br>nERRORS: " . $this->_sqlUpgrader->getErrors( "<br /><br />nn", "<br />n" );
- echo "<br>nLOGS: " . $this->_sqlUpgrader->getLogs( "<br /><br />nn", "<br />n" );
- exit;
- */
- } else {
- $success = array( sprintf( 'Error: could not find element table name="%s" in XML file', $tableName ), null );
- }
- } else {
- $success = array( 'Error: could not find element "database" in XML file', null );
- }
- } else {
- $success = array( 'Error: could not find XML file', null );
- }
- return $success;
- }
- /**
- * Checks the comprofiler_fields table and upgrades if needed
- * Backend-use only.
- * @access private
- *
- * @param boolean $upgrade False: only check table, True: upgrades table (depending on $dryRun)
- * @param boolean $dryRun True: doesn't do the modifying queries, but lists them, False: does the job
- * @return string Message to display
- */
- function checkDatabase( $upgrade = false, $dryRun = false ) {
- $xml = $this->_getCbDbXml();
- if ( $xml !== null ) {
- $db =& $xml->getElementByPath( 'database' );
- if ( $db ) {
- cbimport( 'cb.sql.upgrader' );
- $this->_sqlUpgrader = new CBSQLupgrader( $this->_db, $this->_silentWhenOK );
- $this->_sqlUpgrader->setDryRun( $dryRun );
- $success = $this->_sqlUpgrader->checkXmlDatabaseDescription( $db, '', $upgrade, true );
- /*
- var_dump( $success );
- echo "<br>nERRORS: " . $this->_sqlUpgrader->getErrors( "<br /><br />nn", "<br />n" );
- echo "<br>nLOGS: " . $this->_sqlUpgrader->getLogs( "<br /><br />nn", "<br />n" );
- exit;
- */
- } else {
- $success = array( 'Error: could not find element database in XML file', null );
- }
- } else {
- $success = array( 'Error: could not find XML file', null );
- }
- return $success;
- }
- /**
- * Handles SQL XML for the type of the field (backend use only!)
- * e.g.: array( '#__comprofiler_fields' ), true
- * array( '#__comprofiler', '#__comprofiler_field_values', '#__comprofiler_fields', '#__comprofiler_lists', '#__comprofiler_members', '#__comprofiler_plugin', '#__comprofiler_tabs', '#__comprofiler_userreports', '#__comprofiler_views' ), false
- * $_CB_database->getTableList(), false
- *
- * @param array $tablesArray Array of tableNames:
- * @return string XML
- */
- function _dumpAll( $tablesArray, $withContent ) {
- global $_CB_database;
- cbimport( 'cb.sql.upgrader' );
- $sqlUpgrader = new CBSQLupgrader( $_CB_database );
- $sqlUpgrader->setDryRun( true );
- $tableXml = $sqlUpgrader->dumpTableToXml( $tablesArray, $withContent );
- if ( class_exists( 'DOMDocument' ) ) {
- $doc = new DOMDocument( '1.0', 'UTF-8' );
- $doc->formatOutput = true;
- $domnode = dom_import_simplexml($tableXml);
- $domnode = $doc->importNode($domnode, true);
- $domnode = $doc->appendChild($domnode);
- $text = str_replace( array( '/>', "nn" ), array( ' />', "n" ), $doc->saveXML() );
- } else {
- $text = $tableXml->asXML();
- }
- return $text;
- }
- }
- ?>