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

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

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

Change 796361 had a related patch set uploaded (by DannyS712; author: DannyS712):

[mediawiki/extensions/PageTriage@master] Javascript cleanup: remove unused `optionsToken` variables

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

Change 796361 merged by jenkins-bot:

[mediawiki/extensions/PageTriage@master] Javascript cleanup: remove unused `optionsToken` variables

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