| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
namespace MediaWiki\Actions; |
| 21 |
|
| 22 |
use Action; |
| 23 |
use Article; |
| 24 |
use CreditsAction; |
| 25 |
use InfoAction; |
| 26 |
use MarkpatrolledAction; |
| 27 |
use McrRestoreAction; |
| 28 |
use McrUndoAction; |
| 29 |
use MediaWiki\Context\IContextSource; |
| 30 |
use MediaWiki\Context\RequestContext; |
| 31 |
use MediaWiki\HookContainer\HookContainer; |
| 32 |
use MediaWiki\HookContainer\HookRunner; |
| 33 |
use MediaWiki\Page\PageIdentity; |
| 34 |
use MediaWiki\Title\Title; |
| 35 |
use Psr\Log\LoggerInterface; |
| 36 |
use RawAction; |
| 37 |
use RevertAction; |
| 38 |
use RollbackAction; |
| 39 |
use UnwatchAction; |
| 40 |
use WatchAction; |
| 41 |
use Wikimedia\ObjectFactory\ObjectFactory; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
class ActionFactory { |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
private $actionsConfig; |
| 54 |
|
| 55 |
private LoggerInterface $logger; |
| 56 |
private ObjectFactory $objectFactory; |
| 57 |
private HookContainer $hookContainer; |
| 58 |
private HookRunner $hookRunner; |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
private const CORE_ACTIONS = [ |
| 72 |
'delete' => true, |
| 73 |
'edit' => true, |
| 74 |
'history' => true, |
| 75 |
'protect' => true, |
| 76 |
'purge' => true, |
| 77 |
'render' => true, |
| 78 |
'submit' => true, |
| 79 |
'unprotect' => true, |
| 80 |
'view' => true, |
| 81 |
|
| 82 |
|
| 83 |
'credits' => [ |
| 84 |
'class' => CreditsAction::class, |
| 85 |
'services' => [ |
| 86 |
'LinkRenderer', |
| 87 |
'UserFactory', |
| 88 |
], |
| 89 |
], |
| 90 |
'info' => [ |
| 91 |
'class' => InfoAction::class, |
| 92 |
'services' => [ |
| 93 |
'ContentLanguage', |
| 94 |
'LanguageNameUtils', |
| 95 |
'LinkBatchFactory', |
| 96 |
'LinkRenderer', |
| 97 |
'DBLoadBalancerFactory', |
| 98 |
'MagicWordFactory', |
| 99 |
'NamespaceInfo', |
| 100 |
'PageProps', |
| 101 |
'RepoGroup', |
| 102 |
'RevisionLookup', |
| 103 |
'MainWANObjectCache', |
| 104 |
'WatchedItemStore', |
| 105 |
'RedirectLookup', |
| 106 |
'RestrictionStore', |
| 107 |
'LinksMigration', |
| 108 |
'UserFactory', |
| 109 |
], |
| 110 |
], |
| 111 |
'markpatrolled' => [ |
| 112 |
'class' => MarkpatrolledAction::class, |
| 113 |
'services' => [ |
| 114 |
'LinkRenderer', |
| 115 |
], |
| 116 |
], |
| 117 |
'mcrundo' => [ |
| 118 |
'class' => McrUndoAction::class, |
| 119 |
'services' => [ |
| 120 |
|
| 121 |
'ReadOnlyMode', |
| 122 |
'RevisionLookup', |
| 123 |
'RevisionRenderer', |
| 124 |
'CommentFormatter', |
| 125 |
'MainConfig', |
| 126 |
], |
| 127 |
], |
| 128 |
'mcrrestore' => [ |
| 129 |
'class' => McrRestoreAction::class, |
| 130 |
'services' => [ |
| 131 |
|
| 132 |
'ReadOnlyMode', |
| 133 |
'RevisionLookup', |
| 134 |
'RevisionRenderer', |
| 135 |
'CommentFormatter', |
| 136 |
'MainConfig', |
| 137 |
], |
| 138 |
], |
| 139 |
'raw' => [ |
| 140 |
'class' => RawAction::class, |
| 141 |
'services' => [ |
| 142 |
'Parser', |
| 143 |
'PermissionManager', |
| 144 |
'RevisionLookup', |
| 145 |
'RestrictionStore', |
| 146 |
'UserFactory', |
| 147 |
], |
| 148 |
], |
| 149 |
'revert' => [ |
| 150 |
'class' => RevertAction::class, |
| 151 |
'services' => [ |
| 152 |
'ContentLanguage', |
| 153 |
'RepoGroup', |
| 154 |
], |
| 155 |
], |
| 156 |
'rollback' => [ |
| 157 |
'class' => RollbackAction::class, |
| 158 |
'services' => [ |
| 159 |
'ContentHandlerFactory', |
| 160 |
'RollbackPageFactory', |
| 161 |
'UserOptionsLookup', |
| 162 |
'WatchlistManager', |
| 163 |
'CommentFormatter' |
| 164 |
], |
| 165 |
], |
| 166 |
'unwatch' => [ |
| 167 |
'class' => UnwatchAction::class, |
| 168 |
'services' => [ |
| 169 |
'WatchlistManager', |
| 170 |
'WatchedItemStore', |
| 171 |
], |
| 172 |
], |
| 173 |
'watch' => [ |
| 174 |
'class' => WatchAction::class, |
| 175 |
'services' => [ |
| 176 |
'WatchlistManager', |
| 177 |
'WatchedItemStore', |
| 178 |
], |
| 179 |
], |
| 180 |
]; |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
public function __construct( |
| 189 |
array $actionsConfig, |
| 190 |
LoggerInterface $logger, |
| 191 |
ObjectFactory $objectFactory, |
| 192 |
HookContainer $hookContainer |
| 193 |
) { |
| 194 |
$this->actionsConfig = $actionsConfig; |
| 195 |
$this->logger = $logger; |
| 196 |
$this->objectFactory = $objectFactory; |
| 197 |
$this->hookContainer = $hookContainer; |
| 198 |
$this->hookRunner = new HookRunner( $hookContainer ); |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
private function getActionSpec( string $actionName ) { |
| 208 |
if ( isset( $this->actionsConfig[ $actionName ] ) ) { |
| 209 |
$this->logger->debug( |
| 210 |
'{actionName} is being set in configuration rather than CORE_ACTIONS', |
| 211 |
[ |
| 212 |
'actionName' => $actionName |
| 213 |
] |
| 214 |
); |
| 215 |
return $this->actionsConfig[ $actionName ]; |
| 216 |
} |
| 217 |
return ( self::CORE_ACTIONS[ $actionName ] ?? null ); |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
public function getAction( |
| 230 |
string $actionName, |
| 231 |
$article, |
| 232 |
IContextSource $context |
| 233 |
) { |
| 234 |
|
| 235 |
$actionName = strtolower( $actionName ); |
| 236 |
|
| 237 |
$spec = $this->getActionSpec( $actionName ); |
| 238 |
if ( $spec === false ) { |
| 239 |
|
| 240 |
return $spec; |
| 241 |
} |
| 242 |
|
| 243 |
if ( $article instanceof PageIdentity ) { |
| 244 |
if ( !$article->canExist() ) { |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
return null; |
| 250 |
} |
| 251 |
|
| 252 |
$article = Article::newFromTitle( |
| 253 |
Title::newFromPageIdentity( $article ), |
| 254 |
$context |
| 255 |
); |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
$overrides = $article->getActionOverrides(); |
| 261 |
if ( isset( $overrides[ $actionName ] ) ) { |
| 262 |
|
| 263 |
$spec = $overrides[ $actionName ]; |
| 264 |
$this->logger->debug( |
| 265 |
'Overriding normal handler for {actionName}', |
| 266 |
[ 'actionName' => $actionName ] |
| 267 |
); |
| 268 |
} |
| 269 |
|
| 270 |
if ( !$spec ) { |
| 271 |
|
| 272 |
|
| 273 |
return $spec; |
| 274 |
} |
| 275 |
|
| 276 |
if ( $spec === true ) { |
| 277 |
|
| 278 |
$spec = ucfirst( $actionName ) . 'Action'; |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
if ( is_string( $spec ) ) { |
| 284 |
if ( !class_exists( $spec ) ) { |
| 285 |
$this->logger->info( |
| 286 |
'Missing action class {actionClass}, treating as disabled', |
| 287 |
[ 'actionClass' => $spec ] |
| 288 |
); |
| 289 |
return false; |
| 290 |
} |
| 291 |
|
| 292 |
$spec = [ 'class' => $spec ]; |
| 293 |
} elseif ( is_callable( $spec ) ) { |
| 294 |
$spec = [ 'factory' => $spec ]; |
| 295 |
} elseif ( !is_array( $spec ) ) { |
| 296 |
|
| 297 |
return $spec; |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
$actionObj = $this->objectFactory->createObject( |
| 303 |
$spec, |
| 304 |
[ |
| 305 |
'extraArgs' => [ $article, $context ], |
| 306 |
'assertClass' => Action::class |
| 307 |
] |
| 308 |
); |
| 309 |
$actionObj->setHookContainer( $this->hookContainer ); |
| 310 |
return $actionObj; |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
public function getActionInfo( string $name, $target = null ): ?ActionInfo { |
| 331 |
$context = RequestContext::getMain(); |
| 332 |
|
| 333 |
if ( !$target ) { |
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
$spec = $this->getActionSpec( $name ); |
| 338 |
if ( !$spec ) { |
| 339 |
return null; |
| 340 |
} |
| 341 |
|
| 342 |
$target = Title::newMainPage(); |
| 343 |
} |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
$actionObj = $this->getAction( $name, $target, $context ); |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
if ( !$actionObj ) { |
| 353 |
return null; |
| 354 |
} |
| 355 |
|
| 356 |
return new ActionInfo( [ |
| 357 |
'name' => $actionObj->getName(), |
| 358 |
'restriction' => $actionObj->getRestriction(), |
| 359 |
'needsReadRights' => $actionObj->needsReadRights(), |
| 360 |
'requiresWrite' => $actionObj->requiresWrite(), |
| 361 |
'requiresUnblock' => $actionObj->requiresUnblock(), |
| 362 |
] ); |
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
public function getActionName( IContextSource $context ): string { |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
if ( !$context->canUseWikiPage() ) { |
| 378 |
return 'view'; |
| 379 |
} |
| 380 |
|
| 381 |
$request = $context->getRequest(); |
| 382 |
$actionName = $request->getRawVal( 'action' ) ?? 'view'; |
| 383 |
|
| 384 |
|
| 385 |
$actionName = strtolower( $actionName ); |
| 386 |
|
| 387 |
|
| 388 |
if ( $this->getActionSpec( $actionName ) === false ) { |
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
return 'nosuchaction'; |
| 394 |
} |
| 395 |
|
| 396 |
if ( $actionName === 'historysubmit' ) { |
| 397 |
|
| 398 |
|
| 399 |
$actionName = 'view'; |
| 400 |
} elseif ( $actionName === 'editredlink' ) { |
| 401 |
$actionName = 'edit'; |
| 402 |
} |
| 403 |
|
| 404 |
$this->hookRunner->onGetActionName( $context, $actionName ); |
| 405 |
|
| 406 |
$action = $this->getAction( |
| 407 |
$actionName, |
| 408 |
$this->getArticle( $context ), |
| 409 |
$context |
| 410 |
); |
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
if ( $action instanceof Action ) { |
| 416 |
return $action->getName(); |
| 417 |
} |
| 418 |
|
| 419 |
return 'nosuchaction'; |
| 420 |
} |
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
protected function getArticle( IContextSource $context ): Article { |
| 431 |
return Article::newFromWikiPage( $context->getWikiPage(), $context ); |
| 432 |
} |
| 433 |
|
| 434 |
} |