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_uri; // varchar(191) unique_key not 255 because utf8mb4 takes more space
33 public $act_created; // datetime() not_null
34 public $created; // datetime() not_null
36 public static function schemaDef()
40 'id' => array('type' => 'int', 'not null' => true, 'description' => 'notice ID'),
41 'profile_id' => array('type' => 'int', 'not null' => true, 'description' => 'profile that deleted the notice'),
42 'uri' => array('type' => 'varchar', 'length' => 191, 'description' => 'URI of the deleted notice'),
43 'act_uri' => array('type' => 'varchar', 'length' => 191, 'description' => 'URI of the delete activity, may exist in notice table'),
44 'act_created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date the notice record was created'),
45 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date the notice record was deleted'),
47 'primary key' => array('id'),
48 'unique keys' => array(
49 'deleted_notice_uri_key' => array('uri'),
50 'deleted_notice_act_uri_key' => array('act_uri'),
53 'deleted_notice_profile_id_idx' => array('profile_id'),
58 public static function addNew(Notice $notice, Profile $actor=null)
60 if (is_null($actor)) {
61 $actor = $notice->getProfile();
64 if ($notice->getProfile()->hasRole(Profile_role::DELETED)) {
65 // Don't emit notices if the notice author is (being) deleted
69 $act = new Activity();
70 $act->verb = ActivityVerb::DELETE;
72 $act->id = $notice->getUri();
74 $act->content = sprintf(_m('<a href="%1$s">%2$s</a> deleted notice <a href="%3$s">{{%4$s}}</a>.'),
75 htmlspecialchars($actor->getUrl()),
76 htmlspecialchars($actor->getBestName()),
77 htmlspecialchars($notice->getUrl()),
78 htmlspecialchars($notice->getUri())
81 $act->actor = $actor->asActivityObject();
82 $act->target = new ActivityObject(); // We don't save the notice object, as it's supposed to be removed!
83 $act->target->id = $notice->getUri();
84 $act->target->type = $notice->getObjectType();
85 $act->objects = array(clone($act->target));
87 $url = $notice->getUrl();
88 $act->selfLink = $url;
89 $act->editLink = $url;
91 // This will make ActivityModeration run saveObjectFromActivity which adds
92 // a new Deleted_notice entry in the database as well as deletes the notice
93 // if the actor has permission to do so.
94 $stored = Notice::saveActivity($act, $actor);
99 static public function fromStored(Notice $stored)
101 $class = get_called_class();
102 return self::getByPK(array('uri' => $stored->getUri()));
105 // The one who deleted the notice, not the notice's author
106 public function getActor()
108 return Profile::getByID($this->profile_id);
111 // As above: The one who deleted the notice, not the notice's author
112 public function getActorObject()
114 return $this->getActor()->asActivityObject();
117 static public function getObjectType()
122 protected $_stored = array();
124 public function getStored()
126 $uri = $this->getUri();
127 if (!isset($this->_stored[$uri])) {
128 $this->_stored[$uri] = Notice::getByPK(array('uri' => $uri));
130 return $this->_stored[$uri];
133 public function getUri()
138 public function asActivityObject(Profile $scoped=null)
140 $actobj = new ActivityObject();
141 $actobj->id = $this->getUri();
142 $actobj->type = ActivityObject::ACTIVITY;
143 $actobj->actor = $this->getActorObject();
144 $actobj->target = new ActivityObject();
145 $actobj->target->id = $this->getUri();
146 // FIXME: actobj->target->type? as in extendActivity, and actobj->target->actor maybe?
147 $actobj->objects = array(clone($actobj->target));
148 $actobj->verb = ActivityVerb::DELETE;
149 $actobj->title = ActivityUtils::verbToTitle($actobj->verb);
151 $actor = $this->getActor();
152 $actobj->content = sprintf(_m('<a href="%1$s">%2$s</a> deleted notice {{%3$s}}.'),
153 htmlspecialchars($actor->getUrl()),
154 htmlspecialchars($actor->getBestName()),
155 htmlspecialchars($this->getUri())
161 static public function extendActivity(Notice $stored, Activity $act, Profile $scoped=null)
163 // the original notice id and type is still stored in the Notice table
164 // so we use that information to describe the delete activity
165 $act->target = new ActivityObject();
166 $act->target->id = $stored->getUri();
167 $act->target->type = $stored->getObjectType();
168 $act->objects = array(clone($act->target));
170 $act->title = ActivityUtils::verbToTitle($act->verb);
173 static public function beforeSchemaUpdate()
175 $table = strtolower(get_called_class());
176 $schema = Schema::get();
177 $schemadef = $schema->getTableDef($table);
179 // 2015-10-03 We change the meaning of the 'uri' field and move its
180 // content to the 'act_uri' for the deleted activity. act_created is
182 if (isset($schemadef['fields']['act_uri'])) {
183 // We already have the act_uri field, so no need to migrate to it.
186 echo "\nFound old $table table, upgrading it to contain 'act_uri' and 'act_created' field...";
188 $schemadef['fields']['act_uri'] = array('type' => 'varchar', 'not null' => true, 'length' => 191, 'description' => 'URI of the deleted notice');
189 $schemadef['fields']['act_created'] = array('type' => 'datetime', 'not null' => true, 'description' => 'datetime the notice record was created');
190 unset($schemadef['unique keys']);
191 $schema->ensureTable($table, $schemadef);
193 $deleted = new Deleted_notice();
194 $result = $deleted->find();
195 if ($result === false) {
196 print "\nFound no deleted_notice entries, continuing...";
199 print "\nFound $result deleted_notice entries, aligning with new database layout: ";
200 while($deleted->fetch()) {
201 $orig = clone($deleted);
202 $deleted->act_uri = $deleted->uri;
203 // this is a fake URI just to have something to put there to avoid NULL. crc32 of uri is to avoid collisions
204 $deleted->uri = TagURI::mint(strtolower(get_called_class()).':%d:%s:%s:%s:crc32=%x',
205 $deleted->profile_id,
206 ActivityUtils::resolveUri(self::getObjectType(), true),
208 common_date_iso8601($deleted->created),
209 crc32($deleted->act_uri)
211 $deleted->act_created = $deleted->created; // we don't actually know when the notice was created
212 $deleted->updateWithKeys($orig, 'id');
216 print "Resuming core schema upgrade...";