PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/irc/wd.removals.php

https://github.com/Dispositif/addbot
PHP | 157 lines | 101 code | 36 blank | 20 comment | 17 complexity | b8b6924f92d69bbc4326cc43488e23bc MD5 | raw file
  1. <?PHP
  2. /* -------------------------------- Bot Setup -------------------------------- */
  3. //From http://toolserver.org/~chris/highlight.php?d=chris/classes/&f=botclasses.php
  4. require '../classes/botclasses.php';
  5. $wiki = new wikipedia;
  6. $wiki->url = 'http://en.wikipedia.org/w/api.php';
  7. global $wiki;
  8. $parentpid = posix_getpid();
  9. $user = "Addbot";
  10. $nickname = "Addbot-wdr";
  11. $owner = "Addshore";
  12. set_time_limit(0);
  13. require '/home/addshore/.password.addbot';
  14. $wiki->login($user,$pass);
  15. echo "USER: Logged In!\n";
  16. unset($pass);
  17. $rc_host = "irc.wikimedia.org";
  18. $rc_port = 6667;
  19. $rc_channel = "#en.wikipedia";
  20. $regex = "\n\[\[(nostalgia|ten|test|aa|ab|ace|af|ak|als|am|an|ang|ar|arc|arz|as|ast|av|ay|az|ba|bar|bat-smg|bcl|be|be-x-old|bg|bh|bi|bjn|bm|bn|bo|bpy|br|bs|bug|bxr|ca|cbk-zam|cdo|ce|ceb|ch|cho|chr|chy|ckb|co|cr|crh|cs|csb|cu|cv|cy|da|de|diq|dsb|dv|dz|ee|el|eml|eo|es|et|eu|ext|fa|ff|fi|fiu-vro|fj|fo|fr|frp|frr|fur|fy|ga|gag|gan|gd|gl|glk|gn|got|gu|gv|ha|hak|haw|he|hi|hif|ho|hr|hsb|ht|hu|hy|hz|ia|id|ie|ig|ii|ik|ilo|io|is|it|iu|ja|jbo|jv|ka|kaa|kab|kbd|kg|ki|kj|kk|kl|km|kn|ko|koi|kr|krc|ks|ksh|ku|kv|kw|ky|la|lad|lb|lbe|lez|lg|li|lij|lmo|ln|lo|lt|ltg|lv|map-bms|mdf|mg|mh|mhr|mi|min|mk|ml|mn|mo|mr|mrj|ms|mt|mus|mwl|my|myv|mzn|na|nah|nap|nds|nds-nl|ne|new|ng|nl|nn|no|nov|nrm|nso|nv|ny|oc|om|or|os|pa|pag|pam|pap|pcd|pdc|pfl|pi|pih|pl|pms|pnb|pnt|ps|pt|qu|rm|rmy|rn|ro|roa-rup|roa-tara|ru|rue|rw|sa|sah|sc|scn|sco|sd|se|sg|sh|si|simple|sk|sl|sm|sn|so|sq|sr|srn|ss|st|stq|su|sv|sw|szl|ta|te|tet|tg|th|ti|tk|tl|tn|to|tpi|tr|ts|tt|tum|tw|ty|udm|ug|uk|ur|ve|vec|vep|vi|vls|vo|wa|war|wo|wuu|xal|xh|xmf|yi|yo|za|zea|zh|zh-classical|zh-min-nan|zh-yue|zu):([^\]]+)\]\]";
  21. /* -------------------------------- Wikimedia RC IRC feed -------------------------------- */
  22. $wikimedia = array();
  23. $wikimedia['SOCKET'] = @fsockopen($rc_host, $rc_port, $errno, $errstr, 30);
  24. if($wikimedia['SOCKET'])
  25. {
  26. wikimediaCommand("NICK $user");
  27. wikimediaCommand("USER $user Addbot Wikipedia Bot");
  28. wikimediaCommand("JOIN $rc_channel");
  29. while(!feof($wikimedia['SOCKET']))//while connected to the server
  30. {
  31. $rawline = fgets($wikimedia['SOCKET'], 1024);
  32. $line = str_replace(array("\n","\r","\002"),'',$rawline);
  33. $line = preg_replace('/\003(\d\d?(,\d\d?)?)?/','',$line);
  34. // echo 'FEED: '.$line."\n";
  35. if (!$line) { fclose($feed); break; }
  36. $linea= explode(' ',$line,4);
  37. if (strtolower($linea[0]) == 'ping') {
  38. wikimediaCommand("PONG :".substr($wikimedia['READ_BUFFER'], 6)); //Reply with pong
  39. } elseif ((strtolower($linea[1]) == 'privmsg') and (strtolower($linea[2]) == strtolower($rc_channel))) {
  40. $message = substr($linea[3],1);
  41. //[[Special:Log/abusefilter]] hit \* (.+) \* \1 triggered [[Special:AbuseFilter/3|filter 3]], performing the action "edit" on [[([^\]]+)]].
  42. echo ".";
  43. //if we think it matches our tag
  44. //
  45. if (preg_match('/(Filter 531|Removal of all interwiki links)/i',$message))
  46. {
  47. echo "*";
  48. //If the line looks right split it up lots :P (from cluebot)
  49. if (preg_match('/^\[\[((Talk|User|Wikipedia|File|MediaWiki|Template|Help|Category|Portal|Book|Special)(( |_)talk)?:)?([^\x5d]*)\]\] (\S*) (http:\/\/en\.wikipedia\.org\/w\/index\.php\?(diff|oldid)=(\d*)&(oldid|rcid)=(\d*).*|http:\/\/en\.wikipedia\.org\/wiki\/\S+)? \* ([^*]*) \* (\(([^)]*)\))? (.*)$/S',$message,$m)) {
  50. $change['received'] = microtime(1);
  51. $change['namespace'] = $m[1];
  52. $change['title'] = $m[5];
  53. $change['flags'] = $m[6];
  54. $change['url'] = $m[7];
  55. $change['id1'] = $m[9]; //Normal=diff Newpage=oldid
  56. $change['id2'] = $m[11]; //Normal=oldid Newpage=rcid
  57. $change['user'] = $m[12];
  58. $change['length'] = $m[14];
  59. $change['comment'] = $m[15];
  60. $change['name'] = $m[1].$m[5];
  61. if (preg_match('/(Joe Decker|Addshore|Delusion23|Lugnuts|Naddy)/i',$change['user'])){continue;}
  62. //skipnonmain
  63. //CHECKS
  64. preg_match('/action "edit" on \[\[([^\]]+)\]\]\./',$change['comment'],$tits);
  65. $title = $tits[1];
  66. //$title = $wiki->lastedit($change['user']);
  67. //print_r($title);
  68. echo $change['user']." on ".$title."\n";
  69. $revid = $wiki->last2edits($title);
  70. $old = $wiki->getpage($title,$revid[1]);
  71. //get the other links
  72. $r = $wiki->wikidatasitelinks($title);
  73. echo "w";
  74. //if there is only 1 entity (i.e. the wikidata stuff isnt broken somewhere)
  75. if(count($r) == 1)
  76. {
  77. //foreach entitiy found
  78. foreach($r as $ent)
  79. {
  80. $id = $ent['id'];
  81. //Check if we have site links
  82. if(isset($ent['sitelinks']))
  83. {
  84. //get links that were removed $iws
  85. preg_match_all('/'.$regex.'/',$old,$iws);
  86. $edited = false;
  87. $link = "";
  88. //foreahc sitelink on page
  89. foreach($iws[1] as $key => $iw)
  90. {
  91. echo ",";
  92. //if it is not found on wikidata
  93. if($iw != "" && !isset($ent['sitelinks'][str_replace("-","_",$iw).'wiki']))
  94. {
  95. echo "l";
  96. $edited = true;
  97. $link = $link."[http://www.wikidata.org/w/index.php?title=Special%3AItemByTitle&site=".urlencode($iw)."wiki&page=".urlencode($iws[2][$key])." $iw],";
  98. //post
  99. }
  100. }
  101. if($edited == true)
  102. {
  103. $log = "=== [[User talk:".$change['user']."]] - $title ===\nRemoved iw-($link) on [https://en.wikipedia.org/w/index.php?title=".urlencode($title)."&action=history ".$title."] that is not on [[d:$id]]";
  104. $wiki->edit("User:Addbot/log/wikidata",$wiki->getPage("User:Addbot/log/wikidata")."\n".$log,"Found possible incorrect removal of IW links by ".$change['user'],true);
  105. continue;
  106. }
  107. }
  108. }
  109. }
  110. }
  111. }
  112. }
  113. flush();
  114. }
  115. }
  116. function wikimediaCommand ($cmd) {
  117. global $wikimedia; //Extends our $server array to this function
  118. @fwrite($wikimedia['SOCKET'], $cmd."\r"); //sends the command to the server
  119. echo "IRC<: $cmd\n"; //displays it on the screen
  120. }
  121. ?>