Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 32 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| InputBoxHooks | |
0.00% |
0 / 32 |
|
0.00% |
0 / 5 |
210 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| onParserFirstCallInit | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| onSpecialPageBeforeExecute | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
42 | |||
| render | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| onMediaWikiPerformAction | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
20 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Hooks for InputBox extension |
| 4 | * |
| 5 | * @file |
| 6 | * @ingroup Extensions |
| 7 | */ |
| 8 | |
| 9 | namespace MediaWiki\Extension\InputBox; |
| 10 | |
| 11 | use MediaWiki\Actions\ActionEntryPoint; |
| 12 | use MediaWiki\Config\Config; |
| 13 | use MediaWiki\Hook\MediaWikiPerformActionHook; |
| 14 | use MediaWiki\Output\OutputPage; |
| 15 | use MediaWiki\Page\Article; |
| 16 | use MediaWiki\Parser\Hook\ParserFirstCallInitHook; |
| 17 | use MediaWiki\Parser\Parser; |
| 18 | use MediaWiki\Parser\PPFrame; |
| 19 | use MediaWiki\Registration\ExtensionRegistry; |
| 20 | use MediaWiki\Request\WebRequest; |
| 21 | use MediaWiki\SpecialPage\Hook\SpecialPageBeforeExecuteHook; |
| 22 | use MediaWiki\SpecialPage\SpecialPage; |
| 23 | use MediaWiki\Title\Title; |
| 24 | use MediaWiki\User\User; |
| 25 | |
| 26 | /** |
| 27 | * InputBox hooks |
| 28 | */ |
| 29 | class InputBoxHooks implements |
| 30 | ParserFirstCallInitHook, |
| 31 | SpecialPageBeforeExecuteHook, |
| 32 | MediaWikiPerformActionHook |
| 33 | { |
| 34 | public function __construct( |
| 35 | private readonly Config $config, |
| 36 | private readonly ExtensionRegistry $extensionRegistry, |
| 37 | ) { |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Initialization |
| 42 | * @param Parser $parser |
| 43 | */ |
| 44 | public function onParserFirstCallInit( $parser ) { |
| 45 | // Register the hook with the parser |
| 46 | $parser->setHook( 'inputbox', [ $this, 'render' ] ); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * If necssary, prepend prefix to wpNewTitle, or update the search parameter with the searchfilter. |
| 51 | * |
| 52 | * @param SpecialPage $special |
| 53 | * @param string|null $subPage Subpage string, or null if no subpage was specified |
| 54 | * @return bool|void True or no return value to continue or false to prevent execution |
| 55 | */ |
| 56 | public function onSpecialPageBeforeExecute( $special, $subPage ) { |
| 57 | $request = $special->getRequest(); |
| 58 | $prefix = $request->getText( 'prefix', '' ); |
| 59 | $title = $request->getText( 'wpNewTitle', '' ); |
| 60 | $search = $request->getText( 'search', '' ); |
| 61 | $searchfilter = $request->getText( 'searchfilter', '' ); |
| 62 | if ( $special->getName() === 'Movepage' && $prefix !== '' && $title !== '' ) { |
| 63 | $request->setVal( 'wpNewTitle', $prefix . $title ); |
| 64 | $request->unsetVal( 'prefix' ); |
| 65 | } |
| 66 | if ( in_array( $special->getName(), [ 'Search', 'MediaSearch' ] ) && $searchfilter !== '' ) { |
| 67 | // Redirect to Special:Search after modifying the search parameter. |
| 68 | $searchQuery = $request->getQueryValues(); |
| 69 | $searchQuery[ 'search' ] = trim( $search ) . ' ' . $searchfilter; |
| 70 | unset( $searchQuery['searchfilter'], $searchQuery['title'] ); |
| 71 | $special->getOutput()->redirect( $special->getPageTitle()->getFullURL( $searchQuery ) ); |
| 72 | return false; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Render the input box |
| 78 | * @param string|null $input |
| 79 | * @param array $args |
| 80 | * @param Parser $parser |
| 81 | * @param PPFrame $frame |
| 82 | * @return string |
| 83 | */ |
| 84 | public function render( $input, $args, Parser $parser, PPFrame $frame ) { |
| 85 | if ( $input === null ) { |
| 86 | return ''; |
| 87 | } |
| 88 | |
| 89 | // Create InputBox |
| 90 | $inputBox = new InputBox( $this->config, $this->extensionRegistry, $parser ); |
| 91 | |
| 92 | // Configure InputBox |
| 93 | $inputBox->extractOptions( $parser->replaceVariables( $input, $frame ) ); |
| 94 | |
| 95 | // Return output |
| 96 | return $inputBox->render(); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * <inputbox type=create...> sends requests with action=edit, and |
| 101 | * possibly a &prefix=Foo. So we pick that up here, munge prefix |
| 102 | * and title together, and redirect back out to the real page |
| 103 | * @param OutputPage $output |
| 104 | * @param Article $article |
| 105 | * @param Title $title |
| 106 | * @param User $user |
| 107 | * @param WebRequest $request |
| 108 | * @param ActionEntryPoint $wiki |
| 109 | * @return bool |
| 110 | */ |
| 111 | public function onMediaWikiPerformAction( |
| 112 | $output, |
| 113 | $article, |
| 114 | $title, |
| 115 | $user, |
| 116 | $request, |
| 117 | $wiki |
| 118 | ) { |
| 119 | // In order to check for 'action=edit' in URL parameters, even if another extension overrides |
| 120 | // the action, we must not use getActionName() here. (T337436) |
| 121 | if ( $request->getRawVal( 'action' ) !== 'edit' && $request->getRawVal( 'veaction' ) !== 'edit' ) { |
| 122 | // not our problem |
| 123 | return true; |
| 124 | } |
| 125 | $prefix = $request->getText( 'prefix', '' ); |
| 126 | if ( $prefix === '' ) { |
| 127 | // Fine |
| 128 | return true; |
| 129 | } |
| 130 | |
| 131 | $title = $prefix . $request->getText( 'title', '' ); |
| 132 | $params = $request->getValues(); |
| 133 | unset( $params['prefix'] ); |
| 134 | $params['title'] = $title; |
| 135 | |
| 136 | $output->redirect( wfAppendQuery( $output->getConfig()->get( 'Script' ), $params ), '301' ); |
| 137 | return false; |
| 138 | } |
| 139 | } |