| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\EditPage\Constraint; |
| 22 |
|
| 23 |
use MediaWiki\Permissions\Authority; |
| 24 |
use MediaWiki\Title\Title; |
| 25 |
use StatusValue; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
class ContentModelChangeConstraint implements IEditConstraint { |
| 37 |
|
| 38 |
private Authority $performer; |
| 39 |
private Title $title; |
| 40 |
private string $newContentModel; |
| 41 |
private string $result; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public function __construct( |
| 49 |
Authority $performer, |
| 50 |
Title $title, |
| 51 |
string $newContentModel |
| 52 |
) { |
| 53 |
$this->performer = $performer; |
| 54 |
$this->title = $title; |
| 55 |
$this->newContentModel = $newContentModel; |
| 56 |
} |
| 57 |
|
| 58 |
public function checkConstraint(): string { |
| 59 |
if ( $this->newContentModel === $this->title->getContentModel() ) { |
| 60 |
|
| 61 |
$this->result = self::CONSTRAINT_PASSED; |
| 62 |
return self::CONSTRAINT_PASSED; |
| 63 |
} |
| 64 |
|
| 65 |
if ( !$this->performer->isAllowed( 'editcontentmodel' ) ) { |
| 66 |
$this->result = self::CONSTRAINT_FAILED; |
| 67 |
return self::CONSTRAINT_FAILED; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
$titleWithNewContentModel = clone $this->title; |
| 72 |
$titleWithNewContentModel->setContentModel( $this->newContentModel ); |
| 73 |
|
| 74 |
$canEditModel = $this->performer->authorizeWrite( |
| 75 |
'editcontentmodel', |
| 76 |
$titleWithNewContentModel |
| 77 |
); |
| 78 |
|
| 79 |
if ( |
| 80 |
!$canEditModel |
| 81 |
|| !$this->performer->authorizeWrite( 'edit', $titleWithNewContentModel ) |
| 82 |
) { |
| 83 |
$this->result = self::CONSTRAINT_FAILED; |
| 84 |
return self::CONSTRAINT_FAILED; |
| 85 |
} |
| 86 |
|
| 87 |
$this->result = self::CONSTRAINT_PASSED; |
| 88 |
return self::CONSTRAINT_PASSED; |
| 89 |
} |
| 90 |
|
| 91 |
public function getLegacyStatus(): StatusValue { |
| 92 |
$statusValue = StatusValue::newGood(); |
| 93 |
|
| 94 |
if ( $this->result === self::CONSTRAINT_FAILED ) { |
| 95 |
$statusValue->setResult( false, self::AS_NO_CHANGE_CONTENT_MODEL ); |
| 96 |
} |
| 97 |
|
| 98 |
return $statusValue; |
| 99 |
} |
| 100 |
|
| 101 |
} |