BxDolWikiQuery.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaCore UNA Core
  7. * @{
  8. */
  9. /**
  10. * Database queries for WIKI objects.
  11. * @see BxDolWiki
  12. */
  13. class BxDolWikiQuery extends BxDolDb
  14. {
  15. protected $_aObject;
  16. protected $_sTableBlocksWithRevisions = 'sys_pages_wiki_blocks';
  17. public function __construct($aObject)
  18. {
  19. parent::__construct();
  20. $this->_aObject = $aObject;
  21. }
  22. static public function getBlocks ($sModule)
  23. {
  24. $oDb = BxDolDb::getInstance();
  25. return $oDb->getColumn("SELECT `id` FROM `sys_pages_blocks` WHERE `module` = :module", ['module' => $sModule]);
  26. }
  27. static public function getWikiObjectByUri ($sUri)
  28. {
  29. return self::getWikiObjectByField ('uri', $sUri);
  30. }
  31. static public function getWikiObject ($sObject)
  32. {
  33. return self::getWikiObjectByField ('object', $sObject);
  34. }
  35. static public function getWikiObjectByField ($sField, $sValue)
  36. {
  37. $oDb = BxDolDb::getInstance();
  38. $sQuery = $oDb->prepare("SELECT * FROM `sys_objects_wiki` WHERE `$sField` = ?", $sValue);
  39. $aObject = $oDb->getRow($sQuery);
  40. if (!$aObject || !is_array($aObject))
  41. return false;
  42. return $aObject;
  43. }
  44. static public function getAllPages ()
  45. {
  46. $sQuery = "SELECT LOWER(`uri`) as `uri`, `title`, `url` FROM `sys_objects_page` WHERE `url` != '' ORDER BY `uri`";
  47. return BxDolDb::getInstance()->getAllWithKey($sQuery, 'uri');
  48. }
  49. /**
  50. * Get wiki block
  51. * @param $iBlockId block ID
  52. * @param $sLang 2 letters language code
  53. * @param $iRevision [optional, default false] revision number, if false get latest revision for then given lan
  54. * @param $bAutoMainLang [optional, default true] automatically load wiki block for main language if translation for gived language doesn't exist
  55. * @return array with wiki block info
  56. */
  57. public function getBlockContent ($iBlockId, $sLang, $iRevision = false, $bAutoMainLang = true)
  58. {
  59. $sWhere = '';
  60. $aRow = false;
  61. $aBind = array('block' => $iBlockId);
  62. if (false !== $sLang) {
  63. $aBind['language'] = $sLang;
  64. if (false !== $iRevision) {
  65. $sWhere = " AND `revision` = :rev";
  66. $aBind['rev'] = $iRevision;
  67. }
  68. // get latest revision for specific language
  69. $aRow = $this->getRow("SELECT `block_id`, `revision`, `profile_id`, `language`, `main_lang`, `content`, `unsafe`, `notes`, `added` FROM `{$this->_sTableBlocksWithRevisions}` WHERE `block_id` = :block AND `language` = :language $sWhere ORDER BY `revision` DESC LIMIT 1", $aBind);
  70. }
  71. // if translation isn't found for specific language then get latest revision for main language
  72. if ($bAutoMainLang && !$aRow) {
  73. unset($aBind['language']);
  74. $aRow = $this->getRow("SELECT `block_id`, `revision`, `profile_id`, `language`, `main_lang`, `content`, `unsafe`, `notes`, `added` FROM `{$this->_sTableBlocksWithRevisions}` WHERE `block_id` = :block AND `main_lang` = 1 $sWhere ORDER BY `revision` DESC LIMIT 1", $aBind);
  75. }
  76. return $aRow ? $aRow : false;
  77. }
  78. public function getBlockLangs ($iBlockId)
  79. {
  80. return $this->getColumn("SELECT `language` FROM `{$this->_sTableBlocksWithRevisions}` WHERE `block_id` = :block GROUP BY `language`", array('block' => $iBlockId));
  81. }
  82. public function updateBlockIndexingData ($iBlockId, $sText)
  83. {
  84. return $this->query("UPDATE `sys_pages_blocks` SET `text` = :text, `text_updated` = :updated WHERE `id` = :id", array('text' => $sText, 'updated' => time(), 'id' => $iBlockId));
  85. }
  86. public function getBlockHistory ($iBlockId, $sLang)
  87. {
  88. $aBind = array('block' => $iBlockId, 'language' => $sLang);
  89. return $this->getAll("SELECT `block_id`, `language`, `revision`, `profile_id`, `notes`, `added` FROM `{$this->_sTableBlocksWithRevisions}` WHERE `block_id` = :block AND `language` = :language ORDER BY `revision` DESC", $aBind);
  90. }
  91. public function getBlocksWithMissingTranslations ($sLang)
  92. {
  93. $aBind = array('lang' => $sLang, 'module' => $this->_aObject['module']);
  94. return $this->getColumn("
  95. SELECT `wo`.`block_id`
  96. FROM `{$this->_sTableBlocksWithRevisions}` AS `wo`
  97. INNER JOIN `sys_pages_blocks` AS `b` ON (`wo`.`block_id` = `b`.`id` AND `b`.`module` = :module)
  98. LEFT JOIN `{$this->_sTableBlocksWithRevisions}` AS `wt` ON (`wo`.`block_id` = `wt`.`block_id` AND `wt`.`language` = :lang AND `wt`.`main_lang` != 1)
  99. WHERE `wo`.`main_lang` = 1 AND `wo`.`language` != :lang AND `wt`.`block_id` IS NULL
  100. GROUP BY `wo`.`block_id`", $aBind);
  101. }
  102. public function getBlocksWithOutdatedTranslations ($sLang)
  103. {
  104. $aBind = array('lang' => $sLang, 'module' => $this->_aObject['module']);
  105. return $this->getColumn("
  106. SELECT `wo`.`block_id`
  107. FROM `{$this->_sTableBlocksWithRevisions}` AS `wo`
  108. INNER JOIN `sys_pages_blocks` AS `b` ON (`wo`.`block_id` = `b`.`id` AND `b`.`module` = :module)
  109. INNER JOIN `{$this->_sTableBlocksWithRevisions}` AS `wt` ON (`wo`.`block_id` = `wt`.`block_id` AND `wt`.`language` = :lang AND `wt`.`main_lang` != 1)
  110. WHERE `wo`.`main_lang` = 1 AND `wo`.`language` != :lang
  111. GROUP BY `wo`.`block_id`
  112. HAVING MAX(`wt`.`added`) < MAX(`wo`.`added`)", $aBind);
  113. }
  114. public function deleteRevisions ($iBlockId, $sLang, $aRevisions)
  115. {
  116. $aBind = array('block' => $iBlockId, 'language' => $sLang);
  117. $i = $this->query("DELETE FROM `{$this->_sTableBlocksWithRevisions}` WHERE `block_id` = :block AND `language` = :language AND `revision` IN(" . $this->implode_escape($aRevisions) . ")", $aBind);
  118. if ($i) {
  119. // check if main language revisions was deleted
  120. $aRow = $this->getOne("SELECT `block_id` FROM `{$this->_sTableBlocksWithRevisions}` WHERE `block_id` = :block AND `main_lang` = 1 LIMIT 1", array('block' => $iBlockId));
  121. if (!$aRow) {
  122. // if main lang was deleted then mark latest translation from any lang as main
  123. $this->query("UPDATE `{$this->_sTableBlocksWithRevisions}` SET `main_lang` = 1 WHERE `block_id` = :block ORDER BY `added` DESC LIMIT 1", array('block' => $iBlockId));
  124. }
  125. }
  126. return $i;
  127. }
  128. public function getPageByBlockId ($iBlockId)
  129. {
  130. return $this->getRow("SELECT `p`.`title`, `p`.`uri` FROM `sys_objects_page` AS `p` INNER JOIN `sys_pages_blocks` AS `b` ON (`p`.`object` = `b`.`object` AND `b`.`id` = ? AND `p`.`module` = ?) LIMIT 1", array($iBlockId, $this->_aObject['module']));
  131. }
  132. public function getPages ($aAllExceptSpecified = array(), $aOnlySpecified = array())
  133. {
  134. $sWhere = '';
  135. if ($aOnlySpecified)
  136. $sWhere = " AND `uri` IN(" . $this->implode_escape($aOnlySpecified) . ")";
  137. elseif ($aAllExceptSpecified)
  138. $sWhere = " AND `uri` NOT IN(" . $this->implode_escape($aAllExceptSpecified) . ")";
  139. return $this->getAll("SELECT `title`, `uri`, `object` FROM `sys_objects_page` WHERE `module` = ?" . $sWhere, array($this->_aObject['module']));
  140. }
  141. public function insertPage ($sUri, $sUrl, $sTitleLangKey, $iType = 1, $iLayoutId = 20, $iVisibleForLevels = 2147483647, $sClass = 'BxTemplPageWiki')
  142. {
  143. $oQueryPageBuilder = new BxDolStudioBuilderPageQuery();
  144. $iPageId = $oQueryPageBuilder->insertPage($this->_aObject['module'] . '_' . str_replace('-', '_', $sUri), $this->_aObject['module'], $sUri, $sUrl, $sTitleLangKey, $iType, $iLayoutId, $iVisibleForLevels, $sClass);
  145. if ($iPageId) {
  146. $oQueryPageBuilder->updatePage($iPageId, ['sticky_columns' => 1]);
  147. $this->cleanCache('sys_pages_objects_data');
  148. $this->cleanCache('sys_pages_uri_object_map');
  149. $this->cleanCache('sys_pages_urirewrite_object_map');
  150. }
  151. return $iPageId;
  152. }
  153. public static function deleteAllRevisions ($mixedBlockIds)
  154. {
  155. $oDb = BxDolDb::getInstance();
  156. if (!is_array($mixedBlockIds))
  157. $mixedBlockId = array($mixedBlockIds);
  158. // TODO: remake to use $this->_sTableBlocksWithRevisions
  159. return $oDb->query("DELETE FROM `sys_pages_wiki_blocks` WHERE `block_id` IN(" . $oDb->implode_escape($mixedBlockIds) . ")");
  160. }
  161. }
  162. /** @} */