示例#1
0
 public static function edit($title, $md, $url, $time = null, $category = null, $tags = [], $state = self::STATE_OPEN, $id = null)
 {
     if ($id !== null && mb_strlen((string) $id) !== 24) {
         throw new \Exception('Invalid argument: id');
     }
     if ($time == null) {
         $time = time();
     }
     $html = \Breezewish\Marked\Marked::render($md, ['langPrefix' => 'prettyprint lang-', 'breaks' => true]);
     if (P_HTML_FILTER) {
         $html = \BWBlog\Escaper::purify($html);
     }
     // page doesn't need short introductions
     if ($state != self::STATE_PAGE) {
         // process introduction
         foreach (['<!-- more -->', '<!--more -->', '<!-- more-->', '<!--more-->'] as $meta) {
             $pos = mb_stripos($html, $meta, 0, 'UTF-8');
             if ($pos !== false) {
                 break;
             }
         }
         if ($pos !== false) {
             $intro = \BWBlog\Utils::closeTags(mb_substr($html, 0, $pos));
             $more = true;
         } else {
             $intro = $html;
             $more = false;
         }
     } else {
         $more = false;
         $intro = '';
     }
     // page uses direct url
     if ($state != self::STATE_PAGE) {
         $rest_url = \BWBlog\Utils::trimRestURI(str_replace('\\', '/', date('Y/m/d/', $time) . $url));
     } else {
         $rest_url = \BWBlog\Utils::trimRestURI(str_replace('\\', '/', 'page/' . $url));
     }
     $doc = ['title' => $title, 'content' => ['markdown' => $md, 'html' => $html, 'introhtml' => $intro, 'more' => $more], 'time' => ['main' => $time], 'url' => $url, 'rest_url' => $rest_url, 'category' => $category, 'lcategory' => strtolower($category), 'tags' => $tags, 'ltags' => array_map('strtolower', array_map('trim', $tags)), 'state' => (int) $state];
     global $db;
     if ($id === null) {
         // create
         $doc['time']['create'] = time();
         $db->selectCollection(MONGO_PREFIX . 'posts')->insert($doc);
         return $doc['_id'];
     } else {
         // update
         $doc['time']['edit'] = time();
         $result = $db->selectCollection(MONGO_PREFIX . 'posts')->findAndModify(['_id' => new \MongoId((string) $id)], ['$set' => $doc], ['_id' => 1], ['upsert' => true, 'new' => true]);
         return $result['_id'];
     }
 }