MediaWiki master
EditFilterMergedContentHookConstraint.php
Go to the documentation of this file.
1<?php
8
17
26
27 private readonly HookRunner $hookRunner;
28
38 public function __construct(
39 HookContainer $hookContainer,
40 private readonly UserFactory $userFactory,
41 private readonly Content $content,
42 private readonly IContextSource $hookContext,
43 private readonly string $summary,
44 private readonly bool $minorEdit,
45 private readonly UserIdentity $hookUser,
46 ) {
47 $this->hookRunner = new HookRunner( $hookContainer );
48 }
49
50 public function checkConstraint(): PageEditStatus {
51 $status = PageEditStatus::newGood();
52
53 $hookResult = $this->hookRunner->onEditFilterMergedContent(
54 $this->hookContext,
55 $this->content,
56 // Status::wrap() takes references to all internal variables, allowing hook handlers to modify
57 // the $status, without changing the hook interface to use the PageEditStatus type.
58 Status::wrap( $status ),
59 $this->summary,
60 $this->userFactory->newFromUserIdentity( $this->hookUser ),
61 $this->minorEdit
62 );
63 if ( !$hookResult ) {
64 // Error messages etc. could be handled within the hook...
65 if ( $status->isGood() ) {
66 // Not setting a status message here is a hack to allow the hook
67 // to cause a return to the edit page without an error being
68 // displayed.
69 $status->setOK( false );
70 } else {
71 if ( !$status->getMessages() ) {
72 // Provide a fallback error message if none was set
73 $status->fatal( 'hookaborted' );
74 }
75 }
76 // Use the existing $status->value if the hook set it
77 if ( !$status->value ) {
78 // T273354: Should be AS_HOOK_ERROR_EXPECTED to display error message
79 $status->value = self::AS_HOOK_ERROR_EXPECTED;
80 }
81 return $status;
82 }
83
84 if ( !$status->isOK() ) {
85 // ...or the hook could be expecting us to produce an error
86 // FIXME this sucks, we should just use the Status object throughout
87 if ( !$status->getMessages() ) {
88 // Provide a fallback error message if none was set
89 $status->fatal( 'hookaborted' );
90 }
91 $status->value = self::AS_HOOK_ERROR_EXPECTED;
92 return $status;
93 }
94
95 return PageEditStatus::newGood();
96 }
97}
Abstract class for all constraints that can prevent edits.
__construct(HookContainer $hookContainer, private readonly UserFactory $userFactory, private readonly Content $content, private readonly IContextSource $hookContext, private readonly string $summary, private readonly bool $minorEdit, private readonly UserIdentity $hookUser,)
This class provides an implementation of the core hook interfaces, forwarding hook calls to HookConta...
Status returned by edit constraints and other page editing checks.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:44
Create User objects.
setOK( $ok)
Change operation status.
Content objects represent page content, e.g.
Definition Content.php:28
Interface for objects which can provide a MediaWiki context on request.
const AS_HOOK_ERROR_EXPECTED
Status: A hook function returned an error.
Interface for objects representing user identity.