2015-07-01 05:27:23 +00:00
|
|
|
|
<?php
|
|
|
|
|
|
|
2024-02-08 15:30:18 +00:00
|
|
|
|
namespace MediaWiki\HTMLForm\Field;
|
|
|
|
|
|
|
|
|
|
|
|
use InvalidArgumentException;
|
2024-05-16 12:28:33 +00:00
|
|
|
|
use MediaWiki\Json\FormatJson;
|
2023-08-01 00:57:54 +00:00
|
|
|
|
use MediaWiki\MediaWikiServices;
|
2023-09-18 15:00:50 +00:00
|
|
|
|
use MediaWiki\Title\MalformedTitleException;
|
2023-03-01 20:33:26 +00:00
|
|
|
|
use MediaWiki\Title\Title;
|
2015-07-01 05:27:23 +00:00
|
|
|
|
use MediaWiki\Widget\TitleInputWidget;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Implements a text input field for page titles.
|
|
|
|
|
|
* Automatically does validation that the title is valid,
|
|
|
|
|
|
* as well as autocompletion if using the OOUI display format.
|
|
|
|
|
|
*
|
|
|
|
|
|
* Optional parameters:
|
2021-05-03 15:23:01 +00:00
|
|
|
|
* 'namespace' - Namespace the page must be in (use namespace constant; one of the NS_* constants may be used)
|
2015-07-26 20:54:45 +00:00
|
|
|
|
* 'relative' - If true and 'namespace' given, strip/add the namespace from/to the title as needed
|
2015-07-01 05:27:23 +00:00
|
|
|
|
* 'creatable' - Whether to validate the title is creatable (not a special page)
|
|
|
|
|
|
* 'exists' - Whether to validate that the title already exists
|
2021-05-20 07:27:24 +00:00
|
|
|
|
* 'interwiki' – Tolerate interwiki links (other conditions such as 'namespace' or 'exists' will be
|
|
|
|
|
|
* ignored if the title is an interwiki title). Cannot be used together with 'relative'.
|
2015-07-01 05:27:23 +00:00
|
|
|
|
*
|
2020-07-10 19:23:59 +00:00
|
|
|
|
* @stable to extend
|
2015-07-01 05:27:23 +00:00
|
|
|
|
* @since 1.26
|
|
|
|
|
|
*/
|
|
|
|
|
|
class HTMLTitleTextField extends HTMLTextField {
|
2020-07-23 09:41:58 +00:00
|
|
|
|
/**
|
2020-07-10 19:23:59 +00:00
|
|
|
|
* @stable to call
|
2020-07-23 09:41:58 +00:00
|
|
|
|
* @inheritDoc
|
2020-07-10 19:23:59 +00:00
|
|
|
|
*/
|
2015-07-01 05:27:23 +00:00
|
|
|
|
public function __construct( $params ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
|
$params += [
|
2015-07-01 05:27:23 +00:00
|
|
|
|
'namespace' => false,
|
2015-07-26 20:54:45 +00:00
|
|
|
|
'relative' => false,
|
2015-07-01 05:27:23 +00:00
|
|
|
|
'creatable' => false,
|
|
|
|
|
|
'exists' => false,
|
2021-09-14 10:42:21 +00:00
|
|
|
|
'interwiki' => false,
|
2018-05-07 18:57:32 +00:00
|
|
|
|
// This overrides the default from HTMLFormField
|
|
|
|
|
|
'required' => true,
|
2016-02-17 09:09:32 +00:00
|
|
|
|
];
|
2015-07-01 05:27:23 +00:00
|
|
|
|
|
|
|
|
|
|
parent::__construct( $params );
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function validate( $value, $alldata ) {
|
2021-05-20 07:27:24 +00:00
|
|
|
|
if ( $this->mParams['interwiki'] && $this->mParams['relative'] ) {
|
|
|
|
|
|
// relative and interwiki cannot be used together, because we don't have a way to know about
|
|
|
|
|
|
// namespaces used by the other wiki (and it might actually be a non-wiki link, too).
|
|
|
|
|
|
throw new InvalidArgumentException( 'relative and interwiki may not be used together' );
|
|
|
|
|
|
}
|
2018-07-17 04:54:03 +00:00
|
|
|
|
// Default value (from getDefault()) is null, which breaks Title::newFromTextThrow() below
|
2024-08-15 09:48:40 +00:00
|
|
|
|
$value ??= '';
|
2018-07-17 04:54:03 +00:00
|
|
|
|
|
2018-05-07 18:57:32 +00:00
|
|
|
|
if ( !$this->mParams['required'] && $value === '' ) {
|
|
|
|
|
|
// If this field is not required and the value is empty, that's okay, skip validation
|
|
|
|
|
|
return parent::validate( $value, $alldata );
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-08-01 00:57:54 +00:00
|
|
|
|
$titleFactory = MediaWikiServices::getInstance()->getTitleFactory();
|
2015-07-01 05:27:23 +00:00
|
|
|
|
try {
|
2015-07-26 20:54:45 +00:00
|
|
|
|
if ( !$this->mParams['relative'] ) {
|
2023-08-01 00:57:54 +00:00
|
|
|
|
$title = $titleFactory->newFromTextThrow( $value );
|
2015-07-26 20:54:45 +00:00
|
|
|
|
} else {
|
2023-08-01 00:57:54 +00:00
|
|
|
|
// Can't use makeTitleSafe(), because it doesn't throw useful exceptions
|
|
|
|
|
|
$title = $titleFactory->newFromTextThrow( Title::makeName( $this->mParams['namespace'], $value ) );
|
2015-07-26 20:54:45 +00:00
|
|
|
|
}
|
2015-07-01 05:27:23 +00:00
|
|
|
|
} catch ( MalformedTitleException $e ) {
|
2018-08-18 02:53:01 +00:00
|
|
|
|
return $this->msg( $e->getErrorMessage(), $e->getErrorMessageParameters() );
|
2015-07-01 05:27:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-05-20 07:27:24 +00:00
|
|
|
|
if ( $title->isExternal() ) {
|
2021-08-05 03:24:00 +00:00
|
|
|
|
if ( $this->mParams['interwiki'] ) {
|
2021-05-20 07:27:24 +00:00
|
|
|
|
// We cannot validate external titles, skip the rest of the validation
|
|
|
|
|
|
return parent::validate( $value, $alldata );
|
2021-08-05 03:24:00 +00:00
|
|
|
|
} else {
|
|
|
|
|
|
return $this->msg( 'htmlform-title-interwiki', $title->getPrefixedText() );
|
2021-05-20 07:27:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-07-01 05:27:23 +00:00
|
|
|
|
$text = $title->getPrefixedText();
|
2015-09-28 11:35:28 +00:00
|
|
|
|
if ( $this->mParams['namespace'] !== false &&
|
|
|
|
|
|
!$title->inNamespace( $this->mParams['namespace'] )
|
|
|
|
|
|
) {
|
2019-03-09 16:00:26 +00:00
|
|
|
|
return $this->msg( 'htmlform-title-badnamespace', $text, $this->mParams['namespace'] );
|
2015-07-01 05:27:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if ( $this->mParams['creatable'] && !$title->canExist() ) {
|
2016-11-02 17:13:43 +00:00
|
|
|
|
return $this->msg( 'htmlform-title-not-creatable', $text );
|
2015-07-01 05:27:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if ( $this->mParams['exists'] && !$title->exists() ) {
|
2016-11-02 17:13:43 +00:00
|
|
|
|
return $this->msg( 'htmlform-title-not-exists', $text );
|
2015-07-01 05:27:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return parent::validate( $value, $alldata );
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected function getInputWidget( $params ) {
|
|
|
|
|
|
if ( $this->mParams['namespace'] !== false ) {
|
|
|
|
|
|
$params['namespace'] = $this->mParams['namespace'];
|
|
|
|
|
|
}
|
2015-07-26 20:54:45 +00:00
|
|
|
|
$params['relative'] = $this->mParams['relative'];
|
2015-07-01 05:27:23 +00:00
|
|
|
|
return new TitleInputWidget( $params );
|
|
|
|
|
|
}
|
2015-10-29 17:51:02 +00:00
|
|
|
|
|
2016-07-26 12:12:21 +00:00
|
|
|
|
protected function shouldInfuseOOUI() {
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-07-31 14:56:23 +00:00
|
|
|
|
protected function getOOUIModules() {
|
|
|
|
|
|
// FIXME: TitleInputWidget should be in its own module
|
|
|
|
|
|
return [ 'mediawiki.widgets' ];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-10-29 17:51:02 +00:00
|
|
|
|
public function getInputHtml( $value ) {
|
|
|
|
|
|
// add mw-searchInput class to enable search suggestions for non-OOUI, too
|
|
|
|
|
|
$this->mClass .= 'mw-searchInput';
|
|
|
|
|
|
|
|
|
|
|
|
// return the HTMLTextField html
|
2016-03-18 13:55:54 +00:00
|
|
|
|
return parent::getInputHTML( $value );
|
2015-10-29 17:51:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected function getDataAttribs() {
|
|
|
|
|
|
return [
|
|
|
|
|
|
'data-mw-searchsuggest' => FormatJson::encode( [
|
|
|
|
|
|
'wrapAsLink' => false,
|
|
|
|
|
|
] ),
|
|
|
|
|
|
];
|
|
|
|
|
|
}
|
2015-07-01 05:27:23 +00:00
|
|
|
|
}
|
2024-02-08 15:30:18 +00:00
|
|
|
|
|
2024-03-07 21:56:58 +00:00
|
|
|
|
/** @deprecated class alias since 1.42 */
|
2024-02-08 15:30:18 +00:00
|
|
|
|
class_alias( HTMLTitleTextField::class, 'HTMLTitleTextField' );
|