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();
81 $act->target->type = $notice->getObjectType();
82 $act->objects = array(clone($act->target));
84 $url = $notice->getUrl();
85 $act->selfLink = $url;
86 $act->editLink = $url;
88 // This will make ActivityModeration run saveObjectFromActivity which adds
89 // a new Deleted_notice entry in the database as well as deletes the notice
90 // if the actor has permission to do so.
91 $stored = Notice::saveActivity($act, $actor);
96 static public function fromStored(Notice $stored)
98 $class = get_called_class();
99 return self::getByKeys( ['uri' => $stored->getUri()] );
102 // The one who deleted the notice, not the notice's author
103 public function getActor()
105 return Profile::getByID($this->profile_id);
108 // As above: The one who deleted the notice, not the notice's author
109 public function getActorObject()
111 return $this->getActor()->asActivityObject();
114 static public function getObjectType()
119 protected $_stored = array();
121 public function getStored()
123 $uri = $this->getUri();
124 if (!isset($this->_stored[$uri])) {
125 $this->_stored[$uri] = Notice::getByPK(array('uri' => $uri));
127 return $this->_stored[$uri];
130 public function getUri()
135 public function asActivityObject(Profile $scoped=null)
137 $actobj = new ActivityObject();
138 $actobj->id = $this->getUri();
139 $actobj->type = ActivityObject::ACTIVITY;
140 $actobj->actor = $this->getActorObject();
141 $actobj->target = new ActivityObject();
142 $actobj->target->id = $this->getUri();
143 // FIXME: actobj->target->type? as in extendActivity, and actobj->target->actor maybe?
144 $actobj->objects = array(clone($actobj->target));
145 $actobj->verb = ActivityVerb::DELETE;
146 $actobj->title = ActivityUtils::verbToTitle($actobj->verb);
148 $actor = $this->getActor();
149 $actobj->content = sprintf(_m('<a href="%1$s">%2$s</a> deleted notice {{%3$s}}.'),
150 htmlspecialchars($actor->getUrl()),
151 htmlspecialchars($actor->getBestName()),
152 htmlspecialchars($this->getUri())
158 static public function extendActivity(Notice $stored, Activity $act, Profile $scoped=null)
160 // the original notice id and type is still stored in the Notice table
161 // so we use that information to describe the delete activity
162 $act->target = new ActivityObject();
163 $act->target->id = $stored->getUri();
164 $act->target->type = $stored->getObjectType();
165 $act->objects = array(clone($act->target));
167 $act->title = ActivityUtils::verbToTitle($act->verb);
170 static public function beforeSchemaUpdate()
172 $table = strtolower(get_called_class());
173 $schema = Schema::get();
174 $schemadef = $schema->getTableDef($table);
176 // 2015-12-31 If we have the act_uri field we want to remove it
177 // since there's no difference in delete verbs and the original URI
178 // but the act_created field stays.
179 if (!isset($schemadef['fields']['act_uri']) && isset($schemadef['fields']['act_created'])) {
180 // We don't have an act_uri field, and we also have act_created, so no need to migrate.
182 } elseif (isset($schemadef['fields']['act_uri']) && !isset($schemadef['fields']['act_created'])) {
183 throw new ServerException('Something is wrong with your database, you have the act_uri field but NOT act_created in deleted_notice!');
186 if (!isset($schemadef['fields']['act_created'])) {
187 // this is a "normal" upgrade from StatusNet for example
188 echo "\nFound old $table table, upgrading it to add 'act_created' field...";
190 $schemadef['fields']['act_created'] = array('type' => 'datetime', 'not null' => true, 'description' => 'datetime the notice record was created');
191 $schema->ensureTable($table, $schemadef);
193 $deleted = new Deleted_notice();
194 // we don't actually know when the notice was created for the old ones
195 $deleted->query('UPDATE deleted_notice SET act_created=created;');
197 // 2015-10-03 For a while we had act_uri and act_created fields which
198 // apparently wasn't necessary.
199 echo "\nFound old $table table, upgrading it to remove 'act_uri' field...";
201 // we stored what should be in 'uri' in the 'act_uri' field for some night-coding reason.
202 $deleted = new Deleted_notice();
203 $deleted->query('UPDATE deleted_notice SET uri=act_uri;');
206 print "Resuming core schema upgrade...";
211 $result = parent::insert();
213 if ($result === false) {
214 common_log_db_error($this, 'INSERT', __FILE__);
215 // TRANS: Server exception thrown when a stored object entry cannot be saved.
216 throw new ServerException('Could not save Deleted_notice');