From 1986b2455ce6153d82430a885e3b80d2d7dadb21 Mon Sep 17 00:00:00 2001 From: YuviPanda Date: Thu, 7 Feb 2013 03:39:46 +0530 Subject: [PATCH] Do not add duplicate photos when syncing --- .../ContributionsSyncAdapter.java | 32 +++++++++++++++---- 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/commons/src/main/java/org/wikimedia/commons/contributions/ContributionsSyncAdapter.java b/commons/src/main/java/org/wikimedia/commons/contributions/ContributionsSyncAdapter.java index a0ed5e009..b699c97d1 100644 --- a/commons/src/main/java/org/wikimedia/commons/contributions/ContributionsSyncAdapter.java +++ b/commons/src/main/java/org/wikimedia/commons/contributions/ContributionsSyncAdapter.java @@ -25,6 +25,24 @@ public class ContributionsSyncAdapter extends AbstractThreadedSyncAdapter { private int getLimit() { return 500; // FIXME: Parameterize! } + + private static final String[] existsQuery = { Contribution.Table.COLUMN_FILENAME }; + private static final String existsSelection = Contribution.Table.COLUMN_FILENAME + " = ?"; + private boolean fileExists(ContentProviderClient client, String filename) { + Cursor cursor = null; + try { + cursor = client.query(ContributionsContentProvider.BASE_URI, + existsQuery, + existsSelection, + new String[] { filename }, + "" + ); + } catch (RemoteException e) { + throw new RuntimeException(e); + } + return cursor != null && cursor.getCount() != 0; + } + @Override public void onPerformSync(Account account, Bundle bundle, String s, ContentProviderClient contentProviderClient, SyncResult syncResult) { // This code is fraught with possibilities of race conditions, but lalalalala I can't hear you! @@ -52,20 +70,22 @@ public class ContributionsSyncAdapter extends AbstractThreadedSyncAdapter { ArrayList uploads = result.getNodes("/api/query/logevents/item"); Log.d("Commons", uploads.size() + " results!"); - ContentValues[] imageValues = new ContentValues[uploads.size()]; - for(int i=0; i < uploads.size(); i++) { - ApiResult image = uploads.get(i); + ArrayList imageValues = new ArrayList(); + for(ApiResult image: uploads) { String filename = image.getString("@title"); + if(fileExists(contentProviderClient, filename)) { + Log.d("Commons", "Skipping " + filename); + continue; + } String thumbUrl = Utils.makeThumbBaseUrl(filename); Date dateUpdated = Utils.parseMWDate(image.getString("@timestamp")); Contribution contrib = new Contribution(null, thumbUrl, filename, "", -1, dateUpdated, dateUpdated, user, ""); contrib.setState(Contribution.STATE_COMPLETED); - imageValues[i] = contrib.toContentValues(); - Log.d("Commons", "For " + imageValues[i].toString()); + imageValues.add(contrib.toContentValues()); } try { - contentProviderClient.bulkInsert(ContributionsContentProvider.BASE_URI, imageValues); + contentProviderClient.bulkInsert(ContributionsContentProvider.BASE_URI, imageValues.toArray(new ContentValues[]{})); } catch (RemoteException e) { throw new RuntimeException(e); }