29
29
import java .io .InputStream ;
30
30
import java .text .ParseException ;
31
31
import java .util .ArrayList ;
32
- import java .util .Collections ;
33
32
import java .util .Date ;
34
33
import java .util .List ;
35
34
import java .util .Locale ;
36
- import java .util .concurrent .Callable ;
37
35
38
36
import fr .free .nrw .commons .BuildConfig ;
39
37
import fr .free .nrw .commons .CommonsApplication ;
40
38
import fr .free .nrw .commons .R ;
41
39
import fr .free .nrw .commons .auth .AccountUtil ;
42
- import fr .free .nrw .commons .category .CategoryImageUtils ;
43
40
import fr .free .nrw .commons .category .QueryContinue ;
44
41
import fr .free .nrw .commons .kvstore .JsonKvStore ;
45
42
import fr .free .nrw .commons .notification .Notification ;
46
43
import fr .free .nrw .commons .notification .NotificationUtils ;
47
44
import fr .free .nrw .commons .utils .ViewUtil ;
48
- import io .reactivex .Observable ;
49
45
import io .reactivex .Single ;
50
46
import timber .log .Timber ;
51
47
@@ -58,15 +54,13 @@ public class ApacheHttpClientMediaWikiApi implements MediaWikiApi {
58
54
private CustomMwApi wikidataApi ;
59
55
private Context context ;
60
56
private JsonKvStore defaultKvStore ;
61
- private Gson gson ;
62
57
63
58
private final String ERROR_CODE_BAD_TOKEN = "badtoken" ;
64
59
65
60
public ApacheHttpClientMediaWikiApi (Context context ,
66
61
String apiURL ,
67
62
String wikidatApiURL ,
68
- JsonKvStore defaultKvStore ,
69
- Gson gson ) {
63
+ JsonKvStore defaultKvStore ) {
70
64
this .context = context ;
71
65
BasicHttpParams params = new BasicHttpParams ();
72
66
SchemeRegistry schemeRegistry = new SchemeRegistry ();
@@ -82,7 +76,6 @@ public ApacheHttpClientMediaWikiApi(Context context,
82
76
api = new CustomMwApi (apiURL , httpClient );
83
77
wikidataApi = new CustomMwApi (wikidatApiURL , httpClient );
84
78
this .defaultKvStore = defaultKvStore ;
85
- this .gson = gson ;
86
79
}
87
80
88
81
/**
@@ -491,114 +484,6 @@ public boolean markNotificationAsRead(Notification notification) throws IOExcept
491
484
return result .equals ("success" );
492
485
}
493
486
494
- /**
495
- * The method takes categoryName as input and returns a List of Subcategories
496
- * It uses the generator query API to get the subcategories in a category, 500 at a time.
497
- * Uses the query continue values for fetching paginated responses
498
- *
499
- * @param categoryName Category name as defined on commons
500
- * @return
501
- */
502
- @ Override
503
- @ NonNull
504
- public List <String > getSubCategoryList (String categoryName ) {
505
- CustomApiResult apiResult = null ;
506
- try {
507
- CustomMwApi .RequestBuilder requestBuilder = api .action ("query" )
508
- .param ("generator" , "categorymembers" )
509
- .param ("format" , "xml" )
510
- .param ("gcmtype" , "subcat" )
511
- .param ("gcmtitle" , categoryName )
512
- .param ("prop" , "info" )
513
- .param ("gcmlimit" , "500" )
514
- .param ("iiprop" , "url|extmetadata" );
515
-
516
- apiResult = requestBuilder .get ();
517
- } catch (IOException e ) {
518
- Timber .e (e , "Failed to obtain searchCategories" );
519
- }
520
-
521
- if (apiResult == null ) {
522
- return new ArrayList <>();
523
- }
524
-
525
- CustomApiResult categoryImagesNode = apiResult .getNode ("/api/query/pages" );
526
- if (categoryImagesNode == null
527
- || categoryImagesNode .getDocument () == null
528
- || categoryImagesNode .getDocument ().getChildNodes () == null
529
- || categoryImagesNode .getDocument ().getChildNodes ().getLength () == 0 ) {
530
- return new ArrayList <>();
531
- }
532
-
533
- NodeList childNodes = categoryImagesNode .getDocument ().getChildNodes ();
534
- return CategoryImageUtils .getSubCategoryList (childNodes );
535
- }
536
-
537
- /**
538
- * The method takes categoryName as input and returns a List of parent categories
539
- * It uses the generator query API to get the parent categories of a category, 500 at a time.
540
- *
541
- * @param categoryName Category name as defined on commons
542
- * @return
543
- */
544
- @ Override
545
- @ NonNull
546
- public List <String > getParentCategoryList (String categoryName ) {
547
- CustomApiResult apiResult = null ;
548
- try {
549
- CustomMwApi .RequestBuilder requestBuilder = api .action ("query" )
550
- .param ("generator" , "categories" )
551
- .param ("format" , "xml" )
552
- .param ("titles" , categoryName )
553
- .param ("prop" , "info" )
554
- .param ("cllimit" , "500" )
555
- .param ("iiprop" , "url|extmetadata" );
556
-
557
- apiResult = requestBuilder .get ();
558
- } catch (IOException e ) {
559
- Timber .e (e , "Failed to obtain parent Categories" );
560
- }
561
-
562
- if (apiResult == null ) {
563
- return new ArrayList <>();
564
- }
565
-
566
- CustomApiResult categoryImagesNode = apiResult .getNode ("/api/query/pages" );
567
- if (categoryImagesNode == null
568
- || categoryImagesNode .getDocument () == null
569
- || categoryImagesNode .getDocument ().getChildNodes () == null
570
- || categoryImagesNode .getDocument ().getChildNodes ().getLength () == 0 ) {
571
- return new ArrayList <>();
572
- }
573
-
574
- NodeList childNodes = categoryImagesNode .getDocument ().getChildNodes ();
575
- return CategoryImageUtils .getSubCategoryList (childNodes );
576
- }
577
-
578
-
579
- /**
580
- * For APIs that return paginated responses, MediaWiki APIs uses the QueryContinue to facilitate fetching of subsequent pages
581
- * https://www.mediawiki.org/wiki/API:Raw_query_continue
582
- * After fetching images a page of image for a particular category, shared defaultKvStore are updated with the latest QueryContinue Values
583
- *
584
- * @param keyword
585
- * @param queryContinue
586
- */
587
- private void setQueryContinueValues (String keyword , QueryContinue queryContinue ) {
588
- defaultKvStore .putString (keyword , gson .toJson (queryContinue ));
589
- }
590
-
591
- /**
592
- * Before making a paginated API call, this method is called to get the latest query continue values to be used
593
- *
594
- * @param keyword
595
- * @return
596
- */
597
- @ Nullable
598
- private QueryContinue getQueryContinueValues (String keyword ) {
599
- String queryContinueString = defaultKvStore .getString (keyword , null );
600
- return gson .fromJson (queryContinueString , QueryContinue .class );
601
- }
602
487
603
488
@ Override
604
489
@ NonNull
0 commit comments