| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 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 |
|
| 33 |
|
| 34 |
private $pageStore; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
private $titleFormatter; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 86 |
return null; |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
private function getPageStore(): PageStore { |
| 93 |
$this->pageStore ??= MediaWikiServices::getInstance()->getPageStore(); |
| 94 |
return $this->pageStore; |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
private function getTitleFormatter(): TitleFormatter { |
| 101 |
$this->titleFormatter ??= MediaWikiServices::getInstance()->getTitleFormatter(); |
| 102 |
return $this->titleFormatter; |
| 103 |
} |
| 104 |
} |