MediaWiki master
ContentModelChangeConstraint.php
Go to the documentation of this file.
1<?php
22
26use StatusValue;
27
39
40 private PermissionStatus $status;
41
42 private Authority $performer;
43 private Title $title;
44 private string $newContentModel;
45
51 public function __construct(
52 Authority $performer,
53 Title $title,
54 string $newContentModel
55 ) {
56 $this->performer = $performer;
57 $this->title = $title;
58 $this->newContentModel = $newContentModel;
59 }
60
61 public function checkConstraint(): string {
62 $this->status = PermissionStatus::newEmpty();
63
64 if ( $this->newContentModel === $this->title->getContentModel() ) {
65 // No change
66 return self::CONSTRAINT_PASSED;
67 }
68
69 if ( !$this->performer->authorizeWrite( 'editcontentmodel', $this->title, $this->status ) ) {
70 return self::CONSTRAINT_FAILED;
71 }
72
73 // Make sure the user can edit the page under the new content model too.
74 // We rely on caching in UserAuthority to avoid bumping the rate limit counter twice.
75 $titleWithNewContentModel = clone $this->title;
76 $titleWithNewContentModel->setContentModel( $this->newContentModel );
77 if (
78 !$this->performer->authorizeWrite( 'editcontentmodel', $titleWithNewContentModel, $this->status )
79 || !$this->performer->authorizeWrite( 'edit', $titleWithNewContentModel, $this->status )
80 ) {
81 return self::CONSTRAINT_FAILED;
82 }
83
84 return self::CONSTRAINT_PASSED;
85 }
86
87 public function getLegacyStatus(): StatusValue {
88 $statusValue = StatusValue::newGood();
89
90 if ( !$this->status->isGood() ) {
91 if ( $this->status->isRateLimitExceeded() ) {
92 $statusValue->setResult( false, self::AS_RATE_LIMITED );
93 } else {
94 $statusValue->setResult( false, self::AS_NO_CHANGE_CONTENT_MODEL );
95 }
96 }
97
98 // TODO: Use error messages from the PermissionStatus ($this->status) here - T384399
99 return $statusValue;
100 }
101
102}
Verify user permissions if changing content model: Must have editcontentmodel rights Must be able to ...
getLegacyStatus()
Get the legacy status for failure (or success)
__construct(Authority $performer, Title $title, string $newContentModel)
A StatusValue for permission errors.
Represents a title within MediaWiki.
Definition Title.php:78
Generic operation result class Has warning/error list, boolean status and arbitrary value.
setResult( $ok, $value=null)
Change operation result.
Interface for all constraints that can prevent edits.
This interface represents the authority associated with the current execution context,...
Definition Authority.php:37