Skip to content

Commit

Permalink
Merge pull request #157 from x-tools/rev-sha1
Browse files Browse the repository at this point in the history
ArticleInfo: use rev_sha1 for revert detection
  • Loading branch information
MusikAnimal authored Jan 19, 2018
2 parents df08b4b + fb7a7b9 commit 061afa7
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 9 deletions.
33 changes: 27 additions & 6 deletions src/Xtools/ArticleInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,10 @@ private function parseHistory()
// The previous Edit, used to discount content that was reverted.
'prev' => null,

// The SHA-1 of the edit *before* the previous edit. Used for more
// accruate revert detection.
'prevSha' => null,

// The last edit deemed to be the max addition of content. This is kept track of
// in case we find out the next edit was reverted (and was also a max edit),
// in which case we'll want to discount it and use this one instead.
Expand Down Expand Up @@ -759,7 +763,7 @@ private function parseHistory()
/**
* Update various counts based on the current edit.
* @param Edit $edit
* @param Edit[] $prevEdits With 'prev', 'maxAddition' and 'maxDeletion'
* @param Edit[] $prevEdits With 'prev', 'prevSha', 'maxAddition' and 'maxDeletion'
* @return Edit[] Updated version of $prevEdits.
*/
private function updateCounts(Edit $edit, $prevEdits)
Expand All @@ -784,6 +788,12 @@ private function updateCounts(Edit $edit, $prevEdits)

// Now that we've updated all the counts, we can reset
// the prev and last edits, which are used for tracking.
// But first, let's copy over the SHA of the actual previous edit
// and put it in our $prevEdits['prev'], so that we'll know
// that content added after $prevEdit['prev'] was reverted.
if ($prevEdits['prev'] !== null) {
$prevEdits['prevSha'] = $prevEdits['prev']->getSha();
}
$prevEdits['prev'] = $edit;
$this->lastEdit = $edit;

Expand All @@ -793,23 +803,34 @@ private function updateCounts(Edit $edit, $prevEdits)
/**
* Update various figures about content sizes based on the given edit.
* @param Edit $edit
* @param Edit[] $prevEdits With 'prev', 'maxAddition' and 'maxDeletion'
* @param Edit[] $prevEdits With 'prev', 'prevSha', 'maxAddition' and 'maxDeletion'.
* @return Edit[] Updated version of $prevEdits.
*/
private function updateContentSizes(Edit $edit, $prevEdits)
{
// Check if it was a revert
if ($edit->isRevert($this->container)) {
if ($this->isRevert($prevEdits, $edit)) {
return $this->updateContentSizesRevert($prevEdits);
} else {
return $this->updateContentSizesNonRevert($edit, $prevEdits);
}
}

/**
* Is the given Edit a revert?
* @param Edit[] $prevEdits With 'prev', 'prevSha', 'maxAddition' and 'maxDeletion'.
* @param Edit $edit
* @return bool
*/
private function isRevert($prevEdits, $edit)
{
return $edit->getSha() === $prevEdits['prevSha'] || $edit->isRevert($this->container);
}

/**
* Updates the figures on content sizes assuming the given edit was a revert of the previous one.
* In such a case, we don't want to treat the previous edit as legit content addition or removal.
* @param Edit[] $prevEdits With 'prev', 'maxAddition' and 'maxDeletion'.
* @param Edit[] $prevEdits With 'prev', 'prevSha', 'maxAddition' and 'maxDeletion'.
* @return Edit[] Updated version of $prevEdits, for tracking.
*/
private function updateContentSizesRevert($prevEdits)
Expand Down Expand Up @@ -843,7 +864,7 @@ private function updateContentSizesRevert($prevEdits)
* Updates the figures on content sizes assuming the given edit
* was NOT a revert of the previous edit.
* @param Edit $edit
* @param Edit[] $prevEdits With 'prev', 'maxAddition' and 'maxDeletion'.
* @param Edit[] $prevEdits With 'prev', 'prevSha', 'maxAddition' and 'maxDeletion'.
* @return Edit[] Updated version of $prevEdits, for tracking.
*/
private function updateContentSizesNonRevert(Edit $edit, $prevEdits)
Expand Down Expand Up @@ -881,7 +902,7 @@ private function updateContentSizesNonRevert(Edit $edit, $prevEdits)
* @see T148857 for more information.
* @todo Remove once T101631 is resolved.
* @param Edit $edit
* @param Edit[] $prevEdits With 'prev', 'maxAddition' and 'maxDeletion'.
* @param Edit[] $prevEdits With 'prev', 'prevSha', 'maxAddition' and 'maxDeletion'.
* @return Edit[] Updated version of $prevEdits, for tracking.
*/
private function getEditSize(Edit $edit, $prevEdits)
Expand Down
18 changes: 18 additions & 0 deletions src/Xtools/Edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ class Edit extends Model
/** @var string The edit summary */
protected $comment;

/** @var string The SHA-1 of the wikitext as of the revision. */
protected $sha;

/**
* Edit constructor.
* @param Page $page
Expand Down Expand Up @@ -67,6 +70,12 @@ public function __construct(Page $page, $attrs)

$this->user = new User($attrs['username']);
$this->comment = $attrs['comment'];

if (isset($attrs['rev_sha1']) || isset($attrs['sha'])) {
$this->sha = isset($attrs['rev_sha1'])
? $attrs['rev_sha1']
: $attrs['sha'];
}
}

/**
Expand Down Expand Up @@ -197,6 +206,15 @@ public function getSummary()
return $this->getComment();
}

/**
* Get the SHA-1 of the revision.
* @return string
*/
public function getSha()
{
return $this->sha;
}

/**
* Get edit summary as 'wikified' HTML markup
* @param bool $useUnnormalizedPageTitle Use the unnormalized page title to avoid
Expand Down
11 changes: 8 additions & 3 deletions tests/Xtools/ArticleInfoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ public function testGetters()
$this->assertEquals(2, $this->articleInfo->getMaxEditsPerMonth());

$this->assertContains(
'Undo',
'AutoWikiBrowser',
array_keys($this->articleInfo->getTools())
);
}
Expand Down Expand Up @@ -328,6 +328,7 @@ private function setupData()
'length_change' => '30',
'username' => 'Mick Jagger',
'comment' => 'Foo bar',
'rev_sha1' => 'aaaaaa',
]),
new Edit($this->page, [
'id' => 32,
Expand All @@ -337,6 +338,7 @@ private function setupData()
'length_change' => '-5',
'username' => 'Mick Jagger',
'comment' => 'Blah',
'rev_sha1' => 'bbbbbb',
]),
new Edit($this->page, [
'id' => 40,
Expand All @@ -345,7 +347,8 @@ private function setupData()
'length' => '15',
'length_change' => '-10',
'username' => '192.168.0.1',
'comment' => 'Weeee',
'comment' => 'Weeee using ([[WP:AWB|AWB]])',
'rev_sha1' => 'cccccc',
]),
new Edit($this->page, [
'id' => 50,
Expand All @@ -354,12 +357,14 @@ private function setupData()
'length' => '25',
'length_change' => '10',
'username' => '192.168.0.2',
'comment' => 'Undid revision 40 by [[Special:Contributions/192.168.0.1|192.168.0.1]]',
'comment' => 'I undo your edit cuz it bad',
'rev_sha1' => 'bbbbbb',
]),
];

$prevEdits = [
'prev' => null,
'prevSha' => null,
'maxAddition' => null,
'maxDeletion' => null,
];
Expand Down

0 comments on commit 061afa7

Please sign in to comment.