Skip to content

Commit 31c6888

Browse files
cypheropdomdomegg
authored andcommitted
Fix commons-app#1850: Show media talk page in media details (commons-app#2560)
1 parent 3b7b6f9 commit 31c6888

File tree

7 files changed

+89
-0
lines changed

7 files changed

+89
-0
lines changed

app/src/main/java/fr/free/nrw/commons/Media.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public Media[] newArray(int i) {
3939
protected String imageUrl;
4040
protected String filename;
4141
protected String description; // monolingual description on input...
42+
protected String discussion;
4243
protected long dataLength;
4344
protected Date dateCreated;
4445
protected @Nullable Date dateUploaded;
@@ -195,6 +196,22 @@ public void setFilename(String filename) {
195196
this.filename = filename;
196197
}
197198

199+
/**
200+
* Sets the discussion of the file.
201+
* @param discussion
202+
*/
203+
public void setDiscussion(String discussion) {
204+
this.discussion = discussion;
205+
}
206+
207+
/**
208+
* Gets the file discussion as a string.
209+
* @return file discussion as a string
210+
*/
211+
public String getDiscussion() {
212+
return discussion;
213+
}
214+
198215
/**
199216
* Gets the file description.
200217
* @return file description as a string

app/src/main/java/fr/free/nrw/commons/MediaDataExtractor.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package fr.free.nrw.commons;
22

3+
import android.text.Html;
34
import androidx.annotation.Nullable;
45
import android.text.TextUtils;
56

@@ -39,6 +40,7 @@ public class MediaDataExtractor {
3940
private boolean deletionStatus;
4041
private ArrayList<String> categories;
4142
private Map<String, String> descriptions;
43+
private String discussion;
4244
private String license;
4345
private @Nullable LatLng coordinates;
4446

@@ -48,6 +50,7 @@ public MediaDataExtractor(MediaWikiApi mwApi) {
4850
this.descriptions = new HashMap<>();
4951
this.fetched = false;
5052
this.mediaWikiApi = mwApi;
53+
this.discussion = new String();
5154
}
5255

5356
/*
@@ -70,6 +73,9 @@ public void fetch(String filename, LicenseList licenseList) throws IOException {
7073
}
7174

7275
MediaResult result = mediaWikiApi.fetchMediaByFilename(filename);
76+
MediaResult discussion = mediaWikiApi.fetchMediaByFilename(filename.replace("File", "File talk"));
77+
setDiscussion(discussion.getWikiSource());
78+
7379

7480
// In-page category links are extracted from source, as XML doesn't cover [[links]]
7581
extractCategories(result.getWikiSource());
@@ -94,6 +100,14 @@ private void extractCategories(String source) {
94100
}
95101
}
96102

103+
private void setDiscussion(String source) {
104+
try {
105+
discussion = Html.fromHtml(mediaWikiApi.parseWikicode(source)).toString();
106+
} catch (IOException e) {
107+
e.printStackTrace();
108+
}
109+
}
110+
97111
private void processWikiParseTree(String source, LicenseList licenseList) throws IOException {
98112
Document doc;
99113
try {
@@ -307,6 +321,7 @@ public void fill(Media media) {
307321
media.setCategories(categories);
308322
media.setDescriptions(descriptions);
309323
media.setCoordinates(coordinates);
324+
media.setDiscussion(discussion);
310325
if (license != null) {
311326
media.setLicense(license);
312327
}

app/src/main/java/fr/free/nrw/commons/media/MediaDetailFragment.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ public static MediaDetailFragment forMedia(int index, boolean editable, boolean
112112
TextView coordinates;
113113
@BindView(R.id.mediaDetailuploadeddate)
114114
TextView uploadedDate;
115+
@BindView(R.id.mediaDetailDisc)
116+
TextView mediaDiscussion;
115117
@BindView(R.id.seeMore)
116118
TextView seeMore;
117119
@BindView(R.id.nominatedDeletionBanner)
@@ -341,6 +343,7 @@ private void setTextFields(Media media) {
341343
license.setText(prettyLicense(media));
342344
coordinates.setText(prettyCoordinates(media));
343345
uploadedDate.setText(prettyUploadedDate(media));
346+
mediaDiscussion.setText(prettyDiscussion(media));
344347

345348
categoryNames.clear();
346349
categoryNames.addAll(media.getCategories());
@@ -502,6 +505,14 @@ private String prettyDescription(Media media) {
502505
return desc;
503506
}
504507
}
508+
private String prettyDiscussion(Media media) {
509+
String disc = media.getDiscussion().trim();
510+
if (disc.equals("")) {
511+
return getString(R.string.detail_discussion_empty);
512+
} else {
513+
return disc;
514+
}
515+
}
505516

506517
private String prettyLicense(Media media) {
507518
String licenseKey = media.getLicense();

app/src/main/java/fr/free/nrw/commons/mwapi/ApacheHttpClientMediaWikiApi.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,17 @@ public String findThumbnailByFilename(String filename) throws IOException {
306306
.getString("/api/query/pages/page/imageinfo/ii/@thumburl");
307307
}
308308

309+
@Override
310+
public String parseWikicode(String source) throws IOException {
311+
return api.action("flow-parsoid-utils")
312+
.param("from", "wikitext")
313+
.param("to", "html")
314+
.param("content", source)
315+
.param("title", "Main_page")
316+
.get()
317+
.getString("/api/flow-parsoid-utils/@content");
318+
}
319+
309320
@Override
310321
@NonNull
311322
public MediaResult fetchMediaByFilename(String filename) throws IOException {

app/src/main/java/fr/free/nrw/commons/mwapi/MediaWikiApi.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ public interface MediaWikiApi {
6666
@Nullable
6767
boolean addWikidataEditTag(String revisionId) throws IOException;
6868

69+
String parseWikicode(String source) throws IOException;
70+
6971
@NonNull
7072
MediaResult fetchMediaByFilename(String filename) throws IOException;
7173

app/src/main/res/layout/fragment_media_detail.xml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,37 @@
309309
android:textStyle="bold"/>
310310
</LinearLayout>
311311

312+
<fr.free.nrw.commons.media.MediaDetailSpacer
313+
android:layout_width="match_parent"
314+
android:layout_height="@dimen/small_gap" />
315+
316+
<LinearLayout
317+
android:layout_width="match_parent"
318+
android:layout_height="wrap_content"
319+
android:background="?attr/subBackground"
320+
android:orientation="vertical"
321+
android:padding="@dimen/standard_gap">
322+
323+
<TextView
324+
android:layout_width="match_parent"
325+
android:layout_height="wrap_content"
326+
android:paddingBottom="@dimen/tiny_gap"
327+
android:text="@string/media_detail_discussion"
328+
android:textColor="@android:color/white"
329+
android:textSize="@dimen/normal_text"
330+
android:textStyle="bold" />
331+
332+
<TextView
333+
android:id="@+id/mediaDetailDisc"
334+
android:layout_width="match_parent"
335+
android:layout_height="wrap_content"
336+
android:layout_gravity="start"
337+
android:background="?attr/subBackground"
338+
android:padding="@dimen/small_gap"
339+
android:textColor="@android:color/white"
340+
android:textSize="@dimen/description_text_size" />
341+
</LinearLayout>
342+
312343
<Button
313344
android:id="@+id/copyWikicode"
314345
android:layout_width="match_parent"

app/src/main/res/values/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@
158158
<string name="detail_panel_cats_loading">Loading…</string>
159159
<string name="detail_panel_cats_none">None selected</string>
160160
<string name="detail_description_empty">No description</string>
161+
<string name="detail_discussion_empty">No discussion</string>
161162
<string name="detail_license_empty">Unknown license</string>
162163
<string name="menu_refresh">Refresh</string>
163164
<string name="storage_permission_title">Requesting Storage Permission</string>
@@ -173,6 +174,7 @@
173174
<string name="no">No</string>
174175
<string name="media_detail_title">Title</string>
175176
<string name="media_detail_description">Description</string>
177+
<string name="media_detail_discussion">Discussion</string>
176178
<string name="media_detail_author">Author</string>
177179
<string name="media_detail_uploaded_date">Uploaded date</string>
178180
<string name="media_detail_license">License</string>

0 commit comments

Comments
 (0)