database.lib.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Doctrine\Common\Annotations\AnnotationRegistry;
  4. use Doctrine\DBAL\Connection;
  5. use Doctrine\DBAL\Driver\Statement;
  6. use Doctrine\DBAL\Types\Type;
  7. use Doctrine\ORM\EntityManager;
  8. /**
  9. * Class Database
  10. */
  11. class Database
  12. {
  13. /**
  14. * @var EntityManager
  15. */
  16. private static $em;
  17. private static $connection;
  18. public static $utcDateTimeClass;
  19. /**
  20. * @param EntityManager $em
  21. */
  22. public function setManager($em)
  23. {
  24. self::$em = $em;
  25. }
  26. /**
  27. * @param Connection $connection
  28. */
  29. public function setConnection(Connection $connection)
  30. {
  31. self::$connection = $connection;
  32. }
  33. /**
  34. * @return Connection
  35. */
  36. public function getConnection()
  37. {
  38. return self::$connection;
  39. }
  40. /**
  41. * @return EntityManager
  42. */
  43. public static function getManager()
  44. {
  45. return self::$em;
  46. }
  47. /**
  48. * Returns the name of the main database.
  49. *
  50. * @return string
  51. */
  52. public static function get_main_database()
  53. {
  54. return self::getManager()->getConnection()->getDatabase();
  55. }
  56. /**
  57. * Get main table
  58. *
  59. * @param string $table
  60. *
  61. * @return string
  62. */
  63. public static function get_main_table($table)
  64. {
  65. return $table;
  66. }
  67. /**
  68. * Get course table
  69. *
  70. * @param string $table
  71. *
  72. * @return string
  73. */
  74. public static function get_course_table($table)
  75. {
  76. return DB_COURSE_PREFIX.$table;
  77. }
  78. /**
  79. * Counts the number of rows in a table
  80. * @param string $table The table of which the rows should be counted
  81. *
  82. * @return int The number of rows in the given table.
  83. * @deprecated
  84. */
  85. public static function count_rows($table)
  86. {
  87. $obj = self::fetch_object(self::query("SELECT COUNT(*) AS n FROM $table"));
  88. return $obj->n;
  89. }
  90. /**
  91. * Returns the number of affected rows in the last database operation.
  92. * @param Statement $result
  93. *
  94. * @return int
  95. */
  96. public static function affected_rows(Statement $result)
  97. {
  98. return $result->rowCount();
  99. }
  100. /**
  101. * @return string
  102. */
  103. public static function getUTCDateTimeTypeClass()
  104. {
  105. return isset(self::$utcDateTimeClass) ? self::$utcDateTimeClass : 'Application\DoctrineExtensions\DBAL\Types\UTCDateTimeType';
  106. }
  107. /**
  108. * Connect to the database sets the entity manager.
  109. *
  110. * @param array $params
  111. * @param string $sysPath
  112. * @param string $entityRootPath
  113. * @param bool $returnConnection
  114. * @param bool $returnManager
  115. *
  116. * @throws \Doctrine\ORM\ORMException
  117. *
  118. * @return
  119. */
  120. public function connect(
  121. $params = array(),
  122. $sysPath = '',
  123. $entityRootPath = '',
  124. $returnConnection = false,
  125. $returnManager = false
  126. ) {
  127. $config = self::getDoctrineConfig($entityRootPath);
  128. $config->setAutoGenerateProxyClasses(true);
  129. $config->setEntityNamespaces(
  130. array(
  131. 'ChamiloUserBundle' => 'Chamilo\UserBundle\Entity',
  132. 'ChamiloCoreBundle' => 'Chamilo\CoreBundle\Entity',
  133. 'ChamiloCourseBundle' => 'Chamilo\CourseBundle\Entity',
  134. 'ChamiloSkillBundle' => 'Chamilo\SkillBundle\Entity',
  135. 'ChamiloTicketBundle' => 'Chamilo\TicketBundle\Entity',
  136. 'ChamiloPluginBundle' => 'Chamilo\PluginBundle\Entity',
  137. 'ChamiloFaqBundle' => 'Chamilo\FaqBundle\Entity'
  138. )
  139. );
  140. $params['charset'] = 'utf8';
  141. $entityManager = EntityManager::create($params, $config);
  142. $sysPath = !empty($sysPath) ? $sysPath : api_get_path(SYS_PATH);
  143. // Registering Constraints
  144. AnnotationRegistry::registerAutoloadNamespace(
  145. 'Symfony\Component\Validator\Constraint',
  146. $sysPath."vendor/symfony/symfony/src"
  147. );
  148. AnnotationRegistry::registerFile(
  149. $sysPath."vendor/symfony/symfony/src/Symfony/Bridge/Doctrine/Validator/Constraints/UniqueEntity.php"
  150. );
  151. // Registering gedmo extensions
  152. AnnotationRegistry::registerAutoloadNamespace(
  153. 'Gedmo\Mapping\Annotation',
  154. $sysPath."vendor/gedmo/doctrine-extensions/lib"
  155. );
  156. Type::overrideType(
  157. Type::DATETIME,
  158. self::getUTCDateTimeTypeClass()
  159. );
  160. $listener = new \Gedmo\Timestampable\TimestampableListener();
  161. $entityManager->getEventManager()->addEventSubscriber($listener);
  162. $listener = new \Gedmo\Tree\TreeListener();
  163. $entityManager->getEventManager()->addEventSubscriber($listener);
  164. $listener = new \Gedmo\Sortable\SortableListener();
  165. $entityManager->getEventManager()->addEventSubscriber($listener);
  166. $connection = $entityManager->getConnection();
  167. $connection->executeQuery('SET sql_mode = "";');
  168. if ($returnConnection) {
  169. return $connection;
  170. }
  171. if ($returnManager) {
  172. return $entityManager;
  173. }
  174. $this->setConnection($connection);
  175. $this->setManager($entityManager);
  176. }
  177. /**
  178. * Escape MySQL wildchars _ and % in LIKE search
  179. * @param string $text The string to escape
  180. *
  181. * @return string The escaped string
  182. */
  183. public static function escape_sql_wildcards($text)
  184. {
  185. $text = api_preg_replace("/_/", "\_", $text);
  186. $text = api_preg_replace("/%/", "\%", $text);
  187. return $text;
  188. }
  189. /**
  190. * Escapes a string to insert into the database as text
  191. *
  192. * @param string $string
  193. *
  194. * @return string
  195. */
  196. public static function escape_string($string)
  197. {
  198. $string = self::getManager()->getConnection()->quote($string);
  199. return trim($string, "'");
  200. }
  201. /**
  202. * Gets the array from a SQL result (as returned by Database::query)
  203. *
  204. * @param Statement $result
  205. * @param string $option Optional: "ASSOC","NUM" or "BOTH"
  206. *
  207. * @return array|mixed
  208. */
  209. public static function fetch_array(Statement $result, $option = 'BOTH')
  210. {
  211. if ($result === false) {
  212. return array();
  213. }
  214. return $result->fetch(self::customOptionToDoctrineOption($option));
  215. }
  216. /**
  217. * Gets an associative array from a SQL result (as returned by Database::query).
  218. *
  219. * @param Statement $result
  220. *
  221. * @return array
  222. */
  223. public static function fetch_assoc(Statement $result)
  224. {
  225. return $result->fetch(PDO::FETCH_ASSOC);
  226. }
  227. /**
  228. * Gets the next row of the result of the SQL query
  229. * (as returned by Database::query) in an object form
  230. *
  231. * @param Statement $result
  232. *
  233. * @return mixed
  234. */
  235. public static function fetch_object(Statement $result)
  236. {
  237. return $result->fetch(PDO::FETCH_OBJ);
  238. }
  239. /**
  240. * Gets the array from a SQL result (as returned by Database::query)
  241. * help achieving database independence
  242. *
  243. * @param Statement $result
  244. *
  245. * @return mixed
  246. */
  247. public static function fetch_row(Statement $result)
  248. {
  249. if ($result === false) {
  250. return array();
  251. }
  252. return $result->fetch(PDO::FETCH_NUM);
  253. }
  254. /**
  255. * Frees all the memory associated with the provided result identifier.
  256. * @return boolean|null Returns TRUE on success or FALSE on failure.
  257. * Notes: Use this method if you are concerned about how much memory is being used for queries that return large result sets.
  258. * Anyway, all associated result memory is automatically freed at the end of the script's execution.
  259. */
  260. public static function free_result(Statement $result)
  261. {
  262. $result->closeCursor();
  263. }
  264. /**
  265. * Gets the ID of the last item inserted into the database
  266. *
  267. * @return string
  268. */
  269. public static function insert_id()
  270. {
  271. return self::getManager()->getConnection()->lastInsertId();
  272. }
  273. /**
  274. * @param Statement $result
  275. *
  276. * @return int
  277. */
  278. public static function num_rows(Statement $result)
  279. {
  280. if ($result === false) {
  281. return 0;
  282. }
  283. return $result->rowCount();
  284. }
  285. /**
  286. * Acts as the relative *_result() function of most DB drivers and fetches a
  287. * specific line and a field
  288. *
  289. * @param Statement $resource
  290. * @param int $row
  291. * @param string $field
  292. *
  293. * @return mixed
  294. */
  295. public static function result(Statement $resource, $row, $field = '')
  296. {
  297. if ($resource->rowCount() > 0) {
  298. $result = $resource->fetchAll(PDO::FETCH_BOTH);
  299. return $result[$row][$field];
  300. }
  301. }
  302. /**
  303. * @param string $query
  304. *
  305. * @return Statement
  306. *
  307. * @throws \Doctrine\DBAL\DBALException
  308. */
  309. public static function query($query)
  310. {
  311. $connection = self::getManager()->getConnection();
  312. if (api_get_setting('server_type') == 'test') {
  313. $result = $connection->executeQuery($query);
  314. } else {
  315. try {
  316. $result = $connection->executeQuery($query);
  317. } catch (Exception $e) {
  318. error_log($e->getMessage());
  319. api_not_allowed(false, get_lang('GeneralError'));
  320. }
  321. }
  322. return $result;
  323. }
  324. /**
  325. * @param string $option
  326. *
  327. * @return int
  328. */
  329. public static function customOptionToDoctrineOption($option)
  330. {
  331. switch ($option) {
  332. case 'ASSOC':
  333. return PDO::FETCH_ASSOC;
  334. break;
  335. case 'NUM':
  336. return PDO::FETCH_NUM;
  337. break;
  338. case 'BOTH':
  339. default:
  340. return PDO::FETCH_BOTH;
  341. break;
  342. }
  343. }
  344. /**
  345. * Stores a query result into an array.
  346. *
  347. * @author Olivier Brouckaert
  348. * @param Statement $result - the return value of the query
  349. * @param string $option BOTH, ASSOC, or NUM
  350. *
  351. * @return array - the value returned by the query
  352. */
  353. public static function store_result(Statement $result, $option = 'BOTH')
  354. {
  355. return $result->fetchAll(self::customOptionToDoctrineOption($option));
  356. }
  357. /**
  358. * Database insert
  359. * @param string $table_name
  360. * @param array $attributes
  361. * @param bool $show_query
  362. *
  363. * @return false|string
  364. */
  365. public static function insert($table_name, $attributes, $show_query = false)
  366. {
  367. if (empty($attributes) || empty($table_name)) {
  368. return false;
  369. }
  370. $params = array_keys($attributes);
  371. if (!empty($params)) {
  372. $sql = 'INSERT INTO '.$table_name.' ('.implode(',', $params).')
  373. VALUES (:'.implode(', :', $params).')';
  374. $statement = self::getManager()->getConnection()->prepare($sql);
  375. $result = $statement->execute($attributes);
  376. if ($show_query) {
  377. var_dump($sql);
  378. error_log($sql);
  379. }
  380. if ($result) {
  381. return self::getManager()->getConnection()->lastInsertId();
  382. }
  383. }
  384. return false;
  385. }
  386. /**
  387. * @param string $tableName use Database::get_main_table
  388. * @param array $attributes Values to updates
  389. * Example: $params['name'] = 'Julio'; $params['lastname'] = 'Montoya';
  390. * @param array $whereConditions where conditions i.e array('id = ?' =>'4')
  391. * @param bool $showQuery
  392. *
  393. * @return bool|int
  394. */
  395. public static function update(
  396. $tableName,
  397. $attributes,
  398. $whereConditions = array(),
  399. $showQuery = false
  400. ) {
  401. if (!empty($tableName) && !empty($attributes)) {
  402. $updateSql = '';
  403. $count = 1;
  404. foreach ($attributes as $key => $value) {
  405. if ($showQuery) {
  406. echo $key.': '.$value.PHP_EOL;
  407. }
  408. $updateSql .= "$key = :$key ";
  409. if ($count < count($attributes)) {
  410. $updateSql .= ', ';
  411. }
  412. $count++;
  413. }
  414. if (!empty($updateSql)) {
  415. //Parsing and cleaning the where conditions
  416. $whereReturn = self::parse_where_conditions($whereConditions);
  417. $sql = "UPDATE $tableName SET $updateSql $whereReturn ";
  418. $statement = self::getManager()->getConnection()->prepare($sql);
  419. $result = $statement->execute($attributes);
  420. if ($showQuery) {
  421. var_dump($sql);
  422. var_dump($attributes);
  423. var_dump($whereConditions);
  424. }
  425. if ($result) {
  426. return $statement->rowCount();
  427. }
  428. }
  429. }
  430. return false;
  431. }
  432. /**
  433. * Experimental useful database finder
  434. * @todo lot of stuff to do here
  435. * @todo known issues, it doesn't work when using LIKE conditions
  436. * @example array('where'=> array('course_code LIKE "?%"'))
  437. * @example array('where'=> array('type = ? AND category = ?' => array('setting', 'Plugins'))
  438. * @example array('where'=> array('name = "Julio" AND lastname = "montoya"'))
  439. * @param array $columns
  440. * @param string $table_name
  441. * @param array $conditions
  442. * @param string $type_result
  443. * @param string $option
  444. * @return array
  445. */
  446. public static function select($columns, $table_name, $conditions = array(), $type_result = 'all', $option = 'ASSOC')
  447. {
  448. $conditions = self::parse_conditions($conditions);
  449. //@todo we could do a describe here to check the columns ...
  450. if (is_array($columns)) {
  451. $clean_columns = implode(',', $columns);
  452. } else {
  453. if ($columns == '*') {
  454. $clean_columns = '*';
  455. } else {
  456. $clean_columns = (string) $columns;
  457. }
  458. }
  459. $sql = "SELECT $clean_columns FROM $table_name $conditions";
  460. $result = self::query($sql);
  461. $array = array();
  462. if ($type_result === 'all') {
  463. while ($row = self::fetch_array($result, $option)) {
  464. if (isset($row['id'])) {
  465. $array[$row['id']] = $row;
  466. } else {
  467. $array[] = $row;
  468. }
  469. }
  470. } else {
  471. $array = self::fetch_array($result, $option);
  472. }
  473. return $array;
  474. }
  475. /**
  476. * Parses WHERE/ORDER conditions i.e array('where'=>array('id = ?' =>'4'), 'order'=>'id DESC')
  477. * @todo known issues, it doesn't work when using
  478. * LIKE conditions example: array('where'=>array('course_code LIKE "?%"'))
  479. * @param array $conditions
  480. * @return string Partial SQL string to add to longer query
  481. */
  482. public static function parse_conditions($conditions)
  483. {
  484. if (empty($conditions)) {
  485. return '';
  486. }
  487. $return_value = $where_return = '';
  488. foreach ($conditions as $type_condition => $condition_data) {
  489. if ($condition_data == false) {
  490. continue;
  491. }
  492. $type_condition = strtolower($type_condition);
  493. switch ($type_condition) {
  494. case 'where':
  495. foreach ($condition_data as $condition => $value_array) {
  496. if (is_array($value_array)) {
  497. $clean_values = array();
  498. foreach ($value_array as $item) {
  499. $item = self::escape_string($item);
  500. $clean_values[] = $item;
  501. }
  502. } else {
  503. $value_array = self::escape_string($value_array);
  504. $clean_values = $value_array;
  505. }
  506. if (!empty($condition) && $clean_values != '') {
  507. $condition = str_replace('%', "'@percentage@'", $condition); //replace "%"
  508. $condition = str_replace("'?'", "%s", $condition);
  509. $condition = str_replace("?", "%s", $condition);
  510. $condition = str_replace("@%s@", "@-@", $condition);
  511. $condition = str_replace("%s", "'%s'", $condition);
  512. $condition = str_replace("@-@", "@%s@", $condition);
  513. // Treat conditions as string
  514. $condition = vsprintf($condition, $clean_values);
  515. $condition = str_replace('@percentage@', '%', $condition); //replace "%"
  516. $where_return .= $condition;
  517. }
  518. }
  519. if (!empty($where_return)) {
  520. $return_value = " WHERE $where_return";
  521. }
  522. break;
  523. case 'order':
  524. $order_array = $condition_data;
  525. if (!empty($order_array)) {
  526. // 'order' => 'id desc, name desc'
  527. $order_array = self::escape_string($order_array, null, false);
  528. $new_order_array = explode(',', $order_array);
  529. $temp_value = array();
  530. foreach ($new_order_array as $element) {
  531. $element = explode(' ', $element);
  532. $element = array_filter($element);
  533. $element = array_values($element);
  534. if (!empty($element[1])) {
  535. $element[1] = strtolower($element[1]);
  536. $order = 'DESC';
  537. if (in_array($element[1], array('desc', 'asc'))) {
  538. $order = $element[1];
  539. }
  540. $temp_value[] = $element[0].' '.$order.' ';
  541. } else {
  542. //by default DESC
  543. $temp_value[] = $element[0].' DESC ';
  544. }
  545. }
  546. if (!empty($temp_value)) {
  547. $return_value .= ' ORDER BY '.implode(', ', $temp_value);
  548. }
  549. }
  550. break;
  551. case 'limit':
  552. $limit_array = explode(',', $condition_data);
  553. if (!empty($limit_array)) {
  554. if (count($limit_array) > 1) {
  555. $return_value .= ' LIMIT '.intval($limit_array[0]).' , '.intval($limit_array[1]);
  556. } else {
  557. $return_value .= ' LIMIT '.intval($limit_array[0]);
  558. }
  559. }
  560. break;
  561. }
  562. }
  563. return $return_value;
  564. }
  565. /**
  566. * @param array $conditions
  567. *
  568. * @return string
  569. */
  570. public static function parse_where_conditions($conditions)
  571. {
  572. return self::parse_conditions(array('where' => $conditions));
  573. }
  574. /**
  575. * @param string $table_name
  576. * @param array $where_conditions
  577. * @param bool $show_query
  578. *
  579. * @return int
  580. */
  581. public static function delete($table_name, $where_conditions, $show_query = false)
  582. {
  583. $where_return = self::parse_where_conditions($where_conditions);
  584. $sql = "DELETE FROM $table_name $where_return ";
  585. if ($show_query) { echo $sql; echo '<br />'; }
  586. $result = self::query($sql);
  587. $affected_rows = self::affected_rows($result);
  588. //@todo should return affected_rows for
  589. return $affected_rows;
  590. }
  591. /**
  592. * Get Doctrine configuration
  593. * @param string $path
  594. *
  595. * @return \Doctrine\ORM\Configuration
  596. */
  597. public static function getDoctrineConfig($path)
  598. {
  599. $isDevMode = false;
  600. $isSimpleMode = false; // related to annotations @Entity
  601. $cache = null;
  602. $path = !empty($path) ? $path : api_get_path(SYS_PATH);
  603. $paths = array(
  604. //$path.'src/Chamilo/ClassificationBundle/Entity',
  605. //$path.'src/Chamilo/MediaBundle/Entity',
  606. //$path.'src/Chamilo/PageBundle/Entity',
  607. $path.'src/Chamilo/CoreBundle/Entity',
  608. $path.'src/Chamilo/UserBundle/Entity',
  609. $path.'src/Chamilo/CourseBundle/Entity',
  610. $path.'src/Chamilo/TicketBundle/Entity',
  611. $path.'src/Chamilo/SkillBundle/Entity',
  612. $path.'src/Chamilo/PluginBundle/Entity',
  613. //$path.'vendor/sonata-project/user-bundle/Entity',
  614. //$path.'vendor/sonata-project/user-bundle/Model',
  615. //$path.'vendor/friendsofsymfony/user-bundle/FOS/UserBundle/Entity',
  616. );
  617. $proxyDir = $path.'app/cache/';
  618. $config = \Doctrine\ORM\Tools\Setup::createAnnotationMetadataConfiguration(
  619. $paths,
  620. $isDevMode,
  621. $proxyDir,
  622. $cache,
  623. $isSimpleMode
  624. );
  625. return $config;
  626. }
  627. /**
  628. * @param string $table
  629. *
  630. * @return bool
  631. */
  632. public static function tableExists($table)
  633. {
  634. return self::getManager()->getConnection()->getSchemaManager()->tablesExist($table);
  635. }
  636. /**
  637. * @param string $table
  638. * @return \Doctrine\DBAL\Schema\Column[]
  639. */
  640. public static function listTableColumns($table)
  641. {
  642. return self::getManager()->getConnection()->getSchemaManager()->listTableColumns($table);
  643. }
  644. }