| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\Thanks\Api; |
| 4 |
|
| 5 |
use DatabaseLogEntry; |
| 6 |
use LogEntry; |
| 7 |
use MediaWiki\Api\ApiBase; |
| 8 |
use MediaWiki\Api\ApiMain; |
| 9 |
use MediaWiki\Extension\Notifications\DiscussionParser; |
| 10 |
use MediaWiki\Extension\Notifications\Model\Event; |
| 11 |
use MediaWiki\Extension\Thanks\Storage\Exceptions\InvalidLogType; |
| 12 |
use MediaWiki\Extension\Thanks\Storage\Exceptions\LogDeleted; |
| 13 |
use MediaWiki\Extension\Thanks\Storage\LogStore; |
| 14 |
use MediaWiki\Permissions\PermissionManager; |
| 15 |
use MediaWiki\Revision\RevisionRecord; |
| 16 |
use MediaWiki\Revision\RevisionStore; |
| 17 |
use MediaWiki\Title\Title; |
| 18 |
use MediaWiki\User\User; |
| 19 |
use MediaWiki\User\UserFactory; |
| 20 |
use MediaWiki\User\UserIdentity; |
| 21 |
use Wikimedia\ParamValidator\ParamValidator; |
| 22 |
use Wikimedia\ParamValidator\TypeDef\IntegerDef; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
class ApiCoreThank extends ApiThank { |
| 31 |
protected RevisionStore $revisionStore; |
| 32 |
protected UserFactory $userFactory; |
| 33 |
|
| 34 |
public function __construct( |
| 35 |
ApiMain $main, |
| 36 |
$action, |
| 37 |
PermissionManager $permissionManager, |
| 38 |
RevisionStore $revisionStore, |
| 39 |
UserFactory $userFactory, |
| 40 |
LogStore $storage |
| 41 |
) { |
| 42 |
parent::__construct( |
| 43 |
$main, |
| 44 |
$action, |
| 45 |
$permissionManager, |
| 46 |
$storage |
| 47 |
); |
| 48 |
$this->revisionStore = $revisionStore; |
| 49 |
$this->userFactory = $userFactory; |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public function execute() { |
| 58 |
|
| 59 |
$user = $this->getUser(); |
| 60 |
$this->dieOnBadUser( $user ); |
| 61 |
$this->dieOnUserBlockedFromThanks( $user ); |
| 62 |
$params = $this->extractRequestParams(); |
| 63 |
$revcreation = false; |
| 64 |
|
| 65 |
$this->requireOnlyOneParameter( $params, 'rev', 'log' ); |
| 66 |
|
| 67 |
|
| 68 |
if ( isset( $params['rev'] ) && !isset( $params['log'] ) ) { |
| 69 |
$type = 'rev'; |
| 70 |
$id = $params['rev']; |
| 71 |
} elseif ( !isset( $params['rev'] ) && isset( $params['log'] ) ) { |
| 72 |
$type = 'log'; |
| 73 |
$id = $params['log']; |
| 74 |
} else { |
| 75 |
$this->dieWithError( 'thanks-error-api-params', 'thanks-error-api-params' ); |
| 76 |
} |
| 77 |
|
| 78 |
$recipientUsername = null; |
| 79 |
|
| 80 |
if ( $type === 'log' ) { |
| 81 |
$logEntry = $this->getLogEntryFromId( $id ); |
| 82 |
|
| 83 |
if ( $logEntry->getAssociatedRevId() ) { |
| 84 |
$type = 'rev'; |
| 85 |
$id = $logEntry->getAssociatedRevId(); |
| 86 |
} else { |
| 87 |
|
| 88 |
$excerpt = ''; |
| 89 |
$title = $logEntry->getTarget(); |
| 90 |
$recipient = $this->getUserFromLog( $logEntry ); |
| 91 |
$recipientUsername = $recipient->getName(); |
| 92 |
} |
| 93 |
} |
| 94 |
if ( $type === 'rev' ) { |
| 95 |
$revision = $this->getRevisionFromId( $id ); |
| 96 |
$excerpt = DiscussionParser::getEditExcerpt( $revision, $this->getLanguage() ); |
| 97 |
$title = $this->getTitleFromRevision( $revision ); |
| 98 |
$this->dieOnUserBlockedFromTitle( $user, $title ); |
| 99 |
|
| 100 |
$recipient = $this->getUserFromRevision( $revision ); |
| 101 |
$recipientUsername = $recipient->getName(); |
| 102 |
|
| 103 |
|
| 104 |
if ( !$this->revisionStore->getPreviousRevision( $revision ) ) { |
| 105 |
$revcreation = true; |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
if ( $this->userAlreadySentThanks( $user, $type, $id ) ) { |
| 111 |
$this->markResultSuccess( $recipientUsername ); |
| 112 |
} else { |
| 113 |
$this->dieOnBadRecipient( $user, $recipient ); |
| 114 |
$this->sendThanks( |
| 115 |
$user, |
| 116 |
$type, |
| 117 |
$id, |
| 118 |
$excerpt, |
| 119 |
$recipient, |
| 120 |
$this->getSourceFromParams( $params ), |
| 121 |
$title, |
| 122 |
$revcreation |
| 123 |
); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
protected function userAlreadySentThanks( User $user, $type, $id ) { |
| 135 |
if ( $type === 'rev' ) { |
| 136 |
|
| 137 |
$type = ''; |
| 138 |
} |
| 139 |
return (bool)$user->getRequest()->getSessionData( "thanks-thanked-$type$id" ); |
| 140 |
} |
| 141 |
|
| 142 |
private function getRevisionFromId( $revId ) { |
| 143 |
$revision = $this->revisionStore->getRevisionById( $revId ); |
| 144 |
|
| 145 |
|
| 146 |
if ( !$revision || $revision->getId() === 1 ) { |
| 147 |
$this->dieWithError( 'thanks-error-invalidrevision', 'invalidrevision' ); |
| 148 |
} elseif ( $revision->isDeleted( RevisionRecord::DELETED_TEXT ) ) { |
| 149 |
$this->dieWithError( 'thanks-error-revdeleted', 'revdeleted' ); |
| 150 |
} |
| 151 |
return $revision; |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
protected function getLogEntryFromId( $logId ): DatabaseLogEntry { |
| 160 |
$logEntry = null; |
| 161 |
try { |
| 162 |
$logEntry = $this->storage->getLogEntryFromId( $logId ); |
| 163 |
} catch ( InvalidLogType $e ) { |
| 164 |
$err = $this->msg( 'thanks-error-invalid-log-type', $e->getLogType() ); |
| 165 |
$this->dieWithError( $err, 'thanks-error-invalid-log-type' ); |
| 166 |
} catch ( LogDeleted $e ) { |
| 167 |
$this->dieWithError( 'thanks-error-log-deleted', 'thanks-error-log-deleted' ); |
| 168 |
} |
| 169 |
|
| 170 |
if ( !$logEntry ) { |
| 171 |
$this->dieWithError( 'thanks-error-invalid-log-id', 'thanks-error-invalid-log-id' ); |
| 172 |
} |
| 173 |
|
| 174 |
return $logEntry; |
| 175 |
} |
| 176 |
|
| 177 |
private function getTitleFromRevision( RevisionRecord $revision ) { |
| 178 |
$title = Title::castFromPageIdentity( $revision->getPage() ); |
| 179 |
if ( !$title instanceof Title ) { |
| 180 |
$this->dieWithError( 'thanks-error-notitle', 'notitle' ); |
| 181 |
} |
| 182 |
return $title; |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
private function getSourceFromParams( $params ) { |
| 191 |
if ( $params['source'] ) { |
| 192 |
return trim( $params['source'] ); |
| 193 |
} else { |
| 194 |
return 'undefined'; |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
private function getUserFromRevision( RevisionRecord $revision ) { |
| 203 |
$recipient = $revision->getUser(); |
| 204 |
if ( !$recipient ) { |
| 205 |
$this->dieWithError( 'thanks-error-invalidrecipient', 'invalidrecipient' ); |
| 206 |
} |
| 207 |
|
| 208 |
return $this->userFactory->newFromUserIdentity( $recipient ); |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
private function getUserFromLog( LogEntry $logEntry ) { |
| 216 |
$recipient = $logEntry->getPerformerIdentity(); |
| 217 |
return $this->userFactory->newFromUserIdentity( $recipient ); |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
protected function sendThanks( |
| 234 |
User $user, $type, $id, $excerpt, User $recipient, $source, Title $title, $revcreation |
| 235 |
) { |
| 236 |
$uniqueId = $type . '-' . $id; |
| 237 |
|
| 238 |
if ( $this->haveAlreadyThanked( $user, $uniqueId ) ) { |
| 239 |
|
| 240 |
$this->markResultSuccess( $recipient->getName() ); |
| 241 |
return; |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
Event::create( [ |
| 246 |
'type' => 'edit-thank', |
| 247 |
'title' => $title, |
| 248 |
'extra' => [ |
| 249 |
$type . 'id' => $id, |
| 250 |
'thanked-user-id' => $recipient->getId(), |
| 251 |
'source' => $source, |
| 252 |
'excerpt' => $excerpt, |
| 253 |
'revcreation' => $revcreation, |
| 254 |
], |
| 255 |
'agent' => $user, |
| 256 |
] ); |
| 257 |
|
| 258 |
|
| 259 |
$user->getRequest()->setSessionData( "thanks-thanked-$type$id", true ); |
| 260 |
|
| 261 |
$this->markResultSuccess( $recipient->getName() ); |
| 262 |
$this->logThanks( $user, $recipient, $uniqueId ); |
| 263 |
} |
| 264 |
|
| 265 |
public function getAllowedParams() { |
| 266 |
return [ |
| 267 |
'rev' => [ |
| 268 |
ParamValidator::PARAM_TYPE => 'integer', |
| 269 |
IntegerDef::PARAM_MIN => 1, |
| 270 |
ParamValidator::PARAM_REQUIRED => false, |
| 271 |
], |
| 272 |
'log' => [ |
| 273 |
ParamValidator::PARAM_TYPE => 'integer', |
| 274 |
IntegerDef::PARAM_MIN => 1, |
| 275 |
ParamValidator::PARAM_REQUIRED => false, |
| 276 |
], |
| 277 |
'token' => [ |
| 278 |
ParamValidator::PARAM_TYPE => 'string', |
| 279 |
ParamValidator::PARAM_REQUIRED => true, |
| 280 |
], |
| 281 |
'source' => [ |
| 282 |
ParamValidator::PARAM_TYPE => 'string', |
| 283 |
ParamValidator::PARAM_REQUIRED => false, |
| 284 |
] |
| 285 |
]; |
| 286 |
} |
| 287 |
|
| 288 |
public function getHelpUrls() { |
| 289 |
return [ |
| 290 |
'https://www.mediawiki.org/wiki/Extension:Thanks#API_Documentation', |
| 291 |
]; |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
protected function getExamplesMessages() { |
| 299 |
return [ |
| 300 |
'action=thank&revid=456&source=diff&token=123ABC' |
| 301 |
=> 'apihelp-thank-example-1', |
| 302 |
]; |
| 303 |
} |
| 304 |
} |