@@ -47,27 +47,39 @@ public function handle(CSVImportReader $reader)
4747 $ filepath = Storage::disk ('local ' )
4848 ->path ('mismatch-files/ ' . $ this ->meta ->filename );
4949
50- $ db_mismatches_by_current_user = DB ::select (
51- 'select * from users JOIN mismatches ON mismatches.user_id = users.id
52- WHERE mw_userid = :mw_userid ' ,
53- ['mw_userid ' =>$ this ->meta ->user ->mw_userid ]
54- );
50+ // $db_mismatches_by_current_user = DB::select(
51+ // 'select * from users JOIN mismatches ON mismatches.user_id = users.id
52+ // WHERE mw_userid = :mw_userid',
53+ // ['mw_userid' =>$this->meta->user->mw_userid]
54+ // );
5555
56- DB ::transaction (function () use ($ reader , $ filepath , $ db_mismatches_by_current_user ) {
56+ $ mismatches_per_upload_user = DB ::table ('mismatches ' );
57+ // ->join('users', 'mismatches.user_id', '=', 'users.id')
58+ // ->where('mw_userid', '=', $this->meta->user->mw_userid);
5759
60+ // var_dump($mismatches_per_upload_user->first());
5861
59- $ reader ->lines ($ filepath )->each (function ($ mismatchLine ) use ($ db_mismatches_by_current_user ) {
62+ $ mismatches_per_upload_user_get = $ mismatches_per_upload_user ->get ();
63+ Log::info ('$mismatches_per_upload_user: ' . json_encode ($ mismatches_per_upload_user_get ));
6064
61- // TODO: question, should we list all columns one by one or try to do something like
62- // Schema::getColumnListing('mismatches'); // where is mismatches is the table name
63- // and then we remove the column names we dont need... id, username, mw_userid, created at, etc.
64- // too many.
65- // or this is the best option but it needs to be instantiated already to be able to get the attributes
66- // or not?
67- // get attributes from model instance
68- // $column_names = $new_mismatch->getAttributes();
69- // remove column because we dont want to compare with review_status
70- // unset($column_names['review_status']);
65+ $ mismatch_attrs = (new Mismatch ())->getFillable ();
66+ Log::info ('mismatch_attrs ' , $ mismatch_attrs );
67+ // ["item_id","statement_guid","property_id","wikidata_value","meta_wikidata_value","external_value","external_url","review_status","type"]
68+
69+ // $collection = collect($mismatch_attrs);
70+ // $collection->forget('review_status');
71+ // $newArray = [];
72+ // $collection->map(function ($item, $key) use (&$newArray) {
73+ // if ($key != 'type') { // key can be empty in the file but in the db always has statement by default
74+ // $newArray[] = [$key, $item];
75+ // }
76+ // });
77+
78+ // Log::info('$newArray', $newArray);
79+
80+ DB ::transaction (function () use ($ reader , $ filepath , $ mismatches_per_upload_user , $ mismatch_attrs ) {
81+
82+ $ reader ->lines ($ filepath )->each (function ($ mismatchLine ) use ($ mismatches_per_upload_user , $ mismatch_attrs ) {
7183
7284 $ new_mismatch = Mismatch::make ($ mismatchLine );
7385
@@ -80,36 +92,47 @@ public function handle(CSVImportReader $reader)
8092 }
8193 });
8294
83- if (!DB ::table ('mismatches ' )->where ($ newArray )->exists () // checks all fields at the same time
84- // || count($db_mismatches_by_current_user) == 0 //take this out of the lines check. if there are not imports by the current user we import
85- ) {
86- if ($ new_mismatch ->type == null ) {
87- $ new_mismatch ->type = 'statement ' ;
88- }
89- $ new_mismatch ->importMeta ()->associate ($ this ->meta );
90- $ new_mismatch ->save ();
95+ // we add first because there might be duplicates already, so this might return more than 1 result
96+ $ row_in_db = $ mismatches_per_upload_user ->select ($ mismatch_attrs )->where ($ newArray );
97+ $ row_in_db_get = $ row_in_db ->get ();
98+ Log::info ("row_in_db: " . json_encode ($ row_in_db_get ));
99+
100+ // var_dump($mismatches_per_upload_user->get()->count());
101+
102+ // if ($mismatches_per_upload_user->get()->count() == 0) {
103+ // if ($new_mismatch->type == null) {
104+ // $new_mismatch->type = 'statement';
105+ // }
106+ // $new_mismatch->importMeta()->associate($this->meta);
107+ // $new_mismatch->save();
108+ // var_dump('we imported all rows because the user hasnt uploaded any mismatches');
109+ // }
110+ if ($ row_in_db ->doesntExist ()
111+ //|| ( $row_in_db->exists() && $row_in_db->first()->review_status == 'pending'))
112+ ) {
113+ $ this ->saveMismatch ($ new_mismatch );
91114 var_dump ('we imported row that doesnt exist ' );
92115 }
93116
94117 // case 1. there are not mismatches from user in the DB
95- if (count ($ db_mismatches_by_current_user ) == 0 ) {
96- if ($ new_mismatch ->type == null ) {
97- $ new_mismatch ->type = 'statement ' ;
98- }
99- $ new_mismatch ->importMeta ()->associate ($ this ->meta );
100- $ new_mismatch ->save ();
101- var_dump ('we imported all rows because the user hasnt uploaded any mismatches ' );
102- }
118+ // if ($mismatches_per_upload_user-> count() == 0) {
119+ // if ($new_mismatch->type == null) {
120+ // $new_mismatch->type = 'statement';
121+ // }
122+ // $new_mismatch->importMeta()->associate($this->meta);
123+ // $new_mismatch->save();
124+ // var_dump('we imported all rows because the user hasnt uploaded any mismatches');
125+ // }
103126 // else {
104- foreach ($ db_mismatches_by_current_user as $ db_mismatch ) {
105- // we keep all the mismatches that are pending regardless of values in other columns
106- // and stop checking this line
107- if ($ db_mismatch ->review_status == 'pending ' ) {
108- $ new_mismatch ->importMeta ()->associate ($ this ->meta );
109- $ new_mismatch ->save ();
110- var_dump ('we reimported any mismatches still marked as pending in the DB ' );
111- }
112- }
127+ // foreach ($db_mismatches_by_current_user as $db_mismatch) {
128+ // // we keep all the mismatches that are pending regardless of values in other columns
129+ // // and stop checking this line
130+ // if ($db_mismatch->review_status == 'pending') {
131+ // $new_mismatch->importMeta()->associate($this->meta);
132+ // $new_mismatch->save();
133+ // var_dump('we reimported any mismatches still marked as pending in the DB');
134+ // }
135+ // }
113136
114137 // original
115138 // $new_mismatch = Mismatch::make($mismatchLine);
@@ -120,8 +143,8 @@ public function handle(CSVImportReader $reader)
120143 // $new_mismatch->save();
121144 });
122145
123- $ this ->meta ->status = 'completed ' ;
124- $ this ->meta ->save ();
146+ // $this->meta->status = 'completed';
147+ // $this->meta->save();
125148 });
126149 }
127150
@@ -142,4 +165,20 @@ public function failed(Throwable $exception)
142165 $ this ->meta ->status = 'failed ' ;
143166 $ this ->meta ->save ();
144167 }
168+
169+ /**
170+ * Save mismatch to database
171+ *
172+ * @param \Mismatch $new_mismatch
173+ * @return void
174+ */
175+ private function saveMismatch ($ new_mismatch )
176+ {
177+ if ($ new_mismatch ->type == null ) {
178+ $ new_mismatch ->type = 'statement ' ;
179+ }
180+ // if review_status == pending -> save
181+ $ new_mismatch ->importMeta ()->associate ($ this ->meta );
182+ $ new_mismatch ->save ();
183+ }
145184}
0 commit comments