Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 123
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
NewUserMessagesView
0.00% covered (danger)
0.00%
0 / 123
0.00% covered (danger)
0.00%
0 / 7
420
0.00% covered (danger)
0.00%
0 / 1
 htmlForReadButton
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
2
 getReadAllButton
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 getUndoButton
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 1
12
 postDivClass
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 showOnce
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 1
90
 show
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
12
 showWrappedThread
0.00% covered (danger)
0.00%
0 / 32
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3use MediaWiki\Deferred\DeferredUpdates;
4use MediaWiki\Html\Html;
5use MediaWiki\MediaWikiServices;
6
7class NewUserMessagesView extends LqtView {
8    /** @var int[] */
9    protected $highlightThreads;
10    /** @var array[] */
11    protected $messagesInfo;
12
13    protected function htmlForReadButton( $label, $title, $class, $ids ) {
14        $ids_s = implode( ',', $ids );
15        $html = '';
16        $html .= Html::hidden( 'lqt_method', 'mark_as_read' );
17        $html .= Html::hidden( 'lqt_operand', $ids_s );
18        $html .= Xml::submitButton(
19            $label,
20            [
21                'name' => 'lqt_read_button',
22                'title' => $title,
23                'class' => 'lqt-read-button'
24            ]
25        );
26        $html = Xml::tags( 'form', [ 'method' => 'post', 'class' => $class ], $html );
27
28        return $html;
29    }
30
31    public function getReadAllButton() {
32        return $this->htmlForReadButton(
33            wfMessage( 'lqt-read-all' )->text(),
34            wfMessage( 'lqt-read-all-tooltip' )->text(),
35            'lqt_newmessages_read_all_button',
36            [ 'all' ]
37        );
38    }
39
40    public function getUndoButton( $ids ) {
41        if ( count( $ids ) == 1 ) {
42            $t = Threads::withId( (int)$ids[0] );
43            if ( !$t ) {
44                return; // empty or just bogus operand.
45            }
46            $msg = wfMessage( 'lqt-marked-read', LqtView::formatSubject( $t->subject() ) )->parse();
47        } else {
48            $msg = wfMessage( 'lqt-count-marked-read' )->numParams( count( $ids ) )->parse();
49        }
50        $operand = implode( ',', $ids );
51
52        $html = '';
53        $html .= $msg;
54        $html .= Html::hidden( 'lqt_method', 'mark_as_unread' );
55        $html .= Html::hidden( 'lqt_operand', $operand );
56        $html .= ' ' . Xml::submitButton(
57            wfMessage( 'lqt-email-undo' )->text(),
58            [
59                'name' => 'lqt_read_button',
60                'title' => wfMessage( 'lqt-email-info-undo' )->text()
61            ]
62        );
63
64        $html = Xml::tags(
65            'form',
66            [ 'method' => 'post', 'class' => 'lqt_undo_mark_as_read' ],
67            $html
68        );
69
70        return $html;
71    }
72
73    public function postDivClass( Thread $thread ) {
74        $origClass = parent::postDivClass( $thread );
75
76        if ( in_array( $thread->id(), $this->highlightThreads ) ) {
77            return "$origClass lqt_post_new_message";
78        }
79
80        return $origClass;
81    }
82
83    public function showOnce() {
84        NewMessages::recacheMessageCount( $this->user->getId() );
85
86        $user = $this->user;
87        DeferredUpdates::addCallableUpdate( static function () use ( $user ) {
88            MediaWikiServices::getInstance()
89                ->getTalkPageNotificationManager()->removeUserHasNewMessages( $user );
90        } );
91
92        if ( $this->methodApplies( 'mark_as_unread' ) ) {
93            $ids = explode( ',', $this->request->getVal( 'lqt_operand', '' ) );
94
95            foreach ( $ids as $id ) {
96                $tmp_thread = Threads::withId( (int)$id );
97                if ( $tmp_thread ) {
98                    NewMessages::markThreadAsUnreadByUser( $tmp_thread, $this->user );
99                }
100            }
101            $this->output->redirect( $this->title->getLocalURL() );
102        } elseif ( $this->methodApplies( 'mark_as_read' ) ) {
103            $ids = explode( ',', $this->request->getVal( 'lqt_operand' ) );
104            foreach ( $ids as $id ) {
105                if ( $id == 'all' ) {
106                    NewMessages::markAllReadByUser( $this->user );
107                } else {
108                    $tmp_thread = Threads::withId( (int)$id );
109                    if ( $tmp_thread ) {
110                        NewMessages::markThreadAsReadByUser( $tmp_thread, $this->user );
111                    }
112                }
113            }
114            $query = 'lqt_method=undo_mark_as_read&lqt_operand=' . implode( ',', $ids );
115            $this->output->redirect( $this->title->getLocalURL( $query ) );
116        } elseif ( $this->methodApplies( 'undo_mark_as_read' ) ) {
117            $ids = explode( ',', $this->request->getVal( 'lqt_operand', '' ) );
118            $this->output->addHTML( $this->getUndoButton( $ids ) );
119        }
120    }
121
122    /**
123     * @return bool
124     */
125    public function show() {
126        $pager = new LqtNewMessagesPager( $this->user );
127        $this->messagesInfo = $pager->getThreads();
128
129        if ( !$this->messagesInfo ) {
130            $this->output->addWikiMsg( 'lqt-no-new-messages' );
131            return false;
132        }
133
134        $this->output->addModuleStyles( $pager->getModuleStyles() );
135
136        $this->output->addHTML( $this->getReadAllButton() );
137        $this->output->addHTML( $pager->getNavigationBar() );
138
139        $this->output->addHTML( '<table class="lqt-new-messages"><tbody>' );
140
141        foreach ( $this->messagesInfo as $info ) {
142            // It turns out that with lqtviews composed of threads from various talkpages,
143            // each thread is going to have a different article... this is pretty ugly.
144            $thread = $info['top'];
145            $this->highlightThreads = $info['posts'];
146            $this->article = $thread->article();
147
148            $this->showWrappedThread( $thread );
149        }
150
151        $this->output->addHTML( '</tbody></table>' );
152
153        $this->output->addHTML( $pager->getNavigationBar() );
154
155        return false;
156    }
157
158    public function showWrappedThread( $t ) {
159        $read_button = $this->htmlForReadButton(
160            wfMessage( 'lqt-read-message' )->text(),
161            wfMessage( 'lqt-read-message-tooltip' )->text(),
162            'lqt_newmessages_read_button',
163            $this->highlightThreads );
164
165        // Left-hand column read button and context link to the full thread.
166        $topmostThread = $t->topmostThread();
167        $title = clone $topmostThread->getTitle();
168        $title->setFragment( '#' . $t->getAnchorName() );
169
170        // Make sure it points to the right page. The Pager seems to use the DB
171        // representation of a timestamp for its offset field, odd.
172        $dbr = MediaWikiServices::getInstance()->getConnectionProvider()->getReplicaDatabase();
173        $offset = (int)wfTimestamp( TS_UNIX, $topmostThread->modified() ) + 1;
174        $offset = $dbr->timestamp( $offset );
175        $linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
176
177        $contextLink = $linkRenderer->makeKnownLink(
178            $title,
179            new HtmlArmor( wfMessage( 'lqt-newmessages-context' )->parse() ),
180            [],
181            [ 'offset' => $offset ]
182        );
183
184        $talkpageLink = $linkRenderer->makeLink( $topmostThread->getTitle() );
185        $talkpageInfo = wfMessage( 'lqt-newmessages-from' )
186            ->rawParams( $talkpageLink )->parseAsBlock();
187
188        $leftColumn = Xml::tags( 'p', null, $read_button ) .
189                        Xml::tags( 'p', null, $contextLink ) .
190                        $talkpageInfo;
191        $leftColumn = Xml::tags( 'td', [ 'class' => 'lqt-newmessages-left' ],
192                                    $leftColumn );
193        $html = "<tr>$leftColumn<td class='lqt-newmessages-right'>";
194        $this->output->addHTML( $html );
195
196        $mustShowThreads = $this->highlightThreads;
197
198        $this->showThread( $t, 1, 1, [ 'mustShowThreads' => $mustShowThreads ] );
199        $this->output->addModules( 'ext.liquidThreads.newMessages' );
200        $this->output->addHTML( "</td></tr>" );
201    }
202}