Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
23.08% |
6 / 26 |
|
25.00% |
1 / 4 |
CRAP | |
0.00% |
0 / 1 |
| Hooks | |
23.08% |
6 / 26 |
|
25.00% |
1 / 4 |
66.08 | |
0.00% |
0 / 1 |
| onTitleQuickPermissions | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
5 | |||
| onGetActionName | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 | |||
| onBeforeDisplayNoArticleText | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
| getWorkflow | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace ArticleCreationWorkflow; |
| 4 | |
| 5 | use MediaWiki\Actions\Hook\GetActionNameHook; |
| 6 | use MediaWiki\Context\IContextSource; |
| 7 | use MediaWiki\MediaWikiServices; |
| 8 | use MediaWiki\Page\Article; |
| 9 | use MediaWiki\Page\Hook\BeforeDisplayNoArticleTextHook; |
| 10 | use MediaWiki\Permissions\Hook\TitleQuickPermissionsHook; |
| 11 | use MediaWiki\Title\Title; |
| 12 | use MediaWiki\User\User; |
| 13 | |
| 14 | /** |
| 15 | * Hook handlers |
| 16 | */ |
| 17 | class Hooks implements |
| 18 | GetActionNameHook, |
| 19 | BeforeDisplayNoArticleTextHook, |
| 20 | TitleQuickPermissionsHook |
| 21 | { |
| 22 | /** |
| 23 | * TitleQuickPermissions hook handler |
| 24 | * Prohibits creating pages in main namespace for users without a special permission |
| 25 | * |
| 26 | * @see https://www.mediawiki.org/wiki/Manual:Hooks/TitleQuickPermissions |
| 27 | * @param Title $title |
| 28 | * @param User $user |
| 29 | * @param string $action |
| 30 | * @param array &$errors |
| 31 | * @param bool $doExpensiveQueries |
| 32 | * @param bool $short |
| 33 | * @return bool |
| 34 | */ |
| 35 | public function onTitleQuickPermissions( $title, |
| 36 | $user, |
| 37 | $action, |
| 38 | &$errors, |
| 39 | $doExpensiveQueries, |
| 40 | $short |
| 41 | ) { |
| 42 | if ( $action === 'create' |
| 43 | && $title->inNamespace( NS_MAIN ) |
| 44 | && !$user->isAllowed( 'createpagemainns' ) |
| 45 | ) { |
| 46 | $errors[] = !$user->isNamed() ? [ 'nocreatetext' ] : [ 'nocreate-loggedin' ]; |
| 47 | return false; |
| 48 | } |
| 49 | return true; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * GetActionName hook handler |
| 54 | * |
| 55 | * @param IContextSource $context Request context |
| 56 | * @param string &$action Default action name, reassign to change it |
| 57 | * @return void This hook must not abort, it must return no value |
| 58 | */ |
| 59 | public function onGetActionName( IContextSource $context, string &$action ): void { |
| 60 | if ( $action !== 'edit' ) { |
| 61 | return; |
| 62 | } |
| 63 | $workflow = self::getWorkflow(); |
| 64 | $title = $context->getTitle(); |
| 65 | $user = $context->getUser(); |
| 66 | if ( $workflow->shouldInterceptPage( $title, $user ) ) { |
| 67 | // The user wouldn't be allowed to edit anyway, so pretend we're in the 'view' action, |
| 68 | // so that we can intercept it in onBeforeDisplayNoArticleText. |
| 69 | $action = 'view'; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * BeforeDisplayNoArticleText hook handler |
| 75 | * |
| 76 | * @param Article $article The (empty) article |
| 77 | * @return bool This hook can abort |
| 78 | */ |
| 79 | public function onBeforeDisplayNoArticleText( $article ) { |
| 80 | $workflow = self::getWorkflow(); |
| 81 | $context = $article->getContext(); |
| 82 | $user = $context->getUser(); |
| 83 | $title = $article->getTitle(); |
| 84 | |
| 85 | $wasIntercepted = $workflow->interceptIfNeeded( $title, $user, $context ); |
| 86 | |
| 87 | // If we displayed our own message, abort the hook by returning `false` |
| 88 | // to suppress the default message, otherwise let it continue. |
| 89 | return !$wasIntercepted; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * @return Workflow |
| 94 | */ |
| 95 | private static function getWorkflow() { |
| 96 | static $cached; |
| 97 | |
| 98 | if ( !$cached ) { |
| 99 | $config = MediaWikiServices::getInstance() |
| 100 | ->getConfigFactory() |
| 101 | ->makeConfig( 'ArticleCreationWorkflow' ); |
| 102 | $cached = new Workflow( $config ); |
| 103 | } |
| 104 | |
| 105 | return $cached; |
| 106 | } |
| 107 | } |