MediaWiki REL1_37
MultiTitleFilter.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\Preferences;
22
23use Title;
24use TitleFactory;
25
26class MultiTitleFilter implements Filter {
27
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( static 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();
82 }
83}
__construct(TitleFactory $titleFactory=null)
Creates Title objects.
Represents a title within MediaWiki.
Definition Title.php:48
getArticleID( $flags=0)
Get the article ID for this Title from the link cache, adding it if necessary.
Definition Title.php:2902
Base interface for user preference filters that work as a middleware between storage and interface.
Definition Filter.php:27