3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2011, StatusNet, Inc.
6 * Data structure for blog entries
10 * This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU Affero General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Affero General Public License for more details.
20 * You should have received a copy of the GNU Affero General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 * @author Evan Prodromou <evan@status.net>
26 * @copyright 2011 StatusNet, Inc.
27 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
28 * @link http://status.net/
31 if (!defined('STATUSNET')) {
32 // This check helps protect against security problems;
33 // your code file can't be executed directly from the web.
38 * Data structure for blog entries
42 * @author Evan Prodromou <evan@status.net>
43 * @copyright 2011 StatusNet, Inc.
44 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
45 * @link http://status.net/
48 class Blog_entry extends Managed_DataObject
50 public $__table = 'blog_entry';
53 public $profile_id; // int
54 public $title; // varchar(191) not 255 because utf8mb4 takes more space
55 public $summary; // text
56 public $content; // text
57 public $uri; // varchar(191) not 255 because utf8mb4 takes more space
58 public $url; // varchar(191) not 255 because utf8mb4 takes more space
59 public $created; // datetime
60 public $modified; // datetime
62 const TYPE = ActivityObject::ARTICLE;
64 static function schemaDef()
67 'description' => 'lite blog entry',
69 'id' => array('type' => 'char',
72 'description' => 'Unique ID (UUID)'),
73 'profile_id' => array('type' => 'int',
75 'description' => 'Author profile ID'),
76 'title' => array('type' => 'varchar',
78 'description' => 'title of the entry'),
79 'summary' => array('type' => 'text',
80 'description' => 'initial summary'),
81 'content' => array('type' => 'text',
82 'description' => 'HTML content of the entry'),
83 'uri' => array('type' => 'varchar',
85 'description' => 'URI (probably http://) for this entry'),
86 'url' => array('type' => 'varchar',
88 'description' => 'URL (probably http://) for this entry'),
89 'created' => array('type' => 'datetime',
91 'description' => 'date this record was created'),
92 'modified' => array('type' => 'datetime',
94 'description' => 'date this record was created'),
96 'primary key' => array('id'),
97 'unique keys' => array(
98 'blog_entry_uri_key' => array('uri'),
101 'blog_entry_profile_id_idx' => array('profile_id'),
102 'blog_entry_created_idx' => array('created')
104 'foreign keys' => array(
105 'blog_entry_profile_id_fkey' => array('profile', array('profile_id' => 'id')),
110 static function saveNew($profile, $title, $content, $options=null)
112 if (is_null($options)) {
116 $be = new Blog_entry();
117 $be->id = (string) new UUID();
118 $be->profile_id = $profile->id;
119 $be->title = $title; // Note: not HTML-protected
120 $be->content = common_purify($content);
122 if (array_key_exists('summary', $options)) {
123 $be->summary = common_purify($options['summary']);
126 $be->summary = self::summarize($be->content);
129 // Don't save an identical summary
131 if ($be->summary == $be->content) {
135 $url = common_local_url('showblogentry', array('id' => $be->id));
137 if (!array_key_exists('uri', $options)) {
138 $options['uri'] = $url;
141 $be->uri = $options['uri'];
143 if (!array_key_exists('url', $options)) {
144 $options['url'] = $url;
147 $be->url = $options['url'];
149 if (!array_key_exists('created', $options)) {
150 $be->created = common_sql_now();
153 $be->created = $options['created'];
155 $be->modified = common_sql_now();
159 // Use user's preferences for short URLs, if possible
162 $user = $profile->isLocal()
163 ? $profile->getUser()
165 $shortUrl = File_redirection::makeShort($url, $user);
166 } catch (Exception $e) {
167 // Don't let this stop us.
171 // XXX: this might be too long.
173 if (!empty($be->summary)) {
174 $options['rendered'] = $be->summary . ' ' .
175 XMLStringer::estring('a', array('href' => $url,
176 'class' => 'blog-entry'),
178 $text = common_strip_html($be->summary);
180 $options['rendered'] = $be->content;
181 $text = common_strip_html($be->content);
185 if (Notice::contentTooLong($text)) {
186 $text = substr($text, 0, Notice::maxContent() - mb_strlen($shortUrl) - 2) .
190 // Override this no matter what.
192 $options['object_type'] = self::TYPE;
194 $source = array_key_exists('source', $options) ?
195 $options['source'] : 'web';
197 $saved = Notice::saveNew($profile->id, $text, $source, $options);
203 * Summarize the contents of a blog post
205 * We take the first div or paragraph of the blog post if there's a hit;
206 * Otherwise we take the whole thing.
208 * @param string $html HTML of full content
210 static function summarize($html)
212 if (preg_match('#<p>.*?</p>#s', $html, $matches)) {
214 } else if (preg_match('#<div>.*?</div>#s', $html, $matches)) {
221 static function fromNotice($notice)
223 return Blog_entry::getKV('uri', $notice->uri);
228 return Notice::getKV('uri', $this->uri);
231 function asActivityObject()
233 $obj = new ActivityObject();
235 $obj->id = $this->uri;
236 $obj->type = self::TYPE;
237 $obj->title = $this->title;
238 $obj->summary = $this->summary;
239 $obj->content = $this->content;
240 $obj->link = $this->url;