| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\DiscussionTools; |
| 4 |
|
| 5 |
use MediaWiki\Html\Html; |
| 6 |
use MediaWiki\HTMLForm\HTMLForm; |
| 7 |
use MediaWiki\SpecialPage\FormSpecialPage; |
| 8 |
|
| 9 |
class SpecialFindComment extends FormSpecialPage { |
| 10 |
|
| 11 |
private const LIST_LIMIT = 50; |
| 12 |
|
| 13 |
private ThreadItemStore $threadItemStore; |
| 14 |
private ThreadItemFormatter $threadItemFormatter; |
| 15 |
|
| 16 |
public function __construct( |
| 17 |
ThreadItemStore $threadItemStore, |
| 18 |
ThreadItemFormatter $threadItemFormatter |
| 19 |
) { |
| 20 |
parent::__construct( 'FindComment' ); |
| 21 |
$this->threadItemStore = $threadItemStore; |
| 22 |
$this->threadItemFormatter = $threadItemFormatter; |
| 23 |
} |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
protected function getFormFields() { |
| 29 |
return [ |
| 30 |
'idorname' => [ |
| 31 |
'label-message' => 'discussiontools-findcomment-label-idorname', |
| 32 |
'name' => 'idorname', |
| 33 |
'type' => 'text', |
| 34 |
'required' => true, |
| 35 |
], |
| 36 |
]; |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
protected function getSubpageField() { |
| 43 |
return 'idorname'; |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
protected function getDisplayFormat() { |
| 50 |
return 'ooui'; |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public function requiresPost() { |
| 57 |
return false; |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
protected function getShowAlways() { |
| 64 |
return true; |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
protected function alterForm( HTMLForm $form ) { |
| 71 |
$form->setWrapperLegend( true ); |
| 72 |
$form->setSubmitTextMsg( 'discussiontools-findcomment-label-search' ); |
| 73 |
} |
| 74 |
|
| 75 |
private string $idOrName; |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
public function onSubmit( array $data ) { |
| 81 |
|
| 82 |
|
| 83 |
$this->idOrName = str_replace( ' ', '_', $data['idorname'] ); |
| 84 |
return true; |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
public function onSuccess() { |
| 91 |
$out = $this->getOutput(); |
| 92 |
$results = false; |
| 93 |
|
| 94 |
if ( $this->idOrName ) { |
| 95 |
$byId = $this->threadItemStore->findNewestRevisionsById( $this->idOrName, static::LIST_LIMIT + 1 ); |
| 96 |
if ( $byId ) { |
| 97 |
$this->displayItems( $byId, 'discussiontools-findcomment-results-id' ); |
| 98 |
$results = true; |
| 99 |
} |
| 100 |
|
| 101 |
$byName = $this->threadItemStore->findNewestRevisionsByName( $this->idOrName, static::LIST_LIMIT + 1 ); |
| 102 |
if ( $byName ) { |
| 103 |
$this->displayItems( $byName, 'discussiontools-findcomment-results-name' ); |
| 104 |
$results = true; |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
if ( $results ) { |
| 109 |
$out->addHTML( |
| 110 |
$this->msg( 'discussiontools-findcomment-gotocomment', $this->idOrName )->parseAsBlock() ); |
| 111 |
} else { |
| 112 |
$out->addHTML( |
| 113 |
$this->msg( 'discussiontools-findcomment-noresults' )->parseAsBlock() ); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
private function displayItems( array $threadItems, string $msgKey ) { |
| 118 |
$out = $this->getOutput(); |
| 119 |
|
| 120 |
$list = []; |
| 121 |
foreach ( $threadItems as $item ) { |
| 122 |
if ( count( $list ) === static::LIST_LIMIT ) { |
| 123 |
break; |
| 124 |
} |
| 125 |
$line = $this->threadItemFormatter->formatLine( $item, $this ); |
| 126 |
$list[] = Html::rawElement( 'li', [], $line ); |
| 127 |
} |
| 128 |
|
| 129 |
$out->addHTML( $this->msg( $msgKey, count( $list ) )->parseAsBlock() ); |
| 130 |
$out->addHTML( Html::rawElement( 'ul', [], implode( '', $list ) ) ); |
| 131 |
if ( count( $threadItems ) > static::LIST_LIMIT ) { |
| 132 |
$out->addHTML( $this->msg( 'morenotlisted' )->parseAsBlock() ); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
public function getDescription() { |
| 140 |
return $this->msg( 'discussiontools-findcomment-title' ); |
| 141 |
} |
| 142 |
} |