Extension:AddPageService/AddPageService.php
From MediaWiki.org
<?php // Web service to add pages to the wiki. // Copyright (c) 2007, Benner Sistemas SA. // Homepage: http://www.mediawiki.org/wiki/Extension:AddPageService // Author: Fernando Correia // Version: 1.0.1 // Customization required: $wiki_root_url="http://server/wiki"; # Base URL of your wiki include "/php/Snoopy-1.2.3/Snoopy.class.php"; # Location of the Snoopy class $debug_file_name = "/temp/addpage_debug.txt"; $strict_title_rules = true; // End of customization section require_once ('SOAP/Server.php'); require_once ('SOAP/Disco.php'); class AddPageService { var $__dispatch_map = array(); function AddPageService() { // Define the signature of the dispatch map on the Web services method // Necessary for WSDL creation $this->__dispatch_map['addPage']=array ( 'in' => array ( 'title' => 'string', 'text' => 'string', 'user' => 'string', 'password' => 'string' ), 'out' => array('result' => 'string'), ); } function addPage($title, $text, $user, $password) { // Validate parameters if (empty($title)) return "Title is required."; if (empty($text)) return "Text is required."; if (empty($user)) return "User is required."; if (empty($password)) return "Password is required."; // Fill web form $result=AddPageService::fillForm($title, $text, $user, $password); return $result; } private static function fillForm($title, $text, $user, $password) { global $wiki_root_url; global $debug_file_name; $login_url =$wiki_root_url . "/index.php?title=Special:Userlogin&action=submitlogin"; # Set the username and password below: $login_vars['wpName']=$user; $login_vars['wpPassword']=$password; $login_vars['wpRemember']="0"; # Login to Wiki $snoopy =new Snoopy; if (!$snoopy->submit($login_url, $login_vars)) return "Error submiting login form."; if (strpos($snoopy->results, 'errorbox') !== false) { if (!empty($debug_file_name)) { $fp = fopen($debug_file_name, "w"); fwrite($fp, $editpage); fclose($fp); } return "Login error."; } # Make safe title for URL $restricted_characters = array(); $restricted_characters[] ='#'; $restricted_characters[] ='<'; $restricted_characters[] ='>'; $restricted_characters[] ='['; $restricted_characters[] =']'; $restricted_characters[] ='|'; $restricted_characters[] ='{'; $restricted_characters[] ='}'; global $strict_title_rules; if ($strict_title_rules) { $restricted_characters[] ='+'; $restricted_characters[] ='/'; $restricted_characters[] =':'; $restricted_characters[] ='?'; $restricted_characters[] ='%'; $restricted_characters[] ='`'; $restricted_characters[] =utf8_encode('–'); // n-dash, not hyphen $restricted_characters[] =utf8_encode('—'); // m-dash, not hyphen } $title=str_replace($restricted_characters, ' ', $title); $safetitle=str_replace(" ", "_", trim($title)); # Submit to edit page for $title and get contents into $editpage $url =$wiki_root_url . "/index.php?title=" . $safetitle . "&action=edit"; if (!$snoopy->fetch($url)) return "Error fetching edit form."; $editpage=$snoopy->results; if (!empty($debug_file_name)) { $fp = fopen($debug_file_name, "w"); fwrite($fp, $editpage); fclose($fp); } # Pick out Edit Token $ans =preg_match('/.*value="(.*?)".*name="wpEditToken"/', $editpage, $matches); if (empty($matches[1])) return "No edit token on the web form answer."; $edit_token=$matches[1]; # Pick out Edit Time $ans =preg_match('/.*value="(.*?)".*name="wpEdittime"/', $editpage, $matches); if (empty($matches[1])) return "No edit time on the web form answer."; $edit_time =$matches[1]; # Set Post Variables before submitting $text =trim($text); $submit_vars['wpTextbox1'] =$text; $submit_vars['wpSummary'] =""; $submit_vars['wpSection'] =""; $submit_vars['wpStarttime']=gmdate('YmdHis', time()); $submit_vars['wpEdittime'] =$edit_time; $submit_vars['wpSave'] ="Save page"; $submit_vars['wpEditToken']=$edit_token; # Submit or Post to create the page if (!$snoopy->submit($wiki_root_url . "/index.php?title=" . $safetitle . "&action=submit", $submit_vars)) return "Error submiting filled form."; if (strpos($snoopy->results, 'wpTextbox1') !== false) return "Error saving the web form."; # report success return "OK"; } static function main() { global $_SERVER, $HTTP_RAW_POST_DATA; $server =new SOAP_Server(); $webservice=new AddPageService(); $server->addObjectMap($webservice, 'http://schemas.xmlsoap.org/soap/envelope/'); if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'POST') { $server->service($HTTP_RAW_POST_DATA); } else { $disco=new SOAP_DISCO_Server($server, 'AddPageService'); header ("Content-type: text/xml"); if (isset($_SERVER['QUERY_STRING']) && strcasecmp($_SERVER['QUERY_STRING'], 'wsdl') == 0) { echo $disco->getWSDL(); } else { echo $disco->getDISCO(); } } } } AddPageService::main();