Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
InputBoxHooks
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 5
182
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 onParserFirstCallInit
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 onSpecialPageBeforeExecute
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
42
 render
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 onMediaWikiPerformAction
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2/**
3 * Hooks for InputBox extension
4 *
5 * @file
6 * @ingroup Extensions
7 */
8
9namespace MediaWiki\Extension\InputBox;
10
11use Article;
12use MediaWiki\Actions\ActionEntryPoint;
13use MediaWiki\Config\Config;
14use MediaWiki\Hook\MediaWikiPerformActionHook;
15use MediaWiki\Hook\ParserFirstCallInitHook;
16use MediaWiki\Output\OutputPage;
17use MediaWiki\Request\WebRequest;
18use MediaWiki\SpecialPage\Hook\SpecialPageBeforeExecuteHook;
19use MediaWiki\SpecialPage\SpecialPage;
20use MediaWiki\Title\Title;
21use MediaWiki\User\User;
22use Parser;
23
24/**
25 * InputBox hooks
26 */
27class 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 $input
76     * @param array $args
77     * @param Parser $parser
78     * @return string
79     */
80    public function render( $input, $args, Parser $parser ) {
81        // Create InputBox
82        $inputBox = new InputBox( $this->config, $parser );
83
84        // Configure InputBox
85        $inputBox->extractOptions( $parser->replaceVariables( $input ) );
86
87        // Return output
88        return $inputBox->render();
89    }
90
91    /**
92     * <inputbox type=create...> sends requests with action=edit, and
93     * possibly a &prefix=Foo.  So we pick that up here, munge prefix
94     * and title together, and redirect back out to the real page
95     * @param OutputPage $output
96     * @param Article $article
97     * @param Title $title
98     * @param User $user
99     * @param WebRequest $request
100     * @param ActionEntryPoint $wiki
101     * @return bool
102     */
103    public function onMediaWikiPerformAction(
104        $output,
105        $article,
106        $title,
107        $user,
108        $request,
109        $wiki
110    ) {
111        // In order to check for 'action=edit' in URL parameters, even if another extension overrides
112        // the action, we must not use getActionName() here. (T337436)
113        if ( $request->getRawVal( 'action' ) !== 'edit' && $request->getRawVal( 'veaction' ) !== 'edit' ) {
114            // not our problem
115            return true;
116        }
117        $prefix = $request->getText( 'prefix', '' );
118        if ( $prefix === '' ) {
119            // Fine
120            return true;
121        }
122
123        $title = $prefix . $request->getText( 'title', '' );
124        $params = $request->getValues();
125        unset( $params['prefix'] );
126        $params['title'] = $title;
127
128        $output->redirect( wfAppendQuery( $output->getConfig()->get( 'Script' ), $params ), '301' );
129        return false;
130    }
131}