/
massviews.js
1205 lines (1025 loc) · 37.5 KB
/
massviews.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Massviews Analysis tool
* @file Main file for Massviews application
* @author MusikAnimal
* @copyright 2016 MusikAnimal
* @license MIT License: https://opensource.org/licenses/MIT
*/
const config = require('./config');
const siteMap = require('../shared/site_map');
const siteDomains = Object.keys(siteMap).map(key => siteMap[key]);
const Pv = require('../shared/pv');
const ChartHelpers = require('../shared/chart_helpers');
const ListHelpers = require('../shared/list_helpers');
/** Main MassViews class */
class MassViews extends mix(Pv).with(ChartHelpers, ListHelpers) {
constructor() {
super(config);
this.app = 'massviews';
}
/**
* Initialize the application.
* Called in `pv.js` after translations have loaded
* @return {null} Nothing
*/
initialize() {
this.assignDefaults();
this.setupDateRangeSelector();
this.popParams();
this.setupListeners();
/** only show options for line, bar and radar charts */
$('.multi-page-chart-node').hide();
}
/**
* Add general event listeners
* @override
* @returns {null} nothing
*/
setupListeners() {
super.setupListeners();
$('#pv_form').on('submit', e => {
e.preventDefault(); // prevent page from reloading
this.processInput();
});
$('.another-query').on('click', () => {
this.setState('initial');
this.pushParams(true);
});
$('.sort-link').on('click', e => {
const sortType = $(e.currentTarget).data('type');
this.direction = this.sort === sortType ? -this.direction : 1;
this.sort = sortType;
this.renderData();
});
$('.source-option').on('click', e => this.updateSourceInput(e.target));
$('.view-btn').on('click', e => {
document.activeElement.blur();
this.view = e.currentTarget.dataset.value;
this.toggleView(this.view);
});
}
/**
* Copy necessary default values to class instance.
* Called when the view is reset.
* @return {null} Nothing
*/
assignDefaults() {
['sort', 'source', 'sourceProject', 'direction', 'outputData', 'total', 'view', 'subjectpage'].forEach(defaultKey => {
this[defaultKey] = this.config.defaults[defaultKey];
});
}
/**
* Show/hide form elements based on the selected source
* @param {Object} node - HTML element of the selected source
* @return {null} nothing
*/
updateSourceInput(node) {
const source = node.dataset.value;
$('#source_button').data('value', source).html(`${node.textContent} <span class='caret'></span>`);
$(this.config.sourceInput).prop('type', this.config.sources[source].type)
.prop('placeholder', this.config.sources[source].placeholder)
.val('');
if (source === 'category') {
$('.category-subject-toggle').show();
} else {
$('.category-subject-toggle').hide();
}
if (source === 'quarry') {
$('.massviews-source-input').addClass('quarry');
$('.quarry-project').prop('disabled', false);
} else {
$('.massviews-source-input').removeClass('quarry');
$('.quarry-project').prop('disabled', true);
}
$(this.config.sourceInput).focus();
}
/**
* Get the base project name (without language and the .org)
* @returns {boolean} projectname
*/
get baseProject() {
return this.project.split('.')[1];
}
/**
* Get all user-inputted parameters
* @param {boolean} [forCacheKey] whether or not to include the page name, and exclude sort and direction
* in the returned object. This is for the purposes of generating a unique cache key for params affecting the API queries
* @return {Object} project, platform, agent, etc.
*/
getParams(forCacheKey = false) {
let params = {
platform: $(this.config.platformSelector).val(),
agent: $(this.config.agentSelector).val(),
source: $(this.config.sourceButton).data('value'),
target: $(this.config.sourceInput).val().score()
};
/**
* Override start and end with custom range values, if configured (set by URL params or setupDateRangeSelector)
* Valid values are those defined in this.config.specialRanges, constructed like `{range: 'last-month'}`,
* or a relative range like `{range: 'latest-N'}` where N is the number of days.
*/
if (this.specialRange && !forCacheKey) {
params.range = this.specialRange.range;
} else {
params.start = this.daterangepicker.startDate.format('YYYY-MM-DD');
params.end = this.daterangepicker.endDate.format('YYYY-MM-DD');
}
if (params.source === 'category') {
params.subjectpage = $('.category-subject-toggle--input').is(':checked') ? '1' : '0';
} else if (params.source === 'quarry') {
params.project = $('.quarry-project').val();
}
if (!forCacheKey) {
params.sort = this.sort;
params.direction = this.direction;
params.view = this.view;
/** add autolog param only if it was passed in originally, and only if it was false (true would be default) */
if (this.noLogScale) params.autolog = 'false';
}
return params;
}
/**
* Push relevant class properties to the query string
* @param {Boolean} clear - wheter to clear the query string entirely
* @return {null} nothing
*/
pushParams(clear = false) {
if (!window.history || !window.history.replaceState) return;
if (clear) {
return history.replaceState(null, document.title, location.href.split('?')[0]);
}
/** only certain characters within the page name are escaped */
window.history.replaceState({}, document.title, '?' + $.param(this.getParams()));
$('.permalink').prop('href', `/massviews?${$.param(this.getPermaLink())}`);
}
/**
* Render list of massviews into view
* @override
* @returns {null} nothing
*/
renderData() {
super.renderData(sortedDatasets => {
$('.output-totals').html(
`<th scope='row'>${$.i18n('totals')}</th>
<th>${$.i18n('num-pages', sortedDatasets.length)}</th>
<th>${this.formatNumber(this.outputData.sum)}</th>
<th>${this.formatNumber(Math.round(this.outputData.average))} / ${$.i18n('day')}</th>`
);
$('#output_list').html('');
sortedDatasets.forEach((item, index) => {
$('#output_list').append(
`<tr>
<th scope='row'>${index + 1}</th>
<td><a href="https://${this.sourceProject.escape()}/wiki/${item.label.score()}" target="_blank">${item.label.descore()}</a></td>
<td><a target="_blank" href='${this.getPageviewsURL(this.sourceProject, item.label)}'>${this.formatNumber(item.sum)}</a></td>
<td>${this.formatNumber(Math.round(item.average))} / ${$.i18n('day')}</td>
</tr>`
);
});
});
}
/**
* Get value of given langview entry for the purposes of column sorting
* @param {object} item - langview entry within this.outputData
* @param {String} type - type of property to get
* @return {String|Number} - value
*/
getSortProperty(item, type) {
switch (type) {
case 'original':
return item.index;
case 'title':
return item.label;
case 'views':
return Number(item.sum);
}
}
/**
* Loop through given pages and query the pageviews API for each
* Also updates this.outputData with result
* @param {string} project - project such as en.wikipedia.org
* @param {Object} pages - as given by the getPagePile promise
* @return {Deferred} - Promise resolving with data ready to be rendered to view
*/
getPageViewsData(project, pages) {
const startDate = this.daterangepicker.startDate.startOf('day'),
endDate = this.daterangepicker.endDate.startOf('day');
// XXX: throttling
let dfd = $.Deferred(), promises = [], count = 0, hadFailure, failureRetries = {},
totalRequestCount = pages.length, failedPages = [], pageViewsData = [];
const makeRequest = page => {
const uriEncodedPageName = encodeURIComponent(page);
const url = (
`https://wikimedia.org/api/rest_v1/metrics/pageviews/per-article/${project}` +
`/${$(this.config.platformSelector).val()}/${$(this.config.agentSelector).val()}/${uriEncodedPageName}/daily` +
`/${startDate.format(this.config.timestampFormat)}/${endDate.format(this.config.timestampFormat)}`
);
const promise = $.ajax({ url, dataType: 'json' });
promises.push(promise);
promise.done(pvData => {
pageViewsData.push({
title: page,
items: pvData.items
});
}).fail(errorData => {
// XXX: throttling
/** first detect if this was a Cassandra backend error, and if so, schedule a re-try */
const cassandraError = errorData.responseJSON.title === 'Error in Cassandra table storage backend';
if (cassandraError) {
if (failureRetries[project]) {
failureRetries[project]++;
} else {
failureRetries[project] = 1;
}
/** maximum of 3 retries */
if (failureRetries[project] < 3) {
totalRequestCount++;
return this.rateLimit(makeRequest, 100, this)(page);
}
}
if (cassandraError) {
failedPages.push(page);
} else {
this.writeMessage(`${this.getPageLink(page, project)}: ${$.i18n('api-error', 'Pageviews API')} - ${errorData.responseJSON.title}`);
}
hadFailure = true; // don't treat this series of requests as being cached by server
}).always(() => {
this.updateProgressBar((++count / totalRequestCount) * 100);
// XXX: throttling
if (count === totalRequestCount) {
dfd.resolve(pageViewsData);
if (failedPages.length) {
this.writeMessage($.i18n(
'api-error-timeout',
'<ul>' +
failedPages.map(failedPage => `<li>${this.getPageLink(failedPage, project)}</li>`).join('') +
'</ul>'
));
}
/**
* if there were no failures, assume the resource is now cached by the server
* and save this assumption to our own cache so we don't throttle the same requests
*/
// XXX: throttling
if (!hadFailure) {
simpleStorage.set(this.getCacheKey(), true, {TTL: 600000});
}
}
});
};
/**
* We don't want to throttle requests for cached resources. However in our case,
* we're unable to check response headers to see if the resource was cached,
* so we use simpleStorage to keep track of what the user has recently queried.
*/
// XXX: throttling
const requestFn = this.isRequestCached() ? makeRequest : this.rateLimit(makeRequest, 100, this);
pages.forEach((page, index) => {
requestFn(page);
});
return dfd;
}
/**
* Build our mother data set, from which we can draw a chart,
* render a list of data, whatever our heart desires :)
* @param {string} label - label for the dataset (e.g. category:blah, page pile 24, etc)
* @param {string} link - HTML anchor tag for the label
* @param {array} datasets - array of datasets for each article, as returned by the Pageviews API
* @return {object} mother data set, also stored in this.outputData
*/
buildMotherDataset(label, link, datasets) {
/**
* `datasets` structure:
*
* [{
* title: page,
* items: [
* {
* access: '',
* agent: '',
* article: '',
* granularity: '',
* project: '',
* timestamp: '',
* views: 10
* }
* ]
* }]
*
* output structure:
*
* {
* labels: [''],
* listData: [
* {
* label: '',
* data: [1,2,3,4],
* sum: 10,
* average: 2,
* index: 0
* ...
* MERGE in this.config.chartConfig[this.chartType].dataset(this.config.colors[0])
* }
* ],
* totalViewsSet: [1,2,3,4],
* sum: 10,
* average: 2,
* datesWithoutData: ['2016-05-16T00:00:00-00:00']
* }
*/
this.outputData = {
labels: this.getDateHeadings(true), // labels needed for Charts.js, even though we'll only have one dataset
link, // for our own purposes
listData: []
};
const startDate = moment(this.daterangepicker.startDate),
endDate = moment(this.daterangepicker.endDate),
length = this.numDaysInRange();
let totalViewsSet = new Array(length).fill(0),
datesWithoutData = [];
datasets.forEach((dataset, index) => {
const data = dataset.items.map(item => item.views),
sum = data.reduce((a, b) => a + b);
this.outputData.listData.push({
data,
label: dataset.title,
sum,
average: sum / length,
index
});
/**
* Ensure we have data for each day, using null as the view count when data is actually not available yet
* See fillInZeros() comments for more info.
*/
const [viewsSet, incompleteDates] = this.fillInZeros(dataset.items, startDate, endDate);
incompleteDates.forEach(date => {
if (!datesWithoutData.includes(date)) datesWithoutData.push(date);
});
totalViewsSet = totalViewsSet.map((num, i) => num + viewsSet[i].views);
});
const grandSum = totalViewsSet.reduce((a, b) => (a || 0) + (b || 0));
Object.assign(this.outputData, {
datasets: [{
label,
data: totalViewsSet,
sum: grandSum,
average: grandSum / length
}],
datesWithoutData,
sum: grandSum, // nevermind the duplication
average: grandSum / length
});
if (datesWithoutData.length) {
const dateList = datesWithoutData.map(date => moment(date).format(this.dateFormat));
this.writeMessage($.i18n('api-incomplete-data', dateList.sort().join(' · '), dateList.length));
}
return this.outputData;
}
getPileURL(id) {
return `http://tools.wmflabs.org/pagepile/api.php?action=get_data&id=${id}`;
}
getPileLink(id) {
return `<a href='${this.getPileURL(id)}' target='_blank'>Page Pile ${id}</a>`;
}
/**
* Get list of pages from Page Pile API given id
* @param {Number} id - PagePile ID
* @return {Deferred} - Promise resolving with page names
*/
getPagePile(id) {
const dfd = $.Deferred();
const url = `https://tools.wmflabs.org/pagepile/api.php?id=${id}&action=get_data&format=json&metadata=1`;
$.ajax({
url,
dataType: 'jsonp'
}).done(data => {
let pages = Object.keys(data.pages);
if (pages.length > 500) {
this.writeMessage(
$.i18n('massviews-oversized-set', this.getPileLink(id), this.formatNumber(pages.length), this.config.pageLimit)
);
pages = pages.slice(0, this.config.pageLimit);
}
return dfd.resolve({
id: data.id,
wiki: data.wiki,
pages: pages
});
}).fail(error => {
return dfd.reject(
`${this.getPileLink(id)}: ${$.i18n('api-error-no-data')}`
);
});
return dfd;
}
/**
* Parse wiki URL for the wiki and page name
* @param {String} url - full URL to a wiki page
* @return {Array|null} ['wiki', 'page'] or null if invalid
*/
getWikiPageFromURL(url) {
let matches;
if (url.includes('?')) {
matches = url.match(/\/\/(.*?)\/w\/.*\?(?:.*\b)?title=(.*?)(?:&|$)/);
} else {
matches = url.match(/\/\/(.*?)\/wiki\/(.*?)(?:\?|$)/);
}
return matches ? matches.slice(1) : [null, null];
}
/**
* Parses the URL query string and sets all the inputs accordingly
* Should only be called on initial page load, until we decide to support pop states (probably never)
* @returns {null} nothing
*/
popParams() {
let params = this.validateParams(
this.parseQueryString()
);
this.validateDateRange(params);
this.patchUsage();
this.updateSourceInput($(`.source-option[data-value=${params.source}]`)[0]);
// fill in value for the target
if (params.target) {
$(this.config.sourceInput).val(decodeURIComponent(params.target).descore());
}
// If there are invalid params, remove target from params so we don't process the defaults.
// FIXME: we're checking for site messages because super.validateParams doesn't return a boolean
// or any indication the validations failed. This is hacky but necessary.
if ($('.site-notice .alert-danger').length) {
delete params.target;
} else if (params.overflow && params.source === 'pagepile' && params.target) {
/**
* If they requested more than 10 pages in Pageviews (via typing it in the URL)
* they are redirected to Massviews with an auto-generated PagePile.
* This shows a message explaining what happened.
*/
this.addSiteNotice(
'info',
$.i18n('massviews-redirect', $.i18n('title'), 10, this.getPileLink(params.target)),
'',
true
);
}
$(this.config.platformSelector).val(params.platform);
$(this.config.agentSelector).val(params.agent);
/** export necessary params to outer scope */
['sort', 'direction', 'view', 'source', 'subjectpage'].forEach(key => {
this[key] = params[key];
});
if (params.source === 'quarry' && params.project) {
$('.quarry-project').val(params.project);
}
if (params.subjectpage === '1') {
$('.category-subject-toggle--input').prop('checked', true);
}
/** start up processing if necessary params are present */
if (params.target) {
this.processInput();
}
}
/**
* Helper to set a CSS class on the `main` node,
* styling the document based on a 'state'
* @param {String} state - class to be added;
* should be one of ['initial', 'processing', 'complete']
* @param {function} [cb] - Optional function to be called after initial state has been set
* @returns {null} nothing
*/
setState(state, cb) {
$('main').removeClass(this.config.formStates.join(' ')).addClass(state);
switch (state) {
case 'initial':
this.clearMessages();
this.assignDefaults();
this.destroyChart();
$('output').removeClass('list-mode').removeClass('chart-mode');
$('.data-links').addClass('invisible');
if (this.typeahead) this.typeahead.hide();
$(this.config.sourceInput).val('').focus();
if (typeof cb === 'function') cb.call(this);
break;
case 'processing':
this.processStarted();
this.clearMessages();
document.activeElement.blur();
$('.progress-bar').addClass('active');
break;
case 'complete':
this.processEnded();
/** stop hidden animation for slight performance improvement */
this.updateProgressBar(0);
$('.progress-bar').removeClass('active');
$('.data-links').removeClass('invisible');
break;
case 'invalid':
break;
}
}
processPagePile(cb) {
const pileId = $(this.config.sourceInput).val();
this.getPagePile(pileId).done(pileData => {
if (!pileData.pages.length) {
return this.setState('initial', () => {
this.writeMessage($.i18n('massviews-empty-set', this.getPileLink(pileId)));
});
}
/**
* XXX: throttling
* At this point we know we have data to process,
* so set the throttle flag to disallow additional requests for the next 90 seconds
*/
if (!this.isRequestCached()) simpleStorage.set('pageviews-throttle', true, {TTL: 90000});
this.sourceProject = siteMap[pileData.wiki];
/**
* remove Project: prefix if present, only for enwiki, for now,
* see https://phabricator.wikimedia.org/T135437
*/
if (this.sourceProject === 'en.wikipedia.org') {
pileData.pages = pileData.pages.map(page => {
return page.replace(/^Project:Wikipedia:/, 'Wikipedia:');
});
}
this.getPageViewsData(this.sourceProject, pileData.pages).done(pageViewsData => {
const label = `Page Pile #${pileData.id}`;
$('.output-title').text(label).prop('href', this.getPileURL(pileData.id));
$('.output-params').html(
`
${$(this.config.dateRangeSelector).val()}
—
<a href="https://${this.sourceProject.escape()}" target="_blank">${this.sourceProject.replace(/.org$/, '').escape()}</a>
`
);
this.buildMotherDataset(label, this.getPileLink(pileData.id), pageViewsData);
cb();
});
}).fail(error => {
this.setState('initial');
/** structured error comes back as a string, otherwise we don't know what happened */
if (typeof error === 'string') {
this.writeMessage(error);
} else {
this.writeMessage($.i18n('api-error-unknown', 'Page Pile'));
}
});
}
processCategory(project, category, cb) {
if (!category) {
return this.setState('initial', () => {
this.writeMessage($.i18n('invalid-category-url'));
});
}
let requestData = {
action: 'query',
format: 'json',
list: 'categorymembers',
cmlimit: 500,
cmtitle: decodeURIComponent(category),
prop: 'categoryinfo',
titles: decodeURIComponent(category)
};
const promise = $.ajax({
url: `https://${project}/w/api.php`,
jsonp: 'callback',
dataType: 'jsonp',
data: requestData
});
const categoryLink = this.getPageLink(decodeURIComponent(category), project);
this.sourceProject = project; // for caching purposes
promise.done(data => {
if (data.error) {
return this.setState('initial', () => {
this.writeMessage(
`${$.i18n('api-error', 'Category API')}: ${data.error.info.escape()}`
);
});
}
const queryKey = Object.keys(data.query.pages)[0];
if (queryKey === '-1') {
return this.setState('initial', () => {
this.writeMessage($.i18n('api-error-no-data'));
});
}
const size = data.query.pages[queryKey].categoryinfo.size,
// siteInfo is only populated if they've opted to see subject pages instead of talk pages
// Otherwise namespaces are not needed by this.mapCategoryPageNames
namespaces = this.siteInfo ? this.siteInfo.namespaces : undefined;
let pages = data.query.categorymembers;
if (!pages.length) {
return this.setState('initial', () => {
this.writeMessage($.i18n('massviews-empty-set', categoryLink));
});
}
if (size > this.config.pageLimit) {
this.writeMessage(
$.i18n('massviews-oversized-set', categoryLink, this.formatNumber(size), this.config.pageLimit)
);
pages = pages.slice(0, this.config.pageLimit);
}
/**
* XXX: throttling
* At this point we know we have data to process,
* so set the throttle flag to disallow additional requests for the next 90 seconds
*/
if (size > 10) this.setThrottle();
const pageNames = this.mapCategoryPageNames(pages, namespaces);
this.getPageViewsData(project, pageNames).done(pageViewsData => {
$('.output-title').html(categoryLink);
$('.output-params').html($(this.config.dateRangeSelector).val());
this.buildMotherDataset(category, categoryLink, pageViewsData);
cb();
});
}).fail(data => {
this.setState('initial');
/** structured error comes back as a string, otherwise we don't know what happened */
if (data && typeof data.error === 'string') {
this.writeMessage(
$.i18n('api-error', categoryLink + ': ' + data.error)
);
} else {
this.writeMessage($.i18n('api-error-unknown', categoryLink));
}
});
}
processSubpages(project, targetPage, cb) {
if (!targetPage) {
return this.setState('initial', () => {
this.writeMessage($.i18n('invalid-page-url'));
});
}
this.sourceProject = project; // for caching purposes
// determine what namespace the targetPage is in
const descoredTargetPage = targetPage.descore();
let namespace = 0, queryTargetPage;
for (const ns in this.siteInfo.namespaces) {
if (ns === '0') continue; // skip mainspace
const nsName = this.siteInfo.namespaces[ns]['*'] + ':';
if (descoredTargetPage.startsWith(nsName)) {
namespace = this.siteInfo.namespaces[ns].id;
queryTargetPage = targetPage.substring(nsName.length);
}
}
// get namespace number of corresponding talk or subject page
const inverseNamespace = namespace % 2 === 0 ? namespace + 1 : namespace - 1;
let promises = [];
[namespace, inverseNamespace].forEach(apnamespace => {
promises.push(
$.ajax({
url: `https://${project}/w/api.php`,
jsonp: 'callback',
dataType: 'jsonp',
data: {
action: 'query',
format: 'json',
list: 'allpages',
aplimit: 500,
apnamespace,
apprefix: decodeURIComponent(queryTargetPage) + '/'
}
})
);
});
const pageLink = this.getPageLink(decodeURIComponent(targetPage), project);
$.when(...promises).done((data, data2) => {
// show errors, if any
const errors = [data, data2].filter(resp => !!resp[0].error);
if (errors.length) {
errors.forEach(error => {
this.setState('initial', () => {
this.writeMessage(
`${$.i18n('api-error', 'Allpages API')}: ${error[0].error.info.escape()}`
);
});
});
return false;
}
let pages = data[0].query.allpages.concat(data2[0].query.allpages);
const size = pages.length;
if (size === 0) {
return this.setState('initial', () => {
this.writeMessage($.i18n('api-error-no-data'));
});
}
if (size > this.config.pageLimit) {
this.writeMessage(
$.i18n('massviews-oversized-set', pageLink, this.formatNumber(size), this.config.pageLimit)
);
pages = pages.slice(0, this.config.pageLimit);
}
/**
* XXX: throttling
* At this point we know we have data to process,
* so set the throttle flag to disallow additional requests for the next 90 seconds
*/
if (size > 10) this.setThrottle();
const pageNames = pages.map(page => page.title).concat([targetPage]);
this.getPageViewsData(project, pageNames).done(pageViewsData => {
$('.output-title').html(pageLink);
$('.output-params').html($(this.config.dateRangeSelector).val());
this.buildMotherDataset(targetPage, pageLink, pageViewsData);
cb();
});
}).fail(data => {
this.setState('initial');
/** structured error comes back as a string, otherwise we don't know what happened */
if (data && typeof data.error === 'string') {
this.writeMessage(
$.i18n('api-error', pageLink + ': ' + data.error)
);
} else {
this.writeMessage($.i18n('api-error-unknown', pageLink));
}
});
}
processTemplate(cb) {
const [project, template] = this.getWikiPageFromURL($(this.config.sourceInput).val());
if (!this.validateProject(project)) return;
if (!template) {
return this.setState('initial', () => {
this.writeMessage($.i18n('invalid-template-url'));
});
}
const promise = $.ajax({
url: `https://${project}/w/api.php`,
jsonp: 'callback',
dataType: 'jsonp',
data: {
action: 'query',
format: 'json',
tilimit: 500,
titles: decodeURIComponent(template),
prop: 'transcludedin'
}
});
const templateLink = this.getPageLink(decodeURIComponent(template), project);
this.sourceProject = project; // for caching purposes
promise.done(data => {
if (data.error) {
return this.setState('initial', () => {
this.writeMessage(
`${$.i18n('api-error', 'Transclusion API')}: ${data.error.info.escape()}`
);
});
}
const queryKey = Object.keys(data.query.pages)[0];
if (queryKey === '-1') {
return this.setState('initial', () => {
this.writeMessage($.i18n('api-error-no-data'));
});
}
const pages = data.query.pages[queryKey].transcludedin.map(page => page.title);
if (!pages.length) {
return this.setState('initial', () => {
this.writeMessage($.i18n('massviews-empty-set', templateLink));
});
}
// in this case we are limited by the API to 500 pages, not this.config.pageLimit
if (data.continue) {
this.writeMessage(
$.i18n('massviews-oversized-set-unknown', templateLink, 500)
);
}
/**
* XXX: throttling
* At this point we know we have data to process,
* so set the throttle flag to disallow additional requests for the next 90 seconds
*/
if (pages.length > 10) this.setThrottle();
this.getPageViewsData(project, pages).done(pageViewsData => {
$('.output-title').html(templateLink);
$('.output-params').html($(this.config.dateRangeSelector).val());
this.buildMotherDataset(template, templateLink, pageViewsData);
cb();
});
}).fail(data => {
this.setState('initial');
/** structured error comes back as a string, otherwise we don't know what happened */
if (data && typeof data.error === 'string') {
this.writeMessage(
$.i18n('api-error', templateLink + ': ' + data.error)
);
} else {
this.writeMessage($.i18n('api-error-unknown', templateLink));
}
});
}
processWikiPage(cb) {
const [project, page] = this.getWikiPageFromURL($(this.config.sourceInput).val());
if (!this.validateProject(project)) return;
if (!page) {
return this.setState('initial', () => {
this.writeMessage($.i18n('invalid-page-url'));
});
}
const promise = $.ajax({
url: `https://${project}/w/api.php`,
jsonp: 'callback',
dataType: 'jsonp',
data: {
action: 'query',
format: 'json',
pllimit: 500,
titles: decodeURIComponent(page),
prop: 'links'
}
});
const pageLink = this.getPageLink(decodeURIComponent(page), project);
this.sourceProject = project; // for caching purposes
promise.done(data => {
if (data.error) {
return this.setState('initial', () => {
this.writeMessage(
`${$.i18n('api-error', 'Links API')}: ${data.error.info.escape()}`
);
});
}
const queryKey = Object.keys(data.query.pages)[0];
if (queryKey === '-1' || !data.query.pages[queryKey].links) {
return this.setState('initial', () => {
this.writeMessage($.i18n('api-error-no-data'));
});
}
const pages = data.query.pages[queryKey].links.map(page => page.title);
if (!pages.length) {
return this.setState('initial', () => {
this.writeMessage($.i18n('massviews-empty-set', pageLink));
});
}
// in this case we are limited by the API to 500 pages, not this.config.pageLimit
if (data.continue) {
this.writeMessage(
$.i18n('massviews-oversized-set-unknown', pageLink, 500)
);
}
/**
* XXX: throttling
* At this point we know we have data to process,