| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\Notifications\Api; |
| 4 |
|
| 5 |
use MediaWiki\Api\ApiBase; |
| 6 |
use MediaWiki\Api\ApiMain; |
| 7 |
use MediaWiki\Extension\Notifications\Model\Event; |
| 8 |
use MediaWiki\ParamValidator\TypeDef\TitleDef; |
| 9 |
use MediaWiki\ParamValidator\TypeDef\UserDef; |
| 10 |
use MediaWiki\Title\Title; |
| 11 |
use MediaWiki\User\UserIdentity; |
| 12 |
use MediaWiki\User\UserNameUtils; |
| 13 |
use Wikimedia\ParamValidator\ParamValidator; |
| 14 |
use Wikimedia\ParamValidator\TypeDef\StringDef; |
| 15 |
|
| 16 |
class ApiEchoCreateEvent extends ApiBase { |
| 17 |
|
| 18 |
private UserNameUtils $userNameUtils; |
| 19 |
|
| 20 |
public function __construct( |
| 21 |
ApiMain $mainModule, |
| 22 |
$moduleName, |
| 23 |
UserNameUtils $userNameUtils |
| 24 |
) { |
| 25 |
parent::__construct( $mainModule, $moduleName ); |
| 26 |
$this->userNameUtils = $userNameUtils; |
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public function execute() { |
| 34 |
if ( !$this->getConfig()->get( 'EchoEnableApiEvents' ) ) { |
| 35 |
$this->dieWithError( [ 'apierror-moduledisabled', $this->getModuleName() ] ); |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
$user = $this->getUser(); |
| 40 |
if ( !$user->isNamed() ) { |
| 41 |
$this->dieWithError( 'apierror-mustbeloggedin-generic', 'login-required' ); |
| 42 |
} |
| 43 |
|
| 44 |
$params = $this->extractRequestParams(); |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
$userToNotify = $params['user'] ?? $user; |
| 49 |
|
| 50 |
if ( $userToNotify->getName() !== $user->getName() ) { |
| 51 |
$this->checkUserRightsAny( 'echo-create' ); |
| 52 |
} |
| 53 |
|
| 54 |
if ( !$userToNotify->isRegistered() || $this->userNameUtils->isTemp( $userToNotify->getName() ) ) { |
| 55 |
$this->dieWithError( [ 'nosuchusershort', $userToNotify->getName() ] ); |
| 56 |
} |
| 57 |
|
| 58 |
$event = Event::create( [ |
| 59 |
|
| 60 |
'type' => 'api-' . $params['section'], |
| 61 |
'agent' => $user, |
| 62 |
'title' => $params['page'] ? Title::newFromLinkTarget( $params['page'] ) : null, |
| 63 |
'extra' => [ |
| 64 |
'recipients' => [ $userToNotify->getId() ], |
| 65 |
'header' => $params['header'], |
| 66 |
'content' => $params['content'], |
| 67 |
|
| 68 |
'noemail' => !$params['email'], |
| 69 |
] |
| 70 |
] ); |
| 71 |
|
| 72 |
|
| 73 |
$this->getResult()->addValue( |
| 74 |
null, |
| 75 |
$this->getModuleName(), |
| 76 |
[ |
| 77 |
'result' => 'success' |
| 78 |
] |
| 79 |
); |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
public function mustBePosted() { |
| 87 |
return true; |
| 88 |
} |
| 89 |
|
| 90 |
public function isWriteMode() { |
| 91 |
return true; |
| 92 |
} |
| 93 |
|
| 94 |
public function needsToken() { |
| 95 |
return 'csrf'; |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
public function getAllowedParams() { |
| 103 |
return [ |
| 104 |
'user' => [ |
| 105 |
ParamValidator::PARAM_TYPE => 'user', |
| 106 |
UserDef::PARAM_ALLOWED_USER_TYPES => [ 'name', 'id' ], |
| 107 |
UserDef::PARAM_RETURN_OBJECT => true, |
| 108 |
], |
| 109 |
'header' => [ |
| 110 |
ParamValidator::PARAM_REQUIRED => true, |
| 111 |
ParamValidator::PARAM_TYPE => 'string', |
| 112 |
StringDef::PARAM_MAX_BYTES => 160, |
| 113 |
], |
| 114 |
'content' => [ |
| 115 |
ParamValidator::PARAM_REQUIRED => true, |
| 116 |
ParamValidator::PARAM_TYPE => 'string', |
| 117 |
StringDef::PARAM_MAX_BYTES => 5000, |
| 118 |
], |
| 119 |
'page' => [ |
| 120 |
ParamValidator::PARAM_TYPE => 'title', |
| 121 |
TitleDef::PARAM_RETURN_OBJECT => true, |
| 122 |
], |
| 123 |
'section' => [ |
| 124 |
ParamValidator::PARAM_REQUIRED => true, |
| 125 |
ParamValidator::PARAM_TYPE => [ 'alert', 'notice' ], |
| 126 |
ParamValidator::PARAM_DEFAULT => 'notice', |
| 127 |
], |
| 128 |
'email' => [ |
| 129 |
ParamValidator::PARAM_TYPE => 'boolean', |
| 130 |
ParamValidator::PARAM_DEFAULT => false, |
| 131 |
], |
| 132 |
]; |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
protected function getExamplesMessages() { |
| 140 |
return [ |
| 141 |
'action=echocreateevent&header=Hi&content=From_API' => 'apihelp-echocreateevent-example', |
| 142 |
]; |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
public function getHelpUrls() { |
| 150 |
return 'https://www.mediawiki.org/wiki/Special:MyLanguage/Echo_(Notifications)/API'; |
| 151 |
} |
| 152 |
} |