Source for file changeset.php

Documentation is available at changeset.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Libraries
  4.  * @subpackage  Schema
  5.  *
  6.  * @copyright   Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  7.  * @license     GNU General Public License version 2 or later; see LICENSE
  8.  */
  9.  
  10. defined('JPATH_PLATFORM'or die;
  11.  
  12. jimport('joomla.filesystem.folder');
  13.  
  14. /**
  15.  * Contains a set of JSchemaChange objects for a particular instance of Joomla.
  16.  * Each of these objects contains a DDL query that should have been run against
  17.  * the database when this database was created or updated. This enables the
  18.  * Installation Manager to check that the current database schema is up to date.
  19.  *
  20.  * @package     Joomla.Libraries
  21.  * @subpackage  Schema
  22.  * @since       2.5
  23.  */
  24. {
  25.     /**
  26.      * Array of JSchemaChangeitem objects
  27.      *
  28.      * @var    array 
  29.      * @since  2.5
  30.      */
  31.     protected $changeItems = array();
  32.  
  33.     /**
  34.      * JDatabaseDriver object
  35.      *
  36.      * @var    JDatabaseDriver 
  37.      * @since  2.5
  38.      */
  39.     protected $db = null;
  40.  
  41.     /**
  42.      * Folder where SQL update files will be found
  43.      *
  44.      * @var    string 
  45.      */
  46.     protected $folder = null;
  47.  
  48.     /**
  49.      * Constructor: builds array of $changeItems by processing the .sql files in a folder.
  50.      * The folder for the Joomla core updates is administrator/components/com_admin/sql/updates/<database>.
  51.      *
  52.      * @param   JDatabaseDriver  $db      The current database object
  53.      * @param   string           $folder  The full path to the folder containing the update queries
  54.      *
  55.      * @since   2.5
  56.      */
  57.     public function __construct($db$folder null)
  58.     {
  59.         $this->db = $db;
  60.         $this->folder = $folder;
  61.         $updateFiles $this->getUpdateFiles();
  62.         $updateQueries $this->getUpdateQueries($updateFiles);
  63.         foreach ($updateQueries as $obj)
  64.         {
  65.             $this->changeItems[JSchemaChangeitem::getInstance($db$obj->file$obj->updateQuery);
  66.         }
  67.     }
  68.  
  69.     /**
  70.      * Returns a reference to the JSchemaChangeset object, only creating it if it doesn't already exist.
  71.      *
  72.      * @param   JDatabaseDriver  $db      The current database object
  73.      * @param   string           $folder  The full path to the folder containing the update queries
  74.      *
  75.      * @return  JSchemaChangeset 
  76.      *
  77.      * @since   2.5
  78.      */
  79.     public static function getInstance($db$folder)
  80.     {
  81.         static $instance;
  82.  
  83.         if (!is_object($instance))
  84.         {
  85.             $instance new JSchemaChangeset($db$folder);
  86.         }
  87.  
  88.         return $instance;
  89.     }
  90.  
  91.     /**
  92.      * Checks the database and returns an array of any errors found.
  93.      * Note these are not database errors but rather situations where
  94.      * the current schema is not up to date.
  95.      *
  96.      * @return   array Array of errors if any.
  97.      *
  98.      * @since    2.5
  99.      */
  100.     public function check()
  101.     {
  102.         $errors array();
  103.         foreach ($this->changeItems as $item)
  104.         {
  105.             if ($item->check(=== -2)
  106.             {
  107.                 // Error found
  108.                 $errors[$item;
  109.             }
  110.         }
  111.         return $errors;
  112.     }
  113.  
  114.     /**
  115.      * Runs the update query to apply the change to the database
  116.      *
  117.      * @return  void 
  118.      *
  119.      * @since   2.5
  120.      */
  121.     public function fix()
  122.     {
  123.         $this->check();
  124.         foreach ($this->changeItems as $item)
  125.         {
  126.             $item->fix();
  127.         }
  128.     }
  129.  
  130.     /**
  131.     * Returns an array of results for this set
  132.     *
  133.     * @return  array  associative array of changeitems grouped by unchecked, ok, error, and skipped
  134.     *
  135.     * @since   2.5
  136.     */
  137.     public function getStatus()
  138.     {
  139.         $result array('unchecked' => array()'ok' => array()'error' => array()'skipped' => array());
  140.         foreach ($this->changeItems as $item)
  141.         {
  142.             switch ($item->checkStatus)
  143.             {
  144.                 case 0:
  145.                     $result['unchecked'][$item;
  146.                     break;
  147.                 case 1:
  148.                     $result['ok'][$item;
  149.                     break;
  150.                 case -2:
  151.                     $result['error'][$item;
  152.                     break;
  153.                 case -1:
  154.                     $result['skipped'][$item;
  155.                     break;
  156.             }
  157.         }
  158.         return $result;
  159.     }
  160.  
  161.     /**
  162.      * Gets the current database schema, based on the highest version number.
  163.      * Note that the .sql files are named based on the version and date, so
  164.      * the file name of the last file should match the database schema version
  165.      * in the #__schemas table.
  166.      *
  167.      * @return  string  the schema version for the database
  168.      *
  169.      * @since   2.5
  170.      */
  171.     public function getSchema()
  172.     {
  173.         $updateFiles $this->getUpdateFiles();
  174.         $result new SplFileInfo(array_pop($updateFiles));
  175.         return $result->getBasename('.sql');
  176.     }
  177.  
  178.     /**
  179.      * Get list of SQL update files for this database
  180.      *
  181.      * @return  array  list of sql update full-path names
  182.      *
  183.      * @since   2.5
  184.      */
  185.     private function getUpdateFiles()
  186.     {
  187.         // Get the folder from the database name
  188.         $sqlFolder $this->db->name;
  189.  
  190.         if ($sqlFolder == 'mysqli')
  191.         {
  192.             $sqlFolder 'mysql';
  193.         }
  194.         elseif ($sqlFolder == 'sqlsrv')
  195.         {
  196.             $sqlFolder 'sqlazure';
  197.         }
  198.  
  199.         // Default folder to core com_admin
  200.         if (!$this->folder)
  201.         {
  202.             $this->folder = JPATH_ADMINISTRATOR '/components/com_admin/sql/updates/';
  203.         }
  204.  
  205.         return JFolder::files(
  206.             $this->folder . '/' $sqlFolder'\.sql$'1truearray('.svn''CVS''.DS_Store''__MACOSX')array('^\..*''.*~')true
  207.         );
  208.     }
  209.  
  210.     /**
  211.      * Get array of SQL queries
  212.      *
  213.      * @param   array  $sqlfiles  Array of .sql update filenames.
  214.      *
  215.      * @return  array  Array of stdClass objects where:
  216.      *                     file=filename,
  217.      *                     update_query = text of SQL update query
  218.      *
  219.      * @since   2.5
  220.      */
  221.     private function getUpdateQueries(array $sqlfiles)
  222.     {
  223.         // Hold results as array of objects
  224.         $result array();
  225.         foreach ($sqlfiles as $file)
  226.         {
  227.             $buffer file_get_contents($file);
  228.  
  229.             // Create an array of queries from the sql file
  230.             $queries JDatabaseDriver::splitSql($buffer);
  231.             foreach ($queries as $query)
  232.             {
  233.                 if ($trimmedQuery $this->trimQuery($query))
  234.                 {
  235.                     $fileQueries new stdClass;
  236.                     $fileQueries->file $file;
  237.                     $fileQueries->updateQuery $trimmedQuery;
  238.                     $result[$fileQueries;
  239.                 }
  240.             }
  241.         }
  242.         return $result;
  243.     }
  244.  
  245.     /**
  246.      * Trim comment and blank lines out of a query string
  247.      *
  248.      * @param   string  $query  query string to be trimmed
  249.      *
  250.      * @return  string  String with leading comment lines removed
  251.      *
  252.      * @since   3.1
  253.      */
  254.     private function trimQuery($query)
  255.     {
  256.         $query trim($query);
  257.  
  258.         while (substr($query01== '#' || substr($query02== '--' || substr($query02== '/*')
  259.         {
  260.             $endChars (substr($query01== '#' || substr($query02== '--'"\n" "*/";
  261.  
  262.             if ($position strpos($query$endChars))
  263.             {
  264.                 $query trim(substr($query$position strlen($endChars)));
  265.             }
  266.             else
  267.             {
  268.                 // If no newline, the rest of the file is a comment, so return an empty string.
  269.                 return '';
  270.             }
  271.         }
  272.  
  273.         return trim($query);
  274.     }
  275. }

Documentation generated on Tue, 19 Nov 2013 14:55:39 +0100 by phpDocumentor 1.4.3