MediaWiki master
ContentModelChangeConstraint.php
Go to the documentation of this file.
1<?php
8
13
25
31 public function __construct(
32 private readonly Authority $performer,
33 private readonly Title $title,
34 private readonly string $newContentModel,
35 ) {
36 }
37
38 public function checkConstraint(): PageEditStatus {
39 if ( $this->newContentModel === $this->title->getContentModel() ) {
40 return PageEditStatus::newGood();
41 }
42
43 $status = PermissionStatus::newEmpty();
44 $perm = $this->title->exists() ? 'editcontentmodel' : 'createwithcontentmodel';
45
46 if ( !$this->performer->authorizeWrite( $perm, $this->title, $status ) ) {
47 return $this->castPermissionStatus( $status );
48 }
49
50 // Make sure the user can edit the page under the new content model too.
51 // We rely on caching in UserAuthority to avoid bumping the rate limit counter twice.
52 $titleWithNewContentModel = clone $this->title;
53 $titleWithNewContentModel->setContentModel( $this->newContentModel );
54 if (
55 !$this->performer->authorizeWrite( $perm, $titleWithNewContentModel, $status )
56 || !$this->performer->authorizeWrite( 'edit', $titleWithNewContentModel, $status )
57 ) {
58 return $this->castPermissionStatus( $status );
59 }
60
61 return PageEditStatus::newGood();
62 }
63
64 private function castPermissionStatus( PermissionStatus $status ): PageEditStatus {
65 $statusValue = PageEditStatus::newGood();
66
67 if ( !$status->isGood() ) {
68 if ( $status->isRateLimitExceeded() ) {
69 $statusValue->merge( $status );
70 $statusValue->setResult( false, self::AS_RATE_LIMITED );
71 } else {
72 $statusValue
73 ->setResult( false, self::AS_NO_CHANGE_CONTENT_MODEL )
74 ->setErrorFunction( $status->throwErrorPageError( ... ) );
75 }
76 }
77
78 return $statusValue;
79 }
80
81}
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:71
Verify user permissions if changing content model: Must have editcontentmodel rights Must be able to ...
__construct(private readonly Authority $performer, private readonly Title $title, private readonly string $newContentModel,)
Abstract class for all constraints that can prevent edits.
Status returned by edit constraints and other page editing checks.
A StatusValue for permission errors.
Represents a title within MediaWiki.
Definition Title.php:69
This interface represents the authority associated with the current execution context,...
Definition Authority.php:23