User:Topbanana/RLRL SR Utility.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// Create some search-and-replace tools - this uses a slightly modified version
// of [[User:Gerbrant/edit/regexReplace.js]])

mw.loader.load( 'https://en.wikipedia.org/w/index.php?title=User:Topbanana/regexReplace.js&action=raw&ctype=text/javascript');

function RLRL_SR_Utility()
{
   strAdvert = '  - [[w:en:WP:RLR|You can help!]]';

   // Check if we are editing
   if ( document.getElementById("editform") )
   {
      // Check for and extract parameters
      parSearch  = mw.util.getParamValue( 'RLRLS' );
      parReplace  = mw.util.getParamValue( 'RLRLR' );
      parRedirect = mw.util.getParamValue( 'RLRLRD' );

      // First check if we are being asked to create a redirect
     if( parRedirect )
     {
        // Check we have no other parameters
        if( parSearch || parReplace )
          return;
 
        // Check the edit box is empty
        if( document.forms.editform.wpTextbox1.value.length > 0 )
          return;
 
        // Correct the parameters provided
        parRedirect = unescape( decodeURIComponent( parRedirect.replace( /_/g, " " ) ) );

        // Okay, populate the edit box and summary
        document.forms.editform.wpTextbox1.value = '#REDIRECT [[' + parRedirect+ ']]';
        document.forms.editform.wpSummary.value =
            'Link repair:  Redirecting page to ' + parRedirect + strAdvert;
        document.forms.editform.wpWatchthis.checked = false;
 
        return;
     }
 
     // Okay, not a redirect.  Check if we are being asked to search-and-replace
 
     // If we've not got a useful set of parameters, give up   
     if( !parSearch || !parReplace )
       return;
 
     // Correct the parameters provided
     parSearch = unescape( decodeURIComponent( parSearch.replace( /_/g, " " ) ) );
     parReplace = unescape( decodeURIComponent( parReplace.replace( /_/g, " " ) ) );
 
     // Check for Template namespace prefix
     if( parSearch.indexOf( "Template:" ) == 0 &&
         parReplace.indexOf( "Template:" ) == 0 )
     {
        parSearch = parSearch.slice( 9 );
        parReplace = parReplace.slice( 9 );
     }

     // Escape any characters with special meanings in regular expressions
     parRegex = parSearch.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");

     // Specifically ignore the case of the first letter
     parRegex = '[' + parRegex.substring(0, 1).toUpperCase() +
                parRegex.substring(0, 1).toLowerCase() + ']' +
                parRegex.substring(1);

     // Find and manipulate the search and replace controls
     createRegexControls();
 
     document.getElementById('tbRegex_SearchCaseSensitive').checked = true;
     document.getElementById('tbRegex_SearchTextBox').value = parRegex;
     document.getElementById('tbRegex_FindButton').click();
     document.getElementById('tbRegex_ReplaceTextBox').value = parReplace;
 
     // Populate the edit summary
     document.forms.editform.wpSummary.value =
        'Link repair:  ' + parSearch + ' -> ' + parReplace + strAdvert;
     document.forms.editform.wpMinoredit.checked = true;
   }

   // Check if we are editing
   if ( document.getElementById("movepage") )
   {
      // Check for and extract parameters
      parNewNS    = mw.util.getParamValue( 'RLRLMNS' );
      parNewTitle = mw.util.getParamValue( 'RLRLMNT' );
 
     // If we've not got a useful set of parameters, give up   
     if( !parNewNS || !parNewTitle )
       return;
 
     // Correct the parameters provided
     parNewTitle = unescape( decodeURIComponent( parNewTitle.replace( /_/g, " " ) ) );
 
     document.getElementById('wpNewTitleNs').value = parNewNS;
     document.getElementById('wpNewTitleMain').value = parNewTitle;
 
     // Populate the edit summary
     document.getElementById('wpReason').value = strAdvert;
   }
};
 
addOnloadHook( RLRL_SR_Utility );