Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f1f2046
commit 55de83d
Showing
2 changed files
with
122 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
node_modules | ||
package-lock.json | ||
package-lock.json | ||
cookie.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
<?php | ||
|
||
/* | ||
edit.py | ||
MediaWiki API Demos | ||
Demo of `Edit` module: POST request to edit a page | ||
MIT license | ||
*/ | ||
|
||
$endPoint = "https://test.wikipedia.org/w/api.php"; | ||
|
||
$login_Token = getLoginToken(); | ||
loginRequest( $login_Token ); | ||
$csrf_Token = getCSRFToken(); | ||
editRequest($csrf_Token); | ||
|
||
// Get the Login token | ||
function getLoginToken() { | ||
global $endPoint; | ||
|
||
$params1 = [ | ||
"action" => "query", | ||
"meta" => "tokens", | ||
"type" => "login", | ||
"format" => "json" | ||
]; | ||
|
||
$url = $endPoint . "?" . http_build_query( $params1 ); | ||
|
||
$ch = curl_init( $url ); | ||
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); | ||
curl_setopt( $ch, CURLOPT_COOKIEJAR, "cookie.txt" ); | ||
curl_setopt( $ch, CURLOPT_COOKIEFILE, "cookie.txt" ); | ||
|
||
$output = curl_exec( $ch ); | ||
curl_close( $ch ); | ||
|
||
$result = json_decode( $output, true ); | ||
return $result["query"]["tokens"]["logintoken"]; | ||
} | ||
|
||
// POST Request to log in. Use of main account for login is not | ||
// supported. Obtain credentials via Special:BotPasswords | ||
// (https://www.mediawiki.org/wiki/Special:BotPasswords) for lgname & lgpassword | ||
function loginRequest( $logintoken ) { | ||
global $endPoint; | ||
|
||
$params2 = [ | ||
"action" => "login", | ||
"lgname" => "bot_user_name", | ||
"lgpassword" => "bot_password", | ||
"lgtoken" => $logintoken, | ||
"format" => "json" | ||
]; | ||
|
||
$ch = curl_init(); | ||
|
||
curl_setopt( $ch, CURLOPT_URL, $endPoint ); | ||
curl_setopt( $ch, CURLOPT_POST, true ); | ||
curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query( $params2 ) ); | ||
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); | ||
curl_setopt( $ch, CURLOPT_COOKIEJAR, "cookie.txt" ); | ||
curl_setopt( $ch, CURLOPT_COOKIEFILE, "cookie.txt" ); | ||
|
||
$output = curl_exec( $ch ); | ||
curl_close( $ch ); | ||
|
||
} | ||
|
||
// Get csrf token for edit | ||
function getCSRFToken() { | ||
global $endPoint; | ||
|
||
$params3 = [ | ||
"action" => "query", | ||
"meta" => "tokens", | ||
"format" => "json" | ||
]; | ||
|
||
$url = $endPoint . "?" . http_build_query( $params3 ); | ||
|
||
$ch = curl_init( $url ); | ||
|
||
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); | ||
curl_setopt( $ch, CURLOPT_COOKIEJAR, "cookie.txt" ); | ||
curl_setopt( $ch, CURLOPT_COOKIEFILE, "cookie.txt" ); | ||
|
||
$output = curl_exec( $ch ); | ||
curl_close( $ch ); | ||
|
||
$result = json_decode( $output, true ); | ||
return $result["query"]["tokens"]["csrftoken"]; | ||
} | ||
|
||
// Post request for edit | ||
function editRequest( $csrftoken ) { | ||
global $endPoint; | ||
|
||
$params4 = [ | ||
"action" => "edit", | ||
"title" => "Sandbox", | ||
"appendtext" => "Hello", | ||
"token" => $csrftoken, | ||
"format" => "json" | ||
]; | ||
|
||
$ch = curl_init(); | ||
|
||
curl_setopt( $ch, CURLOPT_URL, $endPoint ); | ||
curl_setopt( $ch, CURLOPT_POST, true ); | ||
curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query( $params4 ) ); | ||
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); | ||
curl_setopt( $ch, CURLOPT_COOKIEJAR, "cookie.txt" ); | ||
curl_setopt( $ch, CURLOPT_COOKIEFILE, "cookie.txt" ); | ||
|
||
$output = curl_exec( $ch ); | ||
curl_close( $ch ); | ||
|
||
echo ( $output ); | ||
} |