1
0

BxDolWikiQuery.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. public function __construct($aObject)
  17. {
  18. parent::__construct();
  19. $this->_aObject = $aObject;
  20. }
  21. static public function getWikiObjectByUri ($sUri)
  22. {
  23. return self::getWikiObjectByField ('uri', $sUri);
  24. }
  25. static public function getWikiObject ($sObject)
  26. {
  27. return self::getWikiObjectByField ('object', $sObject);
  28. }
  29. static public function getWikiObjectByField ($sField, $sValue)
  30. {
  31. $oDb = BxDolDb::getInstance();
  32. $sQuery = $oDb->prepare("SELECT * FROM `sys_objects_wiki` WHERE `$sField` = ?", $sValue);
  33. $aObject = $oDb->getRow($sQuery);
  34. if (!$aObject || !is_array($aObject))
  35. return false;
  36. return $aObject;
  37. }
  38. /**
  39. * Get wiki block
  40. * @param $iBlockId block ID
  41. * @param $sLang 2 letters language code
  42. * @param $iRevision [optional, default false] revision number, if false get latest revision for then given lan
  43. * @param $bAutoMainLang [optional, default true] automatically load wiki block for main language if translation for gived language doesn't exist
  44. * @return array with wiki block info
  45. */
  46. public function getBlockContent ($iBlockId, $sLang, $iRevision = false, $bAutoMainLang = true)
  47. {
  48. $sWhere = '';
  49. $aRow = false;
  50. $aBind = array('block' => $iBlockId);
  51. if (false !== $sLang) {
  52. $aBind['language'] = $sLang;
  53. if (false !== $iRevision) {
  54. $sWhere = " AND `revision` = :rev";
  55. $aBind['rev'] = $iRevision;
  56. }
  57. // get latest revision for specific language
  58. $aRow = $this->getRow("SELECT `block_id`, `revision`, `profile_id`, `language`, `main_lang`, `content`, `unsafe`, `notes`, `added` FROM `sys_pages_wiki_blocks` WHERE `block_id` = :block AND `language` = :language $sWhere ORDER BY `revision` DESC LIMIT 1", $aBind);
  59. }
  60. // if translation isn't found for specific language then get latest revision for main language
  61. if ($bAutoMainLang && !$aRow) {
  62. unset($aBind['language']);
  63. $aRow = $this->getRow("SELECT `block_id`, `revision`, `profile_id`, `language`, `main_lang`, `content`, `unsafe`, `notes`, `added` FROM `sys_pages_wiki_blocks` WHERE `block_id` = :block AND `main_lang` = 1 $sWhere ORDER BY `revision` DESC LIMIT 1", $aBind);
  64. }
  65. return $aRow ? $aRow : false;
  66. }
  67. public function getBlockHistory ($iBlockId, $sLang)
  68. {
  69. $aBind = array('block' => $iBlockId, 'language' => $sLang);
  70. return $this->getAll("SELECT `block_id`, `language`, `revision`, `profile_id`, `notes`, `added` FROM `sys_pages_wiki_blocks` WHERE `block_id` = :block AND `language` = :language ORDER BY `revision` DESC", $aBind);
  71. }
  72. public function getBlocksWithMissingTranslations ($sLang)
  73. {
  74. $aBind = array('lang' => $sLang, 'module' => $this->_aObject['module']);
  75. return $this->getColumn("
  76. SELECT `wo`.`block_id`
  77. FROM `sys_pages_wiki_blocks` AS `wo`
  78. INNER JOIN `sys_pages_blocks` AS `b` ON (`wo`.`block_id` = `b`.`id` AND `b`.`module` = :module)
  79. LEFT JOIN `sys_pages_wiki_blocks` AS `wt` ON (`wo`.`block_id` = `wt`.`block_id` AND `wt`.`language` = :lang AND `wt`.`main_lang` != 1)
  80. WHERE `wo`.`main_lang` = 1 AND `wo`.`language` != :lang AND `wt`.`block_id` IS NULL
  81. GROUP BY `wo`.`block_id`", $aBind);
  82. }
  83. public function getBlocksWithOutdatedTranslations ($sLang)
  84. {
  85. $aBind = array('lang' => $sLang, 'module' => $this->_aObject['module']);
  86. return $this->getColumn("
  87. SELECT `wo`.`block_id`
  88. FROM `sys_pages_wiki_blocks` AS `wo`
  89. INNER JOIN `sys_pages_blocks` AS `b` ON (`wo`.`block_id` = `b`.`id` AND `b`.`module` = :module)
  90. INNER JOIN `sys_pages_wiki_blocks` AS `wt` ON (`wo`.`block_id` = `wt`.`block_id` AND `wt`.`language` = :lang AND `wt`.`main_lang` != 1)
  91. WHERE `wo`.`main_lang` = 1 AND `wo`.`language` != :lang
  92. GROUP BY `wo`.`block_id`
  93. HAVING MAX(`wt`.`added`) < MAX(`wo`.`added`)", $aBind);
  94. }
  95. public function deleteRevisions ($iBlockId, $sLang, $aRevisions)
  96. {
  97. $aBind = array('block' => $iBlockId, 'language' => $sLang);
  98. $i = $this->query("DELETE FROM `sys_pages_wiki_blocks` WHERE `block_id` = :block AND `language` = :language AND `revision` IN(" . $this->implode_escape($aRevisions) . ")", $aBind);
  99. if ($i) {
  100. // check if main language revisions was deleted
  101. $aRow = $this->getOne("SELECT `block_id` FROM `sys_pages_wiki_blocks` WHERE `block_id` = :block AND `main_lang` = 1 LIMIT 1", array('block' => $iBlockId));
  102. if (!$aRow) {
  103. // if main lang was deleted then mark latest translation from any lang as main
  104. $this->query("UPDATE `sys_pages_wiki_blocks` SET `main_lang` = 1 WHERE `block_id` = :block ORDER BY `added` DESC LIMIT 1", array('block' => $iBlockId));
  105. }
  106. }
  107. return $i;
  108. }
  109. public function getPageByBlockId ($iBlockId)
  110. {
  111. 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']));
  112. }
  113. public function getPages ($aAllExceptSpecified = array(), $aOnlySpecified = array())
  114. {
  115. $sWhere = '';
  116. if ($aOnlySpecified)
  117. $sWhere = " AND `uri` IN(" . $this->implode_escape($aOnlySpecified) . ")";
  118. elseif ($aAllExceptSpecified)
  119. $sWhere = " AND `uri` NOT IN(" . $this->implode_escape($aAllExceptSpecified) . ")";
  120. return $this->getAll("SELECT `title`, `uri`, `object` FROM `sys_objects_page` WHERE `module` = ?" . $sWhere, array($this->_aObject['module']));
  121. }
  122. public function insertPage ($sUri, $sUrl, $sTitleLangKey, $iType = 1, $iLayoutId = 5, $iVisibleForLevels = 2147483647, $sClass = 'BxTemplPageWiki')
  123. {
  124. $b = $this->query('INSERT INTO `sys_objects_page` SET
  125. `object` = :obj,
  126. `uri` = :uri,
  127. `title` = :title,
  128. `module` = :module,
  129. `cover` = :cover,
  130. `type_id` = :type,
  131. `layout_id` = :layout,
  132. `visible_for_levels` = :levels,
  133. `visible_for_levels_editable` = 1,
  134. `url` = :url,
  135. `cache_lifetime` = 0,
  136. `cache_editable` = 1,
  137. `deletable` = 1,
  138. `override_class_name` = :class
  139. ', array(
  140. 'obj' => $this->_aObject['module'] . '_' . str_replace('-', '_', $sUri),
  141. 'uri' => $sUri,
  142. 'title' => $sTitleLangKey,
  143. 'module' => $this->_aObject['module'],
  144. 'cover' => 0,
  145. 'type' => $iType,
  146. 'layout' => $iLayoutId,
  147. 'levels' => $iVisibleForLevels,
  148. 'url' => $sUrl,
  149. 'class' => $sClass,
  150. ));
  151. if (!$b)
  152. return false;
  153. return $this->lastId();
  154. }
  155. public static function deleteAllRevisions ($mixedBlockIds)
  156. {
  157. $oDb = BxDolDb::getInstance();
  158. if (!is_array($mixedBlockIds))
  159. $mixedBlockId = array($mixedBlockIds);
  160. return $oDb->query("DELETE FROM `sys_pages_wiki_blocks` WHERE `block_id` IN(" . $oDb->implode_escape($mixedBlockIds) . ")");
  161. }
  162. }
  163. /** @} */