public static function createProblem($uid, $title, array $visibleDomains) { if (!Validator::int()->validate($uid)) { throw new InvalidArgumentException('uid', 'type_invalid'); } if (!is_string($title)) { throw new InvalidArgumentException('title', 'type_invalid'); } if (!mb_check_encoding($title, 'UTF-8')) { throw new InvalidArgumentException('title', 'encoding_invalid'); } if (!Validator::length(VJ::PROBLEM_TITLE_MIN, VJ::PROBLEM_TITLE_MAX)) { throw new UserException('ProblemUtil.title_invalid_length'); } foreach ($visibleDomains as &$domain) { if (!$domain instanceof \MongoId) { throw new InvalidArgumentException('visibleDomains', 'type_invalid'); } } $keyword = KeywordFilter::isContainGeneric($title); if ($keyword !== false) { throw new UserException('ProblemUtil.title_forbid', ['keyword' => $keyword]); } self::initParser(); $markdown = Application::coll('System')->findOne(['_id' => 'ProblemTemplate'])['markdown']; $html = self::$parser->parse($markdown); $title = VJ::removeEmoji($title); $link = self::generateLink($title); $doc = VoteUtil::attachDocument(['owner' => (int) $uid, 'at' => new \MongoDate(), 'title' => $title, 'link' => $link, 'llink' => self::canonicalizeLink($link), 'raw' => $markdown, 'html' => $html, 'visible' => $visibleDomains, 'tags' => [], 'history' => self::encodeHistory([])]); try { Application::coll('Problem')->insert($doc); } catch (\MongoCursorException $e) { throw new UserException('ProblemUtil.createProblem.title_exists'); } return $doc['_id']; }
/** * 创建评论 * * @param string $ref * @param int $owner * @param string $markdown * @return array * @throws InvalidArgumentException * @throws UserException */ public static function createComment($ref, $owner, $markdown) { if (!is_string($ref)) { throw new InvalidArgumentException('ref', 'type_invalid'); } if (!mb_check_encoding($ref, 'UTF-8')) { throw new InvalidArgumentException('ref', 'encoding_invalid'); } if (!Validator::int()->validate($owner)) { throw new InvalidArgumentException('owner', 'type_invalid'); } if (!is_string($markdown)) { throw new InvalidArgumentException('markdown', 'type_invalid'); } if (!mb_check_encoding($markdown, 'UTF-8')) { throw new InvalidArgumentException('markdown', 'encoding_invalid'); } if (!Validator::length(VJ::COMMENT_MIN, VJ::COMMENT_MAX)) { throw new UserException('CommentUtil.content_invalid_length'); } self::initParser(); $commentId = new \MongoId(); $html = self::$parser->parse($markdown); $keyword = KeywordFilter::isContainGeneric(strip_tags($html)); if ($keyword !== false) { throw new UserException('CommentUtil.content_forbid', ['keyword' => $keyword]); } $doc = VoteUtil::attachDocument(['_id' => $commentId, 'ref' => $ref, 'owner' => (int) $owner, 'at' => new \MongoDate(), 'raw' => $markdown, 'html' => $html, 'replies' => [], 'deleted' => false]); Application::coll('Comment')->insert($doc); Application::emit('comment.create.succeeded', [$ref, $commentId]); return ['_id' => $commentId, 'html' => $html]; }