MediaWiki master
MultiTitleFilter.php
Go to the documentation of this file.
1<?php
8
14
15class MultiTitleFilter implements Filter {
16
20 private $pageStore;
21
25 private $titleFormatter;
26
32 public function __construct(
33 ?TitleFactory $titleFactory = null, ?PageStore $pageStore = null, ?TitleFormatter $titleFormatter = null ) {
34 $this->pageStore = $pageStore;
35 $this->titleFormatter = $titleFormatter;
36 }
37
41 public function filterForForm( $value ) {
42 $ids = array_map( 'intval', preg_split( '/\n/', $value, -1, PREG_SPLIT_NO_EMPTY ) );
43 $pageRecords = $this->getPageStore()
44 ->newSelectQueryBuilder()
45 ->wherePageIds( $ids )
46 ->caller( __METHOD__ )
47 ->fetchPageRecords();
48 return implode( "\n", array_map( function ( $pageRecord ) {
49 return $this->getTitleFormatter()->getPrefixedText( $pageRecord );
50 }, iterator_to_array( $pageRecords ) ) );
51 }
52
56 public function filterFromForm( $titles ) {
57 $titles = trim( $titles );
58 if ( $titles !== '' ) {
59 $titles = preg_split( '/\n/', $titles, -1, PREG_SPLIT_NO_EMPTY );
60 $ids = array_map( function ( $text ) {
61 $title = $this->getPageStore()->getPageByText( $text );
62 if ( $title instanceof ProperPageIdentity && $title->getId() > 0 ) {
63 return $title->getId();
64 }
65 return false;
66 }, $titles );
67 if ( $ids ) {
68 return implode( "\n", $ids );
69 }
70 }
71 // If the titles list is null, it should be null (don't save) rather than an empty string.
72 return null;
73 }
74
75 private function getPageStore(): PageStore {
76 $this->pageStore ??= MediaWikiServices::getInstance()->getPageStore();
77 return $this->pageStore;
78 }
79
80 private function getTitleFormatter(): TitleFormatter {
81 $this->titleFormatter ??= MediaWikiServices::getInstance()->getTitleFormatter();
82 return $this->titleFormatter;
83 }
84}
Service locator for MediaWiki core services.
__construct(?TitleFactory $titleFactory=null, ?PageStore $pageStore=null, ?TitleFormatter $titleFormatter=null)
Creates Title objects.
A title formatter service for MediaWiki.
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:13