Page MenuHomePhabricator

Use MediaWiki JavaScript API instead of custom code
Closed, ResolvedPublic

Description

In PageTriage, we can remove a lot of custom code that uses $.ajax() (15 matches in 8 files) and make use of API functions provided by the mw JavaScript object (mw.Api().saveOption(), mw.user.options.get(), and mw.Api().postWithToken() , etc. For example, the apiSetFilterParams(), saveFilterParams(), setToolbarPreference(), finishSetToolbarPreference() contain code that request a token, then make a request. For example saveFilterParams() has:

if ( this.optionsToken ) {
	this.apiSetFilterParams();
} else {
	tokenRequest = {
		action: 'tokens',
		type: 'options',
		format: 'json'
	};
	$.ajax( {
		type: 'get',
		url: mw.util.wikiScript( 'api' ),
		data: tokenRequest,
		dataType: 'json',
		success: function ( data ) {
			try {
				that.optionsToken = data.tokens.optionstoken;
			} catch ( e ) {
				throw new Error( 'Could not get token (requires MediaWiki 1.20).' );
			}
			that.apiSetFilterParams();
		}
	} );
}

which can be simplified with mw.Api().postWithToken().

There are other sections of code, for example submit() in ext.pageTriage.delete.js which could be rewritten in a more concise way to use mw.Api().postWithToken().

Event Timeline

kostajh created this task.Aug 17 2018, 11:54 AM
Restricted Application added a subscriber: Aklapper. · View Herald TranscriptAug 17 2018, 11:54 AM

Doing this should also fix this warning which appears in the console: "\"action=tokens\" has been deprecated. Please use \"action=query&meta=tokens\" instead."}},

Catrope assigned this task to kostajh.Aug 17 2018, 10:39 PM
Catrope claimed this task.
Catrope moved this task from Code Review to In Progress on the Growth-Team (Current Sprint) board.
Catrope added a subscriber: Catrope.

So it looks like @kostajh did part of this in https://gerrit.wikimedia.org/r/c/mediawiki/extensions/PageTriage/+/446517 , but not all of it. I'll do the rest now.

Change 453548 had a related patch set uploaded (by Catrope; owner: Catrope):
[mediawiki/extensions/PageTriage@master] Port all uses of $.ajax() to mw.Api

https://gerrit.wikimedia.org/r/453548

kostajh added a subscriber: Etonkovidova.

For QA – in my local environment I tested out the various methods that were worked on (i.e. setting tags, adding a note with a tag, marking an article as reviewed, etc). @Etonkovidova let me know if you have any questions about it!

Change 446517 had a related patch set uploaded (by Kosta Harlan; owner: Kosta Harlan):
[mediawiki/extensions/PageTriage@master] DBPerformance: Master connections and writes via POST not GET

https://gerrit.wikimedia.org/r/446517

Change 446517 merged by jenkins-bot:
[mediawiki/extensions/PageTriage@master] DBPerformance: Master connections and writes via POST not GET

https://gerrit.wikimedia.org/r/446517

Change 453548 merged by jenkins-bot:
[mediawiki/extensions/PageTriage@master] Port all uses of $.ajax() to mw.Api

https://gerrit.wikimedia.org/r/453548

Etonkovidova closed this task as Resolved.Aug 22 2018, 9:59 PM