Make WikiPage a ProperPageIdentity

Bug: T272424
Change-Id: I1dbcf1192390cbdfd6faaa60f9831f62ec1eff0d
This commit is contained in:
Daimona Eaytoy 2021-08-30 14:42:57 +02:00
parent fac3b1bde1
commit 6a3b9daf29
8 changed files with 13 additions and 87 deletions

View file

@ -413,6 +413,9 @@ because of Phabricator reports.
* Content::preloadTransform was hard-deprecated since 1.37,
use ContentTransformer::preloadTransform instead. Extensions defining
a content model should override ContentHandler::preloadTransform.
* Constructing WikiPage objects from Title instances that cannot exist,
hard-deprecated in 1.36, now throws an exception. Additionally, WikiPage
now implements ProperPageIdentity, rather than just PageIdentity.
* Content::preSaveTransform was hard-deprecated since 1.37,
use ContentTransformer::preSaveTransform instead. Extensions defining
a content model should override ContentHandler::preSaveTransform.

View file

@ -42,7 +42,6 @@ use ParserCache;
use Psr\Log\LoggerInterface;
use TitleFormatter;
use WANObjectCache;
use Wikimedia\Assert\Assert;
use Wikimedia\Rdbms\ILBFactory;
use WikiPage;
@ -258,11 +257,6 @@ class PageUpdaterFactory {
UserIdentity $user,
DerivedPageDataUpdater $derivedPageDataUpdater
): PageUpdater {
Assert::precondition(
$page->canExist(),
'The WikiPage instance does not represent a proper page!'
);
$pageUpdater = new PageUpdater(
$user,
$page, // NOTE: eventually, PageUpdater should not know about WikiPage

View file

@ -2,7 +2,6 @@
namespace MediaWiki\Page;
use BadMethodCallException;
use CommentStore;
use Content;
use DeferrableUpdate;
@ -63,14 +62,10 @@ class DeletePage {
private $deleter;
/**
* @param PageIdentity $page
* @param ProperPageIdentity $page
* @param UserIdentity $deleter
* @todo Should use ProperPageIdentity eventually, once WikiPage will extend that
*/
public function __construct( PageIdentity $page, UserIdentity $deleter ) {
if ( !$page->canExist() ) {
throw new BadMethodCallException( __METHOD__ . ' requires a proper page identity.' );
}
public function __construct( ProperPageIdentity $page, UserIdentity $deleter ) {
$services = MediaWikiServices::getInstance();
$this->hookRunner = new HookRunner( $services->getHookContainer() );
$this->revisionStore = $services->getRevisionStore();

View file

@ -12,12 +12,12 @@ namespace MediaWiki\Page;
*
* @since 1.36
*/
interface ExistingPageRecord extends PageRecord, ProperPageIdentity {
interface ExistingPageRecord extends PageRecord {
/**
* Always true.
*
* @return bool
* @return true
*/
public function exists(): bool;
}

View file

@ -18,14 +18,11 @@ namespace MediaWiki\Page;
* ExistingPageRecord instead. Once WikiPage is removed or guaranteed to be immutable and
* existing, ExistingPageRecord will become an alias of PageRecord.
*
* @todo In the future, PageRecord should extend ProperPageIdentity. This will only
* become possible when WikiPage can no longer represent non-proper pages.
*
* @stable to type
*
* @since 1.36
*/
interface PageRecord extends PageIdentity {
interface PageRecord extends ProperPageIdentity {
/**
* False if the page has had more than one edit.

View file

@ -173,13 +173,7 @@ class WikiPage implements Page, IDBAccessObject, PageRecord {
// TODO: remove the need for casting to Title.
$title = Title::castFromPageIdentity( $pageIdentity );
if ( !$title->canExist() ) {
// TODO: In order to allow WikiPage to implement ProperPageIdentity,
// throw here to prevent construction of a WikiPage that doesn't
// represent a proper page.
wfDeprecatedMsg(
"WikiPage constructed on a Title that cannot exist as a page: $title",
'1.36'
);
throw new InvalidArgumentException( "WikiPage constructed on a Title that cannot exist as a page: $title" );
}
$this->mTitle = $title;
@ -471,18 +465,6 @@ class WikiPage implements Page, IDBAccessObject, PageRecord {
* @return void
*/
public function loadPageData( $from = 'fromdb' ) {
if ( !$this->mTitle->canExist() ) {
// NOTE: If and when WikiPage implements PageIdentity but not yet ProperPageIdentity,
// throw here to prevent usage of a WikiPage that doesn't
// represent a proper page.
// NOTE: The constructor will already have triggered a warning, but seeing how
// bad instances of WikiPage are used will be helpful.
wfDeprecatedMsg(
"Accessing WikiPage that cannot exist as a page: {$this->mTitle}. ",
'1.36'
);
}
$from = self::convertSelectType( $from );
if ( is_int( $from ) && $from <= $this->mDataLoadedFrom ) {
// We already have the data from the correct location, no need to load it twice.
@ -595,24 +577,6 @@ class WikiPage implements Page, IDBAccessObject, PageRecord {
$this->mDataLoadedFrom = self::convertSelectType( $from );
}
/**
* Code that requires this WikiPage to be a "proper page" in the sense
* defined by PageIdentity should call this method.
*
* @note In the future, this method should become redundant, as the
* constructor should not allow a WikiPage to be constructed for as title
* that does not represent a proper page. For the time being, we allow
* such instances for backwards compatibility.
*
* @throws PreconditionException
*/
private function assertProperPage() {
Assert::precondition(
$this->mTitle->canExist(),
'This WikiPage instance does not represent a proper page!'
);
}
/**
* @param string|false $wikiId
*
@ -620,7 +584,6 @@ class WikiPage implements Page, IDBAccessObject, PageRecord {
*/
public function getId( $wikiId = self::LOCAL ): int {
$this->assertWiki( $wikiId );
$this->assertProperPage();
if ( !$this->mDataLoaded ) {
$this->loadPageData();
@ -805,18 +768,6 @@ class WikiPage implements Page, IDBAccessObject, PageRecord {
return; // already loaded
}
if ( !$this->mTitle->canExist() ) {
// NOTE: If and when WikiPage implements PageIdentity but not yet ProperPageIdentity,
// throw here to prevent usage of a WikiPage that doesn't
// represent a proper page.
// NOTE: The constructor will already have triggered a warning, but seeing how
// bad instances of WikiPage are used will be helpful.
wfDeprecatedMsg(
"Accessing WikiPage that cannot exist as a page: {$this->mTitle}. ",
'1.36'
);
}
$latest = $this->getLatest();
if ( !$latest ) {
return; // page doesn't exist or is missing page_latest info
@ -1410,8 +1361,6 @@ class WikiPage implements Page, IDBAccessObject, PageRecord {
* page ID is already in use.
*/
public function insertOn( $dbw, $pageId = null ) {
$this->assertProperPage();
$pageIdForInsert = $pageId ? [ 'page_id' => $pageId ] : [];
$dbw->insert(
'page',
@ -2270,8 +2219,6 @@ class WikiPage implements Page, IDBAccessObject, PageRecord {
) {
global $wgCascadingRestrictionLevels;
$this->assertProperPage();
if ( wfReadOnly() ) {
return Status::newFatal( wfMessage( 'readonlytext', wfReadOnlyReason() ) );
}
@ -3336,8 +3283,7 @@ class WikiPage implements Page, IDBAccessObject, PageRecord {
* @since 1.36
*/
public function canExist(): bool {
// NOTE: once WikiPage becomes a ProperPageIdentity, this should always return true
return $this->mTitle->canExist();
return true;
}
/**

View file

@ -56,6 +56,7 @@ class WikiPageFactory {
}
if ( !$pageIdentity->canExist() ) {
// BC with the Title class
throw new InvalidArgumentException(
"The given PageIdentity does not represent a proper page"
);

View file

@ -146,18 +146,8 @@ class WikiPageDbTest extends MediaWikiLangTestCase {
*/
public function testConstructionWithPageThatCannotExist( $ns, $text ) {
$title = Title::makeTitle( $ns, $text );
// NOTE: once WikiPage becomes a ProperPageIdentity, the constructor should throw!
$this->filterDeprecated( '/WikiPage constructed on a Title that cannot exist as a page/' );
$this->filterDeprecated( '/Accessing WikiPage that cannot exist as a page/' );
$page = new WikiPage( $title );
$this->assertFalse( $page->canExist() );
$this->assertFalse( $page->exists() );
$this->assertNull( $page->getRevisionRecord() );
$this->expectException( RuntimeException::class );
$page->getId();
$this->expectException( InvalidArgumentException::class );
new WikiPage( $title );
}
/**