[ Index ]

PHP Cross Reference of Joomla 2.5.4 DE

title

Body

[close]

/libraries/cms/schema/ -> changeitem.php (source)

   1  <?php
   2  /**
   3   * Abstract class for database schema change
   4   *
   5   * @package     CMS.Library
   6   * @subpackage  Schema
   7   *
   8   * @copyright   Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
   9   * @license     GNU General Public License version 2 or later; see LICENSE
  10   */
  11  
  12  defined('JPATH_PLATFORM') or die;
  13  
  14  /**
  15   * Each object represents one query, which is one line from a DDS SQL query.
  16   * This class is used to check the site's database to see if the DDL query has been run.
  17   * If not, it provides the ability to fix the database by re-running the DDL query.
  18   * The queries are parsed from the update files in the folder
  19   * administrator/components/com_admin/sql/updates/<database>.
  20   * These updates are run automatically if the site was updated using com_installer.
  21   * However, it is possible that the program files could be updated without udpating
  22   * the database (for example, if a user just copies the new files over the top of an
  23   * existing installation).
  24   *
  25   * This is an abstract class. We need to extend it for each database and add a
  26   * buildCheckQuery() method that creates the query to check that a DDL query has been run.
  27   *
  28   * @package     CMS.Library
  29   * @subpackage  Schema
  30   * @since       2.5
  31   */
  32  abstract class JSchemaChangeitem extends JObject
  33  {
  34      /**
  35      * Update file: full path file name where query was found
  36      *
  37      * @var    string
  38      */
  39      public $file = null;
  40  
  41      /**
  42       * Update query: query used to change the db schema (one line from the file)
  43       *
  44       * @var    string
  45       */
  46      public $updateQuery = null;
  47  
  48      /**
  49      * Check query: query used to check the db schema
  50      *
  51      * @var    string
  52      */
  53      public $checkQuery = null;
  54  
  55      /**
  56      * Check query result: expected result of check query if database is up to date
  57      *
  58      * @var    string
  59      */
  60      public $checkQueryExpected = 1;
  61  
  62      /**
  63      * JDatabase object
  64      *
  65      * @var    string
  66      */
  67      public $db = null;
  68  
  69      /**
  70       * Query type: To be used in building a language key for a
  71       * message to tell user what was checked / changed
  72       * Possible values: ADD_TABLE, ADD_COLUMN, CHANGE_COLUMN_TYPE, ADD_INDEX
  73       *
  74       * @var   string
  75       *
  76       */
  77      public $queryType = null;
  78  
  79      /**
  80      * Array with values for use in a JText::sprintf statment indicating what was checked
  81      *
  82      *   Tells you what the message should be, based on which elements are defined, as follows:
  83      *     For ADD_TABLE: table
  84      *     For ADD_COLUMN: table, column
  85      *     For CHANGE_COLUMN_TYPE: table, column, type
  86      *     For ADD_INDEX: table, index
  87      *
  88      * @var    array
  89      */
  90      public $msgElements = array();
  91  
  92      /**
  93      * Checked status
  94      *
  95      * @var    int   0=not checked, -1=skipped, -2=failed, 1=succeeded
  96      */
  97      public $checkStatus = 0;
  98  
  99      /**
 100      * Rerun status
 101      *
 102      * @var    int   0=not rerun, -1=skipped, -2=failed, 1=succeeded
 103      */
 104      public $rerunStatus = 0;
 105  
 106      /**
 107       * Constructor: builds check query and message from $updateQuery
 108       *
 109       * @param   JDatabase  $db
 110       * @param   string     $file   full path name of the sql file
 111       * @param   string     $query   text of the sql query (one line of the file)
 112       * @param   string     $checkQuery
 113       *
 114       * @since   2.5
 115       */
 116  	public function __construct($db, $file, $updateQuery)
 117      {
 118          $this->updateQuery = $updateQuery;
 119          $this->file = $file;
 120          $this->db = $db;
 121          $this->buildCheckQuery();
 122      }
 123  
 124      /**
 125       *
 126       * Returns an instance of the correct schemachangeitem for the $db
 127       *
 128       * @param   JDatabase $db
 129       * @param   string    $file   full path name of the sql file
 130       * @param   string    $query  text of the sql query (one line of the file)
 131       *
 132       * @return  JSchemaChangeItem for the $db driver
 133       *
 134       * @since   2.5
 135       */
 136  	public static function getInstance($db, $file, $query)
 137      {
 138          $instance = null;
 139          // Get the class name (mysql and mysqli both use mysql)
 140          $dbname = (substr($db->name, 0, 5) == 'mysql') ? 'mysql' : $db->name;
 141          $path = dirname(__FILE__).'/' . 'changeitem' . $dbname . '.php'  ;
 142          $class = 'JSchemaChangeitem' . $dbname;
 143  
 144          // If the file exists register the class with our class loader.
 145          if (file_exists($path)) {
 146              JLoader::register($class, $path);
 147              $instance = new $class($db, $file, $query);
 148          }
 149          return $instance;
 150  
 151      }
 152  
 153      /**
 154       * Runs the check query and checks that 1 row is returned
 155       * If yes, return true, otherwise return false
 156       *
 157       * @return  boolean  true on success, false otherwise
 158       *
 159       * @since  2.5
 160       */
 161  	public function check()
 162      {
 163          $this->checkStatus = -1;
 164          if ($this->checkQuery) {
 165              $this->db->setQuery($this->checkQuery);
 166              $rows = $this->db->loadObject();
 167              if ($rows !== false) {
 168                  if (count($rows) === $this->checkQueryExpected) {
 169                      $this->checkStatus = 1;
 170                  } else {
 171                      $this->checkStatus = -2;
 172                  }
 173              }
 174              else {
 175                  $this->checkStatus = -2;
 176              }
 177          }
 178          return $this->checkStatus;
 179      }
 180  
 181      /**
 182       * Runs the update query to apply the change to the database
 183       *
 184       * @since  2.5
 185       */
 186  	public function fix()
 187      {
 188          if ($this->checkStatus === -2) {
 189              // at this point we have a failed query
 190              $this->db->setQuery($this->updateQuery);
 191              if ($this->db->query()) {
 192                  if ($this->check()) {
 193                      $this->checkStatus = 1;
 194                      $this->rerunStatus = 1;
 195                  } else {
 196                      $this->rerunStatus = -2;
 197                  }
 198              } else {
 199                  $this->rerunStatus = -2;
 200              }
 201          }
 202      }
 203  }


Generated: Tue Apr 3 11:40:28 2012 Cross-referenced by PHPXref 0.7.1