From de09d806e9288a54cd6b58014d3dae1f1bee4fc1 Mon Sep 17 00:00:00 2001 From: xieqiyu Date: Tue, 13 May 2025 10:48:16 +0800 Subject: [PATCH] * [task#142974,doing,3h] Upgrade doc template of wiki. --- module/upgrade/control.php | 18 +++++ module/upgrade/js/upgradedoctemplates.ui.js | 27 +++++++- module/upgrade/model.php | 73 +++++++++++++++++++++ 3 files changed, 117 insertions(+), 1 deletion(-) diff --git a/module/upgrade/control.php b/module/upgrade/control.php index 0f4e8b27b7..34d3459877 100644 --- a/module/upgrade/control.php +++ b/module/upgrade/control.php @@ -1013,4 +1013,22 @@ class upgrade extends control $this->send(array('result' => 'success', 'data' => $docTemplate)); } + + /** + * 升级wiki类型的文档模板。 + * Upgrade templates of wiki. + * + * @access public + * @return void + */ + public function ajaxUpgradeWikiTemplates() + { + if($_POST) + { + $wikis = isset($_POST['wikis']) ? $_POST['wikis'] : array(); + if(is_string($wikis)) $wikis = explode(',', $wikis); + if($wikis) $this->upgrade->upgradeWikiTemplates($wikis); + $this->send(array('result' => 'success')); + } + } } diff --git a/module/upgrade/js/upgradedoctemplates.ui.js b/module/upgrade/js/upgradedoctemplates.ui.js index a2afa9269f..949e9000d2 100644 --- a/module/upgrade/js/upgradedoctemplates.ui.js +++ b/module/upgrade/js/upgradedoctemplates.ui.js @@ -24,6 +24,24 @@ async function upgradeTemplateContent(docID, options) return result; } +/** + * 处理wiki类型的文档模板。 + * Process templates of wiki. + * + * @param object $idList + * @access public + * @return object + */ +async function upgradeWikiTemplates(idList) +{ + let result; + await $.post($.createLink('upgrade', 'ajaxUpgradeWikiTemplates'), {wikis: idList.join(',')}, (res) => + { + result = res.result === 'success' ? true : res.message; + }, 'json'); + return result; +} + /** * 依次处理文档模板内容。 * Process doc template content. @@ -38,7 +56,7 @@ async function upgradeDocTemplates(idList, options) await zui.Editor.loadModule(); const {onProgress} = options || {}; let current = 0; - const total = idList.html.length; + const total = idList.html.length + idList.wiki.length; for(const id of idList['html']) { await upgradeTemplateContent(id, {processUrl: $.createLink('upgrade', 'ajaxUpgradeDocTemplate', 'docID={docID}')}); @@ -46,6 +64,13 @@ async function upgradeDocTemplates(idList, options) current++; onProgress(current, total); } + + if(idList.wiki.length) + { + await upgradeWikiTemplates(idList.wiki); + current += idList.wiki.length; + } + onProgress(current, total); return idList; } diff --git a/module/upgrade/model.php b/module/upgrade/model.php index 97f955627e..318ecb7e8d 100644 --- a/module/upgrade/model.php +++ b/module/upgrade/model.php @@ -10994,4 +10994,77 @@ class upgradeModel extends model if(dao::isError()) return false; return true; } + + /** + * 升级wiki类型的文档模板。 + * Upgrade templates of wiki. + * + * @param array $wikis + * @return bool + */ + public function upgradeWikiTemplates(array $wikis = array()) + { + $this->app->loadLang('doc'); + $wikiList = $this->dao->select('*')->from(TABLE_DOCCONTENT)->where('doc')->in($wikis)->fetchAll('doc'); + foreach($wikis as $bookID) + { + $templateHtml = $this->convertWiki2Html((int)$bookID); + $oldContent = $wikiList[$bookID]; + + $newContent = clone $oldContent; + $newContent->version = $oldContent->version + 1; + $newContent->type = 'doc'; + $newContent->content = $templateHtml; + $newContent->rawContent = json_encode(array('$migrate' => 'html', '$data' => $templateHtml)); + $newContent->addedBy = 'system'; + $newContent->addedDate = helper::now(); + $newContent->editedBy = 'system'; + $newContent->editedDate = helper::now(); + unset($newContent->id); + + $this->dao->insert(TABLE_DOCCONTENT)->data($newContent)->exec(); + $this->dao->update(TABLE_DOC)->set('version')->eq($newContent->version)->where('id')->eq($bookID)->exec(); + $this->loadModel('action')->create('docTemplate', (int)$bookID, 'convertDocTemplate', sprintf($this->lang->doc->docTemplateConvertComment, "#$oldContent->version", '', 'system')); + + } + if(dao::isError()) return false; + return true; + } + + /** + * 将wiki下的章节和文章合并为一个html。 + * Merge chapters and articles from the wiki into HTML. + * + * @param int $bookID + * @param int $nodeID + * @return string + */ + public function convertWiki2Html(int $bookID = 0, int $nodeID = 0): string + { + $template = $this->dao->select('t1.*, t2.content as content')->from(TABLE_DOC)->alias('t1') + ->leftJoin(TABLE_DOCCONTENT)->alias('t2')->on('t1.id = t2.doc') + ->where('t1.id')->eq($nodeID ? $nodeID : $bookID) + ->fetch(); + + $children = $this->dao->select('*')->from(TABLE_DOC) + ->where('template')->eq($bookID) + ->andWhere('deleted')->eq(0) + ->andWhere('parent')->eq($nodeID ? $nodeID : $bookID) + ->orderBy('`order`, id') + ->fetchAll('id'); + + $childrenHtml = ''; + if($children) + { + foreach($children as $child) $childrenHtml .= $this->convertWiki2Html($bookID, $child->id); + } + + $templateHtml = ''; + $levelMark = $template->grade > 0 && $template->grade < 7 ? "h{$template->grade}" : 'p'; + if($template->type == 'book') $templateHtml .= $childrenHtml; + if(!empty($template->chapterType) && strpos('input,system', $template->chapterType) !== false) $templateHtml .= "<{$levelMark}>" . $template->title . "" . $childrenHtml; + if($template->type == 'article') $templateHtml .= "<{$levelMark}>" . $template->title . "" . $template->content . $childrenHtml; + + return $templateHtml; + } }