/** * 创建一篇文章 * * @param int $category_id 分类ID * @param string $title 标题 * @param string $content 内容 * * @throws \Exception\Nologin * @throws \Exception\Msg * * @return int */ public static function create($category_id, $title, $content) { $uid = \Yaf_Registry::get('current_uid'); $title = trim($title); $content = trim($content); //获取分类信息 $category = Category::show($category_id); if (!$uid) { throw new \Exception\Nologin(); } if (!$category || $category['uid'] && $category['uid'] != $uid) { throw new \Exception\Msg('指定分类不存在'); } if (!$title || !$content) { throw new \Exception\Msg('标题和内容不能为空'); } $data = array('category_id' => $category_id, 'title' => $title, 'content' => $content, 'state' => 0, 'uid' => $uid, 'create_time' => date('Y-m-d H:i:s')); $db = self::db(); $db->insert($data); $id = $db->lastId(); if (!$id) { throw new \Exception\Msg(_('文章发表失败')); } //计数器+1 Counter\Article::incr($category_id); Counter\Article::incr(0); //发布至Github中 $data['id'] = $id; Publish::article($data); return $id; }