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.
|
|
|
|
|
*
|
2015-07-25 01:18:49 +00:00
|
|
|
* Note: Forms using GET requests will need to make sure the title value is not
|
|
|
|
|
* an empty string.
|
2015-07-01 05:27:23 +00:00
|
|
|
*
|
|
|
|
|
* Optional parameters:
|
|
|
|
|
* 'namespace' - Namespace the page must be in
|
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
|
|
|
|
|
*
|
|
|
|
|
* @since 1.26
|
|
|
|
|
*/
|
|
|
|
|
class HTMLTitleTextField extends HTMLTextField {
|
|
|
|
|
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,
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2015-07-01 05:27:23 +00:00
|
|
|
|
|
|
|
|
parent::__construct( $params );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function validate( $value, $alldata ) {
|
2015-07-25 01:18:49 +00:00
|
|
|
if ( $this->mParent->getMethod() === 'get' && $value === '' ) {
|
|
|
|
|
// If the form is a GET form and has no value, assume it hasn't been
|
|
|
|
|
// submitted yet, and 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
|
|
|
|
|
global $wgContLang;
|
|
|
|
|
$namespaceName = $wgContLang->getNsText( $this->mParams['namespace'] );
|
|
|
|
|
$title = Title::newFromTextThrow( $namespaceName . ':' . $value );
|
|
|
|
|
}
|
2015-07-01 05:27:23 +00:00
|
|
|
} catch ( MalformedTitleException $e ) {
|
|
|
|
|
$msg = $this->msg( $e->getErrorMessage() );
|
|
|
|
|
$params = $e->getErrorMessageParameters();
|
|
|
|
|
if ( $params ) {
|
|
|
|
|
$msg->params( $params );
|
|
|
|
|
}
|
|
|
|
|
return $msg->parse();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$text = $title->getPrefixedText();
|
2015-09-28 11:35:28 +00:00
|
|
|
if ( $this->mParams['namespace'] !== false &&
|
|
|
|
|
!$title->inNamespace( $this->mParams['namespace'] )
|
|
|
|
|
) {
|
2015-07-01 05:27:23 +00:00
|
|
|
return $this->msg( 'htmlform-title-badnamespace', $this->mParams['namespace'], $text )->parse();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $this->mParams['creatable'] && !$title->canExist() ) {
|
|
|
|
|
return $this->msg( 'htmlform-title-not-creatable', $text )->escaped();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $this->mParams['exists'] && !$title->exists() ) {
|
|
|
|
|
return $this->msg( 'htmlform-title-not-exists', $text )->parse();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return parent::validate( $value, $alldata );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function getInputWidget( $params ) {
|
|
|
|
|
$this->mParent->getOutput()->addModules( 'mediawiki.widgets' );
|
|
|
|
|
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
|
|
|
|
|
|
|
|
public function getInputHtml( $value ) {
|
|
|
|
|
// add mw-searchInput class to enable search suggestions for non-OOUI, too
|
|
|
|
|
$this->mClass .= 'mw-searchInput';
|
|
|
|
|
|
|
|
|
|
// return the HTMLTextField html
|
|
|
|
|
return parent::getInputHtml( $value );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function getDataAttribs() {
|
|
|
|
|
return [
|
|
|
|
|
'data-mw-searchsuggest' => FormatJson::encode( [
|
|
|
|
|
'wrapAsLink' => false,
|
|
|
|
|
] ),
|
|
|
|
|
];
|
|
|
|
|
}
|
2015-07-01 05:27:23 +00:00
|
|
|
}
|