Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucaswerkmeister/1bf084ae7be97403e0325bdf3bbc1d53 to your computer and use it in GitHub Desktop.
Save lucaswerkmeister/1bf084ae7be97403e0325bdf3bbc1d53 to your computer and use it in GitHub Desktop.
patch for QuickStatements
From 38a7b52a3bb2d7d08639bdf46e5f3cba446fcde2 Mon Sep 17 00:00:00 2001
From: Lucas Werkmeister <mail@lucaswerkmeister.de>
Date: Thu, 18 Oct 2018 16:12:53 +0200
Subject: [PATCH] Run new batches as the submitting user
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
This adds a new table storing OAuth details for a batch, and populates
it when any new batch is created. When running commands for a batch,
QuickStatements will attempt to load the OAuth details for that batch,
and run the commands using those details instead of the bot account in
case of success.
The effect of this is that any new batches will be run as the user who
submitted the batch, instead of as the bot account. Old batches that
were in progress when this commit was deployed will continue to complete
as the bot account, since we don’t have OAuth details for them (and it
would be odd to change the account mid-batch anyways).
Error handling is relaxed compared to surrounding code in several
places, generally falling back to OAuth-less mode instead of aborting.
---
public_html/quickstatements.php | 31 ++++++++++++++++++++++++++++++-
schema.sql | 10 +++++++++-
2 files changed, 39 insertions(+), 2 deletions(-)
diff --git a/public_html/quickstatements.php b/public_html/quickstatements.php
index becb60c..00ed2a4 100644
--- a/public_html/quickstatements.php
+++ b/public_html/quickstatements.php
@@ -108,6 +108,13 @@ public function getOA() {
return $this->oa ;
}
+ public function setOA( MW_OAuth $oa ) {
+ if ( isset($this->oa) ) {
+ $this->log( 'Overriding one OAuth object with another, this is unexpected' );
+ }
+ $this->oa = $oa;
+ }
+
public function getBatch ( $id ) {
$id *= 1 ;
$ret = array('commands'=>array()) ;
@@ -156,6 +163,8 @@ public function addBatch ( $commands , $user_id , $name = '' , $site = '' ) {
$sql = "INSERT INTO batch (name,user,site,ts_created,ts_last_change,status) VALUES ('".$db->real_escape_string($name)."',$user_id,'".$db->real_escape_string($site)."','$ts','$ts','LOADING')" ;
if(!$result = $db->query($sql)) return $this->setErrorMessage ( 'There was an error running the query [' . $db->error . ']'."\n$sql" ) ;
$batch_id = $db->insert_id ;
+ $sql = "INSERT INTO batch_oauth (batch_id,serialized) VALUES ($batch_id,'".$db->real_escape_string(serialize($this->getOA()))."')" ;
+ if(!$result = $db->query($sql)) $this->log( "Could not store OAuth information for batch $batch_id [" . $db->error . ']' );
foreach ( $commands AS $k => $c ) {
$cs = json_encode ( $c ) ;
if ( trim($cs) == '' ) continue ; // Paranoia
@@ -244,6 +253,27 @@ public function runNextCommandInBatch ( $batch_id ) {
return true ;
}
+ // load OAuth, if available
+ $sql = "SELECT serialized FROM batch_oauth WHERE batch_id=$batch_id" ;
+ if($result = $db->query($sql)) {
+ $oauth = $result->fetch_object() ;
+ if ( $oauth !== NULL ) {
+ $oa = unserialize($oauth->serialized) ;
+ if ( $oa === false ) {
+ $this->log( "Could not unserialize OAuth information for batch $batch_id:\n".$oauth->serialized );
+ $this->use_oauth = false ;
+ } else {
+ $this->setOA( $oa ) ;
+ }
+ } else {
+ // no OAuth information for this batch – perfectly normal for older batches, don’t log
+ $this->use_oauth = false ;
+ }
+ } else {
+ $this->log( "Could not load OAuth information for batch $batch_id [" . $db->error . ']' );
+ $this->use_oauth = false ;
+ }
+
// Update status
#if ( !isset($o->id) ) print_r ( $o ) ;
$sql = "UPDATE command SET status='RUN',ts_change='$ts',message='' WHERE id={$o->id}" ;
@@ -254,7 +284,6 @@ public function runNextCommandInBatch ( $batch_id ) {
$cmd = json_decode ( $o->json ) ;
if ( !isset($cmd->summary) ) $cmd->summary = $summary ;
else $cmd->summary .= '; ' . $summary ;
- $this->use_oauth = false ;
$this->runSingleCommand ( $cmd ) ;
// Update batch status
diff --git a/schema.sql b/schema.sql
index 2fbd4ee..8f1616a 100644
--- a/schema.sql
+++ b/schema.sql
@@ -34,4 +34,12 @@ CREATE TABLE `user` (
`api_hash` varchar(64) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
PRIMARY KEY (`id`),
KEY `name` (`name`(191))
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
\ No newline at end of file
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
+
+CREATE TABLE `batch_oauth` (
+ `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
+ `batch_id` int(11) NOT NULL,
+ `serialized` mediumtext COLLATE utf8mb4_unicode_ci NOT NULL,
+ PRIMARY KEY (`id`),
+ KEY `batch_id` (`batch_id`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
--
2.17.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment