2015-07-01 05:27:23 +00:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
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-08-05 03:24:00 +00:00
|
|
|
|
// Default to null during the deprecation process so we can differentiate between
|
|
|
|
|
|
// callers who intentionally want to disallow interwiki titles and legacy callers
|
|
|
|
|
|
// who aren't aware of the setting.
|
|
|
|
|
|
'interwiki' => null,
|
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
|
|
|
|
|
|
if ( $value === null ) {
|
|
|
|
|
|
$value = '';
|
|
|
|
|
|
}
|
|
|
|
|
|
|
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 );
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-07-01 05:27:23 +00:00
|
|
|
|
try {
|
2015-07-26 20:54:45 +00:00
|
|
|
|
if ( !$this->mParams['relative'] ) {
|
|
|
|
|
|
$title = Title::newFromTextThrow( $value );
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// Can't use Title::makeTitleSafe(), because it doesn't throw useful exceptions
|
2018-08-18 02:53:01 +00:00
|
|
|
|
$title = Title::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
|
|
|
|
} elseif ( $this->mParams['interwiki'] === null ) {
|
|
|
|
|
|
// Legacy caller, they probably don't need/want interwiki titles as those don't
|
|
|
|
|
|
// make sense in most use cases. To avoid a B/C break without deprecation, though,
|
|
|
|
|
|
// we let the title through and raise a warning. That way, code that needs to allow
|
|
|
|
|
|
// interwiki titles will get deprecation warnings as long as users actually submit
|
|
|
|
|
|
// interwiki titles to the form. That's not ideal but a less conditional warning
|
|
|
|
|
|
// would be impractical - having to update every title field in the world to avoid
|
|
|
|
|
|
// warnings would be too much of a burden.
|
2021-05-20 07:27:24 +00:00
|
|
|
|
wfDeprecated(
|
2021-06-13 11:58:24 +00:00
|
|
|
|
__METHOD__ . ' will reject external titles in 1.38 when interwiki is false '
|
|
|
|
|
|
. "(field: $this->mName)",
|
2021-05-20 07:27:24 +00:00
|
|
|
|
'1.37'
|
|
|
|
|
|
);
|
2021-08-05 03:24:00 +00:00
|
|
|
|
return parent::validate( $value, $alldata );
|
|
|
|
|
|
} 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
|
|
|
|
}
|