PageRenderTime 72ms CodeModel.GetById 23ms RepoModel.GetById 9ms app.codeStats 0ms

/typo3/sysext/extbase/Classes/Persistence/Storage/Typo3DbBackend.php

https://github.com/andreaswolf/typo3-tceforms
PHP | 1047 lines | 683 code | 60 blank | 304 comment | 132 complexity | d465db1f3a3d2bb2bbc0bfd1cd94efad MD5 | raw file
Possible License(s): Apache-2.0, BSD-2-Clause, LGPL-3.0
  1. <?php
  2. /***************************************************************
  3. * Copyright notice
  4. *
  5. * (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
  6. * All rights reserved
  7. *
  8. * This class is a backport of the corresponding class of FLOW3.
  9. * All credits go to the v5 team.
  10. *
  11. * This script is part of the TYPO3 project. The TYPO3 project is
  12. * free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * The GNU General Public License can be found at
  18. * http://www.gnu.org/copyleft/gpl.html.
  19. *
  20. * This script is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * This copyright notice MUST APPEAR in all copies of the script!
  26. ***************************************************************/
  27. /**
  28. * A Storage backend
  29. *
  30. * @package Extbase
  31. * @subpackage Persistence\Storage
  32. * @version $Id: Typo3DbBackend.php 2297 2010-05-25 15:52:18Z jocrau $
  33. */
  34. class Tx_Extbase_Persistence_Storage_Typo3DbBackend implements Tx_Extbase_Persistence_Storage_BackendInterface, t3lib_Singleton {
  35. const OPERATOR_EQUAL_TO_NULL = 'operatorEqualToNull';
  36. const OPERATOR_NOT_EQUAL_TO_NULL = 'operatorNotEqualToNull';
  37. /**
  38. * The TYPO3 database object
  39. *
  40. * @var t3lib_db
  41. */
  42. protected $databaseHandle;
  43. /**
  44. * @var Tx_Extbase_Persistence_DataMapper
  45. */
  46. protected $dataMapper;
  47. /**
  48. * The TYPO3 page select object. Used for language and workspace overlay
  49. *
  50. * @var t3lib_pageSelect
  51. */
  52. protected $pageSelectObject;
  53. /**
  54. * A first-level TypoScript configuration cache
  55. *
  56. * @var array
  57. */
  58. protected $pageTSConfigCache = array();
  59. /**
  60. * Caches information about tables (esp. the existing column names)
  61. *
  62. * @var array
  63. */
  64. protected $tableInformationCache = array();
  65. /**
  66. * @var Tx_Extbase_Configuration_ConfigurationManagerInterface
  67. */
  68. protected $configurationManager;
  69. /**
  70. * Constructor. takes the database handle from $GLOBALS['TYPO3_DB']
  71. */
  72. public function __construct() {
  73. $this->databaseHandle = $GLOBALS['TYPO3_DB'];
  74. }
  75. /**
  76. * @param Tx_Extbase_Configuration_ConfigurationManagerInterface $configurationManager
  77. * @return void
  78. */
  79. public function injectConfigurationManager(Tx_Extbase_Configuration_ConfigurationManagerInterface $configurationManager) {
  80. $this->configurationManager = $configurationManager;
  81. }
  82. /**
  83. * Injects the DataMapper to map nodes to objects
  84. *
  85. * @param Tx_Extbase_Persistence_Mapper_DataMapper $dataMapper
  86. * @return void
  87. */
  88. public function injectDataMapper(Tx_Extbase_Persistence_Mapper_DataMapper $dataMapper) {
  89. $this->dataMapper = $dataMapper;
  90. }
  91. /**
  92. * Adds a row to the storage
  93. *
  94. * @param string $tableName The database table name
  95. * @param array $row The row to be inserted
  96. * @param boolean $isRelation TRUE if we are currently inserting into a relation table, FALSE by default
  97. * @return int The uid of the inserted row
  98. */
  99. public function addRow($tableName, array $row, $isRelation = FALSE) {
  100. $fields = array();
  101. $values = array();
  102. $parameters = array();
  103. if (isset($row['uid'])) {
  104. unset($row['uid']);
  105. }
  106. foreach ($row as $columnName => $value) {
  107. $fields[] = $columnName;
  108. $values[] = '?';
  109. $parameters[] = $value;
  110. }
  111. $sqlString = 'INSERT INTO ' . $tableName . ' (' . implode(', ', $fields) . ') VALUES (' . implode(', ', $values) . ')';
  112. $this->replacePlaceholders($sqlString, $parameters);
  113. // debug($sqlString,-2);
  114. $this->databaseHandle->sql_query($sqlString);
  115. $this->checkSqlErrors($sqlString);
  116. $uid = $this->databaseHandle->sql_insert_id();
  117. if (!$isRelation) {
  118. $this->clearPageCache($tableName, $uid);
  119. }
  120. return (int)$uid;
  121. }
  122. /**
  123. * Updates a row in the storage
  124. *
  125. * @param string $tableName The database table name
  126. * @param array $row The row to be updated
  127. * @param boolean $isRelation TRUE if we are currently inserting into a relation table, FALSE by default
  128. * @return void
  129. */
  130. public function updateRow($tableName, array $row, $isRelation = FALSE) {
  131. if (!isset($row['uid'])) throw new InvalidArgumentException('The given row must contain a value for "uid".');
  132. $uid = (int)$row['uid'];
  133. unset($row['uid']);
  134. $fields = array();
  135. $parameters = array();
  136. foreach ($row as $columnName => $value) {
  137. $fields[] = $columnName . '=?';
  138. $parameters[] = $value;
  139. }
  140. $parameters[] = $uid;
  141. $sqlString = 'UPDATE ' . $tableName . ' SET ' . implode(', ', $fields) . ' WHERE uid=?';
  142. $this->replacePlaceholders($sqlString, $parameters);
  143. // debug($sqlString,-2);
  144. $returnValue = $this->databaseHandle->sql_query($sqlString);
  145. $this->checkSqlErrors($sqlString);
  146. if (!$isRelation) {
  147. $this->clearPageCache($tableName, $uid);
  148. }
  149. return $returnValue;
  150. }
  151. /**
  152. * Deletes a row in the storage
  153. *
  154. * @param string $tableName The database table name
  155. * @param array $identifier An array of identifier array('fieldname' => value). This array will be transformed to a WHERE clause
  156. * @param boolean $isRelation TRUE if we are currently manipulating a relation table, FALSE by default
  157. * @return void
  158. */
  159. public function removeRow($tableName, array $identifier, $isRelation = FALSE) {
  160. $statement = 'DELETE FROM ' . $tableName . ' WHERE ' . $this->parseIdentifier($identifier);
  161. $this->replacePlaceholders($statement, $identifier);
  162. if (!$isRelation && isset($identifier['uid'])) {
  163. $this->clearPageCache($tableName, $identifier['uid'], $isRelation);
  164. }
  165. // debug($statement, -2);
  166. $returnValue = $this->databaseHandle->sql_query($statement);
  167. $this->checkSqlErrors($statement);
  168. return $returnValue;
  169. }
  170. /**
  171. * Fetches row data from the database
  172. *
  173. * @param string $identifier The Identifier of the row to fetch
  174. * @param Tx_Extbase_Persistence_Mapper_DataMap $dataMap The Data Map
  175. * @return array|FALSE
  176. */
  177. public function getRowByIdentifier($tableName, array $identifier) {
  178. $statement = 'SELECT * FROM ' . $tableName . ' WHERE ' . $this->parseIdentifier($identifier);
  179. $this->replacePlaceholders($statement, $identifier);
  180. // debug($statement,-2);
  181. $res = $this->databaseHandle->sql_query($statement);
  182. $this->checkSqlErrors($statement);
  183. $row = $this->databaseHandle->sql_fetch_assoc($res);
  184. if ($row !== FALSE) {
  185. return $row;
  186. } else {
  187. return FALSE;
  188. }
  189. }
  190. protected function parseIdentifier(array $identifier) {
  191. $fieldNames = array_keys($identifier);
  192. $suffixedFieldNames = array();
  193. foreach ($fieldNames as $fieldName) {
  194. $suffixedFieldNames[] = $fieldName . '=?';
  195. }
  196. return implode(' AND ', $suffixedFieldNames);
  197. }
  198. /**
  199. * Returns the object data matching the $query.
  200. *
  201. * @param Tx_Extbase_Persistence_QueryInterface $query
  202. * @return array
  203. * @author Karsten Dambekalns <karsten@typo3.org>
  204. */
  205. public function getObjectDataByQuery(Tx_Extbase_Persistence_QueryInterface $query) {
  206. $parameters = array();
  207. $statement = $query->getStatement();
  208. if($statement instanceof Tx_Extbase_Persistence_QOM_Statement) {
  209. $sql = $statement->getStatement();
  210. $parameters = $statement->getBoundVariables();
  211. } else {
  212. $parameters = array();
  213. $statementParts = $this->parseQuery($query, $parameters);
  214. $sql = $this->buildQuery($statementParts, $parameters);
  215. }
  216. $this->replacePlaceholders($sql, $parameters);
  217. // debug($sql,-2);
  218. $result = $this->databaseHandle->sql_query($sql);
  219. $this->checkSqlErrors($sql);
  220. $rows = $this->getRowsFromResult($query->getSource(), $result);
  221. $this->databaseHandle->sql_free_result($result);
  222. $rows = $this->doLanguageAndWorkspaceOverlay($query->getSource(), $rows);
  223. // TODO: implement $objectData = $this->processObjectRecords($statementHandle);
  224. return $rows;
  225. }
  226. /**
  227. * Returns the number of tuples matching the query.
  228. *
  229. * @param Tx_Extbase_Persistence_QOM_QueryObjectModelInterface $query
  230. * @return integer The number of matching tuples
  231. */
  232. public function getObjectCountByQuery(Tx_Extbase_Persistence_QueryInterface $query) {
  233. $constraint = $query->getConstraint();
  234. if($constraint instanceof Tx_Extbase_Persistence_QOM_StatementInterface) {
  235. throw new Tx_Extbase_Persistence_Storage_Exception_BadConstraint('Could not execute count on queries with a constraint of type Tx_Extbase_Persistence_QOM_StatementInterface', 1256661045);
  236. }
  237. $parameters = array();
  238. $statementParts = $this->parseQuery($query, $parameters);
  239. // if limit is set, we need to count the rows "manually" as COUNT(*) ignores LIMIT constraints
  240. if (!empty($statementParts['limit'])) {
  241. $statement = $this->buildQuery($statementParts, $parameters);
  242. $this->replacePlaceholders($statement, $parameters);
  243. $result = $this->databaseHandle->sql_query($statement);
  244. $this->checkSqlErrors($statement);
  245. $count = $this->databaseHandle->sql_num_rows($result);
  246. } else {
  247. $statementParts['fields'] = array('COUNT(*)');
  248. $statement = $this->buildQuery($statementParts, $parameters);
  249. $this->replacePlaceholders($statement, $parameters);
  250. $result = $this->databaseHandle->sql_query($statement);
  251. $this->checkSqlErrors($statement);
  252. $rows = $this->getRowsFromResult($query->getSource(), $result);
  253. $count = current(current($rows));
  254. }
  255. $this->databaseHandle->sql_free_result($result);
  256. return $count;
  257. }
  258. /**
  259. * Parses the query and returns the SQL statement parts.
  260. *
  261. * @param Tx_Extbase_Persistence_QueryInterface $query The query
  262. * @return array The SQL statement parts
  263. */
  264. public function parseQuery(Tx_Extbase_Persistence_QueryInterface $query, array &$parameters) {
  265. $sql = array();
  266. $sql['keywords'] = array();
  267. $sql['tables'] = array();
  268. $sql['unions'] = array();
  269. $sql['fields'] = array();
  270. $sql['where'] = array();
  271. $sql['additionalWhereClause'] = array();
  272. $sql['orderings'] = array();
  273. $sql['limit'] = array();
  274. $source = $query->getSource();
  275. $this->parseSource($source, $sql, $parameters);
  276. $this->parseConstraint($query->getConstraint(), $source, $sql, $parameters);
  277. $this->parseOrderings($query->getOrderings(), $source, $sql);
  278. $this->parseLimitAndOffset($query->getLimit(), $query->getOffset(), $sql);
  279. $tableNames = array_unique(array_keys($sql['tables'] + $sql['unions']));
  280. foreach ($tableNames as $tableName) {
  281. if (is_string($tableName) && strlen($tableName) > 0) {
  282. $this->addAdditionalWhereClause($query->getQuerySettings(), $tableName, $sql);
  283. }
  284. }
  285. return $sql;
  286. }
  287. /**
  288. * Returns the statement, ready to be executed.
  289. *
  290. * @param array $sql The SQL statement parts
  291. * @return string The SQL statement
  292. */
  293. public function buildQuery(array $sql) {
  294. $statement = 'SELECT ' . implode(' ', $sql['keywords']) . ' '. implode(',', $sql['fields']) . ' FROM ' . implode(' ', $sql['tables']) . ' '. implode(' ', $sql['unions']);
  295. if (!empty($sql['where'])) {
  296. $statement .= ' WHERE ' . implode('', $sql['where']);
  297. if (!empty($sql['additionalWhereClause'])) {
  298. $statement .= ' AND ' . implode(' AND ', $sql['additionalWhereClause']);
  299. }
  300. } elseif (!empty($sql['additionalWhereClause'])) {
  301. $statement .= ' WHERE ' . implode(' AND ', $sql['additionalWhereClause']);
  302. }
  303. if (!empty($sql['orderings'])) {
  304. $statement .= ' ORDER BY ' . implode(', ', $sql['orderings']);
  305. }
  306. if (!empty($sql['limit'])) {
  307. $statement .= ' LIMIT ' . $sql['limit'];
  308. }
  309. return $statement;
  310. }
  311. /**
  312. * Checks if a Value Object equal to the given Object exists in the data base
  313. *
  314. * @param Tx_Extbase_DomainObject_AbstractValueObject $object The Value Object
  315. * @return array The matching uid
  316. */
  317. public function getUidOfAlreadyPersistedValueObject(Tx_Extbase_DomainObject_AbstractValueObject $object) {
  318. $fields = array();
  319. $parameters = array();
  320. $dataMap = $this->dataMapper->getDataMap(get_class($object));
  321. $properties = $object->_getProperties();
  322. foreach ($properties as $propertyName => $propertyValue) {
  323. // FIXME We couple the Backend to the Entity implementation (uid, isClone); changes there breaks this method
  324. if ($dataMap->isPersistableProperty($propertyName) && ($propertyName !== 'uid') && ($propertyName !== 'pid') && ($propertyName !== 'isClone')) {
  325. if ($propertyValue === NULL) {
  326. $fields[] = $dataMap->getColumnMap($propertyName)->getColumnName() . ' IS NULL';
  327. } else {
  328. $fields[] = $dataMap->getColumnMap($propertyName)->getColumnName() . '=?';
  329. $parameters[] = $this->getPlainValue($propertyValue);
  330. }
  331. }
  332. }
  333. $sql = array();
  334. $sql['additionalWhereClause'] = array();
  335. $tableName = $dataMap->getTableName();
  336. $this->addEnableFieldsStatement($tableName, $sql);
  337. $statement = 'SELECT * FROM ' . $tableName;
  338. $statement .= ' WHERE ' . implode(' AND ', $fields);
  339. if (!empty($sql['additionalWhereClause'])) {
  340. $statement .= ' AND ' . implode(' AND ', $sql['additionalWhereClause']);
  341. }
  342. $this->replacePlaceholders($statement, $parameters);
  343. // debug($statement,-2);
  344. $res = $this->databaseHandle->sql_query($statement);
  345. $this->checkSqlErrors($statement);
  346. $row = $this->databaseHandle->sql_fetch_assoc($res);
  347. if ($row !== FALSE) {
  348. return (int)$row['uid'];
  349. } else {
  350. return FALSE;
  351. }
  352. }
  353. /**
  354. * Transforms a Query Source into SQL and parameter arrays
  355. *
  356. * @param Tx_Extbase_Persistence_QOM_SourceInterface $source The source
  357. * @param array &$sql
  358. * @param array &$parameters
  359. * @return void
  360. */
  361. protected function parseSource(Tx_Extbase_Persistence_QOM_SourceInterface $source, array &$sql) {
  362. if ($source instanceof Tx_Extbase_Persistence_QOM_SelectorInterface) {
  363. $className = $source->getNodeTypeName();
  364. $tableName = $this->dataMapper->getDataMap($className)->getTableName();
  365. $this->addRecordTypeConstraint($className, $sql);
  366. $sql['fields'][$tableName] = $tableName . '.*';
  367. $sql['tables'][$tableName] = $tableName;
  368. } elseif ($source instanceof Tx_Extbase_Persistence_QOM_JoinInterface) {
  369. $this->parseJoin($source, $sql);
  370. }
  371. }
  372. /**
  373. * Adda a constrint to ensure that the record type of the returned tuples is matching the data type of the repository.
  374. *
  375. * @param string $className The class name
  376. * @param array &$sql The query parts
  377. * @return void
  378. */
  379. protected function addRecordTypeConstraint($className, &$sql) {
  380. if ($className !== NULL) {
  381. $dataMap = $this->dataMapper->getDataMap($className);
  382. if ($dataMap->getRecordTypeColumnName() !== NULL) {
  383. $recordTypes = array();
  384. if ($dataMap->getRecordType() !== NULL) {
  385. $recordTypes[] = $dataMap->getRecordType();
  386. }
  387. foreach ($dataMap->getSubclasses() as $subclassName) {
  388. $subclassDataMap = $this->dataMapper->getDataMap($subclassName);
  389. if ($subclassDataMap->getRecordType() !== NULL) {
  390. $recordTypes[] = $subclassDataMap->getRecordType();
  391. }
  392. }
  393. if (count($recordTypes) > 0) {
  394. $recordTypeStatements = array();
  395. foreach ($recordTypes as $recordType) {
  396. $recordTypeStatements[] = $dataMap->getTableName() . '.' . $dataMap->getRecordTypeColumnName() . '=' . $this->databaseHandle->fullQuoteStr($recordType, 'foo');
  397. }
  398. $sql['additionalWhereClause'][] = '(' . implode(' OR ', $recordTypeStatements) . ')';
  399. }
  400. }
  401. }
  402. }
  403. /**
  404. * Transforms a Join into SQL and parameter arrays
  405. *
  406. * @param Tx_Extbase_Persistence_QOM_JoinInterface $join The join
  407. * @param array &$sql The query parts
  408. * @return void
  409. */
  410. protected function parseJoin(Tx_Extbase_Persistence_QOM_JoinInterface $join, array &$sql) {
  411. $leftSource = $join->getLeft();
  412. $leftClassName = $leftSource->getNodeTypeName();
  413. $this->addRecordTypeConstraint($leftClassName, $sql);
  414. $leftTableName = $leftSource->getSelectorName();
  415. // $sql['fields'][$leftTableName] = $leftTableName . '.*';
  416. $rightSource = $join->getRight();
  417. if ($rightSource instanceof Tx_Extbase_Persistence_QOM_JoinInterface) {
  418. $rightClassName = $rightSource->getLeft()->getNodeTypeName();
  419. $rightTableName = $rightSource->getLeft()->getSelectorName();
  420. } else {
  421. $rightClassName = $rightSource->getNodeTypeName();
  422. $rightTableName = $rightSource->getSelectorName();
  423. $sql['fields'][$leftTableName] = $rightTableName . '.*';
  424. }
  425. $this->addRecordTypeConstraint($rightClassName, $sql);
  426. $sql['tables'][$leftTableName] = $leftTableName;
  427. $sql['unions'][$rightTableName] = 'LEFT JOIN ' . $rightTableName;
  428. $joinCondition = $join->getJoinCondition();
  429. if ($joinCondition instanceof Tx_Extbase_Persistence_QOM_EquiJoinCondition) {
  430. $column1Name = $this->dataMapper->convertPropertyNameToColumnName($joinCondition->getProperty1Name(), $leftClassName);
  431. $column2Name = $this->dataMapper->convertPropertyNameToColumnName($joinCondition->getProperty2Name(), $rightClassName);
  432. $sql['unions'][$rightTableName] .= ' ON ' . $joinCondition->getSelector1Name() . '.' . $column1Name . ' = ' . $joinCondition->getSelector2Name() . '.' . $column2Name;
  433. }
  434. if ($rightSource instanceof Tx_Extbase_Persistence_QOM_JoinInterface) {
  435. $this->parseJoin($rightSource, $sql);
  436. }
  437. }
  438. /**
  439. * Transforms a constraint into SQL and parameter arrays
  440. *
  441. * @param Tx_Extbase_Persistence_QOM_ConstraintInterface $constraint The constraint
  442. * @param Tx_Extbase_Persistence_QOM_SourceInterface $source The source
  443. * @param array &$sql The query parts
  444. * @param array &$parameters The parameters that will replace the markers
  445. * @param array $boundVariableValues The bound variables in the query (key) and their values (value)
  446. * @return void
  447. */
  448. protected function parseConstraint(Tx_Extbase_Persistence_QOM_ConstraintInterface $constraint = NULL, Tx_Extbase_Persistence_QOM_SourceInterface $source, array &$sql, array &$parameters) {
  449. if ($constraint instanceof Tx_Extbase_Persistence_QOM_AndInterface) {
  450. $sql['where'][] = '(';
  451. $this->parseConstraint($constraint->getConstraint1(), $source, $sql, $parameters);
  452. $sql['where'][] = ' AND ';
  453. $this->parseConstraint($constraint->getConstraint2(), $source, $sql, $parameters);
  454. $sql['where'][] = ')';
  455. } elseif ($constraint instanceof Tx_Extbase_Persistence_QOM_OrInterface) {
  456. $sql['where'][] = '(';
  457. $this->parseConstraint($constraint->getConstraint1(), $source, $sql, $parameters);
  458. $sql['where'][] = ' OR ';
  459. $this->parseConstraint($constraint->getConstraint2(), $source, $sql, $parameters);
  460. $sql['where'][] = ')';
  461. } elseif ($constraint instanceof Tx_Extbase_Persistence_QOM_NotInterface) {
  462. $sql['where'][] = 'NOT (';
  463. $this->parseConstraint($constraint->getConstraint(), $source, $sql, $parameters);
  464. $sql['where'][] = ')';
  465. } elseif ($constraint instanceof Tx_Extbase_Persistence_QOM_ComparisonInterface) {
  466. $this->parseComparison($constraint, $source, $sql, $parameters);
  467. }
  468. }
  469. /**
  470. * Parse a Comparison into SQL and parameter arrays.
  471. *
  472. * @param Tx_Extbase_Persistence_QOM_ComparisonInterface $comparison The comparison to parse
  473. * @param Tx_Extbase_Persistence_QOM_SourceInterface $source The source
  474. * @param array &$sql SQL query parts to add to
  475. * @param array &$parameters Parameters to bind to the SQL
  476. * @param array $boundVariableValues The bound variables in the query and their values
  477. * @return void
  478. */
  479. protected function parseComparison(Tx_Extbase_Persistence_QOM_ComparisonInterface $comparison, Tx_Extbase_Persistence_QOM_SourceInterface $source, array &$sql, array &$parameters) {
  480. $operand1 = $comparison->getOperand1();
  481. $operator = $comparison->getOperator();
  482. $operand2 = $comparison->getOperand2();
  483. if (($operator === Tx_Extbase_Persistence_QueryInterface::OPERATOR_EQUAL_TO) && (is_array($operand2) || ($operand2 instanceof ArrayAccess) || ($operand2 instanceof Traversable))) {
  484. // this else branch enables equals() to behave like in(). This behavior is deprecated and will be removed in future. Use in() instead.
  485. $operator = Tx_Extbase_Persistence_QueryInterface::OPERATOR_IN;
  486. }
  487. if ($operator === Tx_Extbase_Persistence_QueryInterface::OPERATOR_IN) {
  488. $items = array();
  489. $hasValue = FALSE;
  490. foreach ($operand2 as $value) {
  491. $value = $this->getPlainValue($value);
  492. if ($value !== NULL) {
  493. $items[] = $value;
  494. $hasValue = TRUE;
  495. }
  496. }
  497. if ($hasValue === FALSE) {
  498. $sql['where'][] = '1<>1';
  499. } else {
  500. $this->parseDynamicOperand($operand1, $operator, $source, $sql, $parameters, NULL, $operand2);
  501. $parameters[] = $items;
  502. }
  503. } elseif ($operator === Tx_Extbase_Persistence_QueryInterface::OPERATOR_CONTAINS) {
  504. if ($operand2 === NULL) {
  505. $sql['where'][] = '1<>1';
  506. } else {
  507. $className = $source->getNodeTypeName();
  508. $tableName = $this->dataMapper->convertClassNameToTableName($className);
  509. $propertyName = $operand1->getPropertyName();
  510. while (strpos($propertyName, '.') !== FALSE) {
  511. $this->addUnionStatement($className, $tableName, $propertyName, $sql);
  512. }
  513. $columnName = $this->dataMapper->convertPropertyNameToColumnName($propertyName, $className);
  514. $dataMap = $this->dataMapper->getDataMap($className);
  515. $columnMap = $dataMap->getColumnMap($propertyName);
  516. $typeOfRelation = $columnMap->getTypeOfRelation();
  517. if ($typeOfRelation === Tx_Extbase_Persistence_Mapper_ColumnMap::RELATION_HAS_AND_BELONGS_TO_MANY) {
  518. $relationTableName = $columnMap->getRelationTableName();
  519. $sql['where'][] = $tableName . '.uid IN (SELECT ' . $columnMap->getParentKeyFieldName() . ' FROM ' . $relationTableName . ' WHERE ' . $columnMap->getChildKeyFieldName() . '=' . $this->getPlainValue($operand2) . ')';
  520. } elseif ($typeOfRelation === Tx_Extbase_Persistence_Mapper_ColumnMap::RELATION_HAS_MANY) {
  521. $parentKeyFieldName = $columnMap->getParentKeyFieldName();
  522. if (isset($parentKeyFieldName)) {
  523. $childTableName = $columnMap->getChildTableName();
  524. $sql['where'][] = $tableName . '.uid=(SELECT ' . $childTableName . '.' . $parentKeyFieldName . ' FROM ' . $childTableName . ' WHERE ' . $childTableName . '.uid=' . $this->getPlainValue($operand2) . ')';
  525. } else {
  526. $statement = 'FIND_IN_SET(' . $this->getPlainValue($operand2) . ',' . $tableName . '.' . $columnName . ')';
  527. $sql['where'][] = $statement;
  528. }
  529. } else {
  530. throw new Tx_Extbase_Persistence_Exception_RepositoryException('Unsupported relation for contains().', 1267832524);
  531. }
  532. }
  533. } else {
  534. if ($operand2 === NULL) {
  535. if ($operator === Tx_Extbase_Persistence_QueryInterface::OPERATOR_EQUAL_TO) {
  536. $operator = self::OPERATOR_EQUAL_TO_NULL;
  537. } elseif ($operator === Tx_Extbase_Persistence_QueryInterface::OPERATOR_NOT_EQUAL_TO) {
  538. $operator = self::OPERATOR_NOT_EQUAL_TO_NULL;
  539. }
  540. }
  541. $this->parseDynamicOperand($operand1, $operator, $source, $sql, $parameters);
  542. $parameters[] = $this->getPlainValue($operand2);
  543. }
  544. }
  545. /**
  546. * Returns a plain value, i.e. objects are flattened out if possible.
  547. *
  548. * @param mixed $input
  549. * @return mixed
  550. */
  551. protected function getPlainValue($input) {
  552. if (is_array($input)) {
  553. throw new Tx_Extbase_Persistence_Exception_UnexpectedTypeException('An array could not be converted to a plain value.', 1274799932);
  554. }
  555. if ($input instanceof DateTime) {
  556. return $input->format('U');
  557. } elseif (is_object($input)) {
  558. if ($input instanceof Tx_Extbase_DomainObject_DomainObjectInterface) {
  559. return $input->getUid();
  560. } else {
  561. throw new Tx_Extbase_Persistence_Exception_UnexpectedTypeException('An object of class "' . get_class($input) . '" could not be converted to a plain value.', 1274799934);
  562. }
  563. } elseif (is_bool($input)) {
  564. return $input === TRUE ? 1 : 0;
  565. } else {
  566. return $input;
  567. }
  568. }
  569. /**
  570. * Parse a DynamicOperand into SQL and parameter arrays.
  571. *
  572. * @param Tx_Extbase_Persistence_QOM_DynamicOperandInterface $operand
  573. * @param string $operator One of the JCR_OPERATOR_* constants
  574. * @param Tx_Extbase_Persistence_QOM_SourceInterface $source The source
  575. * @param array &$sql The query parts
  576. * @param array &$parameters The parameters that will replace the markers
  577. * @param string $valueFunction an optional SQL function to apply to the operand value
  578. * @return void
  579. */
  580. protected function parseDynamicOperand(Tx_Extbase_Persistence_QOM_DynamicOperandInterface $operand, $operator, Tx_Extbase_Persistence_QOM_SourceInterface $source, array &$sql, array &$parameters, $valueFunction = NULL, $operand2 = NULL) {
  581. if ($operand instanceof Tx_Extbase_Persistence_QOM_LowerCaseInterface) {
  582. $this->parseDynamicOperand($operand->getOperand(), $operator, $source, $sql, $parameters, 'LOWER');
  583. } elseif ($operand instanceof Tx_Extbase_Persistence_QOM_UpperCaseInterface) {
  584. $this->parseDynamicOperand($operand->getOperand(), $operator, $source, $sql, $parameters, 'UPPER');
  585. } elseif ($operand instanceof Tx_Extbase_Persistence_QOM_PropertyValueInterface) {
  586. $propertyName = $operand->getPropertyName();
  587. if ($source instanceof Tx_Extbase_Persistence_QOM_SelectorInterface) { // FIXME Only necessary to differ from Join
  588. $className = $source->getNodeTypeName();
  589. $tableName = $this->dataMapper->convertClassNameToTableName($className);
  590. while (strpos($propertyName, '.') !== FALSE) {
  591. $this->addUnionStatement($className, $tableName, $propertyName, $sql);
  592. }
  593. } elseif ($source instanceof Tx_Extbase_Persistence_QOM_JoinInterface) {
  594. $tableName = $source->getJoinCondition()->getSelector1Name();
  595. }
  596. $columnName = $this->dataMapper->convertPropertyNameToColumnName($propertyName, $className);
  597. $operator = $this->resolveOperator($operator);
  598. if ($valueFunction === NULL) {
  599. $constraintSQL .= (!empty($tableName) ? $tableName . '.' : '') . $columnName . ' ' . $operator . ' ?';
  600. } else {
  601. $constraintSQL .= $valueFunction . '(' . (!empty($tableName) ? $tableName . '.' : '') . $columnName . ') ' . $operator . ' ?';
  602. }
  603. $sql['where'][] = $constraintSQL;
  604. }
  605. }
  606. protected function addUnionStatement(&$className, &$tableName, &$propertyPath, array &$sql) {
  607. $explodedPropertyPath = explode('.', $propertyPath, 2);
  608. $propertyName = $explodedPropertyPath[0];
  609. $columnName = $this->dataMapper->convertPropertyNameToColumnName($propertyName, $className);
  610. $tableName = $this->dataMapper->convertClassNameToTableName($className);
  611. $columnMap = $this->dataMapper->getDataMap($className)->getColumnMap($propertyName);
  612. $parentKeyFieldName = $columnMap->getParentKeyFieldName();
  613. $childTableName = $columnMap->getChildTableName();
  614. if ($columnMap->getTypeOfRelation() === Tx_Extbase_Persistence_Mapper_ColumnMap::RELATION_HAS_ONE) {
  615. if (isset($parentKeyFieldName)) {
  616. $sql['unions'][$childTableName] = 'LEFT JOIN ' . $childTableName . ' ON ' . $tableName . '.uid=' . $childTableName . '.' . $parentKeyFieldName;
  617. } else {
  618. $sql['unions'][$childTableName] = 'LEFT JOIN ' . $childTableName . ' ON ' . $tableName . '.' . $columnName . '=' . $childTableName . '.uid';
  619. }
  620. $className = $this->dataMapper->getType($className, $propertyName);
  621. } elseif ($columnMap->getTypeOfRelation() === Tx_Extbase_Persistence_Mapper_ColumnMap::RELATION_HAS_MANY) {
  622. if (isset($parentKeyFieldName)) {
  623. $sql['unions'][$childTableName] = 'LEFT JOIN ' . $childTableName . ' ON ' . $tableName . '.uid=' . $childTableName . '.' . $parentKeyFieldName;
  624. } else {
  625. $onStatement = '(FIND_IN_SET(' . $childTableName . '.uid, ' . $tableName . '.' . $columnName . '))';
  626. $sql['unions'][$childTableName] = 'LEFT JOIN ' . $childTableName . ' ON ' . $onStatement;
  627. }
  628. $className = $this->dataMapper->getType($className, $propertyName);
  629. } elseif ($columnMap->getTypeOfRelation() === Tx_Extbase_Persistence_Mapper_ColumnMap::RELATION_HAS_AND_BELONGS_TO_MANY) {
  630. $relationTableName = $columnMap->getRelationTableName();
  631. $sql['unions'][$relationTableName] = 'LEFT JOIN ' . $relationTableName . ' ON ' . $tableName . '.uid=' . $relationTableName . '.uid_local';
  632. $sql['unions'][$childTableName] = 'LEFT JOIN ' . $childTableName . ' ON ' . $relationTableName . '.uid_foreign=' . $childTableName . '.uid';
  633. $className = $this->dataMapper->getType($className, $propertyName);
  634. } else {
  635. throw new Tx_Extbase_Persistence_Exception('Could not determine type of relation.', 1252502725);
  636. }
  637. // TODO check if there is another solution for this
  638. $sql['keywords']['distinct'] = 'DISTINCT';
  639. $propertyPath = $explodedPropertyPath[1];
  640. $tableName = $childTableName;
  641. }
  642. /**
  643. * Returns the SQL operator for the given JCR operator type.
  644. *
  645. * @param string $operator One of the JCR_OPERATOR_* constants
  646. * @return string an SQL operator
  647. */
  648. protected function resolveOperator($operator) {
  649. switch ($operator) {
  650. case self::OPERATOR_EQUAL_TO_NULL:
  651. $operator = 'IS';
  652. break;
  653. case self::OPERATOR_NOT_EQUAL_TO_NULL:
  654. $operator = 'IS NOT';
  655. break;
  656. case Tx_Extbase_Persistence_QueryInterface::OPERATOR_IN:
  657. $operator = 'IN';
  658. break;
  659. case Tx_Extbase_Persistence_QueryInterface::OPERATOR_EQUAL_TO:
  660. $operator = '=';
  661. break;
  662. case Tx_Extbase_Persistence_QueryInterface::OPERATOR_NOT_EQUAL_TO:
  663. $operator = '!=';
  664. break;
  665. case Tx_Extbase_Persistence_QueryInterface::OPERATOR_LESS_THAN:
  666. $operator = '<';
  667. break;
  668. case Tx_Extbase_Persistence_QueryInterface::OPERATOR_LESS_THAN_OR_EQUAL_TO:
  669. $operator = '<=';
  670. break;
  671. case Tx_Extbase_Persistence_QueryInterface::OPERATOR_GREATER_THAN:
  672. $operator = '>';
  673. break;
  674. case Tx_Extbase_Persistence_QueryInterface::OPERATOR_GREATER_THAN_OR_EQUAL_TO:
  675. $operator = '>=';
  676. break;
  677. case Tx_Extbase_Persistence_QueryInterface::OPERATOR_LIKE:
  678. $operator = 'LIKE';
  679. break;
  680. default:
  681. throw new Tx_Extbase_Persistence_Exception('Unsupported operator encountered.', 1242816073);
  682. }
  683. return $operator;
  684. }
  685. /**
  686. * Replace query placeholders in a query part by the given
  687. * parameters.
  688. *
  689. * @param string $sqlString The query part with placeholders
  690. * @param array $parameters The parameters
  691. * @return string The query part with replaced placeholders
  692. */
  693. protected function replacePlaceholders(&$sqlString, array $parameters) {
  694. // TODO profile this method again
  695. if (substr_count($sqlString, '?') !== count($parameters)) throw new Tx_Extbase_Persistence_Exception('The number of question marks to replace must be equal to the number of parameters.', 1242816074);
  696. $offset = 0;
  697. foreach ($parameters as $parameter) {
  698. $markPosition = strpos($sqlString, '?', $offset);
  699. if ($markPosition !== FALSE) {
  700. if ($parameter === NULL) {
  701. $parameter = 'NULL';
  702. } elseif (is_array($parameter) || ($parameter instanceof ArrayAccess) || ($parameter instanceof Traversable)) {
  703. $items = array();
  704. foreach ($parameter as $item) {
  705. $items[] = $this->databaseHandle->fullQuoteStr($item, 'foo');
  706. }
  707. $parameter = '(' . implode(',', $items) . ')';
  708. } else {
  709. $parameter = $this->databaseHandle->fullQuoteStr($parameter, 'foo'); // FIXME This may not work with DBAL; check this
  710. }
  711. $sqlString = substr($sqlString, 0, $markPosition) . $parameter . substr($sqlString, $markPosition + 1);
  712. }
  713. $offset = $markPosition + strlen($parameter);
  714. }
  715. }
  716. /**
  717. * Adds additional WHERE statements according to the query settings.
  718. *
  719. * @param Tx_Extbase_Persistence_QuerySettingsInterface $querySettings The TYPO3 4.x specific query settings
  720. * @param string $tableName The table name to add the additional where clause for
  721. * @param string $sql
  722. * @return void
  723. */
  724. protected function addAdditionalWhereClause(Tx_Extbase_Persistence_QuerySettingsInterface $querySettings, $tableName, &$sql) {
  725. if ($querySettings instanceof Tx_Extbase_Persistence_Typo3QuerySettings) {
  726. if ($querySettings->getRespectEnableFields()) {
  727. $this->addEnableFieldsStatement($tableName, $sql);
  728. }
  729. if ($querySettings->getRespectSysLanguage()) {
  730. $this->addSysLanguageStatement($tableName, $sql);
  731. }
  732. if ($querySettings->getRespectStoragePage()) {
  733. $this->addPageIdStatement($tableName, $sql, $querySettings->getStoragePageIds());
  734. }
  735. }
  736. }
  737. /**
  738. * Builds the enable fields statement
  739. *
  740. * @param string $tableName The database table name
  741. * @param array &$sql The query parts
  742. * @return void
  743. */
  744. protected function addEnableFieldsStatement($tableName, array &$sql) {
  745. if (is_array($GLOBALS['TCA'][$tableName]['ctrl'])) {
  746. if (TYPO3_MODE === 'FE') {
  747. $statement = $GLOBALS['TSFE']->sys_page->enableFields($tableName);
  748. } else { // TYPO3_MODE === 'BE'
  749. $statement = t3lib_BEfunc::deleteClause($tableName);
  750. $statement .= t3lib_BEfunc::BEenableFields($tableName);
  751. }
  752. if(!empty($statement)) {
  753. $statement = substr($statement, 5);
  754. $sql['additionalWhereClause'][] = $statement;
  755. }
  756. }
  757. }
  758. /**
  759. * Builds the language field statement
  760. *
  761. * @param string $tableName The database table name
  762. * @param array &$sql The query parts
  763. * @return void
  764. */
  765. protected function addSysLanguageStatement($tableName, array &$sql) {
  766. if (is_array($GLOBALS['TCA'][$tableName]['ctrl'])) {
  767. if(isset($GLOBALS['TCA'][$tableName]['ctrl']['languageField']) && $GLOBALS['TCA'][$tableName]['ctrl']['languageField'] !== NULL) {
  768. $sql['additionalWhereClause'][] = $tableName . '.' . $GLOBALS['TCA'][$tableName]['ctrl']['languageField'] . ' IN (0,-1)';
  769. }
  770. }
  771. }
  772. /**
  773. * Builds the page ID checking statement
  774. *
  775. * @param string $tableName The database table name
  776. * @param array &$sql The query parts
  777. * @param array $storagePageIds list of storage page ids
  778. * @return void
  779. */
  780. protected function addPageIdStatement($tableName, array &$sql, array $storagePageIds) {
  781. if (empty($this->tableInformationCache[$tableName]['columnNames'])) {
  782. $this->tableInformationCache[$tableName]['columnNames'] = $this->databaseHandle->admin_get_fields($tableName);
  783. }
  784. if (is_array($GLOBALS['TCA'][$tableName]['ctrl']) && array_key_exists('pid', $this->tableInformationCache[$tableName]['columnNames'])) {
  785. $sql['additionalWhereClause'][] = $tableName . '.pid IN (' . implode(', ', $storagePageIds) . ')';
  786. }
  787. }
  788. /**
  789. * Transforms orderings into SQL.
  790. *
  791. * @param array $orderings An array of orderings (Tx_Extbase_Persistence_QOM_Ordering)
  792. * @param Tx_Extbase_Persistence_QOM_SourceInterface $source The source
  793. * @param array &$sql The query parts
  794. * @return void
  795. */
  796. protected function parseOrderings(array $orderings, Tx_Extbase_Persistence_QOM_SourceInterface $source, array &$sql) {
  797. foreach ($orderings as $propertyName => $order) {
  798. switch ($order) {
  799. case Tx_Extbase_Persistence_QOM_QueryObjectModelConstantsInterface::JCR_ORDER_ASCENDING: // Deprecated since Extbase 1.1
  800. case Tx_Extbase_Persistence_QueryInterface::ORDER_ASCENDING:
  801. $order = 'ASC';
  802. break;
  803. case Tx_Extbase_Persistence_QOM_QueryObjectModelConstantsInterface::JCR_ORDER_DESCENDING: // Deprecated since Extbase 1.1
  804. case Tx_Extbase_Persistence_QueryInterface::ORDER_DESCENDING:
  805. $order = 'DESC';
  806. break;
  807. default:
  808. throw new Tx_Extbase_Persistence_Exception_UnsupportedOrder('Unsupported order encountered.', 1242816074);
  809. }
  810. if ($source instanceof Tx_Extbase_Persistence_QOM_SelectorInterface) {
  811. $className = $source->getNodeTypeName();
  812. $tableName = $this->dataMapper->convertClassNameToTableName($className);
  813. while (strpos($propertyName, '.') !== FALSE) {
  814. $this->addUnionStatement($className, $tableName, $propertyName, $sql);
  815. }
  816. } elseif ($source instanceof Tx_Extbase_Persistence_QOM_JoinInterface) {
  817. $tableName = $source->getLeft()->getSelectorName();
  818. }
  819. $columnName = $this->dataMapper->convertPropertyNameToColumnName($propertyName, $className);
  820. if (strlen($tableName) > 0) {
  821. $sql['orderings'][] = $tableName . '.' . $columnName . ' ' . $order;
  822. } else {
  823. $sql['orderings'][] = $columnName . ' ' . $order;
  824. }
  825. }
  826. }
  827. /**
  828. * Transforms limit and offset into SQL
  829. *
  830. * @param int $limit
  831. * @param int $offset
  832. * @param array &$sql
  833. * @return void
  834. */
  835. protected function parseLimitAndOffset($limit, $offset, array &$sql) {
  836. if ($limit !== NULL && $offset !== NULL) {
  837. $sql['limit'] = $offset . ', ' . $limit;
  838. } elseif ($limit !== NULL) {
  839. $sql['limit'] = $limit;
  840. }
  841. }
  842. /**
  843. * Transforms a Resource from a database query to an array of rows.
  844. *
  845. * @param Tx_Extbase_Persistence_QOM_SourceInterface $source The source (selector od join)
  846. * @param resource $result The result
  847. * @return array The result as an array of rows (tuples)
  848. */
  849. protected function getRowsFromResult(Tx_Extbase_Persistence_QOM_SourceInterface $source, $result) {
  850. $rows = array();
  851. while ($row = $this->databaseHandle->sql_fetch_assoc($result)) {
  852. if (is_array($row)) {
  853. // TODO Check if this is necessary, maybe the last line is enough
  854. $arrayKeys = range(0, count($row));
  855. array_fill_keys($arrayKeys, $row);
  856. $rows[] = $row;
  857. }
  858. }
  859. return $rows;
  860. }
  861. /**
  862. * Performs workspace and language overlay on the given row array. The language and workspace id is automatically
  863. * detected (depending on FE or BE context). You can also explicitly set the language/workspace id.
  864. *
  865. * @param Tx_Extbase_Persistence_QOM_SourceInterface $source The source (selector od join)
  866. * @param array $row The row array (as reference)
  867. * @param string $languageUid The language id
  868. * @param string $workspaceUidUid The workspace id
  869. * @return void
  870. */
  871. protected function doLanguageAndWorkspaceOverlay(Tx_Extbase_Persistence_QOM_SourceInterface $source, array $rows, $languageUid = NULL, $workspaceUid = NULL) {
  872. $overlayedRows = array();
  873. foreach ($rows as $row) {
  874. if (!($this->pageSelectObject instanceof t3lib_pageSelect)) {
  875. if (TYPO3_MODE == 'FE') {
  876. if (is_object($GLOBALS['TSFE'])) {
  877. $this->pageSelectObject = $GLOBALS['TSFE']->sys_page;
  878. } else {
  879. $this->pageSelectObject = t3lib_div::makeInstance('t3lib_pageSelect');
  880. }
  881. } else {
  882. $this->pageSelectObject = t3lib_div::makeInstance( 't3lib_pageSelect' );
  883. }
  884. }
  885. if (is_object($GLOBALS['TSFE'])) {
  886. if ($languageUid === NULL) {
  887. $languageUid = $GLOBALS['TSFE']->sys_language_uid;
  888. $languageMode = $GLOBALS['TSFE']->sys_language_mode;
  889. }
  890. if ($workspaceUid !== NULL) {
  891. $this->pageSelectObject->versioningWorkspaceId = $workspaceUid;
  892. }
  893. } else {
  894. if ($languageUid === NULL) {
  895. $languageUid = intval(t3lib_div::_GP('L'));
  896. }
  897. if ($workspaceUid === NULL) {
  898. $workspaceUid = $GLOBALS['BE_USER']->workspace;
  899. }
  900. $this->pageSelectObject->versioningWorkspaceId = $workspaceUid;
  901. }
  902. if ($source instanceof Tx_Extbase_Persistence_QOM_SelectorInterface) {
  903. $tableName = $source->getSelectorName();
  904. } elseif ($source instanceof Tx_Extbase_Persistence_QOM_JoinInterface) {
  905. $tableName = $source->getRight()->getSelectorName();
  906. }
  907. $this->pageSelectObject->versionOL($tableName, $row, TRUE);
  908. if($tableName == 'pages') {
  909. $row = $this->pageSelectObject->getPageOverlay($row, $languageUid);
  910. } elseif(isset($GLOBALS['TCA'][$tableName]['ctrl']['languageField']) && $GLOBALS['TCA'][$tableName]['ctrl']['languageField'] !== '') {
  911. if (in_array($row[$GLOBALS['TCA'][$tableName]['ctrl']['languageField']], array(-1,0))) {
  912. $overlayMode = ($languageMode === 'strict') ? 'hideNonTranslated' : '';
  913. $row = $this->pageSelectObject->getRecordOverlay($tableName, $row, $languageUid, $overlayMode);
  914. }
  915. }
  916. if ($row !== NULL && is_array($row)) {
  917. $overlayedRows[] = $row;
  918. }
  919. }
  920. return $overlayedRows;
  921. }
  922. /**
  923. * Checks if there are SQL errors in the last query, and if yes, throw an exception.
  924. *
  925. * @return void
  926. * @param string $sql The SQL statement
  927. * @throws Tx_Extbase_Persistence_Storage_Exception_SqlError
  928. */
  929. protected function checkSqlErrors($sql='') {
  930. $error = $this->databaseHandle->sql_error();
  931. if ($error !== '') {
  932. $error .= $sql ? ': ' . $sql : '';
  933. throw new Tx_Extbase_Persistence_Storage_Exception_SqlError($error, 1247602160);
  934. }
  935. }
  936. /**
  937. * Clear the TYPO3 page cache for the given record.
  938. * If the record lies on a page, then we clear the cache of this page.
  939. * If the record has no PID column, we clear the cache of the current page as best-effort.
  940. *
  941. * Much of this functionality is taken from t3lib_tcemain::clear_cache() which unfortunately only works with logged-in BE user.
  942. *
  943. * @param $tableName Table name of the record
  944. * @param $uid UID of the record
  945. * @return void
  946. */
  947. protected function clearPageCache($tableName, $uid) {
  948. $frameworkConfiguration = $this->configurationManager->getConfiguration(Tx_Extbase_Configuration_ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
  949. if (isset($frameworkConfiguration['persistence']['enableAutomaticCacheClearing']) && $frameworkConfiguration['persistence']['enableAutomaticCacheClearing'] === '1') {
  950. } else {
  951. // if disabled, return
  952. return;
  953. }
  954. $pageIdsToClear = array();
  955. $storagePage = NULL;
  956. $columns = $this->databaseHandle->admin_get_fields($tableName);
  957. if (array_key_exists('pid', $columns)) {
  958. $result = $this->databaseHandle->exec_SELECTquery('pid', $tableName, 'uid='.intval($uid));
  959. if ($row = $this->databaseHandle->sql_fetch_assoc($result)) {
  960. $storagePage = $row['pid'];
  961. $pageIdsToClear[] = $storagePage;
  962. }
  963. } elseif (isset($GLOBALS['TSFE'])) {
  964. // No PID column - we can do a best-effort to clear the cache of the current page if in FE
  965. $storagePage = $GLOBALS['TSFE']->id;
  966. $pageIdsToClear[] = $storagePage;
  967. }
  968. if ($storagePage === NULL) {
  969. return;
  970. }
  971. if (!isset($this->pageTSConfigCache[$storagePage])) {
  972. $this->pageTSConfigCache[$storagePage] = t3lib_BEfunc::getPagesTSconfig($storagePage);
  973. }
  974. if (isset($this->pageTSConfigCache[$storagePage]['TCEMAIN.']['clearCacheCmd'])) {
  975. $clearCacheCommands = t3lib_div::trimExplode(',',strtolower($this->pageTSConfigCache[$storagePage]['TCEMAIN.']['clearCacheCmd']),1);
  976. $clearCacheCommands = array_unique($clearCacheCommands);
  977. foreach ($clearCacheCommands as $clearCacheCommand) {
  978. if (t3lib_div::testInt($clearCacheCommand)) {
  979. $pageIdsToClear[] = $clearCacheCommand;
  980. }
  981. }
  982. }
  983. // TODO check if we can hand this over to the Dispatcher to clear the page only once, this will save around 10% time while inserting and updating
  984. Tx_Extbase_Utility_Cache::clearPageCache($pageIdsToClear);
  985. }
  986. }
  987. ?>