Jump to content

Wikipedia:Banner standardisation/Code

From Wikipedia, the free encyclopedia
<?php

$username="";
$password="";


$PageNames=array();
$querycontinue="NONE";
$querynumber=0

//Getting articles from Category:WikiProject banners

while($querycontinue) {
  $CatQuery[$querynumber]=new DOMDocument();
  $url="http://wiki.riteme.site/w/api.php?action=query&list=categorymembers&cmtitle=Category:WikiProject banners&cmlimit=500&format=xml&cmprop=ids|title|sortkey|timestamp";
  if($querycontinue&&$querycontinue!="NONE") { $url=$url."$cmcontinue=".ereg_replace("&",'%26',$querycontinue); }
  $CatQuery[$querynumber]->load($url);
  $arrayoffset=$querynumber*500;
  $x=0;
  for($i=$arrayoffset;$i<$arrayoffset+$CatQuery[$querynumber]->getElementsByTagName("cm")->length;$i++) {
    $TitleArray[$i]=$CatQuery[$querynumber]->getElementsByTagName("cm")->item($x)->attributes->getNamedItem("title")->nodeValue;
    $x++;
  }
  if($CatQuery[$querynumber]->getElementsByTagName("query-continue")->length>0) {
    $querycontinue=$CatQuery[$querynumber]->getElementsByTagName("query-continue")->item(0)->childNodes->item(0)->attributes->getNamedItem("cmcontinue")->nodeValue;
    echo $querycontinue;
    $querynumber++;
  }
  else { $querycontinue=false; }
}
echo "\n\n\n";

$MoveName=array();
$NeedsHuman=array();

foreach($TitleArray as $number=>$title) {

  //Checking for Incorrect Syntax

  if(!ereg("Template:WikiProject ",$title) {

    //Checking for Project Mentions

    $ProjectMentions=array();
    $APIInfo=new DOMDocument();
    $APIInfo->load("http://wiki.riteme.site/w/api.php?action=query&prop=revisions&rvprop=content&format=xml&titles=".ereg_replace("&",'%26',$title));
    echo ereg_replace("&",'%26',$title)."-".$number."|";
    ereg("\[\[Wikipedia:WikiProject [^\]\|]",$APIInfo,$ProjectMentions);

    //Matching Obvious Project Mentions and Setting Move Name

    if(isset($ProjectMentions[1])&&((!isset($ProjectMentions[2]))||(isset($ProjectMentions[2])&&$ProjectMentions[2]==$ProjectMentions[1]))) {
      $MoveName[$title]="Template:WikiProject ".ereg_replace("Wikipedia:WikiProject ","",ereg_replace("[\[\]\|]","",$ProjectMentions[1]));

    //OR adding to List that needs to be sorted by hand.

    else { $NeedsHuman[$title]=$title; }
    unset($ProjectMentions);
  }
}

//Checking for existing page

$NeedsDeletion=array();
$NeedsMove=array();

foreach($MoveName as $oldtitle=>$newtitle) {
  $APIInfo=new DOMDocument();
  $APIInfo->load("http://wiki.riteme.site/w/api.php?action=query&prop=revisions&rvprop=content&format=xml&titles=".ereg_replace("&",'%26',$newtitle));

  // Checking for non-existent page

  if(isset($APIInfo->getElementsByTagName("page")->item(0)->attributes->getNamedItem("missing")) {
    $NeedsMove[$oldtitle]=$newtitle;
  }

  // Checking for redirect page

  elseif(ereg("#REDIRECT \[\[".$oldtitle,$APIInfo->getElementsByTagName("rev")->item(0)->nodeValue)) {
    $NeedsDeletion[$oldtitle]=$newtitle;
  }

  // Adding others to NeedsHuman

  else { $NeedsHuman[$oldtitle]=$oldtitle; }
}

unset($MoveName);

// Logging In

$LoginGet=curl_init("http://wiki.riteme.site/w/api.php");
curl_setopt($LoginGet,CURLOPT_POST,1);
curl_setopt($LoginGet,CURLOPT_RETURNTRANSFER,1);
curl_setopt($LoginGet,CURLOPT_COOKIEJAR,"cookies.txt");
curl_setopt($LoginGet,CURLOPT_POSTFIELDS,"lgname=".$username."&lgpassword=".$password."&action=login&format=xml");
$LoginResult=curl_exec($LoginGet);
$LoginDOM=new DOMDocument();
$LoginDOM->loadXML($LoginResult);
if($LoginDOM-getElementsByTagName("login")->item(0)->attributes->getNamedItem('result')->nodeValue!="Success") { die("No Login"); }
curl_close($LoginGet);

// UPDATE: Arrays to be acted upon -- NeedsMove, NeedsDeletion, and NeedsHuman

// Moving pages that can be moved

foreach($NeedsMove as $oldtitle=>$newtitle) {
  $MoveRequest=curl_init("http://wiki.riteme.site/w/index.php?title=Special:Movepage&action=submit");
  curl_setopt($MoveRequest,CURLOPT_RETURNTRANSFER,1);
  curl_setopt($MoveRequest,CURLOPT_POST,1);
  curl_setopt($MoveRequest,CURLOPT_COOKIEJAR,"cookies.txt");
  curl_setopt($MoveRequest,CURLOPT_COOKIEFILE,"cookies.txt");
  $POSTvars="wpNewTitle=".$newtitle."&wpOldTitle=".$oldtitle."&wpReason=WikiProject Banner standardisation";
  $MoveTokenXML=new DOMDocument();
  $MoveTokenXML->load("http://wiki.riteme.site/w/api.php?action=query&prop=info&intoken=move&format=xml&titles=".$oldtitle);
  $MoveToken=$MoveTokenXML->getElementsByTagName("page")->item(0)->attributes->getNamedItem("movetoken")->nodeValue;
  $POSTvars=$POSTvars."&wpMove=Move page&wpEditToken=".$MoveToken;
  curl_setopt($MoveRequest,CURLOPT_POSTFIELDS,$POSTvars);
  curl_exec($MoveRequest);
  curl_close($MoveRequest);
  if(isset($MoveRequest)) { unset($MoveRequest); }
}