-
Notifications
You must be signed in to change notification settings - Fork 4
/
ext.pageTriage.article.js
368 lines (334 loc) · 10.4 KB
/
ext.pageTriage.article.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
// Article represents the metadata for a single article.
// ArticleList is a collection of articles for use in the list view
//
$( function() {
if ( !mw.pageTriage ) {
// make sure this object exists, since this might be run first.
mw.pageTriage = {};
}
mw.pageTriage.Article = Backbone.Model.extend( {
defaults: {
title: 'Empty Article',
pageid: ''
},
initialize: function( options ) {
this.bind( 'change', this.formatMetadata, this );
this.pageId = options.pageId;
if( options.includeHistory ) {
// fetch the history too?
// don't do this when fetching via collection, since it'll generate one ajax request per article.
// don't execute on every model change, just when loading a different page
this.bind( 'change:pageid', this.addHistory, this );
}
},
formatMetadata: function ( article ) {
var creation_date_parsed = Date.parseExact( article.get( 'creation_date' ), 'yyyyMMddHHmmss' );
article.set('creation_date_pretty', creation_date_parsed.toString( mw.msg( 'pagetriage-creation-dateformat' ) ) );
// sometimes user info isn't set, so check that first.
if( article.get( 'user_creation_date' ) ) {
var user_creation_date_parsed = Date.parseExact( article.get( 'user_creation_date' ), 'yyyyMMddHHmmss' );
article.set( 'user_creation_date_pretty', user_creation_date_parsed.toString( mw.msg( 'pagetriage-info-timestamp-date-format' ) ) );
} else {
article.set( 'user_creation_date_pretty', '');
}
// TODO: What if userName doesn't exist?
if( article.get( 'user_name' ) ) {
// decide which byline message to use depending on if the editor is new or not
// but don't show new editor for ip users
if ( article.get( 'user_id' ) > '0' && article.get( 'user_autoconfirmed' ) < '1' ) {
var bylineMessage = 'pagetriage-byline-new-editor';
} else {
var bylineMessage = 'pagetriage-byline';
}
// put it all together in the byline
var byline = mw.msg(
bylineMessage,
this.buildLinkTag(
article.get( 'creator_user_page_url' ),
article.get( 'user_name' ),
article.get( 'creator_user_page_exist' )
),
this.buildLinkTag(
article.get( 'creator_user_talk_page_url' ),
mw.msg( 'sp-contributions-talk' ),
article.get( 'creator_user_talk_page_exist' )
),
mw.msg( 'pipe-separator' ),
this.buildLinkTag(
article.get( 'creator_contribution_page_url' ),
mw.msg( 'contribslink' ),
true
)
);
article.set( 'author_byline', byline );
article.set(
'user_title_url',
this.buildRedLink(
article.get( 'creator_user_page_url' ),
article.get( 'creator_user_page_exist' )
)
);
article.set(
'user_talk_title_url',
this.buildRedLink(
article.get( 'creator_user_talk_page_url' ),
article.get( 'creator_user_talk_page_exist' )
)
);
article.set( 'user_contribs_title', article.get( 'creator_contribution_page' ) );
}
// set the article status
// delete status
if ( article.get( 'afd_status' ) == "1" || article.get( 'blp_prod_status' ) == "1" ||
article.get( 'csd_status' ) == "1" || article.get( 'prod_status' ) == "1" )
{
article.set( 'page_status', mw.msg( 'pagetriage-page-status-delete' ) );
// unreviewed status
} else if ( article.get( 'patrol_status' ) == "0" ) {
article.set( 'page_status', mw.msg( 'pagetriage-page-status-unreviewed' ) );
// auto-reviewed status
} else if ( article.get( 'patrol_status' ) == "3" ) {
article.set( 'page_status', mw.msg( 'pagetriage-page-status-autoreviewed' ) );
// reviewed status
} else {
if ( article.get( 'ptrp_last_reviewed_by' ) != 0 && article.get( 'reviewer' ) ) {
article.set(
'page_status',
mw.msg(
'pagetriage-page-status-reviewed',
Date.parseExact(
article.get( 'ptrp_reviewed_updated' ),
'yyyyMMddHHmmss'
).toString(
mw.msg( 'pagetriage-info-timestamp-date-format' )
),
this.buildLinkTag(
article.get( 'reviewer_user_page_url' ),
article.get( 'reviewer' ),
article.get( 'reviewer_user_page_exist' )
),
this.buildLinkTag(
article.get( 'reviewer_user_talk_page_url' ),
mw.msg( 'sp-contributions-talk' ),
article.get( 'reviewer_user_talk_page_exist' )
),
mw.msg( 'pipe-separator' ),
this.buildLinkTag(
article.get( 'reviewer_contribution_page_url' ),
mw.msg( 'contribslink' ),
true
)
)
);
} else {
article.set( 'page_status', mw.msg( 'pagetriage-page-status-reviewed-anonymous' ) );
}
}
article.set( 'title_url_format', mw.util.wikiUrlencode( article.get( 'title' ) ) );
var titleUrl = mw.util.wikiGetlink( article.get( 'title' ) );
if ( Number( article.get( 'is_redirect' ) ) === 1 ) {
titleUrl = this.buildLink( titleUrl, 'redirect=no' );
}
article.set( 'title_url', titleUrl );
},
tagWarningNotice: function () {
var dateStr = this.get( 'creation_date_utc' );
if ( !dateStr ) {
return '';
}
var now = new Date();
now = new Date(
now.getUTCFullYear(),
now.getUTCMonth(),
now.getUTCDate(),
now.getUTCHours(),
now.getUTCMinutes(),
now.getUTCSeconds()
);
var begin = Date.parseExact( dateStr, 'yyyyMMddHHmmss' );
var diff = Math.round( ( now.getTime() - begin.getTime() ) / ( 1000 * 60 ) );
// only generate a warning if the page is less than 30 minutes old
if ( diff < 30 ) {
if ( diff < 1 ) {
diff = 1;
}
return mw.msg( 'pagetriage-tag-warning-notice', diff );
} else {
return '';
}
},
buildLinkTag: function ( url, text, exists ) {
var style = '';
if ( !exists ) {
url = this.buildRedLink( url, exists );
style = 'new';
}
return mw.html.element(
'a',
{
'href': url,
'class': style
},
text
);
},
buildRedLink: function ( url, exists ) {
if ( !exists ) {
url = this.buildLink( url, 'action=edit&redlink=1' );
}
return url;
},
buildLink: function ( url, param ) {
if ( param ) {
var mark = ( url.indexOf( '?' ) === -1 ) ? '?' : '&';
url += mark + param;
}
return url;
},
// url and parse are used here for retrieving a single article in the curation toolbar.
// articles are retrived for list view using the methods in the Articles collection.
url: function() {
var d = new Date();
var params = $.param( {
action: 'pagetriagelist',
format: 'json',
timestamp: d.getTime()
} );
return mw.util.wikiScript( 'api' ) + '?' + params + '&' + $.param( { page_id: this.pageId } );
},
parse: function( response ) {
if ( response.pagetriagelist !== undefined && response.pagetriagelist.pages !== undefined ) {
// data came directly from the api
// extract the useful bits of json.
return response.pagetriagelist.pages[0];
} else {
// already parsed by the collection's parse function.
return response;
}
},
addHistory: function() {
this.revisions = new mw.pageTriage.RevisionList( { eventBus: this.eventBus, pageId: this.pageId } );
this.revisions.fetch();
}
} );
mw.pageTriage.ArticleList = Backbone.Collection.extend( {
moreToLoad: true,
model: mw.pageTriage.Article,
optionsToken: '',
apiParams: {
limit: 20,
dir: 'newestfirst',
namespace: 0,
showreviewed: 1,
showunreviewed: 1,
showdeleted: 1
/*
showredirs: 0
showbots: 0,
no_category: 0,
no_inbound_links: 0,
non_autoconfirmed_users: 0,
blocked_users: 0,
username: null
*/
},
initialize: function( options ) {
this.eventBus = options.eventBus;
this.eventBus.bind( "filterSet", this.setParams );
// pull any saved filter settings from the user's option
if ( !mw.user.isAnon() && mw.user.options.get( 'userjs-NewPagesFeedFilterOptions' ) ) {
this.setParams( $.parseJSON( mw.user.options.get( 'userjs-NewPagesFeedFilterOptions' ) ) );
}
},
url: function() {
var d = new Date();
var params = $.param( {
action: 'pagetriagelist',
format: 'json',
timestamp: d.getTime()
} );
return mw.util.wikiScript( 'api' ) + '?' + params + '&' + $.param( this.apiParams );
},
parse: function( response ) {
// See if the fetch returned an extra page or not. This lets us know if there are more
// pages to load in a subsequent fetch.
if ( response.pagetriagelist.pages && response.pagetriagelist.pages.length > this.apiParams.limit ) {
// Remove the extra page from the list
response.pagetriagelist.pages.pop();
this.moreToLoad = true;
} else {
// We have no more pages to load.
this.moreToLoad = false;
}
// extract the useful bits of json.
return response.pagetriagelist.pages;
},
setParams: function( apiParams ) {
this.apiParams = apiParams;
},
setParam: function( paramName, paramValue ) {
this.apiParams[paramName] = paramValue;
},
encodeFilterParams: function() {
var encodedString = '';
var paramArray = new Array;
var _this = this;
$.each( this.apiParams, function( key, val ) {
var str = '"' + key + '":';
if ( typeof val === 'string' ) {
val = '"' + val.replace(/[\"]/g, '\\"') + '"';
}
str += val;
paramArray.push( str );
} );
encodedString = '{ ' + paramArray.join( ', ' ) + ' }';
return encodedString;
},
// Save the filter parameters to a user's option
saveFilterParams: function() {
var _this = this;
if ( !mw.user.isAnon() ) {
if ( this.optionsToken ) {
this.apiSetFilterParams();
} else {
var tokenRequest = {
'action': 'tokens',
'type' : 'options',
'format': 'json'
};
$.ajax( {
type: 'get',
url: mw.util.wikiScript( 'api' ),
data: tokenRequest,
dataType: 'json',
success: function( data ) {
try {
_this.optionsToken = data.tokens.optionstoken;
} catch ( e ) {
throw new Error( 'Could not get token (requires MediaWiki 1.20).' );
}
_this.apiSetFilterParams();
}
} );
}
}
},
apiSetFilterParams: function() {
var prefRequest = {
'action': 'options',
'change': 'userjs-NewPagesFeedFilterOptions=' + this.encodeFilterParams(),
'token': this.optionsToken,
'format': 'json'
};
$.ajax( {
type: 'post',
url: mw.util.wikiScript( 'api' ),
data: prefRequest,
dataType: 'json'
} );
},
getParam: function( key ) {
return this.apiParams[key];
}
} );
} );