Advertisement
Guest User

Untitled

a guest
May 21st, 2012
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 30.15 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * botclasses.php - Bot classes for interacting with mediawiki.
  5. *
  6. * (c) 2008-2010 Chris G - http://en.wikipedia.org/wiki/User:Chris_G
  7. * (c) 2009-2010 Fale - http://en.wikipedia.org/wiki/User:Fale
  8. * (c) 2010 Kaldari - http://en.wikipedia.org/wiki/User:Kaldari
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  23. *
  24. * Developers (add your self here if you worked on the code):
  25. * Cobi - [[User:Cobi]] - Wrote the http class and some of the wikipedia class
  26. * Chris - [[User:Chris_G]] - Wrote the most of the wikipedia class
  27. * Fale - [[User:Fale]] - Polish, wrote the extended and some of the wikipedia class
  28. * Kaldari - [[User:Kaldari]] - Submitted a patch for the imagematches function
  29. **/
  30.  
  31. /**
  32. * This class is designed to provide a simplified interface to cURL which maintains cookies.
  33. * @author Cobi
  34. **/
  35. class http {
  36. private $ch;
  37. private $uid;
  38. public $cookie_jar;
  39. public $postfollowredirs;
  40. public $getfollowredirs;
  41.  
  42. function data_encode ($data, $keyprefix = "", $keypostfix = "") {
  43. assert( is_array($data) );
  44. $vars=null;
  45. foreach($data as $key=>$value) {
  46. if(is_array($value))
  47. $vars .= $this->data_encode($value, $keyprefix.$key.$keypostfix.urlencode("["), urlencode("]"));
  48. else
  49. $vars .= $keyprefix.$key.$keypostfix."=".urlencode($value)."&";
  50. }
  51. return $vars;
  52. }
  53.  
  54. function __construct () {
  55. $this->ch = curl_init();
  56. $this->uid = dechex(rand(0,99999999));
  57. curl_setopt($this->ch,CURLOPT_COOKIEJAR,'/tmp/cluewikibot.cookies.'.$this->uid.'.dat');
  58. curl_setopt($this->ch,CURLOPT_COOKIEFILE,'/tmp/cluewikibot.cookies.'.$this->uid.'.dat');
  59. curl_setopt($this->ch,CURLOPT_MAXCONNECTS,100);
  60. curl_setopt($this->ch,CURLOPT_CLOSEPOLICY,CURLCLOSEPOLICY_LEAST_RECENTLY_USED);
  61. $this->postfollowredirs = 0;
  62. $this->getfollowredirs = 1;
  63. $this->cookie_jar = array();
  64. }
  65.  
  66. function post ($url,$data) {
  67. //echo 'POST: '.$url."\n";
  68. $time = microtime(1);
  69. curl_setopt($this->ch,CURLOPT_URL,$url);
  70. curl_setopt($this->ch,CURLOPT_USERAGENT,'php wikibot classes');
  71. /* Crappy hack to add extra cookies, should be cleaned up */
  72. $cookies = null;
  73. foreach ($this->cookie_jar as $name => $value) {
  74. if (empty($cookies))
  75. $cookies = "$name=$value";
  76. else
  77. $cookies .= "; $name=$value";
  78. }
  79. if ($cookies != null)
  80. curl_setopt($this->ch,CURLOPT_COOKIE,$cookies);
  81. curl_setopt($this->ch,CURLOPT_FOLLOWLOCATION,$this->postfollowredirs);
  82. curl_setopt($this->ch,CURLOPT_MAXREDIRS,10);
  83. curl_setopt($this->ch, CURLOPT_HTTPHEADER, array('Expect:'));
  84. curl_setopt($this->ch,CURLOPT_RETURNTRANSFER,1);
  85. curl_setopt($this->ch,CURLOPT_TIMEOUT,30);
  86. curl_setopt($this->ch,CURLOPT_CONNECTTIMEOUT,10);
  87. curl_setopt($this->ch,CURLOPT_POST,1);
  88. //curl_setopt($this->ch,CURLOPT_FAILONERROR,1);
  89. //curl_setopt($this->ch,CURLOPT_POSTFIELDS, substr($this->data_encode($data), 0, -1) );
  90. curl_setopt($this->ch,CURLOPT_POSTFIELDS, $data);
  91. $data = curl_exec($this->ch);
  92. //echo "Error: ".curl_error($this->ch);
  93. //var_dump($data);
  94. // global $logfd;
  95. // if (!is_resource($logfd)) {
  96. // $logfd = fopen('php://stderr','w');
  97. echo 'POST: '.$url.' ('.(microtime(1) - $time).' s) ('.strlen($data)." b)\n";
  98. // }
  99. return $data;
  100. }
  101.  
  102. function get ($url) {
  103. //echo 'GET: '.$url."\n";
  104. $time = microtime(1);
  105. curl_setopt($this->ch,CURLOPT_URL,$url);
  106. curl_setopt($this->ch,CURLOPT_USERAGENT,'php wikibot classes');
  107. /* Crappy hack to add extra cookies, should be cleaned up */
  108. $cookies = null;
  109. foreach ($this->cookie_jar as $name => $value) {
  110. if (empty($cookies))
  111. $cookies = "$name=$value";
  112. else
  113. $cookies .= "; $name=$value";
  114. }
  115. if ($cookies != null)
  116. curl_setopt($this->ch,CURLOPT_COOKIE,$cookies);
  117. curl_setopt($this->ch,CURLOPT_FOLLOWLOCATION,$this->getfollowredirs);
  118. curl_setopt($this->ch,CURLOPT_MAXREDIRS,10);
  119. curl_setopt($this->ch,CURLOPT_HEADER,0);
  120. curl_setopt($this->ch,CURLOPT_RETURNTRANSFER,1);
  121. curl_setopt($this->ch,CURLOPT_TIMEOUT,30);
  122. curl_setopt($this->ch,CURLOPT_CONNECTTIMEOUT,10);
  123. curl_setopt($this->ch,CURLOPT_HTTPGET,1);
  124. //curl_setopt($this->ch,CURLOPT_FAILONERROR,1);
  125. $data = curl_exec($this->ch);
  126. //echo "Error: ".curl_error($this->ch);
  127. //var_dump($data);
  128. //global $logfd;
  129. //if (!is_resource($logfd)) {
  130. // $logfd = fopen('php://stderr','w');
  131. echo 'GET: '.$url.' ('.(microtime(1) - $time).' s) ('.strlen($data)." b)\n";
  132. //}
  133. return $data;
  134. }
  135.  
  136. function __destruct () {
  137. curl_close($this->ch);
  138. @unlink('/tmp/cluewikibot.cookies.'.$this->uid.'.dat');
  139. }
  140. }
  141.  
  142. /**
  143. * This class is interacts with wikipedia using api.php
  144. * @author Chris G and Cobi
  145. **/
  146. class wikipedia {
  147. private $http;
  148. private $token;
  149. private $ecTimestamp;
  150. public $url;
  151.  
  152. /**
  153. * This is our constructor.
  154. * @return void
  155. **/
  156. function __construct ($url='http://en.wikipedia.org/w/api.php') {
  157. $this->http = new http;
  158. $this->token = null;
  159. $this->url = $url;
  160. $this->ecTimestamp = null;
  161. }
  162.  
  163. /**
  164. * Sends a query to the api.
  165. * @param $query The query string.
  166. * @param $post POST data if its a post request (optional).
  167. * @return The api result.
  168. **/
  169. function query ($query,$post=null) {
  170. if ($post==null)
  171. $ret = $this->http->get($this->url.$query);
  172. else
  173. $ret = $this->http->post($this->url.$query,$post);
  174. return unserialize($ret);
  175. }
  176.  
  177. /**
  178. * Gets the content of a page. Returns false on error.
  179. * @param $page The wikipedia page to fetch.
  180. * @param $revid The revision id to fetch (optional)
  181. * @return The wikitext for the page.
  182. **/
  183. function getpage ($page,$revid=null,$detectEditConflict=false) {
  184. $append = '';
  185. if ($revid!=null)
  186. $append = '&rvstartid='.$revid;
  187. $x = $this->query('?action=query&format=php&prop=revisions&titles='.urlencode($page).'&rvlimit=1&rvprop=content|timestamp'.$append);
  188. foreach ($x['query']['pages'] as $ret) {
  189. if (isset($ret['revisions'][0]['*'])) {
  190. if ($detectEditConflict)
  191. $this->ecTimestamp = $ret['revisions'][0]['timestamp'];
  192. return $ret['revisions'][0]['*'];
  193. } else
  194. return false;
  195. }
  196. }
  197.  
  198. /**
  199. * Gets the page id for a page.
  200. * @param $page The wikipedia page to get the id for.
  201. * @return The page id of the page.
  202. **/
  203. function getpageid ($page) {
  204. $x = $this->query('?action=query&format=php&prop=revisions&titles='.urlencode($page).'&rvlimit=1&rvprop=content');
  205. foreach ($x['query']['pages'] as $ret) {
  206. return $ret['pageid'];
  207. }
  208. }
  209.  
  210. /**
  211. * Gets the number of contributions a user has.
  212. * @param $user The username for which to get the edit count.
  213. * @return The number of contributions the user has.
  214. **/
  215. function contribcount ($user) {
  216. $x = $this->query('?action=query&list=allusers&format=php&auprop=editcount&aulimit=1&aufrom='.urlencode($user));
  217. return $x['query']['allusers'][0]['editcount'];
  218. }
  219.  
  220. /**
  221. * Returns an array with all the members of $category
  222. * @param $category The category to use.
  223. * @param $subcat (bool) Go into sub categories?
  224. * @return array
  225. **/
  226. function categorymembers ($category,$subcat=false) {
  227. $continue = '';
  228. $pages = array();
  229. while (true) {
  230. $res = $this->query('?action=query&list=categorymembers&cmtitle='.urlencode($category).'&format=php&cmlimit=500'.$continue);
  231. if (isset($x['error'])) {
  232. return false;
  233. }
  234. foreach ($res['query']['categorymembers'] as $x) {
  235. $pages[] = $x['title'];
  236. }
  237. if (empty($res['query-continue']['categorymembers']['cmcontinue'])) {
  238. if ($subcat) {
  239. foreach ($pages as $p) {
  240. if (substr($p,0,9)=='Category:') {
  241. $pages2 = $this->categorymembers($p,true);
  242. $pages = array_merge($pages,$pages2);
  243. }
  244. }
  245. }
  246. return $pages;
  247. } else {
  248. $continue = '&cmcontinue='.urlencode($res['query-continue']['categorymembers']['cmcontinue']);
  249. }
  250. }
  251. }
  252.  
  253. /**
  254. * Returns a list of pages that link to $page.
  255. * @param $page
  256. * @param $extra (defaults to null)
  257. * @return array
  258. **/
  259. function whatlinkshere ($page,$extra=null) {
  260. $continue = '';
  261. $pages = array();
  262. while (true) {
  263. $res = $this->query('?action=query&list=backlinks&bltitle='.urlencode($page).'&bllimit=500&format=php'.$continue.$extra);
  264. if (isset($res['error'])) {
  265. return false;
  266. }
  267. foreach ($res['query']['backlinks'] as $x) {
  268. $pages[] = $x['title'];
  269. }
  270. if (empty($res['query-continue']['backlinks']['blcontinue'])) {
  271. return $pages;
  272. } else {
  273. $continue = '&blcontinue='.urlencode($res['query-continue']['backlinks']['blcontinue']);
  274. }
  275. }
  276. }
  277.  
  278. /**
  279. * Returns a list of pages that include the image.
  280. * @param $image
  281. * @param $extra (defaults to null)
  282. * @return array
  283. **/
  284. function whereisincluded ($image,$extre=null) {
  285. $continue = '';
  286. $pages = array();
  287. while (true) {
  288. $res = $this->query('?action=query&list=imageusage&iutitle='.urlencode($image).'&iulimit=500&format=php'.$continue.$extra);
  289. if (isset($res['error']))
  290. return false;
  291. foreach ($res['query']['imageusage'] as $x) {
  292. $pages[] = $x['title'];
  293. }
  294. if (empty($res['query-continue']['imageusage']['iucontinue']))
  295. return $pages;
  296. else
  297. $continue = '&iucontinue='.urlencode($res['query-continue']['imageusage']['iucontinue']);
  298. }
  299. }
  300.  
  301. /**
  302. * Returns an array with all the subpages of $page
  303. * @param $page
  304. * @return array
  305. **/
  306. function subpages ($page) {
  307. /* Calculate all the namespace codes */
  308. $ret = $this->query('?action=query&meta=siteinfo&siprop=namespaces&format=php');
  309. foreach ($ret['query']['namespaces'] as $x) {
  310. $namespaces[$x['*']] = $x['id'];
  311. }
  312. $temp = explode(':',$page,2);
  313. $namespace = $namespaces[$temp[0]];
  314. $title = $temp[1];
  315. $continue = '';
  316. $subpages = array();
  317. while (true) {
  318. $res = $this->query('?action=query&format=php&list=allpages&apprefix='.urlencode($title).'&aplimit=500&apnamespace='.$namespace.$continue);
  319. if (isset($x[error])) {
  320. return false;
  321. }
  322. foreach ($res['query']['allpages'] as $p) {
  323. $subpages[] = $p['title'];
  324. }
  325. if (empty($res['query-continue']['allpages']['apfrom'])) {
  326. return $subpages;
  327. } else {
  328. $continue = '&apfrom='.urlencode($res['query-continue']['allpages']['apfrom']);
  329. }
  330. }
  331. }
  332.  
  333. /**
  334. * This function takes a username and password and logs you into wikipedia.
  335. * @param $user Username to login as.
  336. * @param $pass Password that corrisponds to the username.
  337. * @return array
  338. **/
  339. function login ($user,$pass) {
  340. $post = array('lgname' => $user, 'lgpassword' => $pass);
  341. $ret = $this->query('?action=login&format=php',$post);
  342. /* This is now required - see https://bugzilla.wikimedia.org/show_bug.cgi?id=23076 */
  343. if ($ret['login']['result'] == 'NeedToken') {
  344. $post['lgtoken'] = $ret['login']['token'];
  345. $ret = $this->query( '?action=login&format=php', $post );
  346. }
  347. if ($ret['login']['result'] != 'Success') {
  348. echo "Login error: \n";
  349. print_r($ret);
  350. die();
  351. } else {
  352. return $ret;
  353. }
  354. }
  355.  
  356. /* crappy hack to allow users to use cookies from old sessions */
  357. function setLogin($data) {
  358. $this->http->cookie_jar = array(
  359. $data['cookieprefix'].'UserName' => $data['lgusername'],
  360. $data['cookieprefix'].'UserID' => $data['lguserid'],
  361. $data['cookieprefix'].'Token' => $data['lgtoken'],
  362. $data['cookieprefix'].'_session' => $data['sessionid'],
  363. );
  364. }
  365.  
  366. /**
  367. * Check if we're allowed to edit $page.
  368. * See http://en.wikipedia.org/wiki/Template:Bots
  369. * for more info.
  370. * @param $page The page we want to edit.
  371. * @param $user The bot's username.
  372. * @return bool
  373. **/
  374. function nobots ($page,$user=null,$text=null) {
  375. if ($text == null) {
  376. $text = $this->getpage($page);
  377. }
  378. if ($user != null) {
  379. if (preg_match('/\{\{(nobots|bots\|allow=none|bots\|deny=all|bots\|optout=all|bots\|deny=.*?'.preg_quote($user,'/').'.*?)\}\}/iS',$text)) {
  380. return false;
  381. }
  382. } else {
  383. if (preg_match('/\{\{(nobots|bots\|allow=none|bots\|deny=all|bots\|optout=all)\}\}/iS',$text)) {
  384. return false;
  385. }
  386. }
  387. return true;
  388. }
  389.  
  390. /**
  391. * This function returns the edit token for the current user.
  392. * @return edit token.
  393. **/
  394. function getedittoken () {
  395. $x = $this->query('?action=query&prop=info&intoken=edit&titles=Main%20Page&format=php');
  396. foreach ($x['query']['pages'] as $ret) {
  397. return $ret['edittoken'];
  398. }
  399. }
  400.  
  401. /**
  402. * Purges the cache of $page.
  403. * @param $page The page to purge.
  404. * @return Api result.
  405. **/
  406. function purgeCache($page) {
  407. return $this->query('?action=purge&titles='.urlencode($page).'&format=php');
  408. }
  409.  
  410. /**
  411. * Checks if $user has email enabled.
  412. * Uses index.php.
  413. * @param $user The user to check.
  414. * @return bool.
  415. **/
  416. function checkEmail($user) {
  417. $x = $this->query('?action=query&meta=allmessages&ammessages=noemailtext|notargettext&amlang=en&format=php');
  418. $messages[0] = $x['query']['allmessages'][0]['*'];
  419. $messages[1] = $x['query']['allmessages'][1]['*'];
  420. $page = $this->http->get(str_replace('api.php','index.php',$this->url).'?title=Special:EmailUser&target='.urlencode($user));
  421. if (preg_match('/('.preg_quote($messages[0],'/').'|'.preg_quote($messages[1],'/').')/i',$page)) {
  422. return false;
  423. } else {
  424. return true;
  425. }
  426. }
  427.  
  428. /**
  429. * Returns all the pages $page is transcluded on.
  430. * @param $page The page to get the transclusions from.
  431. * @param $sleep The time to sleep between requets (set to null to disable).
  432. * @return array.
  433. **/
  434. function getTransclusions($page,$sleep=null,$extra=null) {
  435. $continue = '';
  436. $pages = array();
  437. while (true) {
  438. $ret = $this->query('?action=query&list=embeddedin&eititle='.urlencode($page).$continue.$extra.'&eilimit=500&format=php');
  439. if ($sleep != null) {
  440. sleep($sleep);
  441. }
  442. foreach ($ret['query']['embeddedin'] as $x) {
  443. $pages[] = $x['title'];
  444. }
  445. if (isset($ret['query-continue']['embeddedin']['eicontinue'])) {
  446. $continue = '&eicontinue='.$ret['query-continue']['embeddedin']['eicontinue'];
  447. } else {
  448. return $pages;
  449. }
  450. }
  451. }
  452.  
  453. /**
  454. * Edits a page.
  455. * @param $page Page name to edit.
  456. * @param $data Data to post to page.
  457. * @param $summary Edit summary to use.
  458. * @param $minor Whether or not to mark edit as minor. (Default false)
  459. * @param $bot Whether or not to mark edit as a bot edit. (Default true)
  460. * @return api result
  461. **/
  462. function edit ($page,$data,$summary = '',$minor = false,$bot = true,$section = null,$detectEC=false,$maxlag='') {
  463. if ($this->token==null) {
  464. $this->token = $this->getedittoken();
  465. }
  466. $params = array(
  467. 'title' => $page,
  468. 'text' => $data,
  469. 'token' => $this->token,
  470. 'summary' => $summary,
  471. ($minor?'minor':'notminor') => '1',
  472. ($bot?'bot':'notbot') => '1'
  473. );
  474. if ($section != null) {
  475. $params['section'] = $section;
  476. }
  477. if ($this->ecTimestamp != null && $detectEC == true) {
  478. $params['basetimestamp'] = $this->ecTimestamp;
  479. $this->ecTimestamp = null;
  480. }
  481. if ($maxlag!='') {
  482. $maxlag='&maxlag='.$maxlag;
  483. }
  484. return $this->query('?action=edit&format=php'.$maxlag,$params);
  485. }
  486.  
  487. /**
  488. * Add a text at the bottom of a page
  489. * @param $page The page we're working with.
  490. * @param $text The text that you want to add.
  491. * @param $summary Edit summary to use.
  492. * @param $minor Whether or not to mark edit as minor. (Default false)
  493. * @param $bot Whether or not to mark edit as a bot edit. (Default true)
  494. * @return api result
  495. **/
  496. function addtext( $page, $text, $summary = '', $minor = false, $bot = true )
  497. {
  498. $data = $this->getpage( $page );
  499. $data.= "\n" . $text;
  500. return $this->edit( $page, $data, $summary, $minor, $bot );
  501. }
  502.  
  503. /**
  504. * Moves a page.
  505. * @param $old Name of page to move.
  506. * @param $new New page title.
  507. * @param $reason Move summary to use.
  508. * @param $movetalk Move the page's talkpage as well.
  509. * @return api result
  510. **/
  511. function move ($old,$new,$reason,$options=null) {
  512. if ($this->token==null) {
  513. $this->token = $this->getedittoken();
  514. }
  515. $params = array(
  516. 'from' => $old,
  517. 'to' => $new,
  518. 'token' => $this->token,
  519. 'reason' => $reason
  520. );
  521. if ($options != null) {
  522. $option = explode('|',$options);
  523. foreach ($option as $o) {
  524. $params[$o] = true;
  525. }
  526. }
  527. return $this->query('?action=move&format=php',$params);
  528. }
  529.  
  530. /**
  531. * Rollback an edit.
  532. * @param $title Title of page to rollback.
  533. * @param $user Username of last edit to the page to rollback.
  534. * @param $reason Edit summary to use for rollback.
  535. * @param $bot mark the rollback as bot.
  536. * @return api result
  537. **/
  538. function rollback ($title,$user,$reason=null,$bot=false) {
  539. $ret = $this->query('?action=query&prop=revisions&rvtoken=rollback&titles='.urlencode($title).'&format=php');
  540. foreach ($ret['query']['pages'] as $x) {
  541. $token = $x['revisions'][0]['rollbacktoken'];
  542. break;
  543. }
  544. $params = array(
  545. 'title' => $title,
  546. 'user' => $user,
  547. 'token' => $token
  548. );
  549. if ($bot) {
  550. $params['markbot'] = true;
  551. }
  552. if ($reason != null) { $params['summary'] = $reason; }
  553. return $this->query('?action=rollback&format=php',$params);
  554. }
  555.  
  556. /**
  557. * Blocks a user.
  558. * @param $user The user to block.
  559. * @param $reason The block reason.
  560. * @param $expiry The block expiry.
  561. * @param $options a piped string containing the block options.
  562. * @return api result
  563. **/
  564. function block ($user,$reason='vand',$expiry='infinite',$options=null,$retry=true) {
  565. if ($this->token==null) {
  566. $this->token = $this->getedittoken();
  567. }
  568. $params = array(
  569. 'expiry' => $expiry,
  570. 'user' => $user,
  571. 'reason' => $reason,
  572. 'token' => $this->token
  573. );
  574. if ($options != null) {
  575. $option = explode('|',$options);
  576. foreach ($option as $o) {
  577. $params[$o] = true;
  578. }
  579. }
  580. $ret = $this->query('?action=block&format=php',$params);
  581. /* Retry on a failed token. */
  582. if ($retry and $ret['error']['code']=='badtoken') {
  583. $this->token = $this->getedittoken();
  584. return $this->block($user,$reason,$expiry,$options,false);
  585. }
  586. return $ret;
  587. }
  588.  
  589. /**
  590. * Unblocks a user.
  591. * @param $user The user to unblock.
  592. * @param $reason The unblock reason.
  593. * @return api result
  594. **/
  595. function unblock ($user,$reason) {
  596. if ($this->token==null) {
  597. $this->token = $this->getedittoken();
  598. }
  599. $params = array(
  600. 'user' => $user,
  601. 'reason' => $reason,
  602. 'token' => $this->token
  603. );
  604. return $this->query('?action=unblock&format=php',$params);
  605. }
  606.  
  607. /**
  608. * Emails a user.
  609. * @param $target The user to email.
  610. * @param $subject The email subject.
  611. * @param $text The body of the email.
  612. * @param $ccme Send a copy of the email to the user logged in.
  613. * @return api result
  614. **/
  615. function email ($target,$subject,$text,$ccme=false) {
  616. if ($this->token==null) {
  617. $this->token = $this->getedittoken();
  618. }
  619. $params = array(
  620. 'target' => $target,
  621. 'subject' => $subject,
  622. 'text' => $text,
  623. 'token' => $this->token
  624. );
  625. if ($ccme) {
  626. $params['ccme'] = true;
  627. }
  628. return $this->query('?action=emailuser&format=php',$params);
  629. }
  630.  
  631. /**
  632. * Deletes a page.
  633. * @param $title The page to delete.
  634. * @param $reason The delete reason.
  635. * @return api result
  636. **/
  637. function delete ($title,$reason) {
  638. if ($this->token==null) {
  639. $this->token = $this->getedittoken();
  640. }
  641. $params = array(
  642. 'title' => $title,
  643. 'reason' => $reason,
  644. 'token' => $this->token
  645. );
  646. return $this->query('?action=delete&format=php',$params);
  647. }
  648.  
  649. /**
  650. * Undeletes a page.
  651. * @param $title The page to undelete.
  652. * @param $reason The undelete reason.
  653. * @return api result
  654. **/
  655. function undelete ($title,$reason) {
  656. if ($this->token==null) {
  657. $this->token = $this->getedittoken();
  658. }
  659. $params = array(
  660. 'title' => $title,
  661. 'reason' => $reason,
  662. 'token' => $this->token
  663. );
  664. return $this->query('?action=undelete&format=php',$params);
  665. }
  666.  
  667. /**
  668. * (Un)Protects a page.
  669. * @param $title The page to (un)protect.
  670. * @param $protections The protection levels (e.g. 'edit=autoconfirmed|move=sysop')
  671. * @param $expiry When the protection should expire (e.g. '1 day|infinite')
  672. * @param $reason The (un)protect reason.
  673. * @param $cascade Enable cascading protection? (defaults to false)
  674. * @return api result
  675. **/
  676. function protect ($title,$protections,$expiry,$reason,$cascade=false) {
  677. if ($this->token==null) {
  678. $this->token = $this->getedittoken();
  679. }
  680. $params = array(
  681. 'title' => $title,
  682. 'protections' => $protections,
  683. 'expiry' => $expiry,
  684. 'reason' => $reason,
  685. 'token' => $this->token
  686. );
  687. if ($cascade) {
  688. $params['cascade'] = true;
  689. }
  690. return $this->query('?action=protect&format=php',$params);
  691. }
  692.  
  693. /**
  694. * Uploads an image.
  695. * Uses index.php as there is no api image uploading yet :(
  696. * @param $page The destination file name.
  697. * @param $file The local file path.
  698. * @param $desc The upload description (defaults to '').
  699. **/
  700. function upload ($page,$file,$desc='') {
  701. $post = array(
  702. 'wpUploadFile' => '@'.$file,
  703. 'wpSourceType' => 'file',
  704. 'wpDestFile' => $page,
  705. 'wpUploadDescription' => $desc,
  706. 'wpLicense' => '',
  707. 'wpWatchthis' => '0',
  708. 'wpIgnoreWarning' => '1',
  709. 'wpUpload' => 'Upload file'
  710. );
  711. return $this->http->post(str_replace('api.php','index.php',$this->url).'?title=Special:Upload&action=submit',$post);
  712. }
  713.  
  714. /*
  715. $page - page
  716. $revs - rev ids to delete (seperated with ,)
  717. $comment - delete comment
  718. */
  719. function revdel ($page,$revs,$comment) {
  720.  
  721. if ($this->token==null) {
  722. $this->token = $this->getedittoken();
  723. }
  724.  
  725. $post = array(
  726. 'wpEditToken' => $this->token,
  727. 'ids' => $revs,
  728. 'target' => $page,
  729. 'type' => 'revision',
  730. 'wpHidePrimary' => 1,
  731. 'wpHideComment' => 1,
  732. 'wpHideUser' => 0,
  733. 'wpRevDeleteReasonList' => 'other',
  734. 'wpReason' => $comment,
  735. 'wpSubmit' => 'Apply to selected revision(s)'
  736. );
  737. return $this->http->post(str_replace('api.php','index.php',$this->url).'?title=Special:RevisionDelete&action=submit',$post);
  738. }
  739.  
  740. /**
  741. * Creates a new account.
  742. * Uses index.php as there is no api to create accounts yet :(
  743. * @param $username The username the new account will have.
  744. * @param $password The password the new account will have.
  745. * @param $email The email the new account will have.
  746. **/
  747. function createaccount ($username,$password,$email=null) {
  748. $post = array(
  749. 'wpName' => $username,
  750. 'wpPassword' => $password,
  751. 'wpRetype' => $password,
  752. 'wpEmail' => $email,
  753. 'wpRemember' => 0,
  754. 'wpIgnoreAntiSpoof' => 0,
  755. 'wpCreateaccount' => 'Create account',
  756. );
  757. return $this->http->post(str_replace('api.php','index.php',$this->url).'?title=Special:UserLogin&action=submitlogin&type=signup',$post);
  758. }
  759.  
  760. /**
  761. * Changes a users rights.
  762. * @param $user The user we're working with.
  763. * @param $add A pipe-separated list of groups you want to add.
  764. * @param $remove A pipe-separated list of groups you want to remove.
  765. * @param $reason The reason for the change (defaults to '').
  766. **/
  767. function userrights ($user,$add,$remove,$reason='') {
  768. // get the userrights token
  769. $token = $this->query('?action=query&list=users&ususers='.urlencode($user).'&ustoken=userrights&format=php');
  770. $token = $token['query']['users'][0]['userrightstoken'];
  771. $params = array(
  772. 'user' => $user,
  773. 'token' => $token,
  774. 'add' => $add,
  775. 'remove' => $remove,
  776. 'reason' => $reason
  777. );
  778. return $this->query('?action=userrights&format=php',$params);
  779. }
  780.  
  781. /**
  782. * Gets the number of images matching a particular sha1 hash.
  783. * @param $hash The sha1 hash for an image.
  784. * @return The number of images with the same sha1 hash.
  785. **/
  786. function imagematches ($hash) {
  787. $x = $this->query('?action=query&list=allimages&format=php&aisha1='.$hash);
  788. return count($x['query']['allimages']);
  789. }
  790.  
  791. }
  792.  
  793. /**
  794. * This class extends the wiki class to provide an high level API for the most commons actions.
  795. * @author Fale
  796. **/
  797. class extended extends wikipedia
  798. {
  799. /**
  800. * Add a category to a page
  801. * @param $page The page we're working with.
  802. * @param $category The category that you want to add.
  803. * @param $summary Edit summary to use.
  804. * @param $minor Whether or not to mark edit as minor. (Default false)
  805. * @param $bot Whether or not to mark edit as a bot edit. (Default true)
  806. * @return api result
  807. **/
  808. function addcategory( $page, $category, $summary = '', $minor = false, $bot = true )
  809. {
  810. $data = $this->getpage( $page );
  811. $data.= "\n[[Category:" . $category . "]]";
  812. return $this->edit( $page, $data, $summary, $minor, $bot );
  813. }
  814.  
  815. /**
  816. * Find a string
  817. * @param $page The page we're working with.
  818. * @param $string The string that you want to find.
  819. * @return bool value (1 found and 0 not-found)
  820. **/
  821. function findstring( $page, $string )
  822. {
  823. $data = $this->getpage( $page );
  824. if( strstr( $data, $string ) )
  825. return 1;
  826. else
  827. return 0;
  828. }
  829.  
  830. /**
  831. * Replace a string
  832. * @param $page The page we're working with.
  833. * @param $string The string that you want to replace.
  834. * @param $newstring The string that will replace the present string.
  835. * @return the new text of page
  836. **/
  837. function replacestring( $page, $string, $newstring )
  838. {
  839. $data = $this->getpage( $page );
  840. return str_replace( $string, $newstring, $data );
  841. }
  842. }
  843. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement