-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathWikilogCommentPager.php
More file actions
344 lines (300 loc) · 10.2 KB
/
WikilogCommentPager.php
File metadata and controls
344 lines (300 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
<?php
/**
* MediaWiki Wikilog extension
* Copyright © 2008-2010 Juliano F. Ravasi
* http://www.mediawiki.org/wiki/Extension:Wikilog
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*/
/**
* @file
* @ingroup Extensions
* @author Juliano F. Ravasi < dev juliano info >
*/
if ( !defined( 'MEDIAWIKI' ) )
die();
/**
* Common wikilog comment pager interface.
* @since Wikilog v1.1.0.
*/
abstract class WikilogCommentPager
extends IndexPager
{
/// Wikilog comment query data.
protected $mQuery = null;
/// Wikilog comment formatter.
protected $mFormatter = null;
/// If the pager is being included.
protected $mIncluding = false;
/// If displaying comments for a single article.
protected $mSingleItem = false;
/// Trigger for displaying a reply comment form.
protected $mReplyTrigger = null;
protected $mReplyCallback = null;
/**
* Constructor.
* @param $query WikilogCommentQuery Query object, containing the
* parameters that will select which comments will be shown.
* @param $formatter WikilogCommentFormatter Comment formatter object.
* @param $including boolean Whether the listing is being included in
* another page.
*/
function __construct( WikilogCommentQuery $query, $formatter = null,
$including = false )
{
global $wgUser, $wgParser;
global $wgWikilogNumComments, $wgWikilogExpensiveLimit;
# WikilogCommentQuery object drives our queries.
$this->mQuery = $query;
$this->mIncluding = $including;
# Prepare the comment formatter.
$this->mFormatter = $formatter ? $formatter :
new WikilogCommentFormatter( $this->getSkin() );
if ( $query->getIncludeSubpageComments() || !$query->getSubject() ) {
$this->mFormatter->setShowItem( true );
}
# Parent constructor.
parent::__construct();
# Fix our limits, Pager defaults are too high.
$this->mDefaultLimit = $wgWikilogNumComments;
list( $this->mLimit, /* $offset */ ) =
$this->mRequest->getLimitOffset( $wgWikilogNumComments, '' );
# This is too expensive, limit listing.
if ( $this->mLimit > $wgWikilogExpensiveLimit ) {
$this->mLimit = $wgWikilogExpensiveLimit;
}
}
/**
* Set the comment formatter.
* @param $formatter Comment formatter object.
* @return WikilogCommentFormatter Previous value.
*/
public function setFormatter( WikilogCommentFormatter $formatter ) {
return wfSetVar( $this->mFormatter, $formatter );
}
/**
* Set the reply trigger. This makes getBody() function to call back
* the given function $callback when the comment $id is displayed.
* This is used to inject a reply comment form after the comment.
*
* @param $id integer Comment ID that will trigger the callback.
* @param $callback callback Callback function, receives the comment
* as argument and should return an HTML fragment.
*/
public function setReplyTrigger( $id, $callback = null ) {
$this->mReplyTrigger = $id;
$this->mReplyCallback = $callback;
}
function getQueryInfo() {
return $this->mQuery->getQueryInfo( $this->mDb );
}
function getDefaultQuery() {
return parent::getDefaultQuery();
}
function getStartBody() {
return Xml::openElement( 'div', array( 'class' => 'wl-threads' ) );
}
function getEndBody() {
return Xml::closeElement( 'div' ); // wl-threads
}
function getEmptyBody() {
return WikilogUtils::wrapDiv( 'wl-empty', wfMessage( 'wikilog-pager-empty' )->text() );
}
function getNavigationBar() {
if ( !$this->isNavigationBarShown() ) {
return '';
}
if ( !isset( $this->mNavigationBar ) ) {
$navbar = new WikilogNavbar( $this );
$this->mNavigationBar = $navbar->getNavigationBar( $this->mLimit );
}
return $this->mNavigationBar;
}
}
/**
* Comment list pager.
*
* Lists wikilog comments in list format. If there are more comments than
* some threshold, navigation links are used to visit other pages of comments.
*/
class WikilogCommentListPager
extends WikilogCommentPager
{
public $mDefaultDirection = true;
/**
* Constructor.
* @param $query WikilogCommentQuery Query object, containing the
* parameters that will select which comments will be shown.
* @param $formatter WikilogCommentFormatter Comment formatter object.
* @param $including boolean Whether the listing is being included in
* another page.
*/
function __construct( WikilogCommentQuery $query, $formatter = null,
$including = false )
{
parent::__construct( $query, $formatter, $including );
}
function getIndexField() {
return 'wlc_timestamp';
}
function formatRow( $row ) {
# Retrieve comment data.
$subject = $this->mQuery->getIncludeSubpageComments() ? NULL : $this->mQuery->getSubject();
$comment = WikilogComment::newFromRow( $row, $subject );
$comment->loadText();
return $this->mFormatter->formatComment( $comment );
}
}
/**
* Comment thread pager.
*
* Lists wikilog comments in thread format. If there are more comments than
* some threshold, navigation links are used to visit other pages of comments.
* The thread pager also supports injecting a reply form below any comment.
*/
class WikilogCommentThreadPager
extends WikilogCommentPager
{
var $mRootLevel = -1;
/**
* Minimal comment nesting level needed for folding "linear" threads.
* @FIXME move into configuration
*/
var $mMinFoldLevel = 4;
/**
* Constructor.
* @param $query WikilogCommentQuery Query object, containing the
* parameters that will select which comments will be shown.
* @param $formatter WikilogCommentFormatter Comment formatter object.
* @param $including boolean Whether the listing is being included in
* another page.
*/
function __construct( WikilogCommentQuery $query, $formatter = false,
$including = false )
{
parent::__construct( $query, $formatter, $including );
}
function getIndexField() {
return 'wlc_id';
}
function getEndBody() {
return $this->mFormatter->closeCommentThreads() . parent::getEndBody();
}
function doQuery() {
if ( $this->mIsBackwards ) {
$this->mQuery->setNextCommentId( $this->mOffset ? $this->mOffset : 'MAX' );
} else {
$this->mQuery->setFirstCommentId( $this->mOffset );
}
$this->mQuery->setLimit( 'thread', $this->mLimit );
// Execute query
$dbr = wfGetDB( DB_SLAVE );
$res = $this->mQuery->select( $dbr, array(), false );
$nchild = array();
$rows = array();
foreach ( $res as $row ) {
if ( !isset( $nchild[ $row->wlc_parent ] ) ) {
$nchild[ $row->wlc_parent ] = 0;
}
$nchild[ $row->wlc_parent ]++;
$rows[ $row->wlc_id ] = $row;
}
// Determine root thread level
$rootThread = $this->mQuery->getThread();
if ( $rootThread ) {
$this->mRootLevel = count( WikilogUtils::decodeVarintArray( $rootThread ) );
}
// Fold non-forking comment threads when level goes above $this->mMinFoldLevel
foreach ( $rows as &$row ) {
if ( !$row->wlc_parent || !isset( $rows[ $row->wlc_parent ] ) ) {
$row->level = $this->mRootLevel+1;
} elseif ( $rows[ $row->wlc_parent ]->level-$this->mRootLevel-1 <= $this->mMinFoldLevel ||
$nchild[ $row->wlc_parent ] > 1 || $nchild[ $rows[ $row->wlc_parent ]->wlc_parent ] > 1 ) {
// Create a nested thread when either:
// - Nesting level is not above $this->mMinFoldLevel
// - This comment has a sibling
// - Parent comment has a sibling
$row->level = $rows[ $row->wlc_parent ]->level + 1;
} else {
// In other cases, do not start a nested thread ("fold" it)
$row->level = $rows[ $row->wlc_parent ]->level;
}
}
$this->mRows = $rows;
// Give Pager the parameters
$this->mIsFirst = !$this->mQuery->getRealFirstCommentId();
$this->mIsLast = !$this->mQuery->getRealNextCommentId();
$this->mLastShown = $this->mQuery->getRealNextCommentId();
$this->mFirstShown = $this->mQuery->getRealFirstCommentId();
}
function getBody() {
if ( !$this->mQueryDone ) {
$this->doQuery();
}
$html = '';
if ( count( $this->mRows ) ) {
// First preload comments to allow batch loading
$comments = array();
$subject = $this->mQuery->getIncludeSubpageComments() ? NULL : $this->mQuery->getSubject();
foreach ( $this->mRows as $i => $row ) {
$comments[$i] = WikilogComment::newFromRow( $row, $subject );
}
wfRunHooks( 'WikilogPreloadComments', array( $this, &$comments ) );
$level = $this->mRootLevel;
foreach ( $comments as $num => $comment ) {
// Open/close comment threads
$curLevel = $this->mRows[ $num ]->level;
if ( $curLevel > $level ) {
for ( $i = $level ; $i < $curLevel; $i++ ) {
$html .= '<div class="wl-thread">';
}
} elseif ( $curLevel < $level ) {
for ( $i = $curLevel; $i < $level; $i++ ) {
$html .= '</div>';
}
}
$level = $curLevel;
// Retrieve comment data.
$comment->loadText();
// Format comment
$doReply = $this->mReplyTrigger && $comment->mID == $this->mReplyTrigger;
$html .= $this->mFormatter->formatComment( $comment, $doReply );
if ( $doReply && is_callable( $this->mReplyCallback ) ) {
if ( ( $res = call_user_func( $this->mReplyCallback, $comment, true ) ) ) {
$html .= WikilogUtils::wrapDiv( 'wl-indent', $res );
}
}
}
for ( $i = $this->mRootLevel; $i < $level; $i++ ) {
$html .= '</div>';
}
} else {
$html .= $this->getEmptyBody();
}
return $html;
}
public function formatRow( $row ) {
$doReply = $this->mReplyTrigger && $comment->mID == $this->mReplyTrigger;
$html = $this->mFormatter->startCommentThread( $comment );
$html .= $this->mFormatter->formatComment( $comment, $doReply );
if ( $doReply && is_callable( $this->mReplyCallback ) ) {
if ( ( $res = call_user_func( $this->mReplyCallback, $comment ) ) ) {
$html .= WikilogUtils::wrapDiv( 'wl-indent', $res );
}
}
return $html;
}
}