MediaWiki  master
HTMLTitleTextField.php
Go to the documentation of this file.
1 <?php
2 
4 
22  public function __construct( $params ) {
23  $params += [
24  'namespace' => false,
25  'relative' => false,
26  'creatable' => false,
27  'exists' => false,
28  // This overrides the default from HTMLFormField
29  'required' => true,
30  ];
31 
32  parent::__construct( $params );
33  }
34 
35  public function validate( $value, $alldata ) {
36  if ( $this->mParent->getMethod() === 'get' && $value === '' ) {
37  // If the form is a GET form and has no value, assume it hasn't been
38  // submitted yet, and skip validation
39  // TODO This doesn't look right, we should be able to tell the difference
40  // between "not submitted" (null) and "submitted but empty" (empty string).
41  return parent::validate( $value, $alldata );
42  }
43 
44  // Default value (from getDefault()) is null, which breaks Title::newFromTextThrow() below
45  if ( $value === null ) {
46  $value = '';
47  }
48 
49  if ( !$this->mParams['required'] && $value === '' ) {
50  // If this field is not required and the value is empty, that's okay, skip validation
51  return parent::validate( $value, $alldata );
52  }
53 
54  try {
55  if ( !$this->mParams['relative'] ) {
57  } else {
58  // Can't use Title::makeTitleSafe(), because it doesn't throw useful exceptions
59  $title = Title::newFromTextThrow( Title::makeName( $this->mParams['namespace'], $value ) );
60  }
61  } catch ( MalformedTitleException $e ) {
62  return $this->msg( $e->getErrorMessage(), $e->getErrorMessageParameters() );
63  }
64 
65  $text = $title->getPrefixedText();
66  if ( $this->mParams['namespace'] !== false &&
67  !$title->inNamespace( $this->mParams['namespace'] )
68  ) {
69  return $this->msg( 'htmlform-title-badnamespace', $text, $this->mParams['namespace'] );
70  }
71 
72  if ( $this->mParams['creatable'] && !$title->canExist() ) {
73  return $this->msg( 'htmlform-title-not-creatable', $text );
74  }
75 
76  if ( $this->mParams['exists'] && !$title->exists() ) {
77  return $this->msg( 'htmlform-title-not-exists', $text );
78  }
79 
80  return parent::validate( $value, $alldata );
81  }
82 
83  protected function getInputWidget( $params ) {
84  if ( $this->mParams['namespace'] !== false ) {
85  $params['namespace'] = $this->mParams['namespace'];
86  }
87  $params['relative'] = $this->mParams['relative'];
88  return new TitleInputWidget( $params );
89  }
90 
91  protected function shouldInfuseOOUI() {
92  return true;
93  }
94 
95  protected function getOOUIModules() {
96  // FIXME: TitleInputWidget should be in its own module
97  return [ 'mediawiki.widgets' ];
98  }
99 
100  public function getInputHtml( $value ) {
101  // add mw-searchInput class to enable search suggestions for non-OOUI, too
102  $this->mClass .= 'mw-searchInput';
103 
104  // return the HTMLTextField html
105  return parent::getInputHTML( $value );
106  }
107 
108  protected function getDataAttribs() {
109  return [
110  'data-mw-searchsuggest' => FormatJson::encode( [
111  'wrapAsLink' => false,
112  ] ),
113  ];
114  }
115 }
MalformedTitleException is thrown when a TitleParser is unable to parse a title string.
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException' returning false will NOT prevent logging $e
Definition: hooks.txt:2159
msg()
Get a translated interface message.
validate( $value, $alldata)
Implements a text input field for page titles.
$value
static encode( $value, $pretty=false, $escaping=0)
Returns the JSON representation of a value.
Definition: FormatJson.php:115
$params
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition: hooks.txt:780
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:925
static newFromTextThrow( $text, $defaultNamespace=NS_MAIN)
Like Title::newFromText(), but throws MalformedTitleException when the title is invalid, rather than returning null.
Definition: Title.php:356
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
<input> field.
static makeName( $ns, $title, $fragment='', $interwiki='', $canonicalNamespace=false)
Make a prefixed DB key from a DB key and a namespace index.
Definition: Title.php:819