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