All files / mobile.editor.overlay parseBlockInfo.js

69.56% Statements 16/23
62.5% Branches 10/16
75% Functions 3/4
69.56% Lines 16/23

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 701x           1x 2x                               2x 2x 2x         2x         2x     2x           2x         2x 2x 2x 2x           2x                 2x    
var util = require( '../mobile.startup/util' );
 
/**
 * @param {string} blockinfo
 * @return {Object}
 */
module.exports = function parseBlockInfo( blockinfo ) {
	const blockInfo = {
		partial: blockinfo.blockpartial || false,
		noCreateAccount: blockinfo.blocknocreate || false,
		anonOnly: blockinfo.blockanononly === undefined ? true : blockinfo.blockanononly,
		creator: {
			name: blockinfo.blockedby,
			url: null
		},
		expiry: null,
		duration: null,
		reason: '',
		blockId: blockinfo.blockid
	};
 
	// Workaround to parse a message parameter for mw.message, see T96885
	function jqueryMsgParse( wikitext ) {
		const parser = new mw.jqueryMsg.Parser();
		try {
			const ast = parser.wikiTextToAst( wikitext );
			return parser.emitter.emit( ast ).html();
		} catch ( e ) {
			// Ignore error as it's probably the parser error. Usually this is because we
			// can't parse templates.
			return false;
		}
	}
 
	// URL only useful if block creator is a local user
	Iif ( blockinfo.blockedbyid === 0 ) {
		blockInfo.creator.url = '';
	} else {
		blockInfo.creator.url = mw.Title.makeTitle(
			mw.config.get( 'wgNamespaceIds' ).user,
			blockInfo.creator.name
		).getUrl();
	}
 
	Iif ( [ 'infinite', 'indefinite', 'infinity', 'never' ].indexOf( blockinfo.blockexpiry ) === -1 ) {
		blockInfo.expiry = mw.message( 'parentheses', blockinfo.blockexpiryformatted ).escaped();
		blockInfo.duration = blockinfo.blockexpiryrelative;
	}
 
	const reason = blockinfo.blockreason;
	Eif ( reason ) {
		blockInfo.reason = jqueryMsgParse( reason ) || mw.html.escape( reason );
		blockInfo.parsedReason = ( new mw.Api() ).get( {
			action: 'parse',
			formatversion: 2,
			text: reason,
			contentmodel: 'wikitext'
		} ).then( function ( result ) {
			return result.parse.text;
		} ).catch( function () {
			return jqueryMsgParse( reason ) || mw.html.escape( reason );
		} );
	} else {
		blockInfo.reason = mw.message( 'mobile-frontend-editor-generic-block-reason' ).escaped();
		blockInfo.parsedReason = util.Deferred().resolve( blockInfo.reason ).promise();
	}
 
	return blockInfo;
};