summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/Translate/resources/js/ext.translate.special.pagepreparation.js
diff options
context:
space:
mode:
Diffstat (limited to 'www/wiki/extensions/Translate/resources/js/ext.translate.special.pagepreparation.js')
-rw-r--r--www/wiki/extensions/Translate/resources/js/ext.translate.special.pagepreparation.js118
1 files changed, 82 insertions, 36 deletions
diff --git a/www/wiki/extensions/Translate/resources/js/ext.translate.special.pagepreparation.js b/www/wiki/extensions/Translate/resources/js/ext.translate.special.pagepreparation.js
index c0305fa8..03c4385d 100644
--- a/www/wiki/extensions/Translate/resources/js/ext.translate.special.pagepreparation.js
+++ b/www/wiki/extensions/Translate/resources/js/ext.translate.special.pagepreparation.js
@@ -1,4 +1,4 @@
-( function ( $, mw ) {
+( function () {
'use strict';
/**
@@ -11,10 +11,8 @@
function savePage( pageName, pageContent ) {
var api = new mw.Api();
- // Change to csrf when support for MW 1.25 is dropped
- return api.postWithToken( 'edit', {
+ return api.postWithToken( 'csrf', {
action: 'edit',
- format: 'json',
title: pageName,
text: pageContent,
summary: $( '#pp-summary' ).val()
@@ -36,7 +34,6 @@
return api.post( {
action: 'query',
prop: 'revisions',
- format: 'json',
rvprop: 'content',
rvlimit: '1',
titles: pageName,
@@ -94,11 +91,11 @@
aliases.push( 'category' );
for ( i = 0; i < aliases.length; i++ ) {
- aliases[ i ] = $.escapeRE( aliases[ i ] );
+ aliases[ i ] = mw.RegExp.escape( aliases[ i ] );
}
aliasList = aliases.join( '|' );
- // Regex: http://regex101.com/r/sJ3gZ4/2
+ // Regex: https://regex101.com/r/sJ3gZ4/2
categoryRegex = new RegExp( '\\[\\[((' + aliasList + ')' +
':[^\\|]+)(\\|[^\\|]*?)?\\]\\]', 'gi' );
pageContent = pageContent.replace( categoryRegex, '\n</translate>\n' +
@@ -134,6 +131,45 @@
}
/**
+ * Add an anchor to a section header with the given headerText
+ *
+ * @param {string} headerText
+ * @param {string} pageContent
+ * @return {string}
+ */
+ function addAnchor( headerText, pageContent ) {
+ var headerSearchRegex, anchorID, replaceAnchorRegex,
+ spanSearchRegex;
+
+ anchorID = headerText.replace( ' ', '-' ).toLowerCase();
+
+ headerText = mw.RegExp.escape( headerText );
+ // Search for the header having text as headerText
+ // Regex: https://regex101.com/r/fD6iL1
+ headerSearchRegex = new RegExp( '(==+[ ]*' + headerText + '[ ]*==+)', 'gi' );
+ // This is to ensure the tags and the anchor are added only once
+
+ if ( pageContent.indexOf( '<span id="' + mw.html.escape( anchorID ) + '"' ) === -1 ) {
+ pageContent = pageContent.replace( headerSearchRegex, '</translate>\n' +
+ '<span id="' + mw.html.escape( anchorID ) + '"></span>\n<translate>\n$1' );
+ }
+
+ // This is to add back the tags which were removed in cleanupTags()
+ if ( pageContent.indexOf( '</translate>\n<span id="' + anchorID + '"' ) === -1 ) {
+ spanSearchRegex = new RegExp( '(<span id="' + mw.RegExp.escape( anchorID ) + '"></span>)', 'gi' );
+ pageContent = pageContent.replace( spanSearchRegex, '\n</translate>\n$1\n</translate>\n' );
+ }
+
+ // Replace the link text with the anchorID defined above
+ // Regex: https://regex101.com/r/kB5bK3
+ replaceAnchorRegex = new RegExp( '(\\[\\[#)' + headerText + '(.*\\]\\])', 'gi' );
+ pageContent = pageContent.replace( replaceAnchorRegex, '$1' +
+ anchorID.replace( '$', '$$$' ) + '$2' );
+
+ return pageContent;
+ }
+
+ /**
* Convert all the links into two-party form and add the 'Special:MyLanguage/' prefix
* to links in valid namespaces for the wiki. For example, [[Example]] would be converted
* to [[Special:MyLanguage/Example|Example]].
@@ -142,22 +178,33 @@
* @return {string}
*/
function fixInternalLinks( pageContent ) {
- var normalizeRegex, linkPrefixRegex,
- namespaces, nsString;
+
+ var normalizeRegex, linkPrefixRegex, sectionLinksRegex,
+ match, searchText, namespaces, nsString;
+ searchText = pageContent;
normalizeRegex = new RegExp( /\[\[(?!Category)([^|]*?)\]\]/gi );
// First convert all links into two-party form. If a link is not having a pipe,
// add a pipe and duplicate the link text
- // Regex : http://regex101.com/r/pO9nN2
+ // Regex: https://regex101.com/r/pO9nN2
pageContent = pageContent.replace( normalizeRegex, '[[$1|$1]]' );
namespaces = getNamespaces();
nsString = namespaces.join( '|' );
+ // Finds all the links to sections on the same page.
+ // Regex: https://regex101.com/r/cX6jT3
+ sectionLinksRegex = new RegExp( /\[\[#(.*?)(\|(.*?))?\]\]/gi );
+ match = sectionLinksRegex.exec( searchText );
+ while ( match !== null ) {
+ pageContent = addAnchor( match[ 1 ], pageContent );
+ match = sectionLinksRegex.exec( searchText );
+ }
+
linkPrefixRegex = new RegExp( '\\[\\[((?:(?:special(?!:MyLanguage\\b)|' + nsString +
'):)?[^:]*?)\\]\\]', 'gi' );
// Add the 'Special:MyLanguage/' prefix for all internal links of valid namespaces and
// mainspace.
- // Regex : http://regex101.com/r/zZ9jH9
+ // Regex: https://regex101.com/r/zZ9jH9
pageContent = pageContent.replace( linkPrefixRegex, '[[Special:MyLanguage/$1]]' );
return pageContent;
}
@@ -204,7 +251,7 @@
aliases.push( 'file' );
for ( i = 0; i < aliases.length; i++ ) {
- aliases[ i ] = $.escapeRE( aliases[ i ] );
+ aliases[ i ] = mw.RegExp.escape( aliases[ i ] );
}
aliasList = aliases.join( '|' );
@@ -231,7 +278,7 @@
*/
function doTemplates( pageContent ) {
var templateRegex;
- // Regex: http://regex101.com/r/wA3iX0
+ // Regex: https://regex101.com/r/wA3iX0
templateRegex = new RegExp( /^({{[\s\S]*?}})/gm );
pageContent = pageContent.replace( templateRegex, '</translate>\n$1\n<translate>' );
@@ -249,6 +296,8 @@
pageContent = pageContent.replace( /\n\n+/gi, '\n\n' );
// Removes redundant <translate> tags
pageContent = pageContent.replace( /\n<translate>(\n*?)<\/translate>/gi, '' );
+ // Removes the Special:MyLanguage/ prefix for section links
+ pageContent = pageContent.replace( /Special:MyLanguage\/#/gi, '#' );
return pageContent;
}
@@ -267,7 +316,6 @@
return api.get( {
action: 'query',
prop: 'revisions',
- format: 'json',
rvprop: 'content',
rvlimit: '1',
titles: pageName
@@ -298,20 +346,17 @@
}
// Remove all what has been already handled somewhere else
- namespaces.splice( $.inArray( '', namespaces ), 1 );
- namespaces.splice( $.inArray( 'category', namespaces ), 1 );
- namespaces.splice( $.inArray( 'category_talk', namespaces ), 1 );
- namespaces.splice( $.inArray( 'special', namespaces ), 1 );
- namespaces.splice( $.inArray( 'file', namespaces ), 1 );
- namespaces.splice( $.inArray( 'file_talk', namespaces ), 1 );
+ [ '', 'category', 'category_talk', 'special', 'file', 'file_talk' ].forEach( function ( ns ) {
+ namespaces.splice( namespaces.indexOf( ns ), 1 );
+ } );
for ( i = 0; i < namespaces.length; i++ ) {
- namespaces[ i ] = $.escapeRE( namespaces[ i ] );
+ namespaces[ i ] = mw.RegExp.escape( namespaces[ i ] );
}
return namespaces;
}
- $( document ).ready( function () {
+ $( function () {
var pageContent,
$input = $( '#page' );
@@ -320,14 +365,16 @@
} );
$( '#action-save' ).click( function () {
- var serverName, pageName,
+ var pageName,
pageUrl = '';
- pageName = $.trim( $input.val() );
- serverName = mw.config.get( 'wgServerName' );
+ pageName = $input.val().trim();
savePage( pageName, pageContent ).done( function () {
pageUrl = mw.Title.newFromText( pageName ).getUrl( { action: 'edit' } );
- $( '.messageDiv' ).html( mw.message( 'pp-save-message', pageUrl ).parse() ).show();
+ $( '.messageDiv' )
+ .empty()
+ .append( mw.message( 'pp-save-message', pageUrl ).parseDom() )
+ .show();
$( '.divDiff' ).hide( 'fast' );
$( '#action-prepare' ).show();
$input.val( '' );
@@ -339,37 +386,36 @@
$( '#action-prepare' ).click( function () {
var pageName, messageDiv = $( '.messageDiv' );
- pageName = $.trim( $input.val() );
+ pageName = $input.val().trim();
messageDiv.hide();
if ( pageName === '' ) {
- window.alert( mw.msg( 'pp-pagename-missing' ) );
+ // eslint-disable-next-line no-alert
+ alert( mw.msg( 'pp-pagename-missing' ) );
return;
}
$.when( getPageContent( pageName ) ).done( function ( content ) {
pageContent = content;
- pageContent = $.trim( pageContent );
+ pageContent = pageContent.trim();
pageContent = cleanupTags( pageContent );
pageContent = addLanguageBar( pageContent );
pageContent = addTranslateTags( pageContent );
pageContent = addNewLines( pageContent );
pageContent = fixInternalLinks( pageContent );
pageContent = doTemplates( pageContent );
- doFiles( pageContent )
- .then( doCategories )
- .done( function ( pageContent ) {
+ doFiles( pageContent ).then( doCategories ).done( function ( pageContent ) {
pageContent = postPreparationCleanup( pageContent );
- pageContent = $.trim( pageContent );
+ pageContent = pageContent.trim();
getDiff( pageName, pageContent ).done( function ( diff ) {
$( '.diff tbody' ).append( diff );
$( '.divDiff' ).show( 'fast' );
if ( diff !== '' ) {
- messageDiv.html( mw.msg( 'pp-prepare-message' ) ).show();
+ messageDiv.text( mw.msg( 'pp-prepare-message' ) ).show();
$( '#action-prepare' ).hide();
$( '#action-save' ).show();
$( '#action-cancel' ).show();
} else {
- messageDiv.html( mw.msg( 'pp-already-prepared-message' ) ).show();
+ messageDiv.text( mw.msg( 'pp-already-prepared-message' ) ).show();
}
} );
} );
@@ -377,4 +423,4 @@
} );
} );
-}( jQuery, mediaWiki ) );
+}() );