2008-10-27 20:33:18 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Hooks for InputBox extension
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @ingroup Extensions
|
|
|
|
*/
|
|
|
|
|
2021-12-10 18:03:35 +00:00
|
|
|
namespace MediaWiki\Extension\InputBox;
|
|
|
|
|
|
|
|
use Article;
|
2024-01-05 20:04:05 +00:00
|
|
|
use MediaWiki\Actions\ActionEntryPoint;
|
2023-10-22 12:06:16 +00:00
|
|
|
use MediaWiki\Config\Config;
|
2021-10-31 17:54:45 +00:00
|
|
|
use MediaWiki\Hook\MediaWikiPerformActionHook;
|
|
|
|
use MediaWiki\Hook\ParserFirstCallInitHook;
|
2023-10-22 12:24:40 +00:00
|
|
|
use MediaWiki\Output\OutputPage;
|
2024-06-09 09:44:04 +00:00
|
|
|
use MediaWiki\Parser\Parser;
|
2023-10-22 12:24:40 +00:00
|
|
|
use MediaWiki\Request\WebRequest;
|
2021-10-31 17:54:45 +00:00
|
|
|
use MediaWiki\SpecialPage\Hook\SpecialPageBeforeExecuteHook;
|
2023-10-22 12:24:40 +00:00
|
|
|
use MediaWiki\SpecialPage\SpecialPage;
|
2023-08-19 04:16:50 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2023-10-22 12:24:40 +00:00
|
|
|
use MediaWiki\User\User;
|
2021-10-31 17:54:45 +00:00
|
|
|
|
2017-10-14 13:45:46 +00:00
|
|
|
/**
|
|
|
|
* InputBox hooks
|
|
|
|
*/
|
2021-10-31 17:54:45 +00:00
|
|
|
class InputBoxHooks implements
|
|
|
|
ParserFirstCallInitHook,
|
|
|
|
SpecialPageBeforeExecuteHook,
|
|
|
|
MediaWikiPerformActionHook
|
|
|
|
{
|
2023-10-22 12:06:16 +00:00
|
|
|
/** @var Config */
|
|
|
|
private $config;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Config $config
|
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
Config $config
|
|
|
|
) {
|
|
|
|
$this->config = $config;
|
|
|
|
}
|
2021-10-31 17:54:45 +00:00
|
|
|
|
2017-10-14 13:45:46 +00:00
|
|
|
/**
|
|
|
|
* Initialization
|
2019-11-15 07:34:06 +00:00
|
|
|
* @param Parser $parser
|
2017-10-14 13:45:46 +00:00
|
|
|
*/
|
2021-10-31 17:54:45 +00:00
|
|
|
public function onParserFirstCallInit( $parser ) {
|
2008-10-27 20:33:18 +00:00
|
|
|
// Register the hook with the parser
|
2021-12-10 18:48:39 +00:00
|
|
|
$parser->setHook( 'inputbox', [ $this, 'render' ] );
|
2008-10-27 20:33:18 +00:00
|
|
|
}
|
2008-10-27 20:56:23 +00:00
|
|
|
|
2017-10-14 13:45:46 +00:00
|
|
|
/**
|
|
|
|
* Prepend prefix to wpNewTitle if necessary
|
|
|
|
* @param SpecialPage $special
|
|
|
|
* @param string $subPage
|
|
|
|
*/
|
2021-10-31 17:54:45 +00:00
|
|
|
public function onSpecialPageBeforeExecute( $special, $subPage ) {
|
2014-04-05 20:43:15 +00:00
|
|
|
$request = $special->getRequest();
|
|
|
|
$prefix = $request->getText( 'prefix', '' );
|
|
|
|
$title = $request->getText( 'wpNewTitle', '' );
|
2017-06-06 18:45:30 +00:00
|
|
|
$search = $request->getText( 'search', '' );
|
|
|
|
$searchfilter = $request->getText( 'searchfilter', '' );
|
2021-10-16 20:10:22 +00:00
|
|
|
if ( $special->getName() === 'Movepage' && $prefix !== '' && $title !== '' ) {
|
2014-04-05 20:43:15 +00:00
|
|
|
$request->setVal( 'wpNewTitle', $prefix . $title );
|
|
|
|
$request->unsetVal( 'prefix' );
|
|
|
|
}
|
2021-10-16 20:10:22 +00:00
|
|
|
if ( $special->getName() === 'Search' && $searchfilter !== '' ) {
|
2017-06-06 18:45:30 +00:00
|
|
|
$request->setVal( 'search', $search . ' ' . $searchfilter );
|
|
|
|
}
|
2014-04-05 20:43:15 +00:00
|
|
|
}
|
|
|
|
|
2017-10-14 13:45:46 +00:00
|
|
|
/**
|
|
|
|
* Render the input box
|
|
|
|
* @param string $input
|
|
|
|
* @param array $args
|
|
|
|
* @param Parser $parser
|
|
|
|
* @return string
|
|
|
|
*/
|
2021-12-10 18:48:39 +00:00
|
|
|
public function render( $input, $args, Parser $parser ) {
|
2008-10-27 20:33:18 +00:00
|
|
|
// Create InputBox
|
2023-10-22 12:06:16 +00:00
|
|
|
$inputBox = new InputBox( $this->config, $parser );
|
2008-10-27 20:56:23 +00:00
|
|
|
|
2008-10-27 20:33:18 +00:00
|
|
|
// Configure InputBox
|
|
|
|
$inputBox->extractOptions( $parser->replaceVariables( $input ) );
|
2008-10-27 20:56:23 +00:00
|
|
|
|
2008-10-27 20:33:18 +00:00
|
|
|
// Return output
|
|
|
|
return $inputBox->render();
|
|
|
|
}
|
2014-05-18 09:07:27 +00:00
|
|
|
|
2010-03-18 19:11:12 +00:00
|
|
|
/**
|
2014-05-18 09:07:27 +00:00
|
|
|
* <inputbox type=create...> sends requests with action=edit, and
|
|
|
|
* possibly a &prefix=Foo. So we pick that up here, munge prefix
|
2010-03-18 19:11:12 +00:00
|
|
|
* and title together, and redirect back out to the real page
|
2017-10-14 13:45:46 +00:00
|
|
|
* @param OutputPage $output
|
|
|
|
* @param Article $article
|
|
|
|
* @param Title $title
|
|
|
|
* @param User $user
|
|
|
|
* @param WebRequest $request
|
2024-01-05 20:04:05 +00:00
|
|
|
* @param ActionEntryPoint $wiki
|
2012-08-30 16:20:32 +00:00
|
|
|
* @return bool
|
2010-03-18 19:11:12 +00:00
|
|
|
*/
|
2021-10-31 17:54:45 +00:00
|
|
|
public function onMediaWikiPerformAction(
|
2014-05-18 09:07:27 +00:00
|
|
|
$output,
|
|
|
|
$article,
|
|
|
|
$title,
|
|
|
|
$user,
|
|
|
|
$request,
|
|
|
|
$wiki
|
|
|
|
) {
|
2023-05-24 22:00:49 +00:00
|
|
|
// In order to check for 'action=edit' in URL parameters, even if another extension overrides
|
|
|
|
// the action, we must not use getActionName() here. (T337436)
|
|
|
|
if ( $request->getRawVal( 'action' ) !== 'edit' && $request->getRawVal( 'veaction' ) !== 'edit' ) {
|
2017-05-18 18:09:49 +00:00
|
|
|
// not our problem
|
2010-03-18 19:11:12 +00:00
|
|
|
return true;
|
|
|
|
}
|
2022-09-09 21:49:48 +00:00
|
|
|
$prefix = $request->getText( 'prefix', '' );
|
|
|
|
if ( $prefix === '' ) {
|
2017-05-18 18:09:49 +00:00
|
|
|
// Fine
|
2010-03-18 19:11:12 +00:00
|
|
|
return true;
|
|
|
|
}
|
2014-05-18 09:07:27 +00:00
|
|
|
|
2022-09-09 21:49:48 +00:00
|
|
|
$title = $prefix . $request->getText( 'title', '' );
|
2010-03-18 19:11:12 +00:00
|
|
|
$params = $request->getValues();
|
2010-03-18 20:04:20 +00:00
|
|
|
unset( $params['prefix'] );
|
|
|
|
$params['title'] = $title;
|
2014-05-18 09:07:27 +00:00
|
|
|
|
2022-09-09 21:49:48 +00:00
|
|
|
$output->redirect( wfAppendQuery( $output->getConfig()->get( 'Script' ), $params ), '301' );
|
2010-03-18 19:11:12 +00:00
|
|
|
return false;
|
|
|
|
}
|
2008-10-27 20:56:23 +00:00
|
|
|
}
|