r107725 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r107724‎ | r107725 | r107726 >
Date:16:22, 31 December 2011
Author:mah
Status:deferred
Tags:tools 
Comment:
Add the vandal fighting undoLastChangeIfBy( ) method and supporting functions.
Modified paths:
  • /trunk/tools/bugzilla/client/bugzilla.php (modified) (history)

Diff [purge]

Index: trunk/tools/bugzilla/client/bugzilla.php
@@ -121,6 +121,62 @@
122122 }
123123 return $this->dependency;
124124 }
 125+
 126+ public function undoLastChangeIfBy( $email ) {
 127+ $hist = $this->getHistory();
 128+ $change = array_pop( $hist['bugs'][0]['history'] );
 129+ $reverse = array();
 130+
 131+ if( $change['who'] == $email ) {
 132+ echo "{$this->id}: Undoing last change by $email made at {$change['when']}:\n";
 133+ foreach($change['changes'] as $c) {
 134+ $reverse = array_merge( $reverse, $this->addResetField( $c ) );
 135+ }
 136+ return $this->bz->update( $this->id, $reverse );
 137+ } else {
 138+ return false;
 139+ }
 140+ }
 141+
 142+ public function addResetField( $changeLog ) {
 143+ if( $this->bz->isListField( $changeLog['field_name'] ) ) {
 144+ return array( $changeLog['field_name'] =>
 145+ array( "add" => (array)$changeLog['removed'], "remove" => (array)$changeLog['added'] ) );
 146+ } else {
 147+ return array( $changeLog['field_name'] => $changeLog['removed'] );
 148+ }
 149+ }
 150+
 151+ public function resetField( $change ) {
 152+ if( !isset( $change['field_name'] ) )
 153+ throw new Exception( "no field_name given!" );
 154+ if( !isset( $change['removed'] ) )
 155+ throw new Exception( "no removed value given!" );
 156+ if( !isset( $change['added'] ) )
 157+ throw new Exception( "no added value given!" );
 158+
 159+ if( $change['removed'] == "" ) {
 160+ return $this->removeFromFieldList( $change['field_name'], $change['added'] );
 161+ }
 162+
 163+ if( $change['added'] == "" ) {
 164+ return $this->addToFieldList( $change['field_name'], $change['removed'] );
 165+ }
 166+
 167+ return $this->setFieldValue( $change['field_name'], $change['removed'] );
 168+ }
 169+
 170+ public function addToFieldList( $field, $value ) {
 171+ return $this->bz->addToFieldList( $this->id, $field, $value );
 172+ }
 173+
 174+ public function removeFromFieldList( $field, $value ) {
 175+ return $this->bz->removeFromFieldList( $this->id, $field, $value );
 176+ }
 177+
 178+ public function setFieldValue( $field, $value ) {
 179+ return $this->bz->update( $this->id, array( $field => $value ) );
 180+ }
125181 }
126182
127183 class BugzillaSearchIterator implements Iterator {
@@ -180,6 +236,7 @@
181237
182238 class BugzillaWebClient {
183239 private $bz = null;
 240+ private $lists = array( "blocks", "depends_on", "cc", "groups", "keywords", "see_also" );
184241
185242 public function __construct( $url, $user = null, $password = null, $debug = false ) {
186243 $this->bz = new jsonRPCClient( $url, $debug );
@@ -188,6 +245,10 @@
189246 }
190247 }
191248
 249+ public function isListField( $field ) {
 250+ return in_array( $field, $this->lists );
 251+ }
 252+
192253 public function getById( $id ) {
193254 return new BugzillaBug( $id, $this );
194255 }
@@ -197,6 +258,10 @@
198259 return $this->bz->__call( "Bug.fields", array( "" => "" ) );
199260 }
200261
 262+ public function getState( $id ) {
 263+ $this->bz->__call( "Bug.get", array( "id" => (array)$id ) );
 264+ }
 265+
201266 public function search( $conditions ) {
202267 if(is_array($conditions)) {
203268 return $this->bz->__call( "Bug.search", $conditions );
@@ -223,4 +288,25 @@
224289 if(!is_array($resolution)) $resolution = array($resolution);
225290 return $this->search(array("resolution" => $resolution, "limit" => 10));
226291 }
 292+
 293+ public function addToFieldList( $ids, $field, $value ) {
 294+ if( !in_array( $field, $this->lists ) ) {
 295+ throw new Exception( "This field ($field) isn't a list!" );
 296+ } else {
 297+ return $this->update( $ids, array( $field => array( "add" => (array)$value ) ) );
 298+ }
 299+ }
 300+
 301+ public function removeFromFieldList( $ids, $field, $value ) {
 302+ if( !in_array( $field, $this->lists ) ) {
 303+ throw new Exception( "This field ($field) isn't a list!" );
 304+ } else {
 305+ return $this->update( $ids, array( $field => array( "remove" => (array)$value ) ) );
 306+ }
 307+ }
 308+
 309+ public function update( $ids, $fields ) {
 310+ $fields['ids'] = (array)$ids;
 311+ return $this->bz->__call( "Bug.update", $fields );
 312+ }
227313 }

Status & tagging log