MediaWiki  master
MultiTitleFilter.php
Go to the documentation of this file.
1 <?php
21 namespace MediaWiki\Preferences;
22 
23 use Title;
24 use TitleFactory;
25 
26 class MultiTitleFilter implements Filter {
27 
31  private $titleFactory;
32 
36  public function __construct( TitleFactory $titleFactory = null ) {
37  $this->titleFactory = $titleFactory;
38  }
39 
43  public function filterForForm( $value ) {
44  $ids = array_map( 'intval', preg_split( '/\n/', $value, -1, PREG_SPLIT_NO_EMPTY ) );
45  $titles = $ids ? $this->getTitleFactory()->newFromIDs( $ids ) : [];
46  if ( !$titles ) {
47  return '';
48  }
49  return implode( "\n", array_map( function ( Title $title ) {
50  return $title->getPrefixedText();
51  }, $titles ) );
52  }
53 
57  public function filterFromForm( $titles ) {
58  $titles = trim( $titles );
59  if ( $titles !== '' ) {
60  $titles = preg_split( '/\n/', $titles, -1, PREG_SPLIT_NO_EMPTY );
61  $ids = array_map( function ( $text ) {
62  $title = $this->getTitleFactory()->newFromText( $text );
63  if ( $title instanceof \Title && $title->getArticleID() > 0 ) {
64  return $title->getArticleID();
65  }
66  return false;
67  }, $titles );
68  if ( $ids ) {
69  return implode( "\n", $ids );
70  }
71  }
72  // If the titles list is null, it should be null (don't save) rather than an empty string.
73  return null;
74  }
75 
79  private function getTitleFactory() :TitleFactory {
80  $this->titleFactory = $this->titleFactory ?? new TitleFactory();
81  return $this->titleFactory;
82  }
83 }
MediaWiki\Preferences\MultiTitleFilter\getTitleFactory
getTitleFactory()
Definition: MultiTitleFilter.php:79
MediaWiki\Preferences
Definition: DefaultPreferencesFactory.php:21
MediaWiki\Preferences\MultiTitleFilter\__construct
__construct(TitleFactory $titleFactory=null)
Definition: MultiTitleFilter.php:36
$title
$title
Definition: testCompression.php:38
MediaWiki\Preferences\MultiTitleFilter\$titleFactory
TitleFactory $titleFactory
Definition: MultiTitleFilter.php:31
MediaWiki\Preferences\MultiTitleFilter
Definition: MultiTitleFilter.php:26
Title
Represents a title within MediaWiki.
Definition: Title.php:41
MediaWiki\Preferences\MultiTitleFilter\filterForForm
filterForForm( $value)
mixed
Definition: MultiTitleFilter.php:43
TitleFactory
Creates Title objects.
Definition: TitleFactory.php:33
MediaWiki\Preferences\MultiTitleFilter\filterFromForm
filterFromForm( $titles)
mixed
Definition: MultiTitleFilter.php:57
MediaWiki\Preferences\Filter
Base interface for user preference filters that work as a middleware between storage and interface.
Definition: Filter.php:27