Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 47
0.00% covered (danger)
0.00%
0 / 11
CRAP
0.00% covered (danger)
0.00%
0 / 1
SpecialFindComment
0.00% covered (danger)
0.00%
0 / 47
0.00% covered (danger)
0.00%
0 / 11
342
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 getFormFields
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 getSubpageField
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getDisplayFormat
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 requiresPost
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getShowAlways
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 alterForm
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 onSubmit
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 onSuccess
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
30
 displayItems
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
20
 getDescription
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace MediaWiki\Extension\DiscussionTools;
4
5use MediaWiki\Html\Html;
6use MediaWiki\HTMLForm\HTMLForm;
7use MediaWiki\SpecialPage\FormSpecialPage;
8
9class 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     * @inheritDoc
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     * @inheritDoc
41     */
42    protected function getSubpageField() {
43        return 'idorname';
44    }
45
46    /**
47     * @inheritDoc
48     */
49    protected function getDisplayFormat() {
50        return 'ooui';
51    }
52
53    /**
54     * @inheritDoc
55     */
56    public function requiresPost() {
57        return false;
58    }
59
60    /**
61     * @inheritDoc
62     */
63    protected function getShowAlways() {
64        return true;
65    }
66
67    /**
68     * @inheritDoc
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     * @inheritDoc
79     */
80    public function onSubmit( array $data ) {
81        // They are correctly written with underscores, but allow spaces too for consistency with
82        // the behavior of internal wiki links.
83        $this->idOrName = str_replace( ' ', '_', $data['idorname'] );
84        return true;
85    }
86
87    /**
88     * @inheritDoc
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     * @inheritDoc
138     */
139    public function getDescription() {
140        return $this->msg( 'discussiontools-findcomment-title' );
141    }
142}