Debian Codegraph @

includes/preferences/MultiTitleFilter.php

raw symbols SCIP
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21 namespace MediaWiki\Preferences;
22
23 use MediaWiki\MediaWikiServices;
24 use MediaWiki\Page\PageStore;
25 use MediaWiki\Page\ProperPageIdentity;
26 use MediaWiki\Title\TitleFactory;
27 use MediaWiki\Title\TitleFormatter;
28
29 class MultiTitleFilter implements Filter {
30
31 /**
32 * @var PageStore
33 */
34 private $pageStore;
35
36 /**
37 * @var TitleFormatter
38 */
39 private $titleFormatter;
40
41 /**
42 * @param TitleFactory|null $titleFactory unused
43 * @param PageStore|null $pageStore
44 * @param TitleFormatter|null $titleFormatter
45 */
46 public function __construct(
47 ?TitleFactory $titleFactory = null, ?PageStore $pageStore = null, ?TitleFormatter $titleFormatter = null ) {
48 $this->pageStore = $pageStore;
49 $this->titleFormatter = $titleFormatter;
50 }
51
52 /**
53 * @inheritDoc
54 */
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
67 /**
68 * @inheritDoc
69 */
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
89 /**
90 * @return PageStore
91 */
92 private function getPageStore(): PageStore {
93 $this->pageStore ??= MediaWikiServices::getInstance()->getPageStore();
94 return $this->pageStore;
95 }
96
97 /**
98 * @return TitleFormatter
99 */
100 private function getTitleFormatter(): TitleFormatter {
101 $this->titleFormatter ??= MediaWikiServices::getInstance()->getTitleFormatter();
102 return $this->titleFormatter;
103 }
104 }