| [ Index ] |
PHP Cross Reference of Joomla 2.5.4 DE |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * @package CMS.Library 4 * @subpackage Schema 5 * 6 * @copyright Copyright (C) 2005 - 2012 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.file'); 13 jimport('joomla.filesystem.folder'); 14 JLoader::register('JSchemaChangeitem', JPATH_LIBRARIES . '/cms/schema/changeitem.php'); 15 16 /** 17 * Contains a set of JSchemaChange objects for a particular instance of Joomla. 18 * Each of these objects contains a DDL query that should have been run against 19 * the database when this database was created or updated. This enables the 20 * Installation Manager to check that the current database schema is up to date. 21 * 22 * @package CMS.Library 23 * @subpackage Schema 24 * @since 2.5 25 */ 26 class JSchemaChangeset extends JObject 27 { 28 /** 29 * Array of JSchemaChangeItem objects 30 * 31 * @var string 32 */ 33 protected $changeItems = array(); 34 35 /** 36 * JDatabase object 37 * 38 * @var string 39 */ 40 protected $db = null; 41 42 /** 43 * Folder where SQL update files will be found 44 * 45 * @var string 46 */ 47 protected $folder = null; 48 49 /** 50 * Constructor: builds array of $changeItems by processing the .sql files in a folder. 51 * The folder for the Joomla core updates is administrator/components/com_admin/sql/updates/<database>. 52 * 53 * @param JDatabase $db The current database object 54 * @param string $folder The full path to the folder containing the update queries 55 * 56 * @since 2.5 57 */ 58 public function __construct($db, $folder = null) 59 { 60 $this->db = $db; 61 $this->folder = $folder; 62 $updateFiles = $this->getUpdateFiles(); 63 $updateQueries = $this->getUpdateQueries($updateFiles); 64 foreach ($updateQueries as $obj) 65 { 66 $this->changeItems[] = JSchemaChangeItem::getInstance($db, $obj->file, $obj->updateQuery); 67 } 68 } 69 70 /** 71 * Returns the existing JSchemaChangeset object if it exists. 72 * Otherwise, it creates a new one. 73 * 74 * @param JDatabase $db The current database object 75 * @param string $folder The full path to the folder containing the update queries 76 * 77 * @return JSchemaChangeSet The (possibly chached) instance of JSchemaChangeSet 78 * 79 * @since 2.5 80 */ 81 public static function getInstance($db, $folder) 82 { 83 static $instance; 84 if (!is_object($instance)) 85 { 86 $instance = new JSchemaChangeSet($db, $folder); 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 if (substr($sqlFolder, 0, 5) == 'mysql') 190 { 191 $sqlFolder = 'mysql'; 192 } 193 194 // Default folder to core com_admin 195 if (!$this->folder) 196 { 197 $this->folder = JPATH_ADMINISTRATOR . '/components/com_admin/sql/updates/'; 198 } 199 return JFolder::files($this->folder . '/' . $sqlFolder, '\.sql$', 1, true); 200 } 201 202 /** 203 * Get array of SQL queries 204 * 205 * @param array $sqlfiles Array of .sql update filenames. 206 * 207 * @return array Array of stdClass objects where: 208 * file=filename, 209 * update_query = text of SQL update query 210 * 211 * @since 2.5 212 */ 213 private function getUpdateQueries(array $sqlfiles) 214 { 215 // Hold results as array of objects 216 $result = array(); 217 foreach ($sqlfiles as $file) 218 { 219 $buffer = file_get_contents($file); 220 221 // Create an array of queries from the sql file 222 $queries = $this->db->splitSql($buffer); 223 foreach ($queries as $query) 224 { 225 if (trim($query)) 226 { 227 $fileQueries = new stdClass; 228 $fileQueries->file = $file; 229 $fileQueries->updateQuery = $query; 230 $result[] = $fileQueries; 231 } 232 } 233 } 234 return $result; 235 } 236 237 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Tue Apr 3 11:40:28 2012 | Cross-referenced by PHPXref 0.7.1 |