2015-10-03 01:02:37 +01:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* Laconica - a distributed open-source microblogging tool
|
|
|
|
* Copyright (C) 2008, 2009, Control Yourself, Inc.
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!defined('GNUSOCIAL')) { exit(1); }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Table Definition for deleted_notice
|
|
|
|
*/
|
|
|
|
|
|
|
|
class Deleted_notice extends Managed_DataObject
|
|
|
|
{
|
|
|
|
public $__table = 'deleted_notice'; // table name
|
|
|
|
public $id; // int(4) primary_key not_null
|
|
|
|
public $profile_id; // int(4) not_null
|
|
|
|
public $uri; // varchar(191) unique_key not 255 because utf8mb4 takes more space
|
2015-10-08 12:20:17 +01:00
|
|
|
public $act_created; // datetime() not_null
|
2015-10-03 01:02:37 +01:00
|
|
|
public $created; // datetime() not_null
|
|
|
|
|
|
|
|
public static function schemaDef()
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
'fields' => array(
|
2015-10-03 15:07:19 +01:00
|
|
|
'id' => array('type' => 'int', 'not null' => true, 'description' => 'notice ID'),
|
2015-10-10 10:08:17 +01:00
|
|
|
'profile_id' => array('type' => 'int', 'not null' => true, 'description' => 'profile that deleted the notice'),
|
2015-10-03 01:02:37 +01:00
|
|
|
'uri' => array('type' => 'varchar', 'length' => 191, 'description' => 'URI of the deleted notice'),
|
|
|
|
'act_created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date the notice record was created'),
|
|
|
|
'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date the notice record was deleted'),
|
|
|
|
),
|
|
|
|
'primary key' => array('id'),
|
|
|
|
'unique keys' => array(
|
2015-10-03 15:07:19 +01:00
|
|
|
'deleted_notice_uri_key' => array('uri'),
|
2015-10-03 01:02:37 +01:00
|
|
|
),
|
|
|
|
'indexes' => array(
|
|
|
|
'deleted_notice_profile_id_idx' => array('profile_id'),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-10-03 11:26:09 +01:00
|
|
|
public static function addNew(Notice $notice, Profile $actor=null)
|
2015-10-03 01:02:37 +01:00
|
|
|
{
|
2015-10-03 11:26:09 +01:00
|
|
|
if (is_null($actor)) {
|
|
|
|
$actor = $notice->getProfile();
|
|
|
|
}
|
2015-10-03 01:02:37 +01:00
|
|
|
|
2015-10-03 11:26:09 +01:00
|
|
|
if ($notice->getProfile()->hasRole(Profile_role::DELETED)) {
|
|
|
|
// Don't emit notices if the notice author is (being) deleted
|
|
|
|
return false;
|
2015-10-03 01:02:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$act = new Activity();
|
|
|
|
$act->verb = ActivityVerb::DELETE;
|
|
|
|
$act->time = time();
|
2015-10-10 10:08:17 +01:00
|
|
|
$act->id = $notice->getUri();
|
2015-10-03 01:02:37 +01:00
|
|
|
|
|
|
|
$act->content = sprintf(_m('<a href="%1$s">%2$s</a> deleted notice <a href="%3$s">{{%4$s}}</a>.'),
|
|
|
|
htmlspecialchars($actor->getUrl()),
|
|
|
|
htmlspecialchars($actor->getBestName()),
|
|
|
|
htmlspecialchars($notice->getUrl()),
|
|
|
|
htmlspecialchars($notice->getUri())
|
|
|
|
);
|
|
|
|
|
|
|
|
$act->actor = $actor->asActivityObject();
|
2015-10-03 11:26:09 +01:00
|
|
|
$act->target = new ActivityObject(); // We don't save the notice object, as it's supposed to be removed!
|
2015-10-03 01:02:37 +01:00
|
|
|
$act->target->id = $notice->getUri();
|
2016-01-21 01:10:34 +00:00
|
|
|
try {
|
|
|
|
$act->target->type = $notice->getObjectType();
|
|
|
|
} catch (NoObjectTypeException $e) {
|
|
|
|
// This could be for example an RSVP which is a verb and carries no object-type
|
|
|
|
$act->target->type = null;
|
|
|
|
}
|
2015-10-03 01:02:37 +01:00
|
|
|
$act->objects = array(clone($act->target));
|
|
|
|
|
|
|
|
$url = $notice->getUrl();
|
|
|
|
$act->selfLink = $url;
|
|
|
|
$act->editLink = $url;
|
|
|
|
|
|
|
|
// This will make ActivityModeration run saveObjectFromActivity which adds
|
|
|
|
// a new Deleted_notice entry in the database as well as deletes the notice
|
|
|
|
// if the actor has permission to do so.
|
|
|
|
$stored = Notice::saveActivity($act, $actor);
|
|
|
|
|
|
|
|
return $stored;
|
|
|
|
}
|
|
|
|
|
|
|
|
static public function fromStored(Notice $stored)
|
|
|
|
{
|
|
|
|
$class = get_called_class();
|
2015-12-31 11:41:30 +00:00
|
|
|
return self::getByKeys( ['uri' => $stored->getUri()] );
|
2015-10-03 01:02:37 +01:00
|
|
|
}
|
|
|
|
|
2015-10-10 10:08:17 +01:00
|
|
|
// The one who deleted the notice, not the notice's author
|
2015-10-03 01:02:37 +01:00
|
|
|
public function getActor()
|
|
|
|
{
|
|
|
|
return Profile::getByID($this->profile_id);
|
|
|
|
}
|
|
|
|
|
2015-10-10 10:08:17 +01:00
|
|
|
// As above: The one who deleted the notice, not the notice's author
|
2015-10-03 15:07:19 +01:00
|
|
|
public function getActorObject()
|
|
|
|
{
|
|
|
|
return $this->getActor()->asActivityObject();
|
|
|
|
}
|
|
|
|
|
2015-10-03 01:02:37 +01:00
|
|
|
static public function getObjectType()
|
|
|
|
{
|
|
|
|
return 'activity';
|
|
|
|
}
|
|
|
|
|
|
|
|
protected $_stored = array();
|
|
|
|
|
|
|
|
public function getStored()
|
|
|
|
{
|
2015-10-10 10:08:17 +01:00
|
|
|
$uri = $this->getUri();
|
2015-10-03 01:02:37 +01:00
|
|
|
if (!isset($this->_stored[$uri])) {
|
2015-10-10 21:20:53 +01:00
|
|
|
$this->_stored[$uri] = Notice::getByPK(array('uri' => $uri));
|
2015-10-03 01:02:37 +01:00
|
|
|
}
|
|
|
|
return $this->_stored[$uri];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getUri()
|
|
|
|
{
|
2015-10-03 14:31:56 +01:00
|
|
|
return $this->uri;
|
2015-10-03 01:02:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function asActivityObject(Profile $scoped=null)
|
|
|
|
{
|
|
|
|
$actobj = new ActivityObject();
|
|
|
|
$actobj->id = $this->getUri();
|
2015-10-03 11:29:22 +01:00
|
|
|
$actobj->type = ActivityObject::ACTIVITY;
|
2015-10-03 01:02:37 +01:00
|
|
|
$actobj->actor = $this->getActorObject();
|
|
|
|
$actobj->target = new ActivityObject();
|
2015-10-10 10:08:17 +01:00
|
|
|
$actobj->target->id = $this->getUri();
|
|
|
|
// FIXME: actobj->target->type? as in extendActivity, and actobj->target->actor maybe?
|
2015-10-03 01:02:37 +01:00
|
|
|
$actobj->objects = array(clone($actobj->target));
|
|
|
|
$actobj->verb = ActivityVerb::DELETE;
|
|
|
|
$actobj->title = ActivityUtils::verbToTitle($actobj->verb);
|
|
|
|
|
|
|
|
$actor = $this->getActor();
|
|
|
|
$actobj->content = sprintf(_m('<a href="%1$s">%2$s</a> deleted notice {{%3$s}}.'),
|
|
|
|
htmlspecialchars($actor->getUrl()),
|
|
|
|
htmlspecialchars($actor->getBestName()),
|
2015-10-10 10:08:17 +01:00
|
|
|
htmlspecialchars($this->getUri())
|
2015-10-03 01:02:37 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
return $actobj;
|
|
|
|
}
|
|
|
|
|
2015-10-03 13:39:49 +01:00
|
|
|
static public function extendActivity(Notice $stored, Activity $act, Profile $scoped=null)
|
|
|
|
{
|
2015-10-10 10:08:17 +01:00
|
|
|
// the original notice id and type is still stored in the Notice table
|
|
|
|
// so we use that information to describe the delete activity
|
2015-10-03 14:39:06 +01:00
|
|
|
$act->target = new ActivityObject();
|
2015-10-10 10:08:17 +01:00
|
|
|
$act->target->id = $stored->getUri();
|
|
|
|
$act->target->type = $stored->getObjectType();
|
2015-10-03 13:39:49 +01:00
|
|
|
$act->objects = array(clone($act->target));
|
|
|
|
|
|
|
|
$act->title = ActivityUtils::verbToTitle($act->verb);
|
|
|
|
}
|
|
|
|
|
2015-10-03 01:02:37 +01:00
|
|
|
static public function beforeSchemaUpdate()
|
|
|
|
{
|
|
|
|
$table = strtolower(get_called_class());
|
|
|
|
$schema = Schema::get();
|
|
|
|
$schemadef = $schema->getTableDef($table);
|
|
|
|
|
2015-12-31 11:33:59 +00:00
|
|
|
// 2015-12-31 If we have the act_uri field we want to remove it
|
|
|
|
// since there's no difference in delete verbs and the original URI
|
|
|
|
// but the act_created field stays.
|
|
|
|
if (!isset($schemadef['fields']['act_uri']) && isset($schemadef['fields']['act_created'])) {
|
|
|
|
// We don't have an act_uri field, and we also have act_created, so no need to migrate.
|
2015-10-03 01:02:37 +01:00
|
|
|
return;
|
2015-12-31 11:33:59 +00:00
|
|
|
} elseif (isset($schemadef['fields']['act_uri']) && !isset($schemadef['fields']['act_created'])) {
|
|
|
|
throw new ServerException('Something is wrong with your database, you have the act_uri field but NOT act_created in deleted_notice!');
|
2015-10-03 01:02:37 +01:00
|
|
|
}
|
2015-12-31 11:33:59 +00:00
|
|
|
|
|
|
|
if (!isset($schemadef['fields']['act_created'])) {
|
|
|
|
// this is a "normal" upgrade from StatusNet for example
|
|
|
|
echo "\nFound old $table table, upgrading it to add 'act_created' field...";
|
|
|
|
|
|
|
|
$schemadef['fields']['act_created'] = array('type' => 'datetime', 'not null' => true, 'description' => 'datetime the notice record was created');
|
2016-03-15 15:52:57 +00:00
|
|
|
$schemadef['fields']['uri']['length'] = 191; // we likely don't have to discover too long keys here
|
2015-12-31 11:33:59 +00:00
|
|
|
$schema->ensureTable($table, $schemadef);
|
|
|
|
|
|
|
|
$deleted = new Deleted_notice();
|
|
|
|
// we don't actually know when the notice was created for the old ones
|
|
|
|
$deleted->query('UPDATE deleted_notice SET act_created=created;');
|
|
|
|
} else {
|
|
|
|
// 2015-10-03 For a while we had act_uri and act_created fields which
|
|
|
|
// apparently wasn't necessary.
|
|
|
|
echo "\nFound old $table table, upgrading it to remove 'act_uri' field...";
|
|
|
|
|
|
|
|
// we stored what should be in 'uri' in the 'act_uri' field for some night-coding reason.
|
|
|
|
$deleted = new Deleted_notice();
|
|
|
|
$deleted->query('UPDATE deleted_notice SET uri=act_uri;');
|
2015-10-03 01:02:37 +01:00
|
|
|
}
|
|
|
|
print "DONE.\n";
|
|
|
|
print "Resuming core schema upgrade...";
|
|
|
|
}
|
|
|
|
|
2015-12-31 11:41:30 +00:00
|
|
|
function insert()
|
|
|
|
{
|
|
|
|
$result = parent::insert();
|
|
|
|
|
|
|
|
if ($result === false) {
|
|
|
|
common_log_db_error($this, 'INSERT', __FILE__);
|
|
|
|
// TRANS: Server exception thrown when a stored object entry cannot be saved.
|
|
|
|
throw new ServerException('Could not save Deleted_notice');
|
|
|
|
}
|
|
|
|
}
|
2015-10-03 01:02:37 +01:00
|
|
|
}
|