]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/ActivityModeration/classes/Deleted_notice.php
Some obvious bug fixes for i18n
[quix0rs-gnu-social.git] / plugins / ActivityModeration / classes / Deleted_notice.php
index c450c0eb6389f8bf52b6fddb3bf422d085a680d8..7a0c6a7f050b40a74a9003ee4ffec9da5a66a731 100644 (file)
@@ -29,24 +29,22 @@ class Deleted_notice extends Managed_DataObject
     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
-    public $act_uri;                         // varchar(191)  unique_key   not 255 because utf8mb4 takes more space
+    public $act_created;                     // datetime()   not_null
     public $created;                         // datetime()   not_null
-    public $deleted;                         // datetime()   not_null
 
     public static function schemaDef()
     {
         return array(
             'fields' => array(
-                'id' => array('type' => 'int', 'not null' => true, 'description' => 'identity of notice'),
-                'profile_id' => array('type' => 'int', 'not null' => true, 'description' => 'author of the notice'),
+                'id' => array('type' => 'int', 'not null' => true, 'description' => 'notice ID'),
+                'profile_id' => array('type' => 'int', 'not null' => true, 'description' => 'profile that deleted the notice'),
                 'uri' => array('type' => 'varchar', 'length' => 191, 'description' => 'URI of the deleted notice'),
-                'act_uri' => array('type' => 'varchar', 'length' => 191, 'description' => 'URI of the delete activity, may exist in notice table'),
                 '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(
-                'deleted_notice_act_uri_key' => array('act_uri'),
+                'deleted_notice_uri_key' => array('uri'),
             ),
             'indexes' => array(
                 'deleted_notice_profile_id_idx' => array('profile_id'),
@@ -54,23 +52,21 @@ class Deleted_notice extends Managed_DataObject
         );
     }
 
-    public static function addNew(Notice $notice)
+    public static function addNew(Notice $notice, Profile $actor=null)
     {
-        $actor = $notice->getProfile();
+        if (is_null($actor)) {
+            $actor = $notice->getProfile();
+        }
 
-        if ($actor->hasRole(Profile_role::DELETED)) {
-            // Don't emit notices if the user is deleted
-            return true;
+        if ($notice->getProfile()->hasRole(Profile_role::DELETED)) {
+            // Don't emit notices if the notice author is (being) deleted
+            return false;
         }
 
         $act = new Activity();
-        $act->type = ActivityObject::ACTIVITY;
         $act->verb = ActivityVerb::DELETE;
         $act->time = time();
-        $act->id   = TagURI::mint('deleted_notice:%d:%d:%s',
-                            $actor->getID(),
-                            $notice->getID(),
-                            common_date_iso8601(common_sql_now()));
+        $act->id   = $notice->getUri();
 
         $act->content = sprintf(_m('<a href="%1$s">%2$s</a> deleted notice <a href="%3$s">{{%4$s}}</a>.'),
                             htmlspecialchars($actor->getUrl()),
@@ -80,8 +76,14 @@ class Deleted_notice extends Managed_DataObject
                            );
 
         $act->actor = $actor->asActivityObject();
-        $act->target = new ActivityObject();
+        $act->target = new ActivityObject();    // We don't save the notice object, as it's supposed to be removed!
         $act->target->id = $notice->getUri();
+        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;
+        }
         $act->objects = array(clone($act->target));
 
         $url = $notice->getUrl();
@@ -99,19 +101,21 @@ class Deleted_notice extends Managed_DataObject
     static public function fromStored(Notice $stored)
     {
         $class = get_called_class();
-        $object = new $class;
-        $object->act_uri = $stored->getUri();
-        if (!$object->find(true)) {
-            throw new NoResultException($object);
-        }
-        return $object;
+        return self::getByKeys( ['uri' => $stored->getUri()] );
     }
 
+    // The one who deleted the notice, not the notice's author
     public function getActor()
     {
         return Profile::getByID($this->profile_id);
     }
 
+    // As above: The one who deleted the notice, not the notice's author
+    public function getActorObject()
+    {
+        return $this->getActor()->asActivityObject();
+    }
+
     static public function getObjectType()
     {
         return 'activity';
@@ -121,60 +125,53 @@ class Deleted_notice extends Managed_DataObject
 
     public function getStored()
     {
-        $uri = $this->getTargetUri();
+        $uri = $this->getUri();
         if (!isset($this->_stored[$uri])) {
-            $stored = new Notice();
-            $stored->uri = $uri;
-            if (!$stored->find(true)) {
-                throw new NoResultException($stored);
-            }
-            $this->_stored[$uri] = $stored;
+            $this->_stored[$uri] = Notice::getByPK(array('uri' => $uri));
         }
         return $this->_stored[$uri];
     }
 
-    public function getTargetUri()
-    {
-        return $this->uri;
-    }
-
     public function getUri()
     {
-        return $this->act_uri;
+        return $this->uri;
     }
 
     public function asActivityObject(Profile $scoped=null)
     {
         $actobj = new ActivityObject();
         $actobj->id = $this->getUri();
-        $actobj->type = ActivityUtils::resolveUri(self::getObjectType());
+        $actobj->type = ActivityObject::ACTIVITY;
         $actobj->actor = $this->getActorObject();
         $actobj->target = new ActivityObject();
-        $actobj->target->id = $this->getTargetUri();
+        $actobj->target->id = $this->getUri();
+        // FIXME: actobj->target->type? as in extendActivity, and actobj->target->actor maybe?
         $actobj->objects = array(clone($actobj->target));
         $actobj->verb = ActivityVerb::DELETE;
         $actobj->title = ActivityUtils::verbToTitle($actobj->verb);
 
         $actor = $this->getActor();
+        // TRANS: Notice HTML content of a deleted notice. %1$s is the
+        // TRANS: actor's URL, %2$s its "fancy name" and %3$s the notice URI.
         $actobj->content = sprintf(_m('<a href="%1$s">%2$s</a> deleted notice {{%3$s}}.'),
                             htmlspecialchars($actor->getUrl()),
-                            htmlspecialchars($actor->getBestName()),
-                            htmlspecialchars($actor->getTargetUri())
+                            htmlspecialchars($actor->getFancyName()),
+                            htmlspecialchars($this->getUri())
                            );
 
         return $actobj;
     }
 
-    static function newUri(Profile $actor, Managed_DataObject $object, $created=null)
+    static public function extendActivity(Notice $stored, Activity $act, Profile $scoped=null)
     {
-        if (is_null($created)) {
-            $created = common_sql_now();
-        }
-        return TagURI::mint(strtolower(get_called_class()).':%d:%s:%d:%s',
-                                        $actor->getID(),
-                                        ActivityUtils::resolveUri(self::getObjectType(), true),
-                                        $object->getID(),
-                                        common_date_iso8601($created));
+        // the original notice id and type is still stored in the Notice table
+        // so we use that information to describe the delete activity
+        $act->target = new ActivityObject();
+        $act->target->id = $stored->getUri();
+        $act->target->type = $stored->getObjectType();
+        $act->objects = array(clone($act->target));
+
+        $act->title = ActivityUtils::verbToTitle($act->verb);
     }
 
     static public function beforeSchemaUpdate()
@@ -183,42 +180,48 @@ class Deleted_notice extends Managed_DataObject
         $schema = Schema::get();
         $schemadef = $schema->getTableDef($table);
 
-        // 2015-10-03 We change the meaning of the 'uri' field and move its 
-        // content to the 'act_uri' for the deleted activity. act_created is
-        // added too.
-        if (isset($schemadef['fields']['act_uri'])) {
-            // We already have the act_uri field, so no need to migrate to it.
+        // 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.
             return;
+        } 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!');
         }
-        echo "\nFound old $table table, upgrading it to contain 'act_uri' and 'act_created' field...";
-
-        $schemadef['fields']['act_uri'] = array('type' => 'varchar', 'not null' => true, 'length' => 191, 'description' => 'URI of the delete activity, may exist in notice table');
-        $schemadef['fields']['act_created'] = array('type' => 'datetime', 'not null' => true, 'description' => 'date the notice record was created');
-        unset($schemadef['unique keys']);
-        $schema->ensureTable($table, $schemadef);
 
-        $deleted = new Deleted_notice();
-        $result = $deleted->find();
-        if ($result === false) {
-            print "\nFound no deleted_notice entries, continuing...";
-            return true;
-        }
-        print "\nFound $result deleted_notice entries, aligning with new database layout: ";
-        while($deleted->fetch()) {
-            $orig = clone($deleted);
-            $deleted->act_uri = $deleted->uri;
-            // this is a fake URI just to have something to put there to avoid NULL
-            $deleted->uri = TagURI::mint(strtolower(get_called_class()).':%d:%s:%s:%s',
-                                $deleted->profile_id,
-                                ActivityUtils::resolveUri(self::getObjectType(), true),
-                                'unknown',
-                                common_date_iso8601($deleted->created));
-            $deleted->act_created = $deleted->created;  // we don't actually know when the notice was created
-            $deleted->updateWithKeys($orig, 'id');
-            print ".";
+        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');
+            $schemadef['fields']['uri']['length'] = 191;    // we likely don't have to discover too long keys here
+            $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;');
         }
         print "DONE.\n";
         print "Resuming core schema upgrade...";
     }
 
+    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');
+        }
+    }
 }