Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
SpecialFindComment
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 8
210
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 / 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
 alterForm
0.00% covered (danger)
0.00%
0 / 4
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
 execute
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 1
42
 displayItems
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
 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 FormSpecialPage;
6use Html;
7use HTMLForm;
8
9class SpecialFindComment extends FormSpecialPage {
10
11    private ThreadItemStore $threadItemStore;
12    private ThreadItemFormatter $threadItemFormatter;
13
14    public function __construct(
15        ThreadItemStore $threadItemStore,
16        ThreadItemFormatter $threadItemFormatter
17    ) {
18        parent::__construct( 'FindComment' );
19        $this->threadItemStore = $threadItemStore;
20        $this->threadItemFormatter = $threadItemFormatter;
21    }
22
23    /**
24     * @inheritDoc
25     */
26    protected function getFormFields() {
27        return [
28            'idorname' => [
29                'label-message' => 'discussiontools-findcomment-label-idorname',
30                'name' => 'idorname',
31                'type' => 'text',
32                'default' => $this->par,
33            ],
34        ];
35    }
36
37    /**
38     * @inheritDoc
39     */
40    protected function getDisplayFormat() {
41        return 'ooui';
42    }
43
44    /**
45     * @inheritDoc
46     */
47    protected function alterForm( HTMLForm $form ) {
48        $form->setMethod( 'GET' );
49        $form->setWrapperLegend( true );
50        $form->setSubmitTextMsg( 'discussiontools-findcomment-label-search' );
51        // Remove subpage when submitting
52        $form->setTitle( $this->getPageTitle() );
53    }
54
55    private $idOrName;
56
57    /**
58     * @inheritDoc
59     */
60    public function onSubmit( array $data ) {
61        $this->idOrName = $data['idorname'];
62        // Always display the form again
63        return false;
64    }
65
66    /**
67     * @inheritDoc
68     */
69    public function execute( $par ) {
70        parent::execute( $par );
71
72        $out = $this->getOutput();
73        $results = false;
74
75        if ( $this->idOrName ) {
76            $byId = $this->threadItemStore->findNewestRevisionsById( $this->idOrName );
77            if ( $byId ) {
78                $this->displayItems( $byId, 'discussiontools-findcomment-results-id' );
79                $results = true;
80            }
81
82            $byName = $this->threadItemStore->findNewestRevisionsByName( $this->idOrName );
83            if ( $byName ) {
84                $this->displayItems( $byName, 'discussiontools-findcomment-results-name' );
85                $results = true;
86            }
87        }
88
89        if ( $results ) {
90            $out->addHTML(
91                $this->msg( 'discussiontools-findcomment-gotocomment', $this->idOrName )->parseAsBlock() );
92        } elseif ( $this->idOrName ) {
93            $out->addHTML(
94                $this->msg( 'discussiontools-findcomment-noresults' )->parseAsBlock() );
95        }
96    }
97
98    /**
99     * @param array $threadItems
100     * @param string $msgKey
101     */
102    private function displayItems( array $threadItems, string $msgKey ) {
103        $out = $this->getOutput();
104
105        $list = [];
106        foreach ( $threadItems as $item ) {
107            $line = $this->threadItemFormatter->formatLine( $item, $this );
108            $list[] = Html::rawElement( 'li', [], $line );
109        }
110
111        $out->addHTML( $this->msg( $msgKey, count( $list ) )->parseAsBlock() );
112        $out->addHTML( Html::rawElement( 'ul', [], implode( '', $list ) ) );
113    }
114
115    /**
116     * @inheritDoc
117     */
118    public function getDescription() {
119        return $this->msg( 'discussiontools-findcomment-title' )->text();
120    }
121}