/**
  * When an edit is made by a user, review it if either:
  * (a) The user can 'autoreview' and the edit's base revision was checked
  * (b) The edit is a self-revert to the stable version (by anyone)
  * (c) The user can 'autoreview' new pages and this edit is a new page
  * (d) The user can 'review' and the "review pending edits" checkbox was checked
  *
  * Note: RC items not inserted yet, RecentChange_save hook does rc_patrolled bit...
  * Note: $article one of Article, ImagePage, Category page as appropriate.
  */
 public static function maybeMakeEditReviewed(Page $article, $rev, $baseRevId = false, $user = null)
 {
     global $wgRequest;
     $title = $article->getTitle();
     // convenience
     # Edit must be non-null, to a reviewable page, with $user set
     $fa = FlaggableWikiPage::getTitleInstance($title);
     $fa->loadPageData('fromdbmaster');
     if (!$rev || !$user || !$fa->isReviewable()) {
         return true;
     }
     $fa->preloadPreparedEdit($article);
     // avoid double parse
     $title->resetArticleID($rev->getPage());
     // Avoid extra DB hit and lag issues
     # Get what was just the current revision ID
     $prevRevId = $rev->getParentId();
     # Get edit timestamp. Existance already validated by EditPage.php.
     $editTimestamp = $wgRequest->getVal('wpEdittime');
     # Is the page manually checked off to be reviewed?
     if ($editTimestamp && $wgRequest->getCheck('wpReviewEdit') && $title->getUserPermissionsErrors('review', $user) === array()) {
         if (self::editCheckReview($fa, $rev, $user, $editTimestamp)) {
             return true;
             // reviewed...done!
         }
     }
     # All cases below require auto-review of edits to be enabled
     if (!FlaggedRevs::autoReviewEnabled()) {
         return true;
         // short-circuit
     }
     # If a $baseRevId is passed in, the edit is using an old revision's text
     $isOldRevCopy = (bool) $baseRevId;
     // null edit or rollback
     # Get the revision ID the incoming one was based off...
     if (!$baseRevId && $prevRevId) {
         $prevTimestamp = Revision::getTimestampFromId($title, $prevRevId);
         # The user just made an edit. The one before that should have
         # been the current version. If not reflected in wpEdittime, an
         # edit may have been auto-merged in between, in that case, discard
         # the baseRevId given from the client.
         if ($editTimestamp && $prevTimestamp === $editTimestamp) {
             $baseRevId = $wgRequest->getInt('baseRevId');
         }
         # If baseRevId not given, assume the previous revision ID (for bots).
         # For auto-merges, this also occurs since the given ID is ignored.
         if (!$baseRevId) {
             $baseRevId = $prevRevId;
         }
     }
     $frev = null;
     // flagged rev this edit was based on
     $flags = null;
     // review flags (null => default flags)
     $srev = $fa->getStableRev();
     # Case A: this user can auto-review edits. Check if either:
     # (a) this new revision creates a new page and new page autoreview is enabled
     # (b) this new revision is based on an old, reviewed, revision
     if ($title->getUserPermissionsErrors('autoreview', $user) === array()) {
         # For rollback/null edits, use the previous ID as the alternate base ID.
         # Otherwise, use the 'altBaseRevId' parameter passed in by the request.
         $altBaseRevId = $isOldRevCopy ? $prevRevId : $wgRequest->getInt('altBaseRevId');
         if (!$prevRevId) {
             // New pages
             $reviewableNewPage = FlaggedRevs::autoReviewNewPages();
             $reviewableChange = false;
         } else {
             // Edits to existing pages
             $reviewableNewPage = false;
             // had previous rev
             # If a edit was automatically merged, do not trust 'baseRevId' (bug 33481)
             if ($editTimestamp && $editTimestamp !== Revision::getTimestampFromId($title, $prevRevId)) {
                 $baseRevId = $prevRevId;
                 $altBaseRevId = 0;
             }
             # Check if the base revision was reviewed...
             if (FlaggedRevs::autoReviewEdits()) {
                 $frev = FlaggedRevision::newFromTitle($title, $baseRevId, FR_MASTER);
                 if (!$frev && $altBaseRevId) {
                     $frev = FlaggedRevision::newFromTitle($title, $altBaseRevId, FR_MASTER);
                 }
             }
             $reviewableChange = $frev || $srev && $rev->getSha1() === $srev->getRevision()->getSha1();
         }
         # Is this an edit directly to a reviewed version or a new page?
         if ($reviewableNewPage || $reviewableChange) {
             if ($isOldRevCopy && $frev) {
                 $flags = $frev->getTags();
                 // null edits & rollbacks keep previous tags
             }
             # Review this revision of the page...
             FlaggedRevs::autoReviewEdit($fa, $user, $rev, $flags);
         }
         # Case B: the user cannot autoreview edits. Check if either:
         # (a) this is a rollback to the stable version
         # (b) this is a self-reversion to the stable version
         # These are subcases of making a new revision based on an old, reviewed, revision.
     } elseif (FlaggedRevs::autoReviewEdits() && $srev) {
         # Check for rollbacks...
         $reviewableChange = $isOldRevCopy && $baseRevId != $prevRevId && $baseRevId == $srev->getRevId() && $title->getUserPermissionsErrors('autoreviewrestore', $user) === array();
         # Check for self-reversions (checks text hashes)...
         if (!$reviewableChange) {
             $reviewableChange = self::isSelfRevertToStable($rev, $srev, $baseRevId, $user);
         }
         # Is this a rollback or self-reversion to the stable rev?
         if ($reviewableChange) {
             $flags = $srev->getTags();
             // use old tags
             # Review this revision of the page...
             FlaggedRevs::autoReviewEdit($fa, $user, $rev, $flags);
         }
     }
     return true;
 }