/extensions/DSMW/tests/p2pBot.php

https://github.com/ChuguluGames/mediawiki-svn · PHP · 303 lines · 243 code · 28 blank · 32 comment · 63 complexity · da8a13fa60d63a09cfd690c962089421 MD5 · raw file

  1. <?php
  2. /*
  3. * To change this template, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. /**
  7. * GET Request => code 200
  8. * POST Request => code 302
  9. *
  10. * @author hantz
  11. */
  12. class p2pBot {
  13. var $bot;
  14. function __construct( $bot ) {
  15. $this->bot = $bot;
  16. }
  17. function createPage( $pageName, $content ) {
  18. $res = $this->bot->wikiFilter( $pageName, 'callbackTestFct', 'summary', $content );
  19. return $res;
  20. }
  21. // static function append($content,$line) {
  22. // $content=$content.$line;
  23. // return $content;
  24. // }
  25. function editPage( $pageName, $line ) {
  26. $res = $this->bot->wikiFilter( $pageName, 'append', 'summary', $line );
  27. return $res;
  28. }
  29. function createPush( $name, $request ) {
  30. // $post_vars['url'] = $url;
  31. $post_vars['name'] = $name;
  32. $post_vars['keyword'] = $request;
  33. $this->bot->maxredirs = 0;
  34. if ( $this->bot->submit( $this->bot->wikiServer . PREFIX . '/index.php?action=pushpage', $post_vars ) ) {
  35. // Now we need to check whether our edit was accepted. If it was, we'll get a 302 redirecting us to the article. If it wasn't (e.g. because of an edit conflict), we'll get a 200.
  36. $code = substr( $this->bot->response_code, 9, 3 ); // shorten 'HTTP 1.1 200 OK' to just '200'
  37. if ( '200' == $code ) {
  38. echo 'Create push failed with error 200 : (' . $this->bot->results . ')';
  39. return false;
  40. }
  41. elseif ( '302' == $code ) {
  42. return true;
  43. }
  44. else {
  45. echo 'push failed error not 200 : (' . $this->bot->results . ')';
  46. return false;
  47. }
  48. } else {
  49. echo 'push submit failed (' . $this->bot->wikiServer . PREFIX . '/index.php?action=pushpage ' . $post_vars . ')';
  50. return false;
  51. }
  52. }
  53. function push( $name ) {
  54. $post_vars['action'] = 'onpush';
  55. if ( is_array( $name ) ) {
  56. $post_vars['name'] = $name;
  57. } else {
  58. $post_vars['name[]'] = $name;
  59. }
  60. $this->bot->maxredirs = 0;
  61. if ( $this->bot->submit( $this->bot->wikiServer . PREFIX . '/index.php', $post_vars ) ) {
  62. // Now we need to check whether our edit was accepted. If it was, we'll get a 302 redirecting us to the article. If it wasn't (e.g. because of an edit conflict), we'll get a 200.
  63. $code = substr( $this->bot->response_code, 9, 3 ); // shorten 'HTTP 1.1 200 OK' to just '200'
  64. if ( '200' == $code ) {
  65. if ( strpos( $this->bot->results, "<html" ) > 0 ) {
  66. echo "push failed with error 200:(" . $this->bot->results . ")\n";
  67. return false;
  68. }
  69. else { // Push return some information with Ajax
  70. echo $this->bot->results . "\n";
  71. return true;
  72. }
  73. }
  74. elseif ( '302' == $code )
  75. return true;
  76. else {
  77. echo "push failed error not 200:(" . $this->bot->results . ")";
  78. return false;
  79. }
  80. } else {
  81. echo "push submit failed:(" . $this->bot->wikiServer . PREFIX . '/index.php' . $post_vars . ")";
  82. return false;
  83. }
  84. }
  85. function createPull( $pullName, $url, $pushName ) {
  86. $post_vars['pullname'] = $pullName;
  87. $post_vars['url'] = $url . '/PushFeed:' . $pushName;
  88. // $post_vars['pushname'] = $pushName;
  89. $this->bot->maxredirs = 0;
  90. if ( $this->bot->submit( $this->bot->wikiServer . PREFIX . '/index.php?action=pullpage', $post_vars ) ) {
  91. // Now we need to check whether our edit was accepted. If it was, we'll get a 302 redirecting us to the article. If it wasn't (e.g. because of an edit conflict), we'll get a 200.
  92. $code = substr( $this->bot->response_code, 9, 3 ); // shorten 'HTTP 1.1 200 OK' to just '200'
  93. if ( '200' == $code ) {
  94. echo 'Create pull failed with error 200 : (' . $this->bot->results . ')';
  95. return false;
  96. }
  97. elseif ( '302' == $code )
  98. return true;
  99. else {
  100. echo 'Create pull failed error not 200 : (' . $this->bot->results . ')';
  101. return false;
  102. }
  103. // return false; // if you get this, it's time to debug.
  104. } else {
  105. echo 'Create pull submit failed (' . $this->bot->wikiServer . PREFIX . '/index.php?action=pullpage ' . $post_vars . ')';
  106. return false;
  107. }
  108. }
  109. function pull( $pullName ) {
  110. if ( is_array( $pullName ) ) {
  111. $post_vars['name'] = $pullName;
  112. } else {
  113. $post_vars['name[]'] = $pullName;
  114. }
  115. $post_vars['action'] = 'onpull';
  116. $this->bot->maxredirs = 0;
  117. $url = $this->bot->wikiServer . PREFIX . '/index.php';
  118. if ( $this->bot->submit( $this->bot->wikiServer . PREFIX . '/index.php', $post_vars ) ) {
  119. // Now we need to check whether our edit was accepted. If it was, we'll get a 302 redirecting us to the article. If it wasn't (e.g. because of an edit conflict), we'll get a 200.
  120. $code = substr( $this->bot->response_code, 9, 3 ); // shorten 'HTTP 1.1 200 OK' to just '200'
  121. if ( '200' == $code ) {
  122. if ( strpos( $this->bot->results, "<html" ) > 0 ) {
  123. echo "pull failed with error 200:(" . $this->bot->results . ")\n";
  124. return false;
  125. }
  126. else { // Pull return some information with Ajax
  127. echo $this->bot->results . "\n";
  128. return true;
  129. }
  130. }
  131. elseif ( '302' == $code )
  132. return true;
  133. else {
  134. echo "pull failed error not 200:(" . $this->bot->results . ")";
  135. return false;
  136. }
  137. } else {
  138. echo "pull submit failed:(" . $this->bot->wikiServer . PREFIX . '/index.php' . $post_vars . ")";
  139. return false;
  140. }
  141. }
  142. function updateProperies() {
  143. $post_vars['server'] = $this->bot->wikiServer;
  144. if ( $this->bot->submit( $this->bot->wikiServer . PREFIX . '/extensions/DSMW/bot/DSMWBot.php', $post_vars ) ) {
  145. // Now we need to check whether our edit was accepted. If it was, we'll get a 302 redirecting us to the article. If it wasn't (e.g. because of an edit conflict), we'll get a 200.
  146. $code = substr( $this->bot->response_code, 9, 3 ); // shorten 'HTTP 1.1 200 OK' to just '200'
  147. if ( '200' == $code ) {
  148. echo "updated properties failed with error 200:(" . $this->bot->results . ")";
  149. return false;
  150. }
  151. elseif ( '302' == $code )
  152. return true;
  153. else {
  154. echo "updated properties failed error not 200:(" . $this->bot->results . ")";
  155. return false;
  156. }
  157. } else {
  158. echo "updated properies submit failed:(" . $this->bot->wikiServer . PREFIX . '/index.php' . $post_vars . ")";
  159. return false;
  160. }
  161. }
  162. function articlesUpdate() {
  163. $post_vars['action'] = 'logootize';
  164. $this->bot->maxredirs = 0;
  165. // $url = $this->bot->wikiServer.PREFIX.'/index.php/Special:DSMWAdmin';
  166. if ( $this->bot->submit( $this->bot->wikiServer . PREFIX . '/index.php/Special:DSMWAdmin', $post_vars ) ) {
  167. // Now we need to check whether our edit was accepted. If it was, we'll get a 302 redirecting us to the article. If it wasn't (e.g. because of an edit conflict), we'll get a 200.
  168. $code = substr( $this->bot->response_code, 9, 3 ); // shorten 'HTTP 1.1 200 OK' to just '200'
  169. if ( '200' == $code ) {
  170. return true;
  171. }
  172. elseif ( '302' == $code )
  173. return true;
  174. else {
  175. echo "articlesUpdate failed error not 200:(" . $this->bot->results . ")";
  176. return false;
  177. }
  178. } else {
  179. echo "articlesUpdate submit failed:(" . $this->bot->wikiServer . PREFIX . '/index.php/Special:DSMWAdmin' . $post_vars . ")";
  180. return false;
  181. }
  182. }
  183. function importXML( $file ) {
  184. if ( !$this->bot->wikiConnect() )
  185. die ( "Unable to connect." );
  186. if ( !$this->bot->fetch( $this->bot->wikiServer . PREFIX . '/index.php?title=Special:Import' ) )
  187. return false;
  188. $val = strpos( $this->bot->results, 'name="editToken" type="hidden" value="' );// starting value
  189. $val2 = strlen( 'name="editToken" type="hidden" value="' );
  190. $val1 = strpos( $this->bot->results, '"', $val + $val2 );// ending value
  191. $editToken = substr( $this->bot->results, $val + $val2, $val1 -( $val + $val2 ) );
  192. $fp = fopen( $file, 'r' );
  193. $xmlContent = fread( $fp, filesize( $file ) );
  194. fclose( $fp );
  195. $post_vars['editToken'] = $editToken;
  196. $post_vars['action'] = 'submit';
  197. $post_vars['source'] = 'upload';
  198. $post_file['xmlimport'] = $file;
  199. // $post_vars['xmlimport']['name']= 'Wikipedia-20091119095555.xml';
  200. // $post_vars['xmlimport']['tmp_name']= $file;
  201. // $post_vars['xmlimport']['type']= 'text/xml';
  202. $this->bot->maxredirs = 0;
  203. $this->bot->set_submit_multipart();
  204. // $url = $this->bot->wikiServer.PREFIX.'/index.php/Special:DSMWAdmin';
  205. if ( $this->bot->submit( $this->bot->wikiServer . PREFIX . '/index.php?title=Special:Import&action=submit', $post_vars, $post_file ) ) {
  206. // Now we need to check whether our edit was accepted. If it was, we'll get a 302 redirecting us to the article. If it wasn't (e.g. because of an edit conflict), we'll get a 200.
  207. $code = substr( $this->bot->response_code, 9, 3 ); // shorten 'HTTP 1.1 200 OK' to just '200'
  208. if ( '200' == $code ) {
  209. echo "import failed with error 200:(" . $this->bot->results . ")";
  210. return true;
  211. }
  212. elseif ( '302' == $code )
  213. return true;
  214. else {
  215. return false;
  216. }
  217. } else {
  218. echo "import submit failed:(" . $this->bot->wikiServer . PREFIX . '/index.php?title=Special:Import&action=submit' . $post_vars . ")";
  219. return false;
  220. }
  221. }
  222. function uploadFile( $file, $fname, $reUpload ) {
  223. if ( !$this->bot->wikiConnect() )
  224. die ( "Unable to connect." );
  225. if ( !$this->bot->fetch( $this->bot->wikiServer . PREFIX . '/index.php?title=Special:Upload' ) )
  226. return false;
  227. // re uploade file
  228. $formvars['wpForReUpload'] = $reUpload;
  229. // ignore warning
  230. $formvars['wpIgnoreWarning'] = '1';
  231. $formvars['wpDestFile'] = $fname;
  232. $formvars['wpUpload'] = "Upload file";
  233. $formfiles['wpUploadFile'] = $file;
  234. $this->bot->maxredirs = 0;
  235. $this->bot->set_submit_multipart();
  236. if ( $this->bot->submit( $this->bot->wikiServer . PREFIX . '/index.php?title=Special:Upload', $formvars, $formfiles ) ) {
  237. // Now we need to check whether our edit was accepted. If it was, we'll get a 302 redirecting us to the article. If it wasn't (e.g. because of an edit conflict), we'll get a 200.
  238. $code = substr( $this->bot->response_code, 9, 3 ); // shorten 'HTTP 1.1 200 OK' to just '200'
  239. if ( '200' == $code ) {
  240. echo "import failed with error 200:(" . $this->bot->results . ")";
  241. return true;
  242. }
  243. elseif ( '302' == $code )
  244. return true;
  245. else {
  246. return false;
  247. }
  248. } else {
  249. echo "import submit failed:(" . $this->bot->wikiServer . PREFIX . '/index.php?title=Special:Upload&action=submit' . $formvars . ")";
  250. return false;
  251. }
  252. }
  253. function getFileFeatures( $file, $size ) {
  254. $url = $this->bot->wikiServer . PREFIX . '/api.php?action=query&titles=File:' . $file . '&prop=imageinfo&iilimit=50&iiend=20071231235959&iiprop=timestamp|user|url|size&format=php';
  255. $image = file_get_contents( $url );
  256. $image = unserialize( $image );
  257. $array = array_shift( $image );
  258. $array = array_shift( $array );
  259. $array = array_shift( $array );
  260. if ( $array['imageinfo'][0]['size'] == '' )
  261. return false;
  262. elseif ( $array['imageinfo'][0]['size'] == $size )
  263. return true;
  264. else
  265. return false;
  266. }
  267. }
  268. function callbackTestFct( $content1, $content2 ) {
  269. return $content2;
  270. }
  271. function append( $content, $line ) {
  272. $content .= $line;
  273. return $content;
  274. }
  275. ?>