MediaWiki:Gadget-WiP.js

if ( $.inArray( 'Works In Progress', mw.config.get( 'wgCategories' ) ) !== -1 && $( '.WorkInProgressDone' ).attr( 'id' ) !== undefined ) { var wipArticleId = mw.config.get( 'wgArticleId' ); function removeTag( wipIndexNumber ) { ( new mw.Api ).get( {     action: 'query',      prop: 'revisions',      rvprop: 'content',      pageids: wipArticleId    } ).done( function ( rvResult ) {      var content = rvResult.query.pages[ wipArticleId ].revisions[ 0 ][ '*' ];/* TRON */console.log( 'Pretending to remove WiP%d from:\n%o', wipIndexNumber, content );      var matchCountIndex = 0;      var matchCountStart = 0;      while ( matchCountIndex !== -1 ) {        var matchText = content.match( /\{\{(?:WiP|Work-in-progress)(?:\|(?:.*?))?\}\}(?:\n*)?/g );/* TRON */console.log( 'Templates found: %o', matchText );        if ( matchText[ matchCountIndex ] === undefined ) {          matchCountIndex = -1;        } else {          var preTextStart = content.indexOf( matchText[ matchCountIndex ], matchCountStart );          var matchTextLength = matchText[ matchCountIndex ].length;          matchCountStart = preTextStart + matchTextLength; if ( matchCountIndex === ( wipIndexNumber - 1 ) ) {/* TRON */console.log( '%d === %d', matchCountIndex, ( wipIndexNumber - 1 ) ); var preText = content.substr( 0, preTextStart );/* TRON */console.log( 'preText = %s', preText ); var remText = content.substr( preTextStart, matchCountStart );/* TRON */console.log( 'remText = %s', remText ); remText = remText.match( /\{\{(WiP|Work-in-progress)(?:\|(.*?))?\}\}\n+/ );/* TRON */console.log( 'remText = %o', remText ); var postText = content.substr( matchCountStart );/* TRON */console.log( 'postText = %s', postText ); var newText = preText + postText;/* TRON */console.log( 'postWithToken:action=edit&ids=%d&minor=true&text=%s&summary=Removing T:%s #%d from page.', wipArticleId, newText, remText[ 1 ], wipIndexNumber ); ( new mw.Api ).postWithToken( 'edit', {             action: 'edit',              pageid: wipArticleId,              minor: true,              text: newText,              summary: 'Removing T:' + remText[ 1 ] + ' #' + wipIndexNumber + ' from page.'            } ).done( function ( editResult ) {               if ( editResult.edit.result === 'Success' ) {                /* if Link replace line with big text that say the template has been successfuly removed from the source then fades away */console.log( 'success:\n%o', remText[ 2 ] );                /* if Button replace red color with green, icon with green check, and text to say the template has been successfuly removed from the source then fades away */                if ( remText[ 2 ] !== undefined ) {console.log('test `%s` for small= results in %s', remText[ 2 ], /small=/.test( remText[ 2 ] ) );                  if ( /small=/.test( remText[ 2 ] ) === false ) { /* Notify the user's who's name was in the template that it has been removed from the page on their talk page. */console.log( 'Notify: %s', remText[ 2 ] ); }               }              } else { /* indicate the edit failed */console.log('failed'); }           } );            matchCountIndex = -2;          }          matchCountIndex++;        }      }    } ); } $( '.WorkInProgressDone' ).each( function( i ) {    var wipID = $( this ).attr( 'id' );    var wipIndexNumber = wipID.replace( 'WiP',  ).replace( /(Link|Button)/gi,  );    var wipLinkOrButton = wipID.replace( 'WiP' + wipIndexNumber, '' );    if ( wipLinkOrButton === 'Link' ) {      $( 'div#WiP' + wipIndexNumber + 'Link' ).html( '[&thinsp;Mark as done&thinsp;]' ).toggle;    } else if ( wipLinkOrButton === 'Button' ) {      $( 'div#WiP' + wipIndexNumber + 'Button' ).html( '' ).toggle;    } else {      alert( 'Invalid WiP template detected. Please report this page to an administrator. Thanks.' );   }  } ); }