diff --git a/app/Console/Migrations/Migration.php b/app/Console/Migrations/Migration.php new file mode 100644 index 00000000..98d848d0 --- /dev/null +++ b/app/Console/Migrations/Migration.php @@ -0,0 +1,13 @@ +initMarkdownConverter(); + $this->courseMarkdownToHtml(); + $this->articleMarkdownToHtml(); + $this->questionMarkdownToHtml(); + $this->answerMarkdownToHtml(); + $this->pageMarkdownToHtml(); + $this->helpMarkdownToHtml(); + $this->pointGiftMarkdownToHtml(); + } + + protected function initMarkdownConverter() + { + $this->markdownConverter = new GithubFlavoredMarkdownConverter([ + 'html_input' => 'escape', + 'allow_unsafe_links' => false, + ]); + } + + protected function articleMarkdownToHtml() + { + /** + * @var $articles Resultset|ArticleModel[] + */ + $articles = ArticleModel::query()->execute(); + + if ($articles->count() == 0) return; + + foreach ($articles as $article) { + $content = $this->markdownConverter->convertToHtml($article->content); + $article->content = $content; + $article->update(); + } + } + + protected function courseMarkdownToHtml() + { + /** + * @var $courses Resultset|CourseModel[] + */ + $courses = CourseModel::query()->execute(); + + if ($courses->count() == 0) return; + + foreach ($courses as $course) { + $details = $this->markdownConverter->convertToHtml($course->details); + $course->details = $details; + $course->update(); + } + } + + protected function questionMarkdownToHtml() + { + /** + * @var $questions Resultset|QuestionModel[] + */ + $questions = QuestionModel::query()->execute(); + + if ($questions->count() == 0) return; + + foreach ($questions as $question) { + $content = $this->markdownConverter->convertToHtml($question->content); + $question->content = $content; + $question->update(); + } + } + + protected function answerMarkdownToHtml() + { + /** + * @var $answers Resultset|AnswerModel[] + */ + $answers = AnswerModel::query()->execute(); + + if ($answers->count() == 0) return; + + foreach ($answers as $answer) { + $content = $this->markdownConverter->convertToHtml($answer->content); + $answer->content = $content; + $answer->update(); + } + } + + protected function pageMarkdownToHtml() + { + /** + * @var $pages Resultset|PageModel[] + */ + $pages = PageModel::query()->execute(); + + if ($pages->count() == 0) return; + + foreach ($pages as $page) { + $content = $this->markdownConverter->convertToHtml($page->content); + $page->content = $content; + $page->update(); + } + } + + protected function helpMarkdownToHtml() + { + /** + * @var $helps Resultset|HelpModel[] + */ + $helps = HelpModel::query()->execute(); + + if ($helps->count() == 0) return; + + foreach ($helps as $help) { + $content = $this->markdownConverter->convertToHtml($help->content); + $help->content = $content; + $help->update(); + } + } + + protected function pointGiftMarkdownToHtml() + { + /** + * @var $gifts Resultset|PointGiftModel[] + */ + $gifts = PointGiftModel::query() + ->where('type = :type:', ['type' => PointGiftModel::TYPE_GOODS]) + ->execute(); + + if ($gifts->count() == 0) return; + + foreach ($gifts as $gift) { + $details = $this->markdownConverter->convertToHtml($gift->details); + $gift->details = $details; + $gift->update(); + } + } + +} \ No newline at end of file diff --git a/app/Console/Tasks/UpgradeTask.php b/app/Console/Tasks/UpgradeTask.php index 47533f97..42a581ef 100644 --- a/app/Console/Tasks/UpgradeTask.php +++ b/app/Console/Tasks/UpgradeTask.php @@ -7,16 +7,20 @@ namespace App\Console\Tasks; +use App\Caches\AppInfo as AppInfoCache; use App\Caches\NavTreeList as NavTreeListCache; use App\Caches\Setting as SettingCache; -use App\Caches\AppInfo as AppInfoCache; +use App\Models\MigrationTask as MigrationTaskModel; use App\Models\Setting as SettingModel; +use Phalcon\Mvc\Model\Resultset; +use Phalcon\Mvc\Model\ResultsetInterface; class UpgradeTask extends Task { public function mainAction() { + $this->migrateAction(); $this->resetAppInfoAction(); $this->resetSettingAction(); $this->resetAnnotationAction(); @@ -25,6 +29,51 @@ class UpgradeTask extends Task $this->resetNavAction(); } + /** + * 执行迁移 + * + * @command: php console.php upgrade migrate + */ + public function migrateAction() + { + $tasks = $this->findMigrationTasks(); + + $versionList = []; + + if ($tasks->count() > 0) { + $versionList = kg_array_column($tasks->toArray(), 'version'); + } + + $files = scandir(app_path('Console/Migrations')); + + foreach ($files as $file) { + + if (preg_match('/^V[0-9]+\.php$/', $file)) { + + $version = substr($file, 0, -4); + + if (!in_array($version, $versionList)) { + + $startTime = time(); + + $className = "\App\Console\Migrations\\{$version}"; + $obj = new $className(); + $obj->run(); + + $endTime = time(); + + $task = new MigrationTaskModel(); + $task->version = $version; + $task->start_time = $startTime; + $task->end_time = $endTime; + $task->create(); + + echo "------ console migration {$version} ok ------" . PHP_EOL; + } + } + } + } + /** * 重置应用信息 * @@ -144,6 +193,14 @@ class UpgradeTask extends Task echo '------ end reset navigation ------' . PHP_EOL; } + /** + * @return ResultsetInterface|Resultset|MigrationTaskModel[] + */ + protected function findMigrationTasks() + { + return MigrationTaskModel::query()->execute(); + } + protected function handlePhKeys($keys) { return array_map(function ($key) { diff --git a/app/Http/Admin/Views/answer/add.volt b/app/Http/Admin/Views/answer/add.volt index a079013f..c75f39bb 100644 --- a/app/Http/Admin/Views/answer/add.volt +++ b/app/Http/Admin/Views/answer/add.volt @@ -13,8 +13,7 @@