]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
Move notice location data to Notice_location
authorMikael Nordfeldth <mmn@hethane.se>
Fri, 4 Sep 2015 20:25:11 +0000 (22:25 +0200)
committerMikael Nordfeldth <mmn@hethane.se>
Fri, 4 Sep 2015 20:25:11 +0000 (22:25 +0200)
Will probably take a _long_ time to do scripts/upgrade.php but don't
worry, it can be aborted and resumed.

classes/Notice.php
lib/apiaction.php
lib/noticelistitem.php
lib/rss10action.php
plugins/Mapstraction/actions/map.php

index 050eeaf90db666ef36284b4fab57c5717c4c766c..0d0933115e74615ac24f73ccfec7fe79592b2719 100644 (file)
@@ -65,10 +65,6 @@ class Notice extends Managed_DataObject
     public $is_local;                        // int(4)
     public $source;                          // varchar(32)
     public $conversation;                    // int(4)
-    public $lat;                             // decimal(10,7)
-    public $lon;                             // decimal(10,7)
-    public $location_id;                     // int(4)
-    public $location_ns;                     // int(4)
     public $repeat_of;                       // int(4)
     public $verb;                            // varchar(191)   not 255 because utf8mb4 takes more space
     public $object_type;                     // varchar(191)   not 255 because utf8mb4 takes more space
@@ -93,10 +89,6 @@ class Notice extends Managed_DataObject
                 'is_local' => array('type' => 'int', 'size' => 'tiny', 'default' => 0, 'description' => 'notice was generated by a user'),
                 'source' => array('type' => 'varchar', 'length' => 32, 'description' => 'source of comment, like "web", "im", or "clientname"'),
                 'conversation' => array('type' => 'int', 'description' => 'id of root notice in this conversation'),
-                'lat' => array('type' => 'numeric', 'precision' => 10, 'scale' => 7, 'description' => 'latitude'),
-                'lon' => array('type' => 'numeric', 'precision' => 10, 'scale' => 7, 'description' => 'longitude'),
-                'location_id' => array('type' => 'int', 'description' => 'location id if possible'),
-                'location_ns' => array('type' => 'int', 'description' => 'namespace for location'),
                 'repeat_of' => array('type' => 'int', 'description' => 'notice this is a repeat of'),
                 'object_type' => array('type' => 'varchar', 'length' => 191, 'description' => 'URI representing activity streams object type', 'default' => 'http://activitystrea.ms/schema/1.0/note'),
                 'verb' => array('type' => 'varchar', 'length' => 191, 'description' => 'URI representing activity streams verb', 'default' => 'http://activitystrea.ms/schema/1.0/post'),
@@ -196,6 +188,7 @@ class Notice extends Managed_DataObject
             // Clear related records
 
             $this->clearReplies();
+            $this->clearLocation();
             $this->clearRepeats();
             $this->clearTags();
             $this->clearGroupInboxes();
@@ -621,14 +614,15 @@ class Notice extends Managed_DataObject
             }
         }
 
+        $notloc = new Notice_location();
         if (!empty($lat) && !empty($lon)) {
-            $notice->lat = $lat;
-            $notice->lon = $lon;
+            $notloc->lat = $lat;
+            $notloc->lon = $lon;
         }
 
         if (!empty($location_ns) && !empty($location_id)) {
-            $notice->location_id = $location_id;
-            $notice->location_ns = $location_ns;
+            $notloc->location_id = $location_id;
+            $notloc->location_ns = $location_ns;
         }
 
         if (!empty($rendered)) {
@@ -667,7 +661,13 @@ class Notice extends Managed_DataObject
             // XXX: some of these functions write to the DB
 
             try {
-                $notice->insert();  // throws exception on failure
+                $notice->insert();  // throws exception on failure, if successful we have an ->id
+
+                if (($notloc->lat && $notloc->lon) || ($notloc->location_id && $notloc->location_ns)) {
+                    $notloc->notice_id = $notice->getID();
+                    $notloc->insert();  // store the notice location if it had any information
+                }
+
                 // If it's not part of a conversation, it's
                 // the beginning of a new conversation.
                 if (empty($notice->conversation)) {
@@ -868,15 +868,10 @@ class Notice extends Managed_DataObject
             }
         }
 
+        $notloc = null;
         if ($act->context instanceof ActivityContext) {
-            $location = $act->context->location;
-            if ($location) {
-                $stored->lat = $location->lat;
-                $stored->lon = $location->lon;
-                if ($location->location_id) {
-                    $stored->location_ns = $location->location_ns;
-                    $stored->location_id = $location->location_id;
-                }
+            if ($act->context->location instanceof Location) {
+                $notloc = Notice_location::fromLocation($act->context->location);
             }
         } else {
             $act->context = new ActivityContext();
@@ -903,6 +898,12 @@ class Notice extends Managed_DataObject
 
             try {
                 $stored->insert();    // throws exception on error
+
+                if ($notloc instanceof Notice_location) {
+                    $notloc->notice_id = $stored->getID();
+                    $notloc->insert();
+                }
+
                 $orig = clone($stored); // for updating later in this try clause
 
                 $object = null;
@@ -1844,7 +1845,11 @@ class Notice extends Managed_DataObject
                 // This is not a reply to something
             }
 
-            $ctx->location = $this->getLocation();
+            try {
+                $ctx->location = Notice_location::locFromStored($this);
+            } catch (ServerException $e) {
+                $ctx->location = null;
+            }
 
             $conv = null;
 
@@ -2090,23 +2095,6 @@ class Notice extends Managed_DataObject
         return ($contentlimit > 0 && !empty($content) && (mb_strlen($content) > $contentlimit));
     }
 
-    function getLocation()
-    {
-        $location = null;
-
-        if (!empty($this->location_id) && !empty($this->location_ns)) {
-            $location = Location::fromId($this->location_id, $this->location_ns);
-        }
-
-        if (is_null($location)) { // no ID, or Location::fromId() failed
-            if (!empty($this->lat) && !empty($this->lon)) {
-                $location = Location::fromLatLon($this->lat, $this->lon);
-            }
-        }
-
-        return $location;
-    }
-
     /**
      * Convenience function for posting a repeat of an existing message.
      *
@@ -2277,6 +2265,16 @@ class Notice extends Managed_DataObject
         $reply->free();
     }
 
+    function clearLocation()
+    {
+        $loc = new Notice_location();
+        $loc->notice_id = $this->id;
+
+        if ($loc->find()) {
+            $loc->delete();
+        }
+    }
+
     function clearFiles()
     {
         $f2p = new File_to_post();
@@ -2885,4 +2883,51 @@ class Notice extends Managed_DataObject
             $notice->_setReplies($ids);
         }
     }
+
+    static public function beforeSchemaUpdate()
+    {
+        $table = strtolower(get_called_class());
+        $schema = Schema::get();
+        $schemadef = $schema->getTableDef($table);
+
+        // 2015-09-04 We move Notice location data to Notice_location
+        // First we see if we have to do this at all
+        if (!isset($schemadef['fields']['lat'])
+                && !isset($schemadef['fields']['lon'])
+                && !isset($schemadef['fields']['location_id'])
+                && !isset($schemadef['fields']['location_ns'])) {
+            // We have already removed the location fields, so no need to migrate.
+            return;
+        }
+        // Then we make sure the Notice_location table is created!
+        $schema->ensureTable('notice_location', Notice_location::schemaDef());
+
+        // Then we continue on our road to migration!
+        echo "\nFound old $table table, moving location data to 'notice_location' table... (this will probably take a LONG time, but can be aborted and continued)";
+
+        $notice = new Notice();
+        $notice->query(sprintf('SELECT id, lat, lon, location_id, location_ns FROM %1$s ' .
+                             'WHERE lat IS NOT NULL ' .
+                                'OR lon IS NOT NULL ' .
+                                'OR location_id IS NOT NULL ' .
+                                'OR location_ns IS NOT NULL',
+                             $schema->quoteIdentifier($table)));
+        print "\nFound {$notice->N} notices with location data, inserting";
+        while ($notice->fetch()) {
+            $notloc = Notice_location::getKV('notice_id', $notice->id);
+            if ($notloc instanceof Notice_location) {
+                print "-";
+                continue;
+            }
+            $notloc = new Notice_location();
+            $notloc->notice_id = $notice->id;
+            $notloc->lat= $notice->lat;
+            $notloc->lon= $notice->lon;
+            $notloc->location_id= $notice->location_id;
+            $notloc->location_ns= $notice->location_ns;
+            $notloc->insert();
+            print ".";
+        }
+        print "\n";
+    }
 }
index cd2207b378ae15a2ec6794d948661a1f6c203217..fae8f33d0e0707150612f70c7e2be0cb56d30893 100644 (file)
@@ -366,12 +366,13 @@ class ApiAction extends Action
         $twitter_status['in_reply_to_screen_name'] =
             ($replier_profile) ? $replier_profile->nickname : null;
 
-        if (isset($notice->lat) && isset($notice->lon)) {
+        try {
+            $notloc = Notice_location::locFromStored($notice);
             // This is the format that GeoJSON expects stuff to be in
             $twitter_status['geo'] = array('type' => 'Point',
-                                           'coordinates' => array((float) $notice->lat,
-                                                                  (float) $notice->lon));
-        } else {
+                                           'coordinates' => array((float) $notloc->lat,
+                                                                  (float) $notloc->lon));
+        } catch (ServerException $e) {
             $twitter_status['geo'] = null;
         }
 
@@ -547,13 +548,14 @@ class ApiAction extends Action
             $entry['pubDate'] = common_date_rfc2822($notice->created);
             $entry['guid'] = $entry['link'];
 
-            if (isset($notice->lat) && isset($notice->lon)) {
+            try {
+                $notloc = Notice_location::locFromStored($notice);
                 // This is the format that GeoJSON expects stuff to be in.
                 // showGeoRSS() below uses it for XML output, so we reuse it
                 $entry['geo'] = array('type' => 'Point',
-                                      'coordinates' => array((float) $notice->lat,
-                                                             (float) $notice->lon));
-            } else {
+                                      'coordinates' => array((float) $notloc->lat,
+                                                             (float) $notloc->lon));
+            } catch (ServerException $e) {
                 $entry['geo'] = null;
             }
 
@@ -997,7 +999,13 @@ class ApiAction extends Action
         $statuses = array();
 
         if (is_array($notice)) {
-            $notice = new ArrayWrapper($notice);
+            //FIXME: make everything calling showJsonTimeline use only Notice objects
+            common_debug('ArrayWrapper avoidance in progress! Beep boop, make showJsonTimeline only receive Notice objects!');
+            $ids = array();
+            foreach ($notice as $n) {
+                $ids[] = $n->getID();
+            }
+            $notice = Notice::multiGet('id', $ids);
         }
 
         while ($notice->fetch()) {
index f25613b9a96988972946b96b024cda003c735697..dc171409f41e8042040c38a174a242d3c0c9f3cd 100644 (file)
@@ -368,18 +368,19 @@ class NoticeListItem extends Widget
      */
     function showNoticeLocation()
     {
-        $id = $this->notice->id;
-
-        $location = $this->notice->getLocation();
-
-        if (empty($location)) {
+        return;
+        try {
+            $location = Notice_location::locFromStored($this->notice);
+        } catch (NoResultException $e) {
+            return;
+        } catch (ServerException $e) {
             return;
         }
 
         $name = $location->getName();
 
-        $lat = $this->notice->lat;
-        $lon = $this->notice->lon;
+        $lat = $location->lat;
+        $lon = $location->lon;
         $latlon = (!empty($lat) && !empty($lon)) ? $lat.';'.$lon : '';
 
         if (empty($name)) {
index 744d1e4b7ab758d708c0219a9d285fc7f003e1ec..137015530b0af7b2b8457eea44e0913efb1ec8ba 100644 (file)
@@ -216,15 +216,19 @@ class Rss10Action extends ManagedAction
         $this->element('dc:creator', null, ($profile->fullname) ? $profile->fullname : $profile->nickname);
         $this->element('foaf:maker', array('rdf:resource' => $creator_uri));
         $this->element('sioc:has_creator', array('rdf:resource' => $creator_uri.'#acct'));
-        $location = $notice->getLocation();
-        if ($location && isset($location->lat) && isset($location->lon)) {
-            $location_uri = $location->getRdfURL();
-            $attrs = array('geo:lat' => $location->lat,
-                'geo:long' => $location->lon);
-            if (strlen($location_uri)) {
-                $attrs['rdf:resource'] = $location_uri;
+        try {
+            $location = Notice_location::locFromStored($notice);
+            if (isset($location->lat) && isset($location->lon)) {
+                $location_uri = $location->getRdfURL();
+                $attrs = array('geo:lat' => $location->lat,
+                               'geo:long' => $location->lon);
+                if (strlen($location_uri)) {
+                    $attrs['rdf:resource'] = $location_uri;
+                }
+                $this->element('statusnet:origin', $attrs);
             }
-            $this->element('statusnet:origin', $attrs);
+        } catch (ServerException $e) {
+            // No result, so no location data
         }
         $this->element('statusnet:postIcon', array('rdf:resource' => $profile->avatarUrl()));
         $this->element('cc:licence', array('rdf:resource' => common_config('license', 'url')));
index be59f5ba0b5ecb63f755dbeb8ac853a9e4a3649f..48861af9942605295b9b81bf775ed9c7032420c3 100644 (file)
@@ -120,9 +120,11 @@ class MapAction extends Action
         $jsonArray = array();
 
         while ($this->notice->fetch()) {
-            if (!empty($this->notice->lat) && !empty($this->notice->lon)) {
-                $jsonNotice = $this->noticeAsJson($this->notice);
-                $jsonArray[] = $jsonNotice;
+            try {
+                $notloc = Notice_location::locFromStored($this->notice);
+                $jsonArray[] = $this->noticeAsJson($this->notice);
+            } catch (ServerException $e) {
+                // no location data
             }
         }