MediaWiki  master
MultiTitleFilter.php
Go to the documentation of this file.
1 <?php
21 namespace MediaWiki\Preferences;
22 
28 
29 class MultiTitleFilter implements Filter {
30 
34  private $pageStore;
35 
39  private $titleFormatter;
40 
46  public function __construct(
47  TitleFactory $titleFactory = null, PageStore $pageStore = null, TitleFormatter $titleFormatter = null ) {
48  $this->pageStore = $pageStore;
49  $this->titleFormatter = $titleFormatter;
50  }
51 
55  public function filterForForm( $value ) {
56  $ids = array_map( 'intval', preg_split( '/\n/', $value, -1, PREG_SPLIT_NO_EMPTY ) );
57  $pageRecords = $this->getPageStore()
58  ->newSelectQueryBuilder()
59  ->wherePageIds( $ids )
60  ->caller( __METHOD__ )
61  ->fetchPageRecords();
62  return implode( "\n", array_map( function ( $pageRecord ) {
63  return $this->getTitleFormatter()->getPrefixedText( $pageRecord );
64  }, iterator_to_array( $pageRecords ) ) );
65  }
66 
70  public function filterFromForm( $titles ) {
71  $titles = trim( $titles );
72  if ( $titles !== '' ) {
73  $titles = preg_split( '/\n/', $titles, -1, PREG_SPLIT_NO_EMPTY );
74  $ids = array_map( function ( $text ) {
75  $title = $this->getPageStore()->getPageByText( $text );
76  if ( $title instanceof ProperPageIdentity && $title->getId() > 0 ) {
77  return $title->getId();
78  }
79  return false;
80  }, $titles );
81  if ( $ids ) {
82  return implode( "\n", $ids );
83  }
84  }
85  // If the titles list is null, it should be null (don't save) rather than an empty string.
86  return null;
87  }
88 
92  private function getPageStore(): PageStore {
93  $this->pageStore ??= MediaWikiServices::getInstance()->getPageStore();
94  return $this->pageStore;
95  }
96 
100  private function getTitleFormatter(): TitleFormatter {
101  $this->titleFormatter ??= MediaWikiServices::getInstance()->getTitleFormatter();
102  return $this->titleFormatter;
103  }
104 }
Service locator for MediaWiki core services.
__construct(TitleFactory $titleFactory=null, PageStore $pageStore=null, TitleFormatter $titleFormatter=null)
Creates Title objects.
Interface for a page that is (or could be, or used to be) an editable wiki page.
getId( $wikiId=self::LOCAL)
Returns the page ID.
Base interface for user preference filters that work as a middleware between storage and interface.
Definition: Filter.php:27
A title formatter service for MediaWiki.