3 * Laconica - a distributed open-source microblogging tool
4 * Copyright (C) 2008, 2009, Control Yourself, Inc.
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 if (!defined('GNUSOCIAL')) { exit(1); }
23 * Table Definition for deleted_notice
26 class Deleted_notice extends Managed_DataObject
28 public $__table = 'deleted_notice'; // table name
29 public $id; // int(4) primary_key not_null
30 public $profile_id; // int(4) not_null
31 public $uri; // varchar(191) unique_key not 255 because utf8mb4 takes more space
32 public $act_created; // datetime() not_null
33 public $created; // datetime() not_null
35 public static function schemaDef()
39 'id' => array('type' => 'int', 'not null' => true, 'description' => 'notice ID'),
40 'profile_id' => array('type' => 'int', 'not null' => true, 'description' => 'profile that deleted the notice'),
41 'uri' => array('type' => 'varchar', 'length' => 191, 'description' => 'URI of the deleted notice'),
42 'act_created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date the notice record was created'),
43 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date the notice record was deleted'),
45 'primary key' => array('id'),
46 'unique keys' => array(
47 'deleted_notice_uri_key' => array('uri'),
50 'deleted_notice_profile_id_idx' => array('profile_id'),
55 public static function addNew(Notice $notice, Profile $actor=null)
57 if (is_null($actor)) {
58 $actor = $notice->getProfile();
61 if ($notice->getProfile()->hasRole(Profile_role::DELETED)) {
62 // Don't emit notices if the notice author is (being) deleted
66 $act = new Activity();
67 $act->verb = ActivityVerb::DELETE;
69 $act->id = $notice->getUri();
71 $act->content = sprintf(_m('<a href="%1$s">%2$s</a> deleted notice <a href="%3$s">{{%4$s}}</a>.'),
72 htmlspecialchars($actor->getUrl()),
73 htmlspecialchars($actor->getBestName()),
74 htmlspecialchars($notice->getUrl()),
75 htmlspecialchars($notice->getUri())
78 $act->actor = $actor->asActivityObject();
79 $act->target = new ActivityObject(); // We don't save the notice object, as it's supposed to be removed!
80 $act->target->id = $notice->getUri();
82 $act->target->type = $notice->getObjectType();
83 } catch (NoObjectTypeException $e) {
84 // This could be for example an RSVP which is a verb and carries no object-type
85 $act->target->type = null;
87 $act->objects = array(clone($act->target));
89 $url = $notice->getUrl();
90 $act->selfLink = $url;
91 $act->editLink = $url;
93 // This will make ActivityModeration run saveObjectFromActivity which adds
94 // a new Deleted_notice entry in the database as well as deletes the notice
95 // if the actor has permission to do so.
96 $stored = Notice::saveActivity($act, $actor);
101 static public function fromStored(Notice $stored)
103 $class = get_called_class();
104 return self::getByKeys( ['uri' => $stored->getUri()] );
107 // The one who deleted the notice, not the notice's author
108 public function getActor()
110 return Profile::getByID($this->profile_id);
113 // As above: The one who deleted the notice, not the notice's author
114 public function getActorObject()
116 return $this->getActor()->asActivityObject();
119 static public function getObjectType()
124 protected $_stored = array();
126 public function getStored()
128 $uri = $this->getUri();
129 if (!isset($this->_stored[$uri])) {
130 $this->_stored[$uri] = Notice::getByPK(array('uri' => $uri));
132 return $this->_stored[$uri];
135 public function getUri()
140 public function asActivityObject(Profile $scoped=null)
142 $actobj = new ActivityObject();
143 $actobj->id = $this->getUri();
144 $actobj->type = ActivityObject::ACTIVITY;
145 $actobj->actor = $this->getActorObject();
146 $actobj->target = new ActivityObject();
147 $actobj->target->id = $this->getUri();
148 // FIXME: actobj->target->type? as in extendActivity, and actobj->target->actor maybe?
149 $actobj->objects = array(clone($actobj->target));
150 $actobj->verb = ActivityVerb::DELETE;
151 $actobj->title = ActivityUtils::verbToTitle($actobj->verb);
153 $actor = $this->getActor();
154 $actobj->content = sprintf(_m('<a href="%1$s">%2$s</a> deleted notice {{%3$s}}.'),
155 htmlspecialchars($actor->getUrl()),
156 htmlspecialchars($actor->getBestName()),
157 htmlspecialchars($this->getUri())
163 static public function extendActivity(Notice $stored, Activity $act, Profile $scoped=null)
165 // the original notice id and type is still stored in the Notice table
166 // so we use that information to describe the delete activity
167 $act->target = new ActivityObject();
168 $act->target->id = $stored->getUri();
169 $act->target->type = $stored->getObjectType();
170 $act->objects = array(clone($act->target));
172 $act->title = ActivityUtils::verbToTitle($act->verb);
175 static public function beforeSchemaUpdate()
177 $table = strtolower(get_called_class());
178 $schema = Schema::get();
179 $schemadef = $schema->getTableDef($table);
181 // 2015-12-31 If we have the act_uri field we want to remove it
182 // since there's no difference in delete verbs and the original URI
183 // but the act_created field stays.
184 if (!isset($schemadef['fields']['act_uri']) && isset($schemadef['fields']['act_created'])) {
185 // We don't have an act_uri field, and we also have act_created, so no need to migrate.
187 } elseif (isset($schemadef['fields']['act_uri']) && !isset($schemadef['fields']['act_created'])) {
188 throw new ServerException('Something is wrong with your database, you have the act_uri field but NOT act_created in deleted_notice!');
191 if (!isset($schemadef['fields']['act_created'])) {
192 // this is a "normal" upgrade from StatusNet for example
193 echo "\nFound old $table table, upgrading it to add 'act_created' field...";
195 $schemadef['fields']['act_created'] = array('type' => 'datetime', 'not null' => true, 'description' => 'datetime the notice record was created');
196 $schema->ensureTable($table, $schemadef);
198 $deleted = new Deleted_notice();
199 // we don't actually know when the notice was created for the old ones
200 $deleted->query('UPDATE deleted_notice SET act_created=created;');
202 // 2015-10-03 For a while we had act_uri and act_created fields which
203 // apparently wasn't necessary.
204 echo "\nFound old $table table, upgrading it to remove 'act_uri' field...";
206 // we stored what should be in 'uri' in the 'act_uri' field for some night-coding reason.
207 $deleted = new Deleted_notice();
208 $deleted->query('UPDATE deleted_notice SET uri=act_uri;');
211 print "Resuming core schema upgrade...";
216 $result = parent::insert();
218 if ($result === false) {
219 common_log_db_error($this, 'INSERT', __FILE__);
220 // TRANS: Server exception thrown when a stored object entry cannot be saved.
221 throw new ServerException('Could not save Deleted_notice');