Debian Codegraph @

extensions/InputBox/includes/InputBoxHooks.php

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