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(255)
55 public $summary; // text
56 public $content; // text
59 public $created; // datetime
60 public $modified; // datetime
62 const TYPE = ActivityObject::ARTICLE;
64 function staticGet($k, $v=null)
66 return Managed_DataObject::staticGet('blog_entry', $k, $v);
69 static function schemaDef()
72 'description' => 'lite blog entry',
74 'id' => array('type' => 'char',
77 'description' => 'Unique ID (UUID)'),
78 'profile_id' => array('type' => 'int',
80 'description' => 'Author profile ID'),
81 'title' => array('type' => 'varchar',
83 'description' => 'title of the entry'),
84 'summary' => array('type' => 'text',
85 'description' => 'initial summary'),
86 'content' => array('type' => 'text',
87 'description' => 'HTML content of the entry'),
88 'uri' => array('type' => 'varchar',
90 'description' => 'URI (probably http://) for this entry'),
91 'url' => array('type' => 'varchar',
93 'description' => 'URL (probably http://) for this entry'),
94 'created' => array('type' => 'datetime',
96 'description' => 'date this record was created'),
97 'modified' => array('type' => 'datetime',
99 'description' => 'date this record was created'),
101 'primary key' => array('id'),
102 'foreign keys' => array(
103 'blog_entry_profile_id_fkey' => array('profile', array('profile_id' => 'id')),
106 'blog_entry_created_idx' => array('created'),
107 'blog_entry_uri_idx' => array('uri'),
112 static function saveNew($profile, $title, $content, $options=null)
114 if (is_null($options)) {
118 $be = new Blog_entry();
119 $be->id = (string) new UUID();
120 $be->profile_id = $profile->id;
121 $be->title = $title; // Note: not HTML-protected
122 $be->content = self::purify($content);
124 if (array_key_exists('summary', $options)) {
125 $be->summary = self::purify($options['summary']);
128 $be->summary = self::summarize($be->content);
131 // Don't save an identical summary
133 if ($be->summary == $be->content) {
137 $url = common_local_url('showblogentry', array('id' => $be->id));
139 if (!array_key_exists('uri', $options)) {
140 $options['uri'] = $url;
143 $be->uri = $options['uri'];
145 if (!array_key_exists('url', $options)) {
146 $options['url'] = $url;
149 $be->url = $options['url'];
151 if (!array_key_exists('created', $options)) {
152 $be->created = common_sql_now();
155 $be->created = $options['created'];
157 $be->modified = common_sql_now();
161 // Use user's preferences for short URLs, if possible
164 $user = $profile->getUser();
165 $shortUrl = File_redirection::makeShort($url,
166 empty($user) ? null : $user);
167 } catch (Exception $e) {
168 // Don't let this stop us.
172 // XXX: this might be too long.
174 if (!empty($be->summary)) {
175 $options['rendered'] = $be->summary . ' ' .
176 XMLStringer::estring('a', array('href' => $url,
177 'class' => 'blog-entry'),
179 $text = html_entity_decode(strip_tags($be->summary), ENT_QUOTES, 'UTF-8');
181 $options['rendered'] = $be->content;
182 $text = html_entity_decode(strip_tags($be->content), ENT_QUOTES, 'UTF-8');
186 if (Notice::contentTooLong($text)) {
187 $text = substr($text, 0, Notice::maxContent() - mb_strlen($shortUrl) - 2) .
191 // Override this no matter what.
193 $options['object_type'] = self::TYPE;
195 $source = array_key_exists('source', $options) ?
196 $options['source'] : 'web';
198 $saved = Notice::saveNew($profile->id, $text, $source, $options);
204 * Summarize the contents of a blog post
206 * We take the first div or paragraph of the blog post if there's a hit;
207 * Otherwise we take the whole thing.
209 * @param string $html HTML of full content
211 static function summarize($html)
213 if (preg_match('#<p>.*?</p>#s', $html, $matches)) {
215 } else if (preg_match('#<div>.*?</div>#s', $html, $matches)) {
222 static function fromNotice($notice)
224 return Blog_entry::staticGet('uri', $notice->uri);
229 return Notice::staticGet('uri', $this->uri);
232 function asActivityObject()
234 $obj = new ActivityObject();
236 $obj->id = $this->uri;
237 $obj->type = self::TYPE;
238 $obj->title = $this->title;
239 $obj->summary = $this->summary;
240 $obj->content = $this->content;
241 $obj->link = $this->url;
247 * Clean up input HTML
249 static function purify($html)
251 require_once INSTALLDIR.'/extlib/htmLawed/htmLawed.php';
253 $config = array('safe' => 1,
254 'deny_attribute' => 'id,style,on*');
255 $pure = htmLawed($html, $config);