MediaWiki  master
ContentModelChangeConstraint.php
Go to the documentation of this file.
1 <?php
22 
24 use StatusValue;
25 use Title;
26 
37 
39  private $performer;
40 
42  private $title;
43 
46 
48  private $result;
49 
55  public function __construct(
57  Title $title,
58  string $newContentModel
59  ) {
60  $this->performer = $performer;
61  $this->title = $title;
62  $this->newContentModel = $newContentModel;
63  }
64 
65  public function checkConstraint(): string {
66  if ( $this->newContentModel === $this->title->getContentModel() ) {
67  // No change
68  $this->result = self::CONSTRAINT_PASSED;
69  return self::CONSTRAINT_PASSED;
70  }
71 
72  if ( !$this->performer->isAllowed( 'editcontentmodel' ) ) {
73  $this->result = self::CONSTRAINT_FAILED;
74  return self::CONSTRAINT_FAILED;
75  }
76 
77  // Make sure the user can edit the page under the new content model too
78  $titleWithNewContentModel = clone $this->title;
79  $titleWithNewContentModel->setContentModel( $this->newContentModel );
80 
81  $canEditModel = $this->performer->authorizeWrite(
82  'editcontentmodel',
83  $titleWithNewContentModel
84  );
85 
86  if (
87  !$canEditModel
88  || !$this->performer->authorizeWrite( 'edit', $titleWithNewContentModel )
89  ) {
90  $this->result = self::CONSTRAINT_FAILED;
91  return self::CONSTRAINT_FAILED;
92  }
93 
94  $this->result = self::CONSTRAINT_PASSED;
95  return self::CONSTRAINT_PASSED;
96  }
97 
98  public function getLegacyStatus(): StatusValue {
99  $statusValue = StatusValue::newGood();
100 
101  if ( $this->result === self::CONSTRAINT_FAILED ) {
102  $statusValue->setResult( false, self::AS_NO_CHANGE_CONTENT_MODEL );
103  }
104 
105  return $statusValue;
106  }
107 
108 }
StatusValue
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition: StatusValue.php:43
MediaWiki\EditPage\Constraint\ContentModelChangeConstraint
Verify user permissions if changing content model: Must have editcontentmodel rights Must be able to ...
Definition: ContentModelChangeConstraint.php:36
MediaWiki\EditPage\Constraint\ContentModelChangeConstraint\$performer
Authority $performer
Definition: ContentModelChangeConstraint.php:39
MediaWiki\EditPage\Constraint\ContentModelChangeConstraint\$newContentModel
string $newContentModel
Definition: ContentModelChangeConstraint.php:45
MediaWiki\EditPage\Constraint\ContentModelChangeConstraint\__construct
__construct(Authority $performer, Title $title, string $newContentModel)
Definition: ContentModelChangeConstraint.php:55
MediaWiki\EditPage\Constraint\ContentModelChangeConstraint\$result
string null $result
Definition: ContentModelChangeConstraint.php:48
MediaWiki\Permissions\Authority
This interface represents the authority associated the current execution context, such as a web reque...
Definition: Authority.php:37
StatusValue\newGood
static newGood( $value=null)
Factory function for good results.
Definition: StatusValue.php:82
MediaWiki\EditPage\Constraint\ContentModelChangeConstraint\getLegacyStatus
getLegacyStatus()
Get the legacy status for failure (or success)
Definition: ContentModelChangeConstraint.php:98
Title
Represents a title within MediaWiki.
Definition: Title.php:48
MediaWiki\EditPage\Constraint\ContentModelChangeConstraint\checkConstraint
checkConstraint()
Definition: ContentModelChangeConstraint.php:65
MediaWiki\EditPage\Constraint
Definition: AccidentalRecreationConstraint.php:21
Title\setContentModel
setContentModel( $model)
Set a proposed content model for the page for permissions checking.
Definition: Title.php:1134
MediaWiki\EditPage\Constraint\ContentModelChangeConstraint\$title
Title $title
Definition: ContentModelChangeConstraint.php:42
MediaWiki\EditPage\Constraint\IEditConstraint
Interface for all constraints that can prevent edits.
Definition: IEditConstraint.php:33