/** * Import user accounts from wordpress * * @param int $limit * The amount of records to import at one time */ public function import($limit = 0) { $count = 0; $table = $this->model->table; $id = (int) DB::table($table)->max('id'); $wordpressLogs = $this->db->table('wp_badgeos_logs')->where('id', '>', $id)->orderBy('id', 'asc')->limit($limit)->get(); // Use dummy model to get action types $l = new $this->model(); foreach ($wordpressLogs as $wlog) { if (!in_array($wlog->action, $l->actionTypes)) { continue; } $object = false; $log = new $this->model(); $log->id = $wlog->id; $log->user_id = $wlog->user_id; $log->action = $wlog->action; $log->message = $wlog->message; $log->points_earned = $wlog->points_earned; $log->total_points = $wlog->total_points; $log->timestamp = $wlog->timestamp; $log->timezone = $wlog->timezone; if ($wlog->action == 'artwork') { $log->artwork_id = $wlog->object_id; $object = OctoberActivity::where('activity_type', '=', 'LikeWorkOfArt')->first(); } else { // Get the wordpress post type $post_type = $this->db->table('wp_posts')->select('post_type')->where('ID', $wlog->object_id)->first(); if (isset($post_type->post_type)) { // Convert the post type to a usable object model switch ($post_type->post_type) { case 'activity': $object = OctoberActivity::findWordpress($wlog->object_id); break; case 'badge': $object = OctoberBadge::findWordpress($wlog->object_id); break; case 'badgeos-rewards': $object = OctoberReward::findWordpress($wlog->object_id); break; case 'dma-location': $object = OctoberLocation::findWordpress($wlog->object_id); break; case 'step': $object = OctoberStep::findWordpress($wlog->object_id); break; default: continue; } } else { continue; } } try { if ($log->save()) { // If the log is related to an object, save that relation if ($object) { $object = $object->first(); // Ugly hack to get sync working for now if (get_class($object) == 'DMA\\Friends\\Wordpress\\ActivityLog') { continue; } $object->activityLogs()->save($log); } $count++; } } catch (Exception $e) { echo "Failed to import log entry id: " . $log->id . "\n"; echo $e->getMessage() . "\n"; } } return $count; }
/** * Execute the console command. * @return void */ public function fire() { // Taxonomy terms $termRelations = $this->db->table('wp_term_relationships')->get(); foreach ($termRelations as $relation) { $activity = Activity::findWordpress($relation->object_id)->first(); if ($activity) { if (!$activity->categories->contains($relation->term_taxonomy_id)) { $category = Category::find($relation->term_taxonomy_id); if ($category) { $activity->categories()->save($category); } } } } // p2p connections $p2ps = $this->db->table('wp_p2p')->get(); foreach ($p2ps as $p2p) { list($from, $t, $to) = explode('-', $p2p->p2p_type); switch ($from) { case 'activity': $from = Activity::findWordpress($p2p->p2p_from)->first(); $from_table = 'activity'; break; case 'badge': $from = Badge::findWordpress($p2p->p2p_from)->first(); $from_table = 'badge'; break; case 'dma-location': $from = Location::findWordpress($p2p->p2p_from)->first(); $from_table = 'location'; break; case 'step': $from = Step::findWordpress($p2p->p2p_from)->first(); $from_table = 'step'; break; default: $from = false; } switch ($to) { case 'activity': $to = Activity::findWordpress($p2p->p2p_to)->first(); $to_table = 'activity'; break; case 'badge': $to = Badge::findWordpress($p2p->p2p_to)->first(); $to_table = 'badge'; break; case 'dma-location': $to = Location::findWordpress($p2p->p2p_to)->first(); $to_table = 'location'; break; case 'step': $to = Step::findWordpress($p2p->p2p_to)->first(); $to_table = 'step'; break; default: $to = false; } if ($from && $to) { $table = 'dma_friends_' . $from_table . '_' . $to_table; switch ($table) { case 'dma_friends_activity_step': $from->steps()->save($to); $this->info('activity: ' . $from->title . ' --> step: ' . $to->title); break; case 'dma_friends_step_badge': $to->steps()->save($from); $this->info('step: ' . $from->title . ' --> badge: ' . $to->title); break; default: $values = [$from_table . '_id' => $from->id, $to_table . '_id' => $to->id]; if (Schema::hasTable($table)) { DB::table($table)->insert($values); $this->info('from: ' . $from->title . ' ----- ' . $to->title); } else { $this->error('table doesnt exist: ' . $table); } } } } // User achievements $achievements = $this->db->table('wp_usermeta')->where('meta_key', '_badgeos_achievements')->get(); $post = new Post(); $this->info('Sync Achievements'); foreach ($achievements as $achievement) { $user = User::find($achievement->user_id); if (empty($user)) { continue; } // Flush existing records DB::table($this->userStepTable)->where('user_id', $user->id)->delete(); DB::table($this->userBadgeTable)->where('user_id', $user->id)->delete(); $data = unserialize($achievement->meta_value); // wtf we don't need arrays in our arrays if we want to array $data = array_pop($data); foreach ($data as $d) { $link = ['user_id' => $user->id, 'created_at' => $post->epochToTimestamp($d->date_earned)]; // About half way thru the data for the location key changes. // so lets deal with that if (isset($d->location)) { $location_id = $d->location; } elseif (isset($d->location_earned)) { $location_id = $d->location_earned; } else { $location_id = null; } $location = Location::findWordpress($location_id)->first(); if (isset($location->id)) { $link['location_id'] = $location->id; } if ($d->post_type == 'step') { $step = Step::findWordpress($d->ID)->first(); if ($step) { $link['step_id'] = $step->id; DB::table($this->userStepTable)->insert($link); } } elseif ($d->post_type == 'badge') { $badge = Badge::findWordpress($d->ID)->first(); if ($badge) { $link['badge_id'] = $badge->id; DB::table($this->userBadgeTable)->insert($link); } } } } // Syncronize activities and users $this->info('Importing Activity/User relations'); $table = 'dma_friends_activity_user'; DB::table($table)->delete(); ActivityLog::where('action', '=', 'activity')->chunk(100, function ($activityLogs) use($table) { foreach ($activityLogs as $activityLog) { if ($activityLog->object_id) { echo '.'; $pivotTable = ['user_id' => $activityLog->user_id, 'activity_id' => $activityLog->object_id, 'created_at' => $activityLog->timestamp, 'updated_at' => $activityLog->timestamp]; DB::table($table)->insert($pivotTable); } } }); // Syncronize rewards and users $this->info('Importing Reward/User relations'); $table = 'dma_friends_reward_user'; //DB::table($table)->delete(); ActivityLog::where('action', '=', 'reward')->where('timestamp', '<', '2015-02-02 12:10:35')->chunk(100, function ($activityLogs) use($table) { foreach ($activityLogs as $activityLog) { if ($activityLog->object_id) { echo '.'; if (Reward::find($activityLog->object_id) && User::find($activityLog->user_id)) { $pivotTable = ['user_id' => $activityLog->user_id, 'reward_id' => $activityLog->object_id, 'created_at' => $activityLog->timestamp, 'updated_at' => $activityLog->timestamp]; DB::table($table)->insert($pivotTable); } } } }); $this->info('Sync complete'); }