XOOPS  2.6.0
manager.php
Go to the documentation of this file.
1 <?php
28 {
32  private $s_tables = array();
33 
37  private $f_tables = array();
38 
42  public $db;
43 
47  public $successStrings = array();
48 
52  public $failureStrings = array();
53 
57  public function __construct()
58  {
60  $xoops->db();
61  global $xoopsDB;
62  $this->db = $xoopsDB;
63  $this->db->setPrefix(\XoopsBaseConfig::get('db-prefix'));
64  $this->successStrings = array(
69  );
70  $this->failureStrings = array(
75  );
76  }
77 
83  public function isConnectable()
84  {
85  return ($this->db->connect(false) != false) ? true : false;
86  }
87 
93  public function dbExists()
94  {
95  return ($this->db->connect() != false) ? true : false;
96  }
97 
103  public function createDB()
104  {
105  $this->db->connect(false);
106 
107  $result = $this->db->query("CREATE DATABASE " . \XoopsBaseConfig::get('db-name'));
108 
109  return ($result != false) ? true : false;
110  }
111 
120  public function queryFromFile($sql_file_path, $force = false)
121  {
122  if (!XoopsLoad::fileExists($sql_file_path)) {
123  return false;
124  }
125  $queryFunc = (bool)$force ? "queryF" : "query";
126  $sql_query = trim(fread(fopen($sql_file_path, 'r'), filesize($sql_file_path)));
127  SqlUtility::splitMySqlFile($pieces, $sql_query);
128  $this->db->connect();
129  foreach ($pieces as $piece) {
130  $piece = trim($piece);
131  // [0] contains the prefixed query
132  // [4] contains unprefixed table name
133  $prefixed_query = SqlUtility::prefixQuery($piece, $this->db->prefix());
134  if ($prefixed_query != false) {
135  $table = $this->db->prefix($prefixed_query[4]);
136  if ($prefixed_query[1] == 'CREATE TABLE') {
137  if ($this->db->$queryFunc($prefixed_query[0]) != false) {
138  if (!isset($this->s_tables['create'][$table])) {
139  $this->s_tables['create'][$table] = 1;
140  }
141  } else {
142  if (!isset($this->f_tables['create'][$table])) {
143  $this->f_tables['create'][$table] = 1;
144  }
145  }
146  } else {
147  if ($prefixed_query[1] == 'INSERT INTO') {
148  if ($this->db->$queryFunc($prefixed_query[0]) != false) {
149  if (!isset($this->s_tables['insert'][$table])) {
150  $this->s_tables['insert'][$table] = 1;
151  } else {
152  $this->s_tables['insert'][$table]++;
153  }
154  } else {
155  if (!isset($this->f_tables['insert'][$table])) {
156  $this->f_tables['insert'][$table] = 1;
157  } else {
158  $this->f_tables['insert'][$table]++;
159  }
160  }
161  } else {
162  if ($prefixed_query[1] == 'ALTER TABLE') {
163  if ($this->db->$queryFunc($prefixed_query[0]) != false) {
164  if (!isset($this->s_tables['alter'][$table])) {
165  $this->s_tables['alter'][$table] = 1;
166  }
167  } else {
168  if (!isset($this->s_tables['alter'][$table])) {
169  $this->f_tables['alter'][$table] = 1;
170  }
171  }
172  } else {
173  if ($prefixed_query[1] == 'DROP TABLE') {
174  if ($this->db->$queryFunc('DROP TABLE ' . $table) != false) {
175  if (!isset($this->s_tables['drop'][$table])) {
176  $this->s_tables['drop'][$table] = 1;
177  }
178  } else {
179  if (!isset($this->s_tables['drop'][$table])) {
180  $this->f_tables['drop'][$table] = 1;
181  }
182  }
183  }
184  }
185  }
186  }
187  }
188  }
189  return true;
190  }
191 
197  public function report()
198  {
199  $commands = array('create', 'insert', 'alter', 'drop');
200  $content = '<ul class="log">';
201  foreach ($commands as $cmd) {
202  if (!@empty($this->s_tables[$cmd])) {
203  foreach ($this->s_tables[$cmd] as $key => $val) {
204  $content .= '<li class="success">';
205  $content .= ($cmd != 'insert')
206  ? sprintf($this->successStrings[$cmd], $key)
207  : sprintf($this->successStrings[$cmd], $val, $key);
208  $content .= "</li>\n";
209  }
210  }
211  }
212  foreach ($commands as $cmd) {
213  if (!@empty($this->f_tables[$cmd])) {
214  foreach ($this->f_tables[$cmd] as $key => $val) {
215  $content .= '<li class="failure">';
216  $content .= ($cmd != 'insert')
217  ? sprintf($this->failureStrings[$cmd], $key)
218  : sprintf($this->failureStrings[$cmd], $val, $key);
219  $content .= "</li>\n";
220  }
221  }
222  }
223  $content .= '</ul>';
224  return $content;
225  }
226 
234  public function query($sql)
235  {
236  $this->db->connect();
237  return $this->db->query($sql);
238  }
239 
247  public function prefix($table)
248  {
249  $this->db->connect();
250  return $this->db->prefix($table);
251  }
252 
260  public function fetchArray($ret)
261  {
262  $this->db->connect();
263  return $this->db->fetchArray($ret);
264  }
265 
274  public function insert($table, $query)
275  {
276  $this->db->connect();
277  $table = $this->db->prefix($table);
278  $query = 'INSERT INTO ' . $table . ' ' . $query;
279  if (!$this->db->queryF($query)) {
280  if (!isset($this->f_tables['insert'][$table])) {
281  $this->f_tables['insert'][$table] = 1;
282  } else {
283  $this->f_tables['insert'][$table]++;
284  }
285  return false;
286  } else {
287  if (!isset($this->s_tables['insert'][$table])) {
288  $this->s_tables['insert'][$table] = 1;
289  } else {
290  $this->s_tables['insert'][$table]++;
291  }
292  return $this->db->getInsertId();
293  }
294  }
295 
301  public function isError()
302  {
303  return (isset($this->f_tables)) ? true : false;
304  }
305 
316  public function deleteTables($tables)
317  {
318  $deleted = array();
319  $this->db->connect();
320  foreach ($tables as $key => $val) {
321  //was: if (!$this->db->query("DROP TABLE " . $this->db->prefix($key))) {
322  if (!$this->db->query("DROP TABLE " . $this->db->prefix($val))) {
323  $deleted[] = $val;
324  }
325  }
326  return $deleted;
327  }
328 
336  public function tableExists($table)
337  {
338  $table = trim($table);
339  $ret = false;
340  if ($table != '') {
341  $this->db->connect();
342  $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix($table);
343  $ret = (false != $this->db->query($sql)) ? true : false;
344  }
345  return $ret;
346  }
347 
359  public function copyFields(
360  $fieldsMap,
361  $oTableName,
362  $nTableName,
363  $dropTable = false
364  ) {
365  $sql = "SHOW COLUMNS FROM " . $this->db->prefix($oTableName);
366  $result = $this->db->queryF($sql);
367  if (($rows = $this->db->getRowsNum($result)) == count($fieldsMap)) {
368  $sql = "SELECT * FROM " . $this->db->prefix($oTableName);
369  $result = $this->db->queryF($sql);
370  while ($myrow = $this->db->fetchArray($result)) {
371  ksort($fieldsMap);
372  ksort($myrow);
373  $sql = "INSERT INTO `" . $this->db->prefix($nTableName)
374  . "` " . "(`" . implode("`,`", $fieldsMap) . "`)" .
375  " VALUES ('" . implode("','", $myrow) . "')";
376 
377  $this->db->queryF($sql);
378  }
379  if ($dropTable) {
380  $sql = "DROP TABLE " . $this->db->prefix($oTableName);
381  $this->db->queryF($sql);
382  }
383  }
384  }
385 }
static prefixQuery($query, $prefix)
Definition: sqlutility.php:126
static getInstance()
Definition: Xoops.php:160
const SF_TABLE_CREATED
Definition: en_US.php:995
$query
Definition: index.php:37
$result
Definition: pda.php:33
$xoops
Definition: admin.php:25
static splitMySqlFile(&$ret, $sql)
Definition: sqlutility.php:43
static fileExists($file)
Definition: xoopsload.php:506
const EF_TABLE_NOT_CREATED
Definition: en_US.php:270
const SF_TABLE_DROPPED
Definition: en_US.php:996
static get($name)
global $xoopsDB
Definition: common.php:36
$sql
Definition: pda.php:32
queryFromFile($sql_file_path, $force=false)
Definition: manager.php:120
copyFields($fieldsMap, $oTableName, $nTableName, $dropTable=false)
Definition: manager.php:359
deleteTables($tables)
Definition: manager.php:316
insert($table, $query)
Definition: manager.php:274
const EF_ENTRIES_NOT_INSERTED_TO_TABLE
Definition: en_US.php:237
const SF_TABLE_UPDATED
Definition: en_US.php:997
const EF_TABLE_NOT_UPDATED
Definition: en_US.php:273
const SF_ENTRIES_INSERTED_TO_TABLE
Definition: en_US.php:984
const EF_TABLE_NOT_DROPPED
Definition: en_US.php:272