BxDolWikiQuery.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 getBlockLangs ($iBlockId)
  68. {
  69. return $this->getColumn("SELECT `language` FROM `sys_pages_wiki_blocks` WHERE `block_id` = :block GROUP BY `language`", array('block' => $iBlockId));
  70. }
  71. public function updateBlockIndexingData ($iBlockId, $sText)
  72. {
  73. return $this->query("UPDATE `sys_pages_blocks` SET `text` = :text, `text_updated` = :updated WHERE `id` = :id", array('text' => $sText, 'updated' => time(), 'id' => $iBlockId));
  74. }
  75. public function getBlockHistory ($iBlockId, $sLang)
  76. {
  77. $aBind = array('block' => $iBlockId, 'language' => $sLang);
  78. 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);
  79. }
  80. public function getBlocksWithMissingTranslations ($sLang)
  81. {
  82. $aBind = array('lang' => $sLang, 'module' => $this->_aObject['module']);
  83. return $this->getColumn("
  84. SELECT `wo`.`block_id`
  85. FROM `sys_pages_wiki_blocks` AS `wo`
  86. INNER JOIN `sys_pages_blocks` AS `b` ON (`wo`.`block_id` = `b`.`id` AND `b`.`module` = :module)
  87. LEFT JOIN `sys_pages_wiki_blocks` AS `wt` ON (`wo`.`block_id` = `wt`.`block_id` AND `wt`.`language` = :lang AND `wt`.`main_lang` != 1)
  88. WHERE `wo`.`main_lang` = 1 AND `wo`.`language` != :lang AND `wt`.`block_id` IS NULL
  89. GROUP BY `wo`.`block_id`", $aBind);
  90. }
  91. public function getBlocksWithOutdatedTranslations ($sLang)
  92. {
  93. $aBind = array('lang' => $sLang, 'module' => $this->_aObject['module']);
  94. return $this->getColumn("
  95. SELECT `wo`.`block_id`
  96. FROM `sys_pages_wiki_blocks` AS `wo`
  97. INNER JOIN `sys_pages_blocks` AS `b` ON (`wo`.`block_id` = `b`.`id` AND `b`.`module` = :module)
  98. INNER JOIN `sys_pages_wiki_blocks` 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
  100. GROUP BY `wo`.`block_id`
  101. HAVING MAX(`wt`.`added`) < MAX(`wo`.`added`)", $aBind);
  102. }
  103. public function deleteRevisions ($iBlockId, $sLang, $aRevisions)
  104. {
  105. $aBind = array('block' => $iBlockId, 'language' => $sLang);
  106. $i = $this->query("DELETE FROM `sys_pages_wiki_blocks` WHERE `block_id` = :block AND `language` = :language AND `revision` IN(" . $this->implode_escape($aRevisions) . ")", $aBind);
  107. if ($i) {
  108. // check if main language revisions was deleted
  109. $aRow = $this->getOne("SELECT `block_id` FROM `sys_pages_wiki_blocks` WHERE `block_id` = :block AND `main_lang` = 1 LIMIT 1", array('block' => $iBlockId));
  110. if (!$aRow) {
  111. // if main lang was deleted then mark latest translation from any lang as main
  112. $this->query("UPDATE `sys_pages_wiki_blocks` SET `main_lang` = 1 WHERE `block_id` = :block ORDER BY `added` DESC LIMIT 1", array('block' => $iBlockId));
  113. }
  114. }
  115. return $i;
  116. }
  117. public function getPageByBlockId ($iBlockId)
  118. {
  119. 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']));
  120. }
  121. public function getPages ($aAllExceptSpecified = array(), $aOnlySpecified = array())
  122. {
  123. $sWhere = '';
  124. if ($aOnlySpecified)
  125. $sWhere = " AND `uri` IN(" . $this->implode_escape($aOnlySpecified) . ")";
  126. elseif ($aAllExceptSpecified)
  127. $sWhere = " AND `uri` NOT IN(" . $this->implode_escape($aAllExceptSpecified) . ")";
  128. return $this->getAll("SELECT `title`, `uri`, `object` FROM `sys_objects_page` WHERE `module` = ?" . $sWhere, array($this->_aObject['module']));
  129. }
  130. public function insertPage ($sUri, $sUrl, $sTitleLangKey, $iType = 1, $iLayoutId = 5, $iVisibleForLevels = 2147483647, $sClass = 'BxTemplPageWiki')
  131. {
  132. $b = $this->query('INSERT INTO `sys_objects_page` SET
  133. `object` = :obj,
  134. `uri` = :uri,
  135. `title` = :title,
  136. `module` = :module,
  137. `cover` = :cover,
  138. `type_id` = :type,
  139. `layout_id` = :layout,
  140. `visible_for_levels` = :levels,
  141. `visible_for_levels_editable` = 1,
  142. `url` = :url,
  143. `cache_lifetime` = 0,
  144. `cache_editable` = 1,
  145. `deletable` = 1,
  146. `override_class_name` = :class
  147. ', array(
  148. 'obj' => $this->_aObject['module'] . '_' . str_replace('-', '_', $sUri),
  149. 'uri' => $sUri,
  150. 'title' => $sTitleLangKey,
  151. 'module' => $this->_aObject['module'],
  152. 'cover' => 0,
  153. 'type' => $iType,
  154. 'layout' => $iLayoutId,
  155. 'levels' => $iVisibleForLevels,
  156. 'url' => $sUrl,
  157. 'class' => $sClass,
  158. ));
  159. if (!$b)
  160. return false;
  161. return $this->lastId();
  162. }
  163. public static function deleteAllRevisions ($mixedBlockIds)
  164. {
  165. $oDb = BxDolDb::getInstance();
  166. if (!is_array($mixedBlockIds))
  167. $mixedBlockId = array($mixedBlockIds);
  168. return $oDb->query("DELETE FROM `sys_pages_wiki_blocks` WHERE `block_id` IN(" . $oDb->implode_escape($mixedBlockIds) . ")");
  169. }
  170. }
  171. /** @} */