HTMLForm: Allow IP adresses as username in HTMLUserTextField
[lhc/web/wiklou.git] / includes / htmlform / HTMLTitleTextField.php
1 <?php
2
3 use MediaWiki\Widget\TitleInputWidget;
4
5 /**
6 * Implements a text input field for page titles.
7 * Automatically does validation that the title is valid,
8 * as well as autocompletion if using the OOUI display format.
9 *
10 * FIXME: Does not work for forms that support GET requests.
11 *
12 * Optional parameters:
13 * 'namespace' - Namespace the page must be in
14 * 'creatable' - Whether to validate the title is creatable (not a special page)
15 * 'exists' - Whether to validate that the title already exists
16 *
17 * @since 1.26
18 */
19 class HTMLTitleTextField extends HTMLTextField {
20 public function __construct( $params ) {
21 $params += array(
22 'namespace' => false,
23 'creatable' => false,
24 'exists' => false,
25 );
26
27 parent::__construct( $params );
28 }
29
30 public function validate( $value, $alldata ) {
31 try {
32 $title = Title::newFromTextThrow( $value );
33 } catch ( MalformedTitleException $e ) {
34 $msg = $this->msg( $e->getErrorMessage() );
35 $params = $e->getErrorMessageParameters();
36 if ( $params ) {
37 $msg->params( $params );
38 }
39 return $msg->parse();
40 }
41
42 $text = $title->getPrefixedText();
43 if ( $this->mParams['namespace'] !== false && !$title->inNamespace( $this->mParams['namespace'] ) ) {
44 return $this->msg( 'htmlform-title-badnamespace', $this->mParams['namespace'], $text )->parse();
45 }
46
47 if ( $this->mParams['creatable'] && !$title->canExist() ) {
48 return $this->msg( 'htmlform-title-not-creatable', $text )->escaped();
49 }
50
51 if ( $this->mParams['exists'] && !$title->exists() ) {
52 return $this->msg( 'htmlform-title-not-exists', $text )->parse();
53 }
54
55 return parent::validate( $value, $alldata );
56 }
57
58 protected function getInputWidget( $params ) {
59 $this->mParent->getOutput()->addModules( 'mediawiki.widgets' );
60 if ( $this->mParams['namespace'] !== false ) {
61 $params['namespace'] = $this->mParams['namespace'];
62 }
63 $params['relative'] = false;
64 return new TitleInputWidget( $params );
65 }
66 }