Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
85.19% covered (warning)
85.19%
23 / 27
60.00% covered (warning)
60.00%
3 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
MultiTitleFilter
85.19% covered (warning)
85.19%
23 / 27
60.00% covered (warning)
60.00%
3 / 5
9.26
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 filterForForm
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
1
 filterFromForm
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
5
 getPageStore
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getTitleFormatter
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
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
21namespace MediaWiki\Preferences;
22
23use MediaWiki\MediaWikiServices;
24use MediaWiki\Page\PageStore;
25use MediaWiki\Page\ProperPageIdentity;
26use MediaWiki\Title\TitleFactory;
27use MediaWiki\Title\TitleFormatter;
28
29class 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}