2017-07-12 15:12:40 +00:00
|
|
|
( function ( M, $ ) {
|
2018-05-08 22:03:52 +00:00
|
|
|
var AB = M.require( 'skins.minerva.scripts/AB' ),
|
2018-08-14 14:45:54 +00:00
|
|
|
Page = M.require( 'mobile.startup/Page' ),
|
2018-07-02 22:12:37 +00:00
|
|
|
allIssues = {},
|
|
|
|
KEYWORD_ALL_SECTIONS = 'all',
|
2018-07-11 15:16:21 +00:00
|
|
|
config = mw.config,
|
|
|
|
user = mw.user,
|
2018-07-02 22:12:37 +00:00
|
|
|
NS_MAIN = 0,
|
|
|
|
NS_TALK = 1,
|
|
|
|
NS_CATEGORY = 14,
|
2018-07-11 15:16:21 +00:00
|
|
|
CURRENT_NS = config.get( 'wgNamespaceNumber' ),
|
2018-08-16 19:00:38 +00:00
|
|
|
pageIssuesLogger = M.require( 'skins.minerva.scripts/pageIssuesLogger' ),
|
|
|
|
pageIssuesParser = M.require( 'skins.minerva.scripts/pageIssuesParser' ),
|
|
|
|
PageIssuesOverlay = M.require( 'skins.minerva.scripts/PageIssuesOverlay' ),
|
2018-07-11 15:16:21 +00:00
|
|
|
// setup ab test
|
|
|
|
abTest = new AB( {
|
|
|
|
testName: 'WME.PageIssuesAB',
|
|
|
|
// Run AB only on article namespace, otherwise set samplingRate to 0,
|
|
|
|
// forcing user into control (i.e. ignored/not logged) group.
|
|
|
|
samplingRate: ( CURRENT_NS === NS_MAIN ) ? config.get( 'wgMinervaABSamplingRate', 0 ) : 0,
|
|
|
|
sessionId: user.sessionId()
|
|
|
|
} ),
|
2018-09-19 22:21:32 +00:00
|
|
|
QUERY_STRING_FLAG = mw.util.getParamValue( 'minerva-issues' ),
|
|
|
|
// Per T204746 a user can request the new treatment regardless of test group
|
|
|
|
isUserRequestingNewTreatment = QUERY_STRING_FLAG === 'b',
|
|
|
|
newTreatmentEnabled = abTest.isB() || isUserRequestingNewTreatment;
|
2018-07-11 15:16:21 +00:00
|
|
|
|
2018-08-13 21:54:24 +00:00
|
|
|
function isLoggingRequired( pageIssues ) {
|
|
|
|
// No logging necessary when the A/B test is disabled (control group).
|
2018-09-19 22:21:32 +00:00
|
|
|
return !isUserRequestingNewTreatment && abTest.isEnabled() && pageIssues.length;
|
2018-07-11 15:16:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-29 12:59:37 +00:00
|
|
|
* Array.reduce callback that returns the severity of page issues.
|
|
|
|
* In the case that a page-issue is part of a "multiple issues" template,
|
|
|
|
* returns the maximum severity for that group of issues.
|
|
|
|
*
|
2018-09-07 00:49:43 +00:00
|
|
|
* @param {array} formattedArr - the return array containing severities
|
|
|
|
* @param {IssueSummary} currentItem current IssueSummary object
|
2018-08-29 12:59:37 +00:00
|
|
|
* @param {number} currentIndex current index of pageIssues
|
|
|
|
* @param {array} pageIssues array of pageIssues
|
|
|
|
*
|
|
|
|
* @return {array} acc
|
2018-07-11 15:16:21 +00:00
|
|
|
*/
|
2018-09-07 00:49:43 +00:00
|
|
|
function formatPageIssuesSeverity( formattedArr, currentItem, currentIndex, pageIssues ) {
|
|
|
|
var lastItem = pageIssues[ currentIndex - 1 ],
|
2018-10-16 22:46:28 +00:00
|
|
|
issue = currentItem.issue,
|
2018-09-07 00:49:43 +00:00
|
|
|
lastFormattedIndex = formattedArr.length - 1,
|
|
|
|
lastFormattedValue = formattedArr[ lastFormattedIndex ];
|
2018-10-16 22:46:28 +00:00
|
|
|
// If the last and current item `grouped`, fold the maxSeverity
|
2018-09-07 00:49:43 +00:00
|
|
|
// of the two items into a single value.
|
2018-10-16 22:46:28 +00:00
|
|
|
if ( lastItem && lastItem.issue && lastItem.issue.grouped && issue.grouped ) {
|
2018-09-07 00:49:43 +00:00
|
|
|
formattedArr[ lastFormattedIndex ] = pageIssuesParser.maxSeverity(
|
2018-10-16 22:46:28 +00:00
|
|
|
[ lastFormattedValue, issue.severity ]
|
2018-08-29 12:59:37 +00:00
|
|
|
);
|
|
|
|
} else {
|
2018-10-16 22:46:28 +00:00
|
|
|
formattedArr.push( issue.severity );
|
2018-08-29 12:59:37 +00:00
|
|
|
}
|
2018-09-07 00:49:43 +00:00
|
|
|
return formattedArr;
|
2018-07-11 15:16:21 +00:00
|
|
|
}
|
2018-08-17 19:00:57 +00:00
|
|
|
|
2018-07-30 11:33:34 +00:00
|
|
|
/**
|
|
|
|
* Create a link element that opens the issues overlay.
|
|
|
|
*
|
|
|
|
* @ignore
|
|
|
|
*
|
|
|
|
* @param {string} labelText The text value of the element
|
|
|
|
* @return {JQuery}
|
|
|
|
*/
|
|
|
|
function createLinkElement( labelText ) {
|
|
|
|
return $( '<a class="cleanup mw-mf-cleanup"></a>' )
|
|
|
|
.text( labelText );
|
|
|
|
}
|
2017-07-12 15:12:40 +00:00
|
|
|
|
2018-07-30 11:33:34 +00:00
|
|
|
/**
|
|
|
|
* Render a banner in a containing element.
|
|
|
|
* if in group B, a learn more link will be append to any amboxes inside $container
|
|
|
|
* if in group A or control, any amboxes in container will be removed and a link "page issues"
|
|
|
|
* will be rendered above the heading.
|
|
|
|
* This function comes with side effects. It will populate a global "allIssues" object which
|
|
|
|
* will link section numbers to issues.
|
2018-07-26 10:13:33 +00:00
|
|
|
* @param {Page} page to search for page issues inside
|
2018-07-30 11:33:34 +00:00
|
|
|
* @param {string} labelText what the label of the page issues banner should say
|
2018-08-23 20:57:47 +00:00
|
|
|
* @param {string} section that the banner and its issues belong to.
|
2018-07-30 11:33:34 +00:00
|
|
|
* If string KEYWORD_ALL_SECTIONS banner should apply to entire page.
|
2018-09-13 15:33:20 +00:00
|
|
|
* @param {boolean} inline - if true the first ambox in the section will become the entry point
|
|
|
|
* for the issues overlay
|
2018-07-30 11:33:34 +00:00
|
|
|
* and if false, a link will be rendered under the heading.
|
2018-07-30 14:45:44 +00:00
|
|
|
* @param {OverlayManager} overlayManager
|
2018-07-30 11:33:34 +00:00
|
|
|
* @ignore
|
2018-07-30 14:35:54 +00:00
|
|
|
*
|
|
|
|
* @return {JQuery.Object}
|
2018-07-30 11:33:34 +00:00
|
|
|
*/
|
2018-07-26 10:13:33 +00:00
|
|
|
function createBanner( page, labelText, section, inline, overlayManager ) {
|
|
|
|
var $learnMore, $metadata,
|
2018-07-30 11:33:34 +00:00
|
|
|
issueUrl = section === KEYWORD_ALL_SECTIONS ? '#/issues/' + KEYWORD_ALL_SECTIONS : '#/issues/' + section,
|
|
|
|
selector = 'table.ambox, table.tmbox, table.cmbox, table.fmbox',
|
|
|
|
issues = [],
|
2018-10-16 22:46:28 +00:00
|
|
|
$link;
|
2017-07-12 15:12:40 +00:00
|
|
|
|
2018-07-26 10:13:33 +00:00
|
|
|
if ( section === KEYWORD_ALL_SECTIONS ) {
|
|
|
|
$metadata = page.$( selector );
|
|
|
|
} else {
|
|
|
|
// find heading associated with the section
|
|
|
|
$metadata = page.findChildInSectionLead( parseInt( section, 10 ), selector );
|
|
|
|
}
|
2018-07-30 11:33:34 +00:00
|
|
|
// clean it up a little
|
|
|
|
$metadata.find( '.NavFrame' ).remove();
|
|
|
|
$metadata.each( function () {
|
|
|
|
var issue,
|
|
|
|
$this = $( this );
|
2017-07-12 15:12:40 +00:00
|
|
|
|
2018-07-30 11:33:34 +00:00
|
|
|
if ( $this.find( selector ).length === 0 ) {
|
2018-10-16 23:02:22 +00:00
|
|
|
issue = pageIssuesParser.extract( $this );
|
|
|
|
// Some issues after "extract" has been run will have no text.
|
2018-07-30 11:33:34 +00:00
|
|
|
// For example in Template:Talk header the table will be removed and no issue found.
|
|
|
|
// These should not be rendered.
|
|
|
|
if ( issue.text ) {
|
|
|
|
issues.push( issue );
|
2017-07-12 15:12:40 +00:00
|
|
|
}
|
2018-07-30 11:33:34 +00:00
|
|
|
}
|
|
|
|
} );
|
2018-07-11 15:16:21 +00:00
|
|
|
// store it for later
|
2018-07-30 11:33:34 +00:00
|
|
|
allIssues[section] = issues;
|
2017-07-12 15:12:40 +00:00
|
|
|
|
2018-10-16 22:46:28 +00:00
|
|
|
// If issues were extracted and there are inline amboxes, add learn more
|
|
|
|
// and icon to the UI element.
|
|
|
|
if ( issues.length && $metadata.length && inline ) {
|
|
|
|
issues[0].issue.icon.$el.prependTo( $metadata.eq( 0 ).find( '.mbox-text' ) );
|
2018-07-30 11:33:34 +00:00
|
|
|
$learnMore = $( '<span>' )
|
|
|
|
.addClass( 'ambox-learn-more' )
|
|
|
|
.text( mw.msg( 'skin-minerva-issue-learn-more' ) );
|
|
|
|
if ( $( '.mw-collapsible-content' ).length ) {
|
|
|
|
// e.g. Template:Multiple issues
|
|
|
|
$learnMore.insertAfter( $metadata.find( '.mbox-text-span' ) );
|
2018-05-08 22:03:52 +00:00
|
|
|
} else {
|
2018-07-30 11:33:34 +00:00
|
|
|
// e.g. Template:merge from
|
|
|
|
$learnMore.appendTo( $metadata.find( '.mbox-text' ) );
|
|
|
|
}
|
|
|
|
$metadata.click( function () {
|
2018-08-16 19:00:38 +00:00
|
|
|
var pageIssue = pageIssuesParser.parse( this );
|
|
|
|
pageIssuesLogger.log( {
|
2018-07-11 15:16:21 +00:00
|
|
|
action: 'issueClicked',
|
2018-08-23 20:57:47 +00:00
|
|
|
issuesSeverity: [ pageIssue.severity ],
|
|
|
|
sectionNumbers: [ section ]
|
2018-07-11 15:16:21 +00:00
|
|
|
} );
|
2018-07-30 11:33:34 +00:00
|
|
|
overlayManager.router.navigate( issueUrl );
|
|
|
|
return false;
|
|
|
|
} );
|
|
|
|
} else {
|
|
|
|
$link = createLinkElement( labelText );
|
2018-09-05 19:49:31 +00:00
|
|
|
$link.attr( 'href', '#/issues/' + section );
|
2018-07-11 15:16:21 +00:00
|
|
|
$link.click( function () {
|
2018-08-16 19:00:38 +00:00
|
|
|
pageIssuesLogger.log( {
|
2018-07-11 15:16:21 +00:00
|
|
|
action: 'issueClicked',
|
2018-08-23 20:57:47 +00:00
|
|
|
issuesSeverity: [
|
|
|
|
pageIssuesParser.maxSeverity(
|
|
|
|
getIssues( '0' )
|
|
|
|
.map( function ( issue ) { return issue.severity; } )
|
|
|
|
)
|
|
|
|
],
|
2018-09-07 00:37:33 +00:00
|
|
|
// In the old treatment, an issuesClicked event will always be '0'
|
|
|
|
// as the old treatment is always associated with the lead section and we
|
|
|
|
// are only sending one maximum severity for all of them.
|
|
|
|
// An issuesClicked event should only ever be associated with one issue box.
|
|
|
|
sectionNumbers: [ '0' ]
|
2018-07-11 15:16:21 +00:00
|
|
|
} );
|
|
|
|
} );
|
2018-07-30 11:33:34 +00:00
|
|
|
if ( $metadata.length ) {
|
|
|
|
$link.insertAfter( $( 'h1#section_0' ) );
|
|
|
|
$metadata.remove();
|
2018-05-08 22:03:52 +00:00
|
|
|
}
|
2018-07-02 22:12:37 +00:00
|
|
|
}
|
2018-07-30 14:35:54 +00:00
|
|
|
|
|
|
|
return $metadata;
|
2018-07-30 11:33:34 +00:00
|
|
|
}
|
2017-07-12 15:12:40 +00:00
|
|
|
|
2018-07-30 11:33:34 +00:00
|
|
|
/**
|
|
|
|
* Obtains the list of issues for the current page and provided section
|
2018-09-13 15:33:20 +00:00
|
|
|
* @param {number|string} section either KEYWORD_ALL_SECTIONS or a number relating to the
|
|
|
|
* section the issues belong to
|
2018-07-30 11:33:34 +00:00
|
|
|
* @return {jQuery.Object[]} array of all issues.
|
|
|
|
*/
|
|
|
|
function getIssues( section ) {
|
2018-08-31 22:48:34 +00:00
|
|
|
if ( section !== KEYWORD_ALL_SECTIONS ) {
|
2018-07-30 11:33:34 +00:00
|
|
|
return allIssues[section] || [];
|
2018-07-02 22:12:37 +00:00
|
|
|
}
|
2018-08-31 22:48:34 +00:00
|
|
|
// Note section.all may not exist, depending on the structure of the HTML page.
|
|
|
|
// It will only exist when Minerva has been run in desktop mode.
|
|
|
|
// If it's absent, we'll reduce all the other lists into one.
|
|
|
|
return allIssues.all || Object.keys( allIssues ).reduce(
|
|
|
|
function ( all, key ) {
|
|
|
|
return all.concat( allIssues[key] );
|
|
|
|
},
|
|
|
|
[]
|
|
|
|
);
|
2018-07-30 11:33:34 +00:00
|
|
|
}
|
2018-07-02 22:12:37 +00:00
|
|
|
|
2018-08-23 20:57:47 +00:00
|
|
|
/**
|
2018-08-29 09:45:04 +00:00
|
|
|
* Returns an array containing the section of each page issue.
|
2018-08-29 12:59:37 +00:00
|
|
|
* In the case that several page issues are grouped in a 'multiple issues' template,
|
|
|
|
* returns the section of those issues as one item.
|
2018-08-29 09:45:04 +00:00
|
|
|
* @param {Object} allIssues mapping section {Number} to {IssueSummary}
|
2018-08-23 20:57:47 +00:00
|
|
|
* @return {array}
|
|
|
|
*/
|
2018-08-29 09:45:04 +00:00
|
|
|
function getAllIssuesSections( allIssues ) {
|
|
|
|
return Object.keys( allIssues ).reduce( function ( acc, section ) {
|
|
|
|
if ( allIssues[ section ].length ) {
|
2018-08-29 12:59:37 +00:00
|
|
|
allIssues[ section ].forEach( function ( issue, i ) {
|
|
|
|
var lastIssue = allIssues[ section ][i - 1];
|
|
|
|
// If the last issue belongs to a "Multiple issues" template,
|
|
|
|
// and so does the current one, don't add the current one.
|
2018-10-16 22:46:28 +00:00
|
|
|
if ( lastIssue && lastIssue.grouped && issue.grouped ) {
|
2018-08-29 12:59:37 +00:00
|
|
|
acc[ acc.length - 1 ] = section;
|
|
|
|
} else {
|
|
|
|
acc.push( section );
|
|
|
|
}
|
2018-08-29 09:45:04 +00:00
|
|
|
} );
|
|
|
|
}
|
|
|
|
return acc;
|
|
|
|
}, [] );
|
2018-08-23 20:57:47 +00:00
|
|
|
}
|
|
|
|
|
2018-07-30 11:33:34 +00:00
|
|
|
/**
|
|
|
|
* Scan an element for any known cleanup templates and replace them with a button
|
|
|
|
* that opens them in a mobile friendly overlay.
|
|
|
|
* @ignore
|
2018-07-30 14:45:44 +00:00
|
|
|
* @param {OverlayManager} overlayManager
|
|
|
|
* @param {Page} page
|
2018-07-30 11:33:34 +00:00
|
|
|
*/
|
2018-07-30 14:45:44 +00:00
|
|
|
function initPageIssues( overlayManager, page ) {
|
2018-07-11 15:16:21 +00:00
|
|
|
var label,
|
2018-07-30 11:33:34 +00:00
|
|
|
$lead = page.getLeadSectionElement(),
|
2018-08-16 18:43:28 +00:00
|
|
|
issueOverlayShowAll = CURRENT_NS === NS_CATEGORY || CURRENT_NS === NS_TALK || !$lead,
|
2018-07-26 10:13:33 +00:00
|
|
|
inline = newTreatmentEnabled && CURRENT_NS === 0;
|
2017-07-12 15:12:40 +00:00
|
|
|
|
2018-07-30 11:33:34 +00:00
|
|
|
// set A-B test class.
|
2018-08-13 21:54:24 +00:00
|
|
|
$( 'html' ).addClass( newTreatmentEnabled ? 'issues-group-B' : 'issues-group-A' );
|
2018-07-02 22:12:37 +00:00
|
|
|
|
2018-08-16 18:43:28 +00:00
|
|
|
if ( CURRENT_NS === NS_TALK || CURRENT_NS === NS_CATEGORY ) {
|
2018-07-30 11:33:34 +00:00
|
|
|
// e.g. Template:English variant category; Template:WikiProject
|
2018-07-26 10:13:33 +00:00
|
|
|
createBanner( page, mw.msg( 'mobile-frontend-meta-data-issues-header-talk' ),
|
2018-07-30 14:45:44 +00:00
|
|
|
KEYWORD_ALL_SECTIONS, inline, overlayManager );
|
2018-07-11 15:16:21 +00:00
|
|
|
} else if ( CURRENT_NS === NS_MAIN ) {
|
2018-07-30 11:33:34 +00:00
|
|
|
label = mw.msg( 'mobile-frontend-meta-data-issues-header' );
|
|
|
|
if ( issueOverlayShowAll ) {
|
2018-07-26 10:13:33 +00:00
|
|
|
createBanner( page, label, KEYWORD_ALL_SECTIONS, inline, overlayManager );
|
2018-07-30 11:33:34 +00:00
|
|
|
} else {
|
|
|
|
// parse lead
|
2018-08-23 20:57:47 +00:00
|
|
|
createBanner( page, label, '0', inline, overlayManager );
|
2018-08-13 21:54:24 +00:00
|
|
|
if ( newTreatmentEnabled ) {
|
2018-09-13 15:33:20 +00:00
|
|
|
// parse other sections but only in group B. In treatment A no issues are shown
|
|
|
|
// for sections.
|
2018-07-26 10:13:33 +00:00
|
|
|
page.$( Page.HEADING_SELECTOR ).each( function ( i, headingEl ) {
|
2018-07-30 11:33:34 +00:00
|
|
|
var $headingEl = $( headingEl ),
|
|
|
|
sectionNum = $headingEl.find( '.edit-page' ).data( 'section' );
|
2018-07-13 20:23:55 +00:00
|
|
|
|
2018-09-13 15:33:20 +00:00
|
|
|
// Note certain headings matched using Page.HEADING_SELECTOR may not be
|
|
|
|
// headings and will not have a edit link. E.g. table of contents.
|
2018-08-23 20:57:47 +00:00
|
|
|
if ( sectionNum ) {
|
2018-09-13 15:33:20 +00:00
|
|
|
// Render banner for sectionNum associated with headingEl inside
|
|
|
|
// Page
|
|
|
|
createBanner(
|
|
|
|
page, label, sectionNum.toString(), inline, overlayManager
|
|
|
|
);
|
2018-08-23 20:57:47 +00:00
|
|
|
}
|
2018-07-30 11:33:34 +00:00
|
|
|
} );
|
2018-07-02 22:12:37 +00:00
|
|
|
}
|
2017-07-12 15:12:40 +00:00
|
|
|
}
|
2018-07-30 11:33:34 +00:00
|
|
|
}
|
2017-07-12 15:12:40 +00:00
|
|
|
|
2018-08-13 21:54:24 +00:00
|
|
|
if ( isLoggingRequired( getIssues( KEYWORD_ALL_SECTIONS ) ) ) {
|
2018-08-16 21:59:48 +00:00
|
|
|
// Enable logging of the PageIssues schema, setting up defaults.
|
2018-08-16 19:00:38 +00:00
|
|
|
pageIssuesLogger.subscribe(
|
2018-08-13 21:54:24 +00:00
|
|
|
newTreatmentEnabled,
|
2018-08-16 19:00:38 +00:00
|
|
|
pageIssuesLogger.newPageIssueSchemaData(
|
2018-08-13 21:54:24 +00:00
|
|
|
newTreatmentEnabled,
|
|
|
|
CURRENT_NS,
|
2018-08-29 12:59:37 +00:00
|
|
|
getIssues( KEYWORD_ALL_SECTIONS ).reduce( formatPageIssuesSeverity, [] ),
|
2018-08-29 09:45:04 +00:00
|
|
|
getAllIssuesSections( allIssues )
|
2018-08-13 21:54:24 +00:00
|
|
|
)
|
|
|
|
);
|
2018-08-17 19:00:57 +00:00
|
|
|
|
|
|
|
// Report that the page has been loaded.
|
|
|
|
pageIssuesLogger.log( {
|
2018-08-27 17:52:55 +00:00
|
|
|
action: 'pageLoaded'
|
2018-08-17 19:00:57 +00:00
|
|
|
} );
|
2018-08-13 21:54:24 +00:00
|
|
|
}
|
|
|
|
|
2018-07-30 11:33:34 +00:00
|
|
|
// Setup the overlay route.
|
|
|
|
overlayManager.add( new RegExp( '^/issues/(\\d+|' + KEYWORD_ALL_SECTIONS + ')$' ), function ( section ) {
|
2018-09-13 15:33:20 +00:00
|
|
|
return new PageIssuesOverlay(
|
|
|
|
getIssues( section ), pageIssuesLogger, section, CURRENT_NS );
|
2018-07-30 11:33:34 +00:00
|
|
|
} );
|
|
|
|
}
|
2017-07-12 15:12:40 +00:00
|
|
|
|
2018-08-16 19:00:38 +00:00
|
|
|
M.define( 'skins.minerva.scripts/pageIssues', {
|
2018-07-30 14:35:54 +00:00
|
|
|
init: initPageIssues,
|
2018-09-13 15:33:20 +00:00
|
|
|
// The logger requires initialization (subscription). Ideally, the logger would be
|
|
|
|
// initialized and passed to initPageIssues() by the client. Since it's not, expose a log
|
|
|
|
// method and hide the subscription call in cleanuptemplates.
|
2018-08-16 19:00:38 +00:00
|
|
|
log: pageIssuesLogger.log,
|
2018-07-30 14:35:54 +00:00
|
|
|
test: {
|
2018-09-07 00:49:43 +00:00
|
|
|
formatPageIssuesSeverity: formatPageIssuesSeverity,
|
2018-08-29 09:45:04 +00:00
|
|
|
getAllIssuesSections: getAllIssuesSections,
|
2018-07-30 14:35:54 +00:00
|
|
|
createBanner: createBanner
|
|
|
|
}
|
2018-07-30 14:45:44 +00:00
|
|
|
} );
|
2017-07-12 15:12:40 +00:00
|
|
|
|
|
|
|
}( mw.mobileFrontend, jQuery ) );
|