Add some category stuff back in to review (#1538)

* Use standalone category extraction code in MediaDataExtractor

* Add categories to category review page
This commit is contained in:
Elliott Eggleston 2018-05-19 15:02:44 -05:00 committed by maskara
parent 2cedf44124
commit 3b1cf20d27
7 changed files with 72 additions and 26 deletions

View file

@ -25,6 +25,7 @@ import javax.xml.parsers.ParserConfigurationException;
import fr.free.nrw.commons.location.LatLng;
import fr.free.nrw.commons.mwapi.MediaResult;
import fr.free.nrw.commons.mwapi.MediaWikiApi;
import fr.free.nrw.commons.utils.MediaDataExtractorUtil;
import timber.log.Timber;
/**
@ -72,28 +73,13 @@ public class MediaDataExtractor {
MediaResult result = mediaWikiApi.fetchMediaByFilename(filename);
// In-page category links are extracted from source, as XML doesn't cover [[links]]
extractCategories(result.getWikiSource());
categories = MediaDataExtractorUtil.extractCategories(result.getWikiSource());
// Description template info is extracted from preprocessor XML
processWikiParseTree(result.getParseTreeXmlSource(), licenseList);
fetched = true;
}
/**
* We could fetch all category links from API, but we actually only want the ones
* directly in the page source so they're editable. In the future this may change.
*
* @param source wikitext source code
*/
private void extractCategories(String source) {
Pattern regex = Pattern.compile("\\[\\[\\s*Category\\s*:([^]]*)\\s*\\]\\]", Pattern.CASE_INSENSITIVE);
Matcher matcher = regex.matcher(source);
while (matcher.find()) {
String cat = matcher.group(1).trim();
categories.add(cat);
}
}
private void processWikiParseTree(String source, LicenseList licenseList) throws IOException {
Document doc;
try {

View file

@ -20,6 +20,7 @@ import android.widget.EditText;
import com.viewpagerindicator.CirclePageIndicator;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.inject.Inject;
@ -30,7 +31,9 @@ import fr.free.nrw.commons.Media;
import fr.free.nrw.commons.R;
import fr.free.nrw.commons.Utils;
import fr.free.nrw.commons.auth.AuthenticatedActivity;
import fr.free.nrw.commons.mwapi.MediaResult;
import fr.free.nrw.commons.mwapi.MediaWikiApi;
import fr.free.nrw.commons.utils.MediaDataExtractorUtil;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
@ -125,8 +128,19 @@ public class ReviewActivity extends AuthenticatedActivity {
reviewController.onImageRefreshed(result.getFilename()); //file name is updated
reviewPagerAdapter.updateFilename();
pager.setCurrentItem(0);
Observable.fromCallable(() -> {
MediaResult media = mwApi.fetchMediaByFilename("File:" + result.getFilename());
return MediaDataExtractorUtil.extractCategories(media.getWikiSource());
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(this::updateCategories);
}
private void updateCategories(ArrayList<String> categories) {
reviewController.onCategoriesRefreshed(categories);
reviewPagerAdapter.updateCategories();
}
/**
* References ReviewPagerAdapter to null before the activity is destroyed

View file

@ -1,15 +1,23 @@
package fr.free.nrw.commons.review;
import java.util.ArrayList;
/**
* Created by root on 19.05.2018.
*/
public class ReviewController implements ReviewActivity.ReviewCallback {
public static String fileName;
protected static ArrayList<String> categories;
@Override
public void onImageRefreshed(String fileName) {
ReviewController.fileName = fileName;
ReviewController.categories = new ArrayList<>();
}
public void onCategoriesRefreshed(ArrayList<String> categories) {
ReviewController.categories = categories;
}
@Override

View file

@ -2,6 +2,7 @@ package fr.free.nrw.commons.review;
import android.app.AlertDialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@ -10,6 +11,8 @@ import android.widget.TextView;
import com.facebook.drawee.view.SimpleDraweeView;
import java.util.ArrayList;
import fr.free.nrw.commons.R;
import fr.free.nrw.commons.Utils;
import fr.free.nrw.commons.di.CommonsDaggerSupportFragment;
@ -26,7 +29,8 @@ public class ReviewImageFragment extends CommonsDaggerSupportFragment {
private int position;
private String fileName;
private View textView;
private String catString;
private View catsView;
private SimpleDraweeView simpleDraweeView;
public void update(int position, String fileName) {
@ -38,6 +42,13 @@ public class ReviewImageFragment extends CommonsDaggerSupportFragment {
}
}
public void updateCategories(Iterable<String> categories) {
catString = TextUtils.join(", ", categories);
if (catsView != null) {
((TextView) catsView).setText(catString);
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -49,7 +60,8 @@ public class ReviewImageFragment extends CommonsDaggerSupportFragment {
position = getArguments().getInt("position");
View layoutView = inflater.inflate(R.layout.fragment_review_image, container,
false);
textView = layoutView.findViewById(R.id.reviewQuestion);
View textView = layoutView.findViewById(R.id.reviewQuestion);
catsView = layoutView.findViewById(R.id.reviewCategories);
String question;
switch(position) {
case COPYRIGHT:
@ -57,6 +69,7 @@ public class ReviewImageFragment extends CommonsDaggerSupportFragment {
break;
case CATEGORY:
question = getString(R.string.review_category);
catsView.setVisibility(View.VISIBLE);
break;
case SPAM:
question = getString(R.string.review_spam);
@ -66,6 +79,13 @@ public class ReviewImageFragment extends CommonsDaggerSupportFragment {
}
((TextView) textView).setText(question);
simpleDraweeView = layoutView.findViewById(R.id.imageView);
if (fileName != null) {
simpleDraweeView.setImageURI(Utils.makeThumbBaseUrl(fileName));
}
if (catString != null) {
((TextView) catsView).setText(catString);
}
return layoutView;
}

View file

@ -35,6 +35,11 @@ public class ReviewPagerAdapter extends FragmentStatePagerAdapter {
}
}
public void updateCategories() {
ReviewImageFragment categoryFragment = reviewImageFragments[ReviewImageFragment.CATEGORY];
categoryFragment.updateCategories(ReviewController.categories);
}
@Override
public Fragment getItem(int position) {
Bundle bundle = new Bundle();

View file

@ -4,12 +4,14 @@ import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Created by root on 19.05.2018.
*/
public class MediaDataExtractorUtil {
/**
* We could fetch all category links from API, but we actually only want the ones
* directly in the page source so they're editable. In the future this may change.
*
* @param source wikitext source code
*/
public static ArrayList<String> extractCategories(String source) {
ArrayList<String> categories = new ArrayList<>();
Pattern regex = Pattern.compile("\\[\\[\\s*Category\\s*:([^]]*)\\s*\\]\\]", Pattern.CASE_INSENSITIVE);

View file

@ -22,7 +22,7 @@
android:layout_height="0dp"
android:layout_weight="1.1"
android:textAlignment="center"
android:textSize="32dp"
android:textSize="32sp"
android:gravity="center_vertical"
android:text="testing1"
/>
@ -32,12 +32,23 @@
android:layout_height="0dp"
android:layout_weight="0.7"
android:textAlignment="center"
android:textSize="24dp"
android:textSize="24sp"
android:layout_marginBottom="15dp"
android:gravity="center_vertical"
android:text="testing2"
/>
<TextView
android:id="@+id/reviewCategories"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.7"
android:textSize="24sp"
android:textAlignment="center"
android:text="testing2"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
@ -52,7 +63,7 @@
android:layout_weight="1"
android:background="@android:color/transparent"
android:text="@string/yes"
android:textSize="20dp"
android:textSize="20sp"
android:textColor="?attr/colorPrimaryDark"
/>
@ -63,7 +74,7 @@
android:background="@android:color/transparent"
android:layout_weight="1"
android:text="@string/no"
android:textSize="20dp"
android:textSize="20sp"
android:textColor="?attr/colorPrimaryDark"
/>