MediaWiki  master
DefaultTextConstraint.php
Go to the documentation of this file.
1 <?php
22 
23 use StatusValue;
24 use Title;
25 
36 
38  private $title;
39 
41  private $allowBlank;
42 
45 
47  private $result;
48 
54  public function __construct(
55  Title $title,
56  bool $allowBlank,
57  string $userProvidedText
58  ) {
59  $this->title = $title;
60  $this->allowBlank = $allowBlank;
61  $this->userProvidedText = $userProvidedText;
62  }
63 
64  public function checkConstraint(): string {
65  $defaultMessageText = $this->title->getDefaultMessageText();
66  if ( $this->title->getNamespace() === NS_MEDIAWIKI && $defaultMessageText !== false ) {
67  $defaultText = $defaultMessageText;
68  } else {
69  $defaultText = '';
70  }
71 
72  if ( !$this->allowBlank && $this->userProvidedText === $defaultText ) {
73  $this->result = self::CONSTRAINT_FAILED;
74  } else {
75  $this->result = self::CONSTRAINT_PASSED;
76  }
77  return $this->result;
78  }
79 
80  public function getLegacyStatus(): StatusValue {
81  $statusValue = StatusValue::newGood();
82  if ( $this->result === self::CONSTRAINT_FAILED ) {
83  $statusValue->fatal( 'blankarticle' );
84  $statusValue->setResult( false, self::AS_BLANK_ARTICLE );
85  }
86  return $statusValue;
87  }
88 
89 }
MediaWiki\EditPage\Constraint\DefaultTextConstraint\$userProvidedText
string $userProvidedText
Definition: DefaultTextConstraint.php:44
StatusValue
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition: StatusValue.php:43
NS_MEDIAWIKI
const NS_MEDIAWIKI
Definition: Defines.php:72
MediaWiki\EditPage\Constraint\DefaultTextConstraint\checkConstraint
checkConstraint()
Definition: DefaultTextConstraint.php:64
MediaWiki\EditPage\Constraint\DefaultTextConstraint
Don't save a new page if it's blank or if it's a MediaWiki: message with content equivalent to defaul...
Definition: DefaultTextConstraint.php:35
MediaWiki\EditPage\Constraint\DefaultTextConstraint\$allowBlank
bool $allowBlank
Definition: DefaultTextConstraint.php:41
MediaWiki\EditPage\Constraint\DefaultTextConstraint\$title
Title $title
Definition: DefaultTextConstraint.php:38
StatusValue\newGood
static newGood( $value=null)
Factory function for good results.
Definition: StatusValue.php:82
MediaWiki\EditPage\Constraint\DefaultTextConstraint\$result
string null $result
Definition: DefaultTextConstraint.php:47
MediaWiki\EditPage\Constraint\DefaultTextConstraint\getLegacyStatus
getLegacyStatus()
Get the legacy status for failure (or success)
Definition: DefaultTextConstraint.php:80
Title
Represents a title within MediaWiki.
Definition: Title.php:47
MediaWiki\EditPage\Constraint\DefaultTextConstraint\__construct
__construct(Title $title, bool $allowBlank, string $userProvidedText)
Definition: DefaultTextConstraint.php:54
MediaWiki\EditPage\Constraint
Definition: AccidentalRecreationConstraint.php:21
MediaWiki\EditPage\Constraint\IEditConstraint
Interface for all constraints that can prevent edits.
Definition: IEditConstraint.php:33