Skip to content

Commit

Permalink
PHP file to edit the wikipage
Browse files Browse the repository at this point in the history
  • Loading branch information
Jayprakash-SE authored and srish committed Jul 11, 2019
1 parent f1f2046 commit 55de83d
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .gitignore
@@ -1,2 +1,3 @@
node_modules
package-lock.json
package-lock.json
cookie.txt
120 changes: 120 additions & 0 deletions php/edit.php
@@ -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 );
}

0 comments on commit 55de83d

Please sign in to comment.