]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/ActivityModeration/classes/Deleted_notice.php
Some obvious bug fixes for i18n
[quix0rs-gnu-social.git] / plugins / ActivityModeration / classes / Deleted_notice.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, 2009, Control Yourself, Inc.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 if (!defined('GNUSOCIAL')) { exit(1); }
21
22 /**
23  * Table Definition for deleted_notice
24  */
25
26 class Deleted_notice extends Managed_DataObject
27 {
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
34
35     public static function schemaDef()
36     {
37         return array(
38             'fields' => array(
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'),
44             ),
45             'primary key' => array('id'),
46             'unique keys' => array(
47                 'deleted_notice_uri_key' => array('uri'),
48             ),
49             'indexes' => array(
50                 'deleted_notice_profile_id_idx' => array('profile_id'),
51             ),
52         );
53     }
54
55     public static function addNew(Notice $notice, Profile $actor=null)
56     {
57         if (is_null($actor)) {
58             $actor = $notice->getProfile();
59         }
60
61         if ($notice->getProfile()->hasRole(Profile_role::DELETED)) {
62             // Don't emit notices if the notice author is (being) deleted
63             return false;
64         }
65
66         $act = new Activity();
67         $act->verb = ActivityVerb::DELETE;
68         $act->time = time();
69         $act->id   = $notice->getUri();
70
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())
76                            );
77
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         try {
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;
86         }
87         $act->objects = array(clone($act->target));
88
89         $url = $notice->getUrl();
90         $act->selfLink = $url;
91         $act->editLink = $url;
92
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);
97
98         return $stored;
99     }
100
101     static public function fromStored(Notice $stored)
102     {
103         $class = get_called_class();
104         return self::getByKeys( ['uri' => $stored->getUri()] );
105     }
106
107     // The one who deleted the notice, not the notice's author
108     public function getActor()
109     {
110         return Profile::getByID($this->profile_id);
111     }
112
113     // As above: The one who deleted the notice, not the notice's author
114     public function getActorObject()
115     {
116         return $this->getActor()->asActivityObject();
117     }
118
119     static public function getObjectType()
120     {
121         return 'activity';
122     }
123
124     protected $_stored = array();
125
126     public function getStored()
127     {
128         $uri = $this->getUri();
129         if (!isset($this->_stored[$uri])) {
130             $this->_stored[$uri] = Notice::getByPK(array('uri' => $uri));
131         }
132         return $this->_stored[$uri];
133     }
134
135     public function getUri()
136     {
137         return $this->uri;
138     }
139
140     public function asActivityObject(Profile $scoped=null)
141     {
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);
152
153         $actor = $this->getActor();
154         // TRANS: Notice HTML content of a deleted notice. %1$s is the
155         // TRANS: actor's URL, %2$s its "fancy name" and %3$s the notice URI.
156         $actobj->content = sprintf(_m('<a href="%1$s">%2$s</a> deleted notice {{%3$s}}.'),
157                             htmlspecialchars($actor->getUrl()),
158                             htmlspecialchars($actor->getFancyName()),
159                             htmlspecialchars($this->getUri())
160                            );
161
162         return $actobj;
163     }
164
165     static public function extendActivity(Notice $stored, Activity $act, Profile $scoped=null)
166     {
167         // the original notice id and type is still stored in the Notice table
168         // so we use that information to describe the delete activity
169         $act->target = new ActivityObject();
170         $act->target->id = $stored->getUri();
171         $act->target->type = $stored->getObjectType();
172         $act->objects = array(clone($act->target));
173
174         $act->title = ActivityUtils::verbToTitle($act->verb);
175     }
176
177     static public function beforeSchemaUpdate()
178     {
179         $table = strtolower(get_called_class());
180         $schema = Schema::get();
181         $schemadef = $schema->getTableDef($table);
182
183         // 2015-12-31 If we have the act_uri field we want to remove it
184         // since there's no difference in delete verbs and the original URI
185         // but the act_created field stays.
186         if (!isset($schemadef['fields']['act_uri']) && isset($schemadef['fields']['act_created'])) {
187             // We don't have an act_uri field, and we also have act_created, so no need to migrate.
188             return;
189         } elseif (isset($schemadef['fields']['act_uri']) && !isset($schemadef['fields']['act_created'])) {
190             throw new ServerException('Something is wrong with your database, you have the act_uri field but NOT act_created in deleted_notice!');
191         }
192
193         if (!isset($schemadef['fields']['act_created'])) {
194             // this is a "normal" upgrade from StatusNet for example
195             echo "\nFound old $table table, upgrading it to add 'act_created' field...";
196
197             $schemadef['fields']['act_created'] = array('type' => 'datetime', 'not null' => true, 'description' => 'datetime the notice record was created');
198             $schemadef['fields']['uri']['length'] = 191;    // we likely don't have to discover too long keys here
199             $schema->ensureTable($table, $schemadef);
200
201             $deleted = new Deleted_notice();
202             // we don't actually know when the notice was created for the old ones
203             $deleted->query('UPDATE deleted_notice SET act_created=created;');
204         } else {
205             // 2015-10-03 For a while we had act_uri and act_created fields which
206             // apparently wasn't necessary.
207             echo "\nFound old $table table, upgrading it to remove 'act_uri' field...";
208
209             // we stored what should be in 'uri' in the 'act_uri' field for some night-coding reason.
210             $deleted = new Deleted_notice();
211             $deleted->query('UPDATE deleted_notice SET uri=act_uri;');
212         }
213         print "DONE.\n";
214         print "Resuming core schema upgrade...";
215     }
216
217     function insert()
218     {
219         $result = parent::insert();
220
221         if ($result === false) {
222             common_log_db_error($this, 'INSERT', __FILE__);
223             // TRANS: Server exception thrown when a stored object entry cannot be saved.
224             throw new ServerException('Could not save Deleted_notice');
225         }
226     }
227 }