Hard deprecate Revision::userCan

Replace remaining uses outside of tests, hide deprecated in tests

Bug: T247259
Change-Id: I91dfb46ed6864ee89d968aea8ec93b5ea2f8389e
This commit is contained in:
DannyS712 2020-03-11 01:41:49 +00:00
parent e67594d077
commit 78bc3ae255
16 changed files with 174 additions and 43 deletions

View file

@ -541,7 +541,6 @@ because of Phabricator reports.
- LogPage::addEntry
- FileDeleteForm::doDelete
Additionally, not passing a user to the following is soft deprecated:
- Revision::userCan
- ArchivedFile::userCan
- OldLocalFile::userCan
* Article::insertProtectNullRevision is deprecated. Instead, use
@ -557,7 +556,7 @@ because of Phabricator reports.
use its methods.
* The Revision class was soft deprecated entirely in 1.31. Specific methods
have now been individually hard deprecated:
- ::userCanBitfield - use RevisionRecord::userCanBitfield instead
- ::userCan and userCanBitfield - use RevisionRecord::userCanBitfield instead
- ::loadFromTitle - use RevisionStore::getRevisionByTitle instead
- ::countByPageId - use RevisionStore::countRevisionsByPageId instead
- ::countByTitle - use RevisionStore::countRevisionsByTitle instead

View file

@ -3216,7 +3216,11 @@ ERROR;
if ( $revision ) {
// Let sysop know that this will make private content public if saved
if ( !$revision->userCan( RevisionRecord::DELETED_TEXT, $user ) ) {
if ( !RevisionRecord::userCanBitfield(
$revision->getVisibility(),
RevisionRecord::DELETED_TEXT,
$user
) ) {
$out->wrapWikiMsg(
"<div class='mw-warning plainlinks'>\n$1\n</div>\n",
'rev-deleted-text-permission'

View file

@ -1101,9 +1101,16 @@ class Linker {
* @return string HTML fragment
*/
public static function revUserLink( $rev, $isPublic = false ) {
// TODO inject a user
$user = RequestContext::getMain()->getUser();
if ( $rev->isDeleted( RevisionRecord::DELETED_USER ) && $isPublic ) {
$link = wfMessage( 'rev-deleted-user' )->escaped();
} elseif ( $rev->userCan( RevisionRecord::DELETED_USER ) ) {
} elseif ( RevisionRecord::userCanBitfield(
$rev->getVisibility(),
RevisionRecord::DELETED_USER,
$user
) ) {
$link = self::userLink( $rev->getUser( RevisionRecord::FOR_THIS_USER ),
$rev->getUserText( RevisionRecord::FOR_THIS_USER ) );
} else {
@ -1124,7 +1131,14 @@ class Linker {
* @return string HTML
*/
public static function revUserTools( $rev, $isPublic = false, $useParentheses = true ) {
if ( $rev->userCan( RevisionRecord::DELETED_USER ) &&
// TODO inject a user
$user = RequestContext::getMain()->getUser();
if ( RevisionRecord::userCanBitfield(
$rev->getVisibility(),
RevisionRecord::DELETED_USER,
$user
) &&
( !$rev->isDeleted( RevisionRecord::DELETED_USER ) || !$isPublic )
) {
$userId = $rev->getUser( RevisionRecord::FOR_THIS_USER );
@ -1575,12 +1589,19 @@ class Linker {
public static function revComment( Revision $rev, $local = false, $isPublic = false,
$useParentheses = true
) {
// TODO inject a user
$user = RequestContext::getMain()->getUser();
if ( $rev->getComment( RevisionRecord::RAW ) == "" ) {
return "";
}
if ( $rev->isDeleted( RevisionRecord::DELETED_COMMENT ) && $isPublic ) {
$block = " <span class=\"comment\">" . wfMessage( 'rev-deleted-comment' )->escaped() . "</span>";
} elseif ( $rev->userCan( RevisionRecord::DELETED_COMMENT ) ) {
} elseif ( RevisionRecord::userCanBitfield(
$rev->getVisibility(),
RevisionRecord::DELETED_COMMENT,
$user
) ) {
$block = self::commentBlock( $rev->getComment( RevisionRecord::FOR_THIS_USER ),
$rev->getTitle(), $local, null, $useParentheses );
} else {
@ -2113,7 +2134,11 @@ class Linker {
return '';
}
if ( !$rev->userCan( RevisionRecord::DELETED_RESTRICTED, $user ) ) {
if ( !RevisionRecord::userCanBitfield(
$rev->getVisibility(),
RevisionRecord::DELETED_RESTRICTED,
$user
) ) {
return self::revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
}
$prefixedDbKey = MediaWikiServices::getInstance()->getTitleFormatter()->

View file

@ -1002,6 +1002,8 @@ class Revision implements IDBAccessObject {
* Determine if the current user is allowed to view a particular
* field of this revision, if it's marked as deleted.
*
* @deprecated since 1.31 (soft), 1.35 (hard)
*
* @param int $field One of self::DELETED_TEXT,
* self::DELETED_COMMENT,
* self::DELETED_USER
@ -1009,8 +1011,8 @@ class Revision implements IDBAccessObject {
* @return bool
*/
public function userCan( $field, User $user = null ) {
wfDeprecated( __METHOD__, '1.31' );
if ( !$user ) {
// TODO check callers and hard deprecate
global $wgUser;
$user = $wgUser;
}

View file

@ -337,12 +337,17 @@ class HistoryPager extends ReverseChronologicalPager {
$canRevDelete = $permissionManager->userHasRight( $user, 'deleterevision' );
// Show checkboxes for each revision, to allow for revision deletion and
// change tags
$visibility = $rev->getVisibility();
if ( $canRevDelete || $this->showTagEditUI ) {
$this->preventClickjacking();
// If revision was hidden from sysops and we don't need the checkbox
// for anything else, disable it
if ( !$this->showTagEditUI
&& !$rev->userCan( RevisionRecord::DELETED_RESTRICTED, $user )
&& !RevisionRecord::userCanBitfield(
$visibility,
RevisionRecord::DELETED_RESTRICTED,
$user
)
) {
$del = Xml::check( 'deleterevisions', false, [ 'disabled' => 'disabled' ] );
// Otherwise, enable the checkbox...
@ -351,10 +356,13 @@ class HistoryPager extends ReverseChronologicalPager {
[ 'name' => 'ids[' . $rev->getId() . ']' ] );
}
// User can only view deleted revisions...
} elseif ( $rev->getVisibility() &&
$permissionManager->userHasRight( $user, 'deletedhistory' ) ) {
} elseif ( $visibility && $permissionManager->userHasRight( $user, 'deletedhistory' ) ) {
// If revision was hidden from sysops, disable the link
if ( !$rev->userCan( RevisionRecord::DELETED_RESTRICTED, $user ) ) {
if ( !RevisionRecord::userCanBitfield(
$visibility,
RevisionRecord::DELETED_RESTRICTED,
$user
) ) {
$del = Linker::revDeleteLinkDisabled( false );
// Otherwise, show the link...
} else {
@ -501,7 +509,11 @@ class HistoryPager extends ReverseChronologicalPager {
$cur = $this->historyPage->message['cur'];
$latest = $this->getWikiPage()->getLatest();
if ( $latest === $rev->getId()
|| !$rev->userCan( RevisionRecord::DELETED_TEXT, $this->getUser() )
|| !RevisionRecord::userCanBitfield(
$rev->getVisibility(),
RevisionRecord::DELETED_TEXT,
$this->getUser()
)
) {
return $cur;
} else {
@ -550,8 +562,15 @@ class HistoryPager extends ReverseChronologicalPager {
$nextRev = new Revision( $next, 0, $this->getTitle() );
if ( !$prevRev->userCan( RevisionRecord::DELETED_TEXT, $this->getUser() )
|| !$nextRev->userCan( RevisionRecord::DELETED_TEXT, $this->getUser() )
if ( !RevisionRecord::userCanBitfield(
$prevRev->getVisibility(),
RevisionRecord::DELETED_TEXT,
$this->getUser()
) || !RevisionRecord::userCanBitfield(
$nextRev->getVisibility(),
RevisionRecord::DELETED_TEXT,
$this->getUser()
)
) {
return $last;
}
@ -590,7 +609,11 @@ class HistoryPager extends ReverseChronologicalPager {
$checkmark = [ 'checked' => 'checked' ];
} else {
# Check visibility of old revisions
if ( !$rev->userCan( RevisionRecord::DELETED_TEXT, $this->getUser() ) ) {
if ( !RevisionRecord::userCanBitfield(
$rev->getVisibility(),
RevisionRecord::DELETED_TEXT,
$this->getUser()
) ) {
$radio['disabled'] = 'disabled';
$checkmark = []; // We will check the next possible one
} elseif ( !$this->oldIdChecked ) {

View file

@ -107,7 +107,11 @@ class ApiParse extends ApiBase {
}
$this->checkTitleUserPermissions( $rev->getTitle(), 'read' );
if ( !$rev->userCan( RevisionRecord::DELETED_TEXT, $this->getUser() ) ) {
if ( !RevisionRecord::userCanBitfield(
$rev->getVisibility(),
RevisionRecord::DELETED_TEXT,
$this->getUser()
) ) {
$this->dieWithError(
[ 'apierror-permissiondenied', $this->msg( 'action-deletedtext' ) ]
);

View file

@ -418,7 +418,11 @@ class ChangesList extends ContextSource {
public static function revDateLink( Revision $rev, User $user, Language $lang, $title = null ) {
$ts = $rev->getTimestamp();
$date = $lang->userTimeAndDate( $ts, $user );
if ( $rev->userCan( RevisionRecord::DELETED_TEXT, $user ) ) {
if ( RevisionRecord::userCanBitfield(
$rev->getVisibility(),
RevisionRecord::DELETED_TEXT,
$user
) ) {
$link = MediaWikiServices::getInstance()->getLinkRenderer()->makeKnownLink(
$title ?? $rev->getTitle(),
$date,

View file

@ -548,9 +548,17 @@ class DifferenceEngine extends ContextSource {
$this->loadRevisionData();
// $this->mNewRev will only be falsy if a loading error occurred
// (in which case the user is allowed to see).
$allowed = !$this->mNewRev || $this->mNewRev->userCan( RevisionRecord::DELETED_TEXT, $user );
$allowed = !$this->mNewRev || RevisionRecord::userCanBitfield(
$this->mNewRev->getVisibility(),
RevisionRecord::DELETED_TEXT,
$user
);
if ( $this->mOldRev &&
!$this->mOldRev->userCan( RevisionRecord::DELETED_TEXT, $user )
!RevisionRecord::userCanBitfield(
$this->mOldRev->getVisibility(),
RevisionRecord::DELETED_TEXT,
$user
)
) {
$allowed = false;
}
@ -1090,11 +1098,19 @@ class DifferenceEngine extends ContextSource {
if ( !$this->loadRevisionData() ) {
return false;
} elseif ( $this->mOldRev &&
!$this->mOldRev->userCan( RevisionRecord::DELETED_TEXT, $this->getUser() )
!RevisionRecord::userCanBitfield(
$this->mOldRev->getVisibility(),
RevisionRecord::DELETED_TEXT,
$this->getUser()
)
) {
return false;
} elseif ( $this->mNewRev &&
!$this->mNewRev->userCan( RevisionRecord::DELETED_TEXT, $this->getUser() )
!RevisionRecord::userCanBitfield(
$this->mNewRev->getVisibility(),
RevisionRecord::DELETED_TEXT,
$this->getUser()
)
) {
return false;
}
@ -1627,7 +1643,11 @@ class DifferenceEngine extends ContextSource {
private function userCanEdit( Revision $rev ) {
$user = $this->getUser();
if ( !$rev->userCan( RevisionRecord::DELETED_TEXT, $user ) ) {
if ( !RevisionRecord::userCanBitfield(
$rev->getVisibility(),
RevisionRecord::DELETED_TEXT,
$user
) ) {
return false;
}

View file

@ -489,9 +489,11 @@ class Article implements Page {
$this->mRevIdFetched = $this->mRevision->getId();
$this->fetchResult = Status::newGood( $this->mRevision );
if (
!$this->mRevision->userCan( RevisionRecord::DELETED_TEXT, $this->getContext()->getUser() )
) {
if ( !RevisionRecord::userCanBitfield(
$this->mRevision->getVisibility(),
RevisionRecord::DELETED_TEXT,
$this->getContext()->getUser()
) ) {
wfDebug( __METHOD__ . " failed to retrieve content of revision " .
$this->mRevision->getId() . "\n" );
@ -1530,7 +1532,11 @@ class Article implements Page {
$outputPage = $this->getContext()->getOutput();
$user = $this->getContext()->getUser();
// If the user is not allowed to see it...
if ( !$this->mRevision->userCan( RevisionRecord::DELETED_TEXT, $user ) ) {
if ( !RevisionRecord::userCanBitfield(
$this->mRevision->getVisibility(),
RevisionRecord::DELETED_TEXT,
$user
) ) {
$outputPage->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n",
'rev-deleted-text-permission' );

View file

@ -67,14 +67,18 @@ class RevDelRevisionItem extends RevDelItem {
}
public function canView() {
return $this->revision->userCan(
RevisionRecord::DELETED_RESTRICTED, $this->list->getUser()
return RevisionRecord::userCanBitfield(
$this->revision->getVisibility(),
RevisionRecord::DELETED_RESTRICTED,
$this->list->getUser()
);
}
public function canViewContent() {
return $this->revision->userCan(
RevisionRecord::DELETED_TEXT, $this->list->getUser()
return RevisionRecord::userCanBitfield(
$this->revision->getVisibility(),
RevisionRecord::DELETED_TEXT,
$this->list->getUser()
);
}
@ -213,13 +217,21 @@ class RevDelRevisionItem extends RevDelItem {
'commenthidden' => (bool)$rev->isDeleted( RevisionRecord::DELETED_COMMENT ),
'texthidden' => (bool)$rev->isDeleted( RevisionRecord::DELETED_TEXT ),
];
if ( $rev->userCan( RevisionRecord::DELETED_USER, $user ) ) {
if ( RevisionRecord::userCanBitfield(
$rev->getVisibility(),
RevisionRecord::DELETED_USER,
$user
) ) {
$ret += [
'userid' => $rev->getUser( RevisionRecord::FOR_THIS_USER ),
'user' => $rev->getUserText( RevisionRecord::FOR_THIS_USER ),
];
}
if ( $rev->userCan( RevisionRecord::DELETED_COMMENT, $user ) ) {
if ( RevisionRecord::userCanBitfield(
$rev->getVisibility(),
RevisionRecord::DELETED_COMMENT,
$user
) ) {
$ret += [
'comment' => $rev->getComment( RevisionRecord::FOR_THIS_USER ),
];

View file

@ -55,14 +55,18 @@ class RevisionItem extends RevisionItemBase {
}
public function canView() {
return $this->revision->userCan(
RevisionRecord::DELETED_RESTRICTED, $this->context->getUser()
return RevisionRecord::userCanBitfield(
$this->revision->getVisibility(),
RevisionRecord::DELETED_RESTRICTED,
$this->context->getUser()
);
}
public function canViewContent() {
return $this->revision->userCan(
RevisionRecord::DELETED_TEXT, $this->context->getUser()
return RevisionRecord::userCanBitfield(
$this->revision->getVisibility(),
RevisionRecord::DELETED_TEXT,
$this->context->getUser()
);
}

View file

@ -300,7 +300,11 @@ class SpecialMergeHistory extends SpecialPage {
}
# Last link
if ( !$rev->userCan( RevisionRecord::DELETED_TEXT, $user ) ) {
if ( !RevisionRecord::userCanBitfield(
$rev->getVisibility(),
RevisionRecord::DELETED_TEXT,
$user
) ) {
$last = $this->msg( 'last' )->escaped();
} elseif ( isset( $this->prevId[$row->rev_id] ) ) {
$last = $linkRenderer->makeKnownLink(

View file

@ -434,7 +434,11 @@ class SpecialUndelete extends SpecialPage {
}
if ( $rev->isDeleted( RevisionRecord::DELETED_TEXT ) ) {
if ( !$rev->userCan( RevisionRecord::DELETED_TEXT, $user ) ) {
if ( !RevisionRecord::userCanBitfield(
$rev->getVisibility(),
RevisionRecord::DELETED_TEXT,
$user
) ) {
$out->wrapWikiMsg(
"<div class='mw-warning plainlinks'>\n$1\n</div>\n",
$rev->isDeleted( RevisionRecord::DELETED_RESTRICTED ) ?
@ -981,7 +985,11 @@ class SpecialUndelete extends SpecialPage {
if ( $this->mCanView ) {
$titleObj = $this->getPageTitle();
# Last link
if ( !$rev->userCan( RevisionRecord::DELETED_TEXT, $this->getUser() ) ) {
if ( !RevisionRecord::userCanBitfield(
$rev->getVisibility(),
RevisionRecord::DELETED_TEXT,
$this->getUser()
) ) {
$pageLink = htmlspecialchars( $this->getLanguage()->userTimeAndDate( $ts, $user ) );
$last = $this->msg( 'diff' )->escaped();
} elseif ( $remaining > 0 || ( $earliestLiveTime && $ts > $earliestLiveTime ) ) {
@ -1104,7 +1112,11 @@ class SpecialUndelete extends SpecialPage {
$user = $this->getUser();
$time = $this->getLanguage()->userTimeAndDate( $ts, $user );
if ( !$rev->userCan( RevisionRecord::DELETED_TEXT, $user ) ) {
if ( !RevisionRecord::userCanBitfield(
$rev->getVisibility(),
RevisionRecord::DELETED_TEXT,
$user
) ) {
return '<span class="history-deleted">' . $time . '</span>';
}

View file

@ -611,7 +611,13 @@ class ContribsPager extends RangeChronologicalPager {
}
}
# Is there a visible previous revision?
if ( $rev->userCan( RevisionRecord::DELETED_TEXT, $user ) && $rev->getParentId() !== 0 ) {
if ( $rev->getParentId() !== 0 &&
RevisionRecord::userCanBitfield(
$rev->getVisibility(),
RevisionRecord::DELETED_TEXT,
$user
)
) {
$difftext = $linkRenderer->makeKnownLink(
$page,
new HtmlArmor( $this->messages['diff'] ),

View file

@ -344,7 +344,11 @@ class DeletedContribsPager extends IndexPager {
$date = $this->getLanguage()->userTimeAndDate( $rev->getTimestamp(), $user );
if ( !$permissionManager->userHasRight( $user, 'undelete' ) ||
!$rev->userCan( RevisionRecord::DELETED_TEXT, $user )
!RevisionRecord::userCanBitfield(
$rev->getVisibility(),
RevisionRecord::DELETED_TEXT,
$user
)
) {
$link = htmlspecialchars( $date ); // unusable link
} else {

View file

@ -1589,6 +1589,8 @@ abstract class RevisionDbTestBase extends MediaWikiTestCase {
* @covers Revision::userCan
*/
public function testUserCan( $bitField, $field, $userGroups, $expected ) {
$this->hideDeprecated( 'Revision::userCan' );
$this->setGroupPermissions(
[
'sysop' => [