Page Menu
Home
WickedGov Phorge
Search
Configure Global Search
Log In
Files
F7482890
ApiEchoCreateEvent.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
3 KB
Referenced Files
None
Subscribers
None
ApiEchoCreateEvent.php
View Options
<?php
namespace
MediaWiki\Extension\Notifications\Api
;
use
MediaWiki\Api\ApiBase
;
use
MediaWiki\Api\ApiMain
;
use
MediaWiki\Extension\Notifications\Model\Event
;
use
MediaWiki\Notification\RecipientSet
;
use
MediaWiki\ParamValidator\TypeDef\TitleDef
;
use
MediaWiki\ParamValidator\TypeDef\UserDef
;
use
MediaWiki\Title\Title
;
use
MediaWiki\User\UserIdentity
;
use
MediaWiki\User\UserNameUtils
;
use
Wikimedia\ParamValidator\ParamValidator
;
use
Wikimedia\ParamValidator\TypeDef\StringDef
;
class
ApiEchoCreateEvent
extends
ApiBase
{
public
function
__construct
(
ApiMain
$mainModule
,
string
$moduleName
,
private
readonly
UserNameUtils
$userNameUtils
,
)
{
parent
::
__construct
(
$mainModule
,
$moduleName
);
}
/**
* @see ApiBase::execute()
* @return void
*/
public
function
execute
()
{
if
(
!
$this
->
getConfig
()->
get
(
'EchoEnableApiEvents'
)
)
{
$this
->
dieWithError
(
[
'apierror-moduledisabled'
,
$this
->
getModuleName
()
]
);
}
// Only for logged in users
$user
=
$this
->
getUser
();
if
(
!
$user
->
isNamed
()
)
{
$this
->
dieWithError
(
'apierror-mustbeloggedin-generic'
,
'login-required'
);
}
$params
=
$this
->
extractRequestParams
();
// Default to self if unspecified
/** @var UserIdentity $userToNotify */
$userToNotify
=
$params
[
'user'
]
??
$user
;
if
(
$userToNotify
->
getName
()
!==
$user
->
getName
()
)
{
$this
->
checkUserRightsAny
(
'echo-create'
);
}
if
(
!
$userToNotify
->
isRegistered
()
||
$this
->
userNameUtils
->
isTemp
(
$userToNotify
->
getName
()
)
)
{
$this
->
dieWithError
(
[
'nosuchusershort'
,
$userToNotify
->
getName
()
]
);
}
$event
=
Event
::
create
(
[
// type is one of api-notice, api-alert
'type'
=>
'api-'
.
$params
[
'section'
],
'agent'
=>
$user
,
'title'
=>
$params
[
'page'
]
?
Title
::
newFromLinkTarget
(
$params
[
'page'
]
)
:
null
,
'extra'
=>
[
'header'
=>
$params
[
'header'
],
'content'
=>
$params
[
'content'
],
// Send email only if specified
'noemail'
=>
!
$params
[
'email'
],
],
],
new
RecipientSet
(
$userToNotify
)
);
// Return a success message
$this
->
getResult
()->
addValue
(
null
,
$this
->
getModuleName
(),
[
'result'
=>
'success'
,
]
);
}
/**
* @see ApiBase::needsToken()
* @return bool
*/
public
function
mustBePosted
()
{
return
true
;
}
/** @inheritDoc */
public
function
isWriteMode
()
{
return
true
;
}
/** @inheritDoc */
public
function
needsToken
()
{
return
'csrf'
;
}
/**
* @see ApiBase::getAllowedParams()
* @return array
*/
public
function
getAllowedParams
()
{
return
[
'user'
=>
[
ParamValidator
::
PARAM_TYPE
=>
'user'
,
UserDef
::
PARAM_ALLOWED_USER_TYPES
=>
[
'name'
,
'id'
],
UserDef
::
PARAM_RETURN_OBJECT
=>
true
,
],
'header'
=>
[
ParamValidator
::
PARAM_REQUIRED
=>
true
,
ParamValidator
::
PARAM_TYPE
=>
'string'
,
StringDef
::
PARAM_MAX_BYTES
=>
160
,
],
'content'
=>
[
ParamValidator
::
PARAM_REQUIRED
=>
true
,
ParamValidator
::
PARAM_TYPE
=>
'string'
,
StringDef
::
PARAM_MAX_BYTES
=>
5000
,
],
'page'
=>
[
ParamValidator
::
PARAM_TYPE
=>
'title'
,
TitleDef
::
PARAM_RETURN_OBJECT
=>
true
,
],
'section'
=>
[
ParamValidator
::
PARAM_REQUIRED
=>
true
,
ParamValidator
::
PARAM_TYPE
=>
[
'alert'
,
'notice'
],
ParamValidator
::
PARAM_DEFAULT
=>
'notice'
,
],
'email'
=>
[
ParamValidator
::
PARAM_TYPE
=>
'boolean'
,
ParamValidator
::
PARAM_DEFAULT
=>
false
,
],
];
}
/**
* @see ApiBase::getExamplesMessages()
* @return string[]
*/
protected
function
getExamplesMessages
()
{
return
[
'action=echocreateevent&header=Hi&content=From_API'
=>
'apihelp-echocreateevent-example'
,
];
}
/**
* @see ApiBase::getHelpUrls()
* @return string
*/
public
function
getHelpUrls
()
{
return
'https://www.mediawiki.org/wiki/Special:MyLanguage/Echo_(Notifications)/API'
;
}
}
File Metadata
Details
Attached
Mime Type
text/x-php
Expires
Mon, Nov 23, 13:24 (5 d, 17 h ago)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
10/99/0849bd333ffbf69700390602677a
Default Alt Text
ApiEchoCreateEvent.php (3 KB)
Attached To
Mode
rMWPROD MediaWiki Production
Attached
Detach File
Event Timeline
Log In to Comment