jpatokal / mediawiki-gateway

Ruby framework for MediaWiki API manipulation
Other
133 stars 50 forks source link

Bot flag fix and minor flag edit support #46

Closed GreyCat closed 11 years ago

GreyCat commented 11 years ago

Hi! I'd like to contribute fixes for bot flag (in its current state it just breaks all possible API queries, starting with login) and implementation of minor edit flag support.

jpatokal commented 11 years ago

Just following up, have you had a chance to look at this?

ochaochaocha3 commented 11 years ago

Excuse me for jumping in, I confirmed the behavior of the "assert=bot" parameter on editing pages as a bot user with and without AssertEdit. As a result, it seemed to raise no problem on wikis that do not have AssertEdit installed.

Log

AssertEdit seemed to determine wheather the user is a bot by confirming that he has the "bot" user right.

# In $IP/
$ diff LocalSettings-original.php LocalSettings-AssertEdit.php
142c142
< # require_once("$IP/extensions/AssertEdit/AssertEdit.php");
---
> require_once("$IP/extensions/AssertEdit/AssertEdit.php");

# Without AssertEdit
$ cp LocalSettings-original.php LocalSettings.php
$ ruby assertbot-edit.rb
Logged in
Login token: a859eb5b465e81b14ab9e28af48ad969
Cookie: {"my_wiki_session"=>"db289415508b699d46a4ed7c3623b752"}
Got edit token: 42b3e5b9ff12ff4be64e268f3cdacf0a+\
Edit result: {"edit"=>{"result"=>"Success", "pageid"=>26, "title"=>"AssertBotTest", "oldrevid"=>65, "newrevid"=>66, "newtimestamp"=>"2013-09-28T11:53:33Z"}}
Edit result (assert): {"edit"=>{"result"=>"Success", "pageid"=>26, "title"=>"AssertBotTest", "oldrevid"=>66, "newrevid"=>67, "newtimestamp"=>"2013-09-28T11:53:33Z"}}
Edit result (nassert): {"edit"=>{"result"=>"Success", "pageid"=>26, "title"=>"AssertBotTest", "oldrevid"=>67, "newrevid"=>68, "newtimestamp"=>"2013-09-28T11:53:34Z"}}
Edit result (bot): {"edit"=>{"result"=>"Success", "pageid"=>26, "title"=>"AssertBotTest", "oldrevid"=>68, "newrevid"=>69, "newtimestamp"=>"2013-09-28T11:53:34Z"}}
Edit result (bot,assert): {"edit"=>{"result"=>"Success", "pageid"=>26, "title"=>"AssertBotTest", "oldrevid"=>69, "newrevid"=>70, "newtimestamp"=>"2013-09-28T11:53:34Z"}}
Edit result (bot,nassert): {"edit"=>{"result"=>"Success", "pageid"=>26, "title"=>"AssertBotTest", "oldrevid"=>70, "newrevid"=>71, "newtimestamp"=>"2013-09-28T11:53:34Z"}}

# With AssertEdit
$ cp LocalSettings-AssertEdit.php LocalSettings.php
$ ruby assertbot-edit.rb
Logged in
Login token: 83df31112365682217fc7d7343977b7c
Cookie: {"my_wiki_session"=>"5be4bb50ce54d584612459c4a54189fb"}
Got edit token: 848c8b7d6e4f55e6b535e1dddf83fec1+\
Edit result: {"edit"=>{"result"=>"Success", "pageid"=>26, "title"=>"AssertBotTest", "oldrevid"=>71, "newrevid"=>72, "newtimestamp"=>"2013-09-28T12:03:08Z"}}
Edit result (assert): {"edit"=>{"result"=>"Success", "pageid"=>26, "title"=>"AssertBotTest", "oldrevid"=>72, "newrevid"=>73, "newtimestamp"=>"2013-09-28T12:03:09Z"}}
Edit result (nassert): {"edit"=>{"nassert"=>"bot", "result"=>"Failure"}}
Edit result (bot): {"edit"=>{"result"=>"Success", "pageid"=>26, "title"=>"AssertBotTest", "oldrevid"=>73, "newrevid"=>74, "newtimestamp"=>"2013-09-28T12:03:09Z"}}
Edit result (bot,assert): {"edit"=>{"result"=>"Success", "pageid"=>26, "title"=>"AssertBotTest", "oldrevid"=>74, "newrevid"=>75, "newtimestamp"=>"2013-09-28T12:03:09Z"}}
Edit result (bot,nassert): {"edit"=>{"nassert"=>"bot", "result"=>"Failure"}}

Test Code

assertbot-edit.rb

# Confirm the behavior of the "assert=bot" parameter on editing page

require "rest_client"
require "uri"
require "yaml"

$MEDIAWIKI_API_URI = "http://localhost/mediawiki/api.php"
$USERNAME = "bot" # belongs to the "bot" user group
$PASSWORD = "pa$$word"

$cookies = nil
$edit_token = nil

def login
  form_data = {
    "format" => "yaml",
    "action" => "login",
    "lgname" => $USERNAME,
    "lgpassword" => $PASSWORD,
    "lgdomain" => "local"
  }

  response = RestClient.post($MEDIAWIKI_API_URI, form_data)
  $cookies = response.cookies
  login_data = YAML.parse(response.body).transform
  login_result = login_data["login"]["result"]

  case login_result
  when "Success"
    puts "Logged in"
    puts "Cookie: #{response.cookies}"
  when "NeedToken"
    form_data["lgtoken"] = login_data["login"]["token"]
    response = RestClient.post($MEDIAWIKI_API_URI, form_data, :cookies => $cookies)
    login_data = YAML.parse(response.body).transform
    login_result = login_data["login"]["result"]

    if login_result == "Success"
      puts "Logged in"
      puts "Login token: #{form_data["lgtoken"]}"
      puts "Cookie: #{$cookies}"
    else
      raise "Login failed: #{login_result}"
    end
  else
    raise "Login failed: #{login_result}"
  end
end

def edit_test(options = {})
  # Get edit token
  unless $edit_token
    form_data_get_token = {
      "format" => "yaml",
      "action" => "query",
      "prop" => "info",
      "intoken" => "edit",
      "titles" => "AssertBotTest"
    }
    response = RestClient.post($MEDIAWIKI_API_URI, form_data_get_token,
                               :cookies => $cookies)
    token_data = YAML.parse(response.body).transform
    $edit_token = token_data["query"]["pages"].first[1]["edittoken"]
    puts "Got edit token: #{$edit_token}"
  end

  # Edit page
  form_data_edit = {
    "format" => "yaml",
    "action" => "edit",
    "title" => "AssertBotTest",
    "section" => "new",
    "text" => "AssertBotTest #{Time.new}",
    "bot" => "1",
    "token" => $edit_token
  }
  form_data_edit["bot"] = 1 if options[:bot]
  form_data_edit["assert"] = "bot" if options[:assert]
  form_data_edit["nassert"] = "bot" if options[:nassert]
  response = RestClient.post($MEDIAWIKI_API_URI, form_data_edit,
                             :cookies => $cookies)
  edit_data = YAML.parse(response.body).transform

  print "Edit result"
  params = options.select {|k, v| v}.map {|k, v| k}
  print " (#{params.join(",")})" unless params.empty?
  puts ": #{edit_data}"
end

login

edit_test :bot => false
edit_test :bot => false, :assert => true
edit_test :bot => false, :nassert => true
edit_test :bot => true
edit_test :bot => true, :assert => true
edit_test :bot => true, :nassert => true