Utilisateur:Dr Brains/BAS

Une page de Wikipédia, l'encyclopédie libre.
  function editPage(page, pattern, replace, comment) {
	new mw.Api().get({
		action:'query',
		format:'json',
		titles: page ,
		prop:'revisions',
		rvprop:['content','timestamp'] // Pas sûr de cette syntaxe
	}).then(function(data) {
		for (var pageid in data.query.pages) break;
		var contenu = data.query.pages[pageid].revisions[0]['*'];
		var ts = data.query.pages[pageid].revisions[0]['timestamp'];
		editPagego(page, pattern, replace, comment, contenu, ts);
	})
};
 
    function editPagego(page, pattern, replace, comment, content, ts) {
      var request = mw.config.get( 'wgServer' ) + mw.config.get( 'wgScriptPath' ) + '/api.php?format=xml&action=query' +
	'&prop=info|templates&tllimit=499&intoken=edit&inprop=protection&titles=' + encodeURIComponent(page);
      PaStec_ajax.http({retry:3,url: request, onSuccess: doEditPage, onFailure: failure_alert,
	    page : page, comment: comment, pattern: pattern, replace: replace, content:content, ts:ts});
    }

 // EDIT PAGE 2 -------------------------------------------------------------------------------------------------------------------------------------------------------------
     function doEditPage2(xmlreq, data) {
      //data.page     //page to edit
      //data.comment  //modification comment
      //data.pattern  //search pattern
      //data.content  //page content
      //   for pattern
      //      "*" replaces the page content with data.replace
      //      "<" adds data.replace to the beginning of the page
      //      ">" adds data.replace to the end of the page
      //data.replace  //replacing string
      var XML = xmlreq.responseXML;
      // Redirect ?
      var Redirect = XML.getElementsByTagName('redirects')[0];
      if(Redirect){
	alert("La page suivante est une redirection : "+page)
        return;
      }
      // no robots ?
      var Templates = XML.getElementsByTagName('tl');
      for(var a=0,l=Templates.length;a<l;a++){
        if(Templates[a].getAttribute('title') == "Modèle:Nobots"){
	  alert("La page suivante n'accepte pas les modifications par les robots : "+page)
          return;
        }
      }
      // Protected page ?
      var PR = XML.getElementsByTagName("pr");
      for(var a=0,l=PR.length;a<l;a++){
        var Type = PR[a].getAttribute("type");
        var Level = PR[a].getAttribute("level");
        if(Type=="edit" && mw.config.get('wgUserGroups').indexOf(Level)===-1){
	  alert("La page suivante est protégée en écriture : "+page)
          return;
        }
      }

      try{ 
	var page = XML.getElementsByTagName('page')[0];
	var pagefortemplate=getPageName();
	var token = page.getAttribute('edittoken');
	var lrev = page.getElementsByTagName('rev');
	var oldtext = "", basetimestamp = "";
	if(pagefortemplate.search(/Modèle:/)!=-1){
         var template ="<noinclude>{{suppression|note=ok}}</noinclude>\n"
    }else{
         var template = "{{Suppression}}"
    }
	var oldtext = data.content;
	var basetimestamp = '&basetimestamp=' + encodeURIComponent(data.ts));
 
	if (data.pattern == "*")       var newtext = data.replace;
	else if (data.pattern == ">")  var newtext = oldtext + data.replace;
	else if (data.pattern == "<")  var newtext = data.replace + oldtext;
	else {
	  var newtext = template + oldtext.replace(data.pattern, data.replace);
	  if (oldtext == newtext){
	    alert('replace failed in : ' +data.page);
	    return; //replace failed
	  }
	}
 
	//document.getElementById('talkpageheader').innerHTML+="<br>"+token+"<br>";
 
 
	var requestEditData = 'title=' + encodeURIComponent(data.page) +
          '&text=' + encodeURIComponent(newtext) +
          '&token=' + encodeURIComponent(token) +
          '&summary=' + encodeURIComponent(data.comment) +
          '&starttimestamp=' + encodeURIComponent(page.getAttribute('starttimestamp')) +
          '&watchlist=nochange' + basetimestamp;
	var headers = new Array();
	headers['Content-Type'] = 'application/x-www-form-urlencoded';
	PaStec_ajax.http({retry:3,url: mw.config.get( 'wgServer' ) + mw.config.get( 'wgScriptPath' ) + '/api.php?format=xml&action=edit',
	      method: "POST", headers: headers, data: requestEditData,
	      onFailure: failure_alert, onSuccess:confirm_executed});
 
 
      }catch(error){
	alert(error+" : "+data.page );
      }
    }
 
    function editPage2(page, pattern, replace, comment) {
      var request = mw.config.get( 'wgServer' ) + mw.config.get( 'wgScriptPath' ) + '/api.php?format=xml&action=query' +
	'&prop=revisions|info&intoken=edit&rvprop=timestamp|content&titles=' + encodeURIComponent(page);
      PaStec_ajax.http({retry:3,url: request, onSuccess: doEditPage2, onFailure: failure_alert,
	    page : page, comment: comment, pattern: pattern, replace: replace});
    }
 //FIN EDIT PAGE 2 ----------------------------------------------------------------------------------------------------------------------------------------------------------