MediaWiki  master
EditFilterMergedContentHookConstraint.php
Go to the documentation of this file.
1 <?php
22 
23 use Content;
24 use IContextSource;
27 use Status;
28 use StatusValue;
29 
38 
40  private $hookRunner;
41 
43  private $content;
44 
46  private $context;
47 
49  private $summary;
50 
52  private $minorEdit;
53 
55  private $status;
56 
58  private $hookError;
59 
67  public function __construct(
68  HookContainer $hookContainer,
71  string $summary,
72  bool $minorEdit
73  ) {
74  $this->hookRunner = new HookRunner( $hookContainer );
75  $this->content = $content;
76  $this->context = $context;
77  $this->summary = $summary;
78  $this->minorEdit = $minorEdit;
79  $this->status = Status::newGood();
80  $this->hookError = '';
81  }
82 
83  public function checkConstraint(): string {
84  $hookResult = $this->hookRunner->onEditFilterMergedContent(
85  $this->context,
86  $this->content,
87  $this->status,
88  $this->summary,
89  $this->context->getUser(),
90  $this->minorEdit
91  );
92  if ( !$hookResult ) {
93  // Error messages etc. could be handled within the hook...
94  if ( $this->status->isGood() ) {
95  $this->status->fatal( 'hookaborted' );
96  // Not setting $this->hookError here is a hack to allow the hook
97  // to cause a return to the edit page without $this->hookError
98  // being set. This is used by ConfirmEdit to display a captcha
99  // without any error message cruft.
100  } else {
101  $this->hookError = $this->formatStatusErrors( $this->status );
102  }
103  // Use the existing $status->value if the hook set it
104  if ( !$this->status->value ) {
105  // T273354: Should be AS_HOOK_ERROR_EXPECTED to display error message
106  $this->status->value = self::AS_HOOK_ERROR_EXPECTED;
107  }
108  return self::CONSTRAINT_FAILED;
109  }
110 
111  if ( !$this->status->isOK() ) {
112  // ...or the hook could be expecting us to produce an error
113  // FIXME this sucks, we should just use the Status object throughout
114  if ( !$this->status->getErrors() ) {
115  // Provide a fallback error message if none was set
116  $this->status->fatal( 'hookaborted' );
117  }
118  $this->hookError = $this->formatStatusErrors( $this->status );
119  $this->status->value = self::AS_HOOK_ERROR_EXPECTED;
120  return self::CONSTRAINT_FAILED;
121  }
122 
123  return self::CONSTRAINT_PASSED;
124  }
125 
126  public function getLegacyStatus(): StatusValue {
127  // This returns a Status instead of a StatusValue since a Status object is
128  // used in the hook
129  return $this->status;
130  }
131 
141  public function getHookError(): string {
142  return $this->hookError;
143  }
144 
150  private function formatStatusErrors( Status $status ): string {
151  $errmsg = $status->getWikiText(
152  'edit-error-short',
153  'edit-error-long',
154  $this->context->getLanguage()
155  );
156  return <<<ERROR
157 <div class="errorbox">
158 {$errmsg}
159 </div>
160 <br clear="all" />
161 ERROR;
162  }
163 
164 }
MediaWiki\EditPage\Constraint\EditFilterMergedContentHookConstraint\__construct
__construct(HookContainer $hookContainer, Content $content, IContextSource $context, string $summary, bool $minorEdit)
Definition: EditFilterMergedContentHookConstraint.php:67
StatusValue
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition: StatusValue.php:43
MediaWiki\EditPage\Constraint\EditFilterMergedContentHookConstraint\$hookError
string $hookError
Definition: EditFilterMergedContentHookConstraint.php:58
MediaWiki\EditPage\Constraint\EditFilterMergedContentHookConstraint\$status
Status $status
Definition: EditFilterMergedContentHookConstraint.php:55
MediaWiki\EditPage\Constraint\EditFilterMergedContentHookConstraint\formatStatusErrors
formatStatusErrors(Status $status)
Wrap status errors in an errorbox for increased visibility.
Definition: EditFilterMergedContentHookConstraint.php:150
MediaWiki\EditPage\Constraint\EditFilterMergedContentHookConstraint\checkConstraint
checkConstraint()
Definition: EditFilterMergedContentHookConstraint.php:83
MediaWiki\EditPage\Constraint\EditFilterMergedContentHookConstraint\$context
IContextSource $context
Definition: EditFilterMergedContentHookConstraint.php:46
MediaWiki\EditPage\Constraint\EditFilterMergedContentHookConstraint
Verify EditFilterMergedContent hook.
Definition: EditFilterMergedContentHookConstraint.php:37
Status
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition: Status.php:44
Status\getWikiText
getWikiText( $shortContext=false, $longContext=false, $lang=null)
Get the error list as a wikitext formatted list.
Definition: Status.php:189
MediaWiki\EditPage\Constraint\EditFilterMergedContentHookConstraint\$minorEdit
bool $minorEdit
Definition: EditFilterMergedContentHookConstraint.php:52
MediaWiki\EditPage\Constraint\EditFilterMergedContentHookConstraint\getLegacyStatus
getLegacyStatus()
Get the legacy status for failure (or success)
Definition: EditFilterMergedContentHookConstraint.php:126
MediaWiki\EditPage\Constraint\EditFilterMergedContentHookConstraint\$content
Content $content
Definition: EditFilterMergedContentHookConstraint.php:43
StatusValue\newGood
static newGood( $value=null)
Factory function for good results.
Definition: StatusValue.php:82
IContextSource
Interface for objects which can provide a MediaWiki context on request.
Definition: IContextSource.php:58
Content
Base interface for content objects.
Definition: Content.php:35
MediaWiki\EditPage\Constraint
Definition: AccidentalRecreationConstraint.php:21
MediaWiki\HookContainer\HookContainer
HookContainer class.
Definition: HookContainer.php:45
MediaWiki\HookContainer\HookRunner
This class provides an implementation of the core hook interfaces, forwarding hook calls to HookConta...
Definition: HookRunner.php:557
MediaWiki\EditPage\IEditObject\AS_HOOK_ERROR_EXPECTED
const AS_HOOK_ERROR_EXPECTED
Status: A hook function returned an error.
Definition: IEditObject.php:41
MediaWiki\EditPage\Constraint\EditFilterMergedContentHookConstraint\$hookRunner
HookRunner $hookRunner
Definition: EditFilterMergedContentHookConstraint.php:40
MediaWiki\EditPage\Constraint\EditFilterMergedContentHookConstraint\$summary
string $summary
Definition: EditFilterMergedContentHookConstraint.php:49
MediaWiki\EditPage\Constraint\IEditConstraint
Interface for all constraints that can prevent edits.
Definition: IEditConstraint.php:33
MediaWiki\EditPage\Constraint\EditFilterMergedContentHookConstraint\getHookError
getHookError()
TODO this is really ugly.
Definition: EditFilterMergedContentHookConstraint.php:141