-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathWikilogQuery.php
919 lines (826 loc) · 28.8 KB
/
WikilogQuery.php
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
<?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();
/**
* Wikilog SQL query driver base class.
*/
abstract class WikilogQuery
{
/**
* Array of defined options. Options are set with setOption(), and cause
* changes to the resulting SQL query generated by the class.
*/
protected $mOptions = array();
/**
* Default options. This array should be overriden by derived classes.
*/
protected $mDefaultOptions = array();
/**
* Whether the query should always return nothing (when invalid options
* are provided, for example).
*/
protected $mEmpty = false;
/**
* Constructor.
*/
public function __construct() {
}
/**
* Get option. Return default value if not set.
* @param $key string Name of the option to get.
* @return mixed Current value of the option.
*/
public function getOption( $key ) {
return isset( $this->mOptions[$key] )
? $this->mOptions[$key]
: $this->mDefaultOptions[$key];
}
/**
* Set option.
* @param $key string Name of the option to set.
* @param $value mixed Target value.
*/
public function setOption( $key, $value = true ) {
$this->mOptions[$key] = $value;
}
/**
* Set options.
* @param $opts mixed Options to set (string or array).
*/
public function setOptions( $opts ) {
if ( is_string( $opts ) ) {
$this->mOptions[$opts] = true;
} elseif ( is_array( $opts ) ) {
$this->mOptions = array_merge( $this->mOptions, $opts );
} elseif ( !is_null( $opts ) ) {
throw new MWException( __METHOD__ . ': Invalid $opts parameter.' );
}
}
/**
* Filter is always returns empty.
*/
public function setEmpty( $empty = true ) { $this->mEmpty = $empty; }
public function getEmpty() { return $this->mEmpty; }
/**
* Generate and return query information.
* @param $db Database Database object used to encode table names, etc.
* @param $opts mixed Misc query options.
* @return Associative array with the following keys:
* 'fields' => array of fields to query
* 'tables' => array of tables to query
* 'conds' => mixed array with select conditions
* 'options' => associative array of options
* 'join_conds' => associative array of table join conditions
* @see Database::select() for details.
*/
abstract function getQueryInfo( $db, $opts = null );
/**
* Return query information as an array of CGI parameters.
* @return array Array of query parameters.
*/
abstract function getDefaultQuery();
/**
* Convert a date tuple to a timestamp interval for database queries.
*
* @param $year Year to query. Current year is assumed if zero or false.
* @param $month Month to query. The whole year is assumed if zero or false.
* @param $day Day to query. The whole month is assumed if zero or false.
* @return Two-element array with the minimum and maximum values to query.
*/
public static function partialDateToInterval( &$year, &$month, &$day ) {
$year = ( $year > 0 && $year <= 9999 ) ? $year : false; // Y10k bug :-P
$month = ( $month > 0 && $month <= 12 ) ? $month : false;
$day = ( $day > 0 && $day <= 31 ) ? $day : false;
if ( !$year && !$month )
return false;
if ( !$year ) {
$year = intval( gmdate( 'Y' ) );
if ( $month > intval( gmdate( 'n' ) ) ) $year--;
}
$date_end = str_pad( $year + 1, 4, '0', STR_PAD_LEFT );
$date_start = str_pad( $year, 4, '0', STR_PAD_LEFT );
if ( $month ) {
$date_end = $date_start . str_pad( $month + 1, 2, '0', STR_PAD_LEFT );
$date_start = $date_start . str_pad( $month, 2, '0', STR_PAD_LEFT );
if ( $day ) {
$date_end = $date_start . str_pad( $day + 1, 2, '0', STR_PAD_LEFT );
$date_start = $date_start . str_pad( $day, 2, '0', STR_PAD_LEFT );
}
}
return array(
str_pad( $date_start, 14, '0', STR_PAD_RIGHT ),
str_pad( $date_end, 14, '0', STR_PAD_RIGHT )
);
}
/**
* Does $dbr->select with additional conditions taken from this query object
*/
public function select( $dbr, $tables, $fields, $conds = array(),
$function = __METHOD__, $options = array(), $join_conds = array()) {
return $dbr->query( $this->selectSQLText( $dbr, $tables, $fields, $conds, $function, $options, $join_conds ), $function );
}
/**
* Generates query text with additional conditions taken from this query object
*/
public function selectSQLText( $dbr, $tables, $fields, $conds = array(),
$function = __METHOD__, $options = array(), $join_conds = array() ) {
$info = $this->getQueryInfo( $dbr );
if ( $tables ) {
$tables = array_merge( $info['tables'], $tables );
} else {
$tables = $info['tables'];
}
if ( $info['conds'] ) {
$conds = $conds ? array_merge( $info['conds'], $conds ) : $info['conds'];
}
if ( $info['options'] ) {
$options = $options ? array_merge( $info['options'], $options ) : $info['options'];
}
if ( $info['join_conds'] ) {
$join_conds = $join_conds ? array_merge( $info['join_conds'], $join_conds ) : $info['join_conds'];
}
if ( !$fields ) {
$fields = $info['fields'];
}
return $dbr->selectSQLText( $tables, $fields, $conds, $function, $options, $join_conds );
}
}
/**
* Wikilog item SQL query driver.
* This class drives queries for wikilog items, given the fields to filter.
*/
class WikilogItemQuery
extends WikilogQuery
{
# Valid filter values for publish status.
const PS_ALL = 0; ///< Return all items
const PS_PUBLISHED = 1; ///< Return only published items
const PS_DRAFTS = 2; ///< Return only drafts
# Local variables.
private $mWikilogTitle = null; ///< Filter by wikilog.
private $mNamespace = false; ///< Filter by namespace.
private $mPubStatus = self::PS_ALL; ///< Filter by published status.
private $mCategory = false; ///< Include items in this category,
/// Include items in blogs blogs in this category.
private $mNotCategory = false; ///< Exclude items belonging to this category,
/// exclude items belonging to blog in this category.
private $mAuthor = false; ///< Filter by author.
private $mTag = false; ///< Filter by tag.
private $mDate = false; ///< Filter by date.
private $mNeedWikilogParam = false; ///< Need wikilog param in queries.
# Options
/** Query options. */
protected $mDefaultOptions = array(
'last-visit-date' => false
);
/**
* Constructor. Creates a new instance and optionally sets the Wikilog
* title to query.
* @param $wikilogTitle Wikilog title object to query for.
*/
public function __construct( $wikilogTitle = null ) {
parent::__construct();
$this->setWikilogTitle( $wikilogTitle );
# If constructed without a title (from Special:Wikilog), it means that
# the listing is global, and needs wikilog parameter to filter.
$this->mNeedWikilogParam = ( $wikilogTitle == null );
}
/**
* Sets the wikilog title to query for.
* @param $wikilogTitle Wikilog title object to query for.
*/
public function setWikilogTitle( $wikilogTitle ) {
$this->mWikilogTitle = $wikilogTitle;
}
/**
* Sets the wikilog namespace to query for.
* @param $ns Namespace to query for.
*/
public function setNamespace( $ns ) {
$this->mNamespace = $ns;
}
/**
* Sets the publish status to query for.
* @param $pubStatus Publish status, string or integer.
*/
public function setPubStatus( $pubStatus ) {
if ( is_null( $pubStatus ) ) {
$pubStatus = self::PS_PUBLISHED;
} elseif ( is_string( $pubStatus ) ) {
$pubStatus = self::parsePubStatusText( $pubStatus );
}
$this->mPubStatus = intval( $pubStatus );
}
/**
* Sets the category to query for.
* @param $category Category title object or text.
*/
public function setCategory( $category ) {
if ( is_null( $category ) || is_object( $category ) ) {
$this->mCategory = $category;
} elseif ( is_string( $category ) ) {
$t = Title::makeTitleSafe( NS_CATEGORY, $category );
if ( $t !== null ) {
$this->mCategory = $t;
}
}
}
/**
* Sets the category not to query for.
* @param $category Category title object or text.
*/
public function setNotCategory( $category ) {
if ( is_object( $category ) ) {
$this->mNotCategory = $category;
} elseif ( is_string( $category ) ) {
$t = Title::makeTitleSafe( NS_CATEGORY, $category );
if ( $t !== null ) {
$this->mNotCategory = $t;
}
}
}
/**
* Sets the author to query for.
* @param $author User page title object or text.
*/
public function setAuthor( $author ) {
if ( is_null( $author ) || is_object( $author ) ) {
$this->mAuthor = $author;
} elseif ( is_string( $author ) ) {
$t = Title::makeTitleSafe( NS_USER, $author );
if ( $t !== null ) {
$this->mAuthor = User::getCanonicalName( $t->getText() );
}
}
}
/**
* Sets the tag to query for.
* @param $tag Tag text.
*/
public function setTag( $tag ) {
global $wgWikilogEnableTags;
if ( $wgWikilogEnableTags ) {
$this->mTag = $tag;
}
}
/**
* Sets the date to query for.
* @param $year Publish date year.
* @param $month Publish date month, optional. If ommited, queries for
* items during the whole year.
* @param $day Publish date day, optional. If ommited, queries for items
* during the whole month or year.
*/
public function setDate( $year, $month = false, $day = false ) {
$interval = self::partialDateToInterval( $year, $month, $day );
if ( $interval ) {
list( $start, $end ) = $interval;
$this->mDate = (object)array(
'year' => $year,
'month' => $month,
'day' => $day,
'start' => $start,
'end' => $end
);
}
}
/**
* Accessor functions.
*/
public function getWikilogTitle() { return $this->mWikilogTitle; }
public function getNamespace() { return $this->mNamespace; }
public function getPubStatus() { return $this->mPubStatus; }
public function getCategory() { return $this->mCategory; }
public function getNotCategory() { return $this->mNotCategory; }
public function getAuthor() { return $this->mAuthor; }
public function getTag() { return $this->mTag; }
public function getDate() { return $this->mDate; }
/**
* Organizes all the query information and constructs the table and
* field lists that will later form the SQL SELECT statement.
* @param $db Database object.
* @param $opts Array with query options. Keys are option names, values
* are option values. Valid options are:
* - 'last-comment-timestamp': If true, the most recent article comment
* timestamps are included in the results. This is used in Atom feeds.
* @return Array with tables, fields, conditions, options and join
* conditions, to be used in a call to $db->select(...).
*/
public function getQueryInfo( $db, $opts = array() ) {
$this->setOptions( $opts );
# Basic defaults.
$wlp_tables = WikilogItem::selectTables( $db );
$q_tables = $wlp_tables['tables'];
$q_fields = WikilogItem::selectFields();
$q_conds = array( 'p.page_is_redirect' => 0 );
$q_options = array();
$q_joins = $wlp_tables['join_conds'];
# Invalid filter.
if ( $this->mEmpty ) {
$q_conds[] = '0=1';
}
# Filter by wikilog name.
if ( $this->mWikilogTitle !== null ) {
$q_conds['wlp_parent'] = $this->mWikilogTitle->getArticleID();
} elseif ( $this->mNamespace !== false ) {
$q_conds['p.page_namespace'] = $this->mNamespace;
}
# Filter by published status.
if ( $this->mPubStatus === self::PS_PUBLISHED ) {
$q_conds['wlp_publish'] = 1;
} elseif ( $this->mPubStatus === self::PS_DRAFTS ) {
$q_conds['wlp_publish'] = 0;
}
# Filter by category.
if ( $this->mCategory ) {
# Items and blogs
$q_tables['clyes'] = 'categorylinks';
$q_joins['clyes'] = array( 'JOIN', '(wlp_page = clyes.cl_from OR wlp_parent = clyes.cl_from)' );
$q_conds['clyes.cl_to'] = $this->mCategory->getDBkey();
$q_options['GROUP BY'] = 'wlp_page';
}
# Exclude items and blogs belonging to category.
if ( $this->mNotCategory ) {
# Items
$q_tables['clno'] = 'categorylinks';
$q_joins['clno'] = array( 'LEFT JOIN', array( 'wlp_page = clno.cl_from', 'clno.cl_to' => $this->mNotCategory->getDBkey() ) );
$q_conds[] = 'clno.cl_to IS NULL';
# Blogs
$q_tables['clnob'] = 'categorylinks';
$q_joins['clnob'] = array( 'LEFT JOIN', array( 'wlp_parent = clnob.cl_from', 'clnob.cl_to' => $this->mNotCategory->getDBkey() ) );
$q_conds[] = 'clnob.cl_to IS NULL';
}
# Filter by author.
if ( $this->mAuthor ) {
$q_tables[] = 'wikilog_authors';
$q_joins['wikilog_authors'] = array( 'JOIN', 'wlp_page = wla_page' );
$q_conds['wla_author_text'] = $this->mAuthor;
}
# Filter by tag.
if ( $this->mTag ) {
$q_tables[] = 'wikilog_tags';
$q_joins['wikilog_tags'] = array( 'JOIN', 'wlp_page = wlt_page' );
$q_conds['wlt_tag'] = $this->mTag;
}
# Filter by date.
if ( $this->mDate ) {
$q_conds[] = 'wlp_pubdate >= ' . $db->addQuotes( $this->mDate->start );
$q_conds[] = 'wlp_pubdate < ' . $db->addQuotes( $this->mDate->end );
}
# Last visit date
global $wgUser;
if ( $this->getOption( 'last-visit-date' ) && $wgUser->getID() ) {
$q_tables[] = 'page_last_visit';
$q_fields[] = 'pv_date wlp_last_visit';
$q_joins['page_last_visit'] = array( 'LEFT JOIN', array( 'pv_page = wlp_page', 'pv_user' => $wgUser->getID() ) );
}
return array(
'tables' => $q_tables,
'fields' => $q_fields,
'conds' => $q_conds,
'options' => $q_options,
'join_conds' => $q_joins
);
}
/**
* Returns the query information as an array suitable to be used to
* construct a URL to a wikilog or Special:Wikilog pages with the proper
* query parameters. Used in navigation links.
*/
public function getDefaultQuery() {
$query = array();
if ( $this->mNeedWikilogParam && $this->mWikilogTitle ) {
$query['wikilog'] = $this->mWikilogTitle->getPrefixedDBKey();
} elseif ( $this->mNamespace !== false ) {
$query['wikilog'] = Title::makeTitle( $this->mNamespace, "*" )->getPrefixedDBKey();
}
if ( $this->mPubStatus == self::PS_ALL ) {
$query['show'] = 'all';
} elseif ( $this->mPubStatus == self::PS_DRAFTS ) {
$query['show'] = 'drafts';
}
if ( $this->mCategory ) {
$query['category'] = $this->mCategory->getDBKey();
}
if ( $this->mNotCategory ) {
$query['notcategory'] = $this->mNotCategory->getDBKey();
}
if ( $this->mAuthor ) {
$query['author'] = $this->mAuthor;
}
if ( $this->mTag ) {
$query['tag'] = $this->mTag;
}
if ( $this->mDate ) {
$query['year'] = $this->mDate->year;
$query['month'] = $this->mDate->month;
$query['day'] = $this->mDate->day;
}
return $query;
}
/**
* Returns whether this query object returns articles from only a single
* wikilog.
*/
public function isSingleWikilog() {
return $this->mWikilogTitle !== null;
}
/**
* Parse a publication status text ( 'drafts', 'published', etc.) and
* return a self::PS_* constant that represents that status.
*/
public static function parsePubStatusText( $show = 'published' ) {
if ( $show == 'all' || $show == 'any' ) {
return self::PS_ALL;
} elseif ( $show == 'draft' || $show == 'drafts' ) {
return self::PS_DRAFTS;
} else {
return self::PS_PUBLISHED;
}
}
}
/**
* Wikilog comment SQL query driver.
* This class drives queries for wikilog comments, given the fields to filter.
* @since Wikilog v1.1.0.
*/
class WikilogCommentQuery
extends WikilogQuery
{
// Valid filter values for moderation status.
const MS_ALL = 'all'; ///< Return all comments.
const MS_ACCEPTED = 'accepted'; ///< Return only accepted comments.
const MS_PENDING = 'pending'; ///< Return only pending comments.
const MS_NOTDELETED = 'notdeleted'; ///< Return all but deleted comments.
const MS_NOTPENDING = 'notpending'; ///< Return all but pending comments.
public static $modStatuses = array(
self::MS_ALL, self::MS_ACCEPTED, self::MS_PENDING,
self::MS_NOTDELETED, self::MS_NOTPENDING
);
// Local variables.
private $mModStatus = self::MS_ALL; ///< Filter by moderation status.
private $mNamespace = false; ///< Filter by namespace.
private $mSubject = false; ///< Filter by subject article.
private $mThread = false; ///< Filter by thread.
private $mAuthor = false; ///< Filter by author.
private $mDate = false; ///< Filter by date.
private $mIncludeSubpages = false; ///< Include comments to all subpages of subject page.
private $mSort = 'thread'; ///< Sort order (only 'thread' is supported by now).
private $mLimit = 0; ///< Limit.
private $mFirstCommentId = false; ///< Forward navigation: ID of the first comment on page.
private $mNextCommentId = false; ///< Backward navigation: ID of the first comment on NEXT page.
// The real page boundaries are saved here after calling getQueryInfo().
// You can pass them back to WikilogCommentQuery using setXXXCommentId().
private $mRealFirstCommentId = false;
private $mRealNextCommentId = false;
/**
* Constructor.
* @param $from Title subject.
*/
public function __construct( $subject = null ) {
parent::__construct();
if ( $subject ) {
$this->setSubject( $subject );
}
}
/**
* Set the moderation status to query for.
* @param $modStatus Moderation status, string or integer.
*/
public function setModStatus( $modStatus ) {
if ( is_null( $modStatus ) ) {
$this->mModStatus = self::MS_ALL;
} elseif ( in_array( $modStatus, self::$modStatuses ) ) {
$this->mModStatus = $modStatus;
} else {
throw new MWException( __METHOD__ . ": Invalid moderation status." );
}
}
/**
* Set the namespace to query for. Only comments for articles published
* in the given namespace are returned. The wikilog and item filters have
* precedence over this filter.
* @param $ns Namespace to query for.
*/
public function setNamespace( $ns ) {
$this->mNamespace = $ns;
}
/**
* Set the page to query for. Only comments for the given article
* are returned. You may set includeSubpageComments() and then
* all comments for all subpages of this page will be also returned.
* @param Title $item page to query for
*/
public function setSubject( Title $page ) {
$this->mSubject = $page;
}
/**
* Set the comment thread to query for. Only replies to the given thread
* is returned. This is intended to be used together with setItem(), in
* order to use the proper database index (see the wlc_post_thread index).
* @param $thread Thread path identifier to query for (array or string).
*/
public function setThread( $thread ) {
$this->mThread = $thread;
}
/**
* Set sort order and limit.
* Note that the query sorted on thread ALWAYS includes full threads
* (threads are not broken)
*/
public function setLimit( $sort, $limit ) {
$this->mSort = $sort;
$this->mLimit = $limit;
}
/**
* Sets the author to query for.
* @param $author User page title object or text.
*/
public function setAuthor( $author ) {
if ( is_null( $author ) || is_object( $author ) ) {
$this->mAuthor = $author;
} elseif ( is_string( $author ) ) {
$t = Title::makeTitleSafe( NS_USER, $author );
if ( $t !== null ) {
$this->mAuthor = User::getCanonicalName( $t->getText() );
}
}
}
/**
* Set the date to query for.
* @param $year Comment year.
* @param $month Comment month, optional. If ommited, look for comments
* during the whole year.
* @param $day Comment day, optional. If ommited, look for comments
* during the whole month or year.
*/
public function setDate( $year, $month = false, $day = false ) {
$interval = self::partialDateToInterval( $year, $month, $day );
if ( $interval ) {
list( $start, $end ) = $interval;
$this->mDate = (object)array(
'year' => $year,
'month' => $month,
'day' => $day,
'start' => $start,
'end' => $end
);
}
}
public function setIncludeSubpageComments( $inc ) {
$this->mIncludeSubpages = $inc;
}
public function setFirstCommentId( $id ) {
$this->mFirstCommentId = $id;
}
public function setNextCommentId( $id ) {
$this->mNextCommentId = $id;
}
/**
* Accessor functions.
*/
public function getModStatus() { return $this->mModStatus; }
public function getNamespace() { return $this->mNamespace; }
public function getSubject() { return $this->mSubject; }
public function getThread() { return $this->mThread; }
public function getAuthor() { return $this->mAuthor; }
public function getDate() { return $this->mDate; }
public function getLimit() { return $this->mLimit; }
public function getIncludeSubpageComments() { return $this->mIncludeSubpages; }
public function getFirstCommentId() { return $this->mFirstCommentId; }
public function getNextCommentId() { return $this->mNextCommentId; }
public function getRealFirstCommentId() { return $this->mRealFirstCommentId; }
public function getRealNextCommentId() { return $this->mRealNextCommentId; }
/**
* Organizes all the query information and constructs the table and
* field lists that will later form the SQL SELECT statement.
* @param $db Database object.
* @param $opts Array with query options. Keys are option names, values
* are option values.
* @return Array with tables, fields, conditions, options and join
* conditions, to be used in a call to $db->select(...).
*/
public function getQueryInfo( $db, $opts = array() ) {
$this->setOptions( $opts );
# Basic defaults.
$wlc_tables = WikilogComment::selectTables();
$q_tables = $wlc_tables['tables'];
$q_fields = WikilogComment::selectFields();
$q_conds = array();
$q_options = array();
$q_joins = $wlc_tables['join_conds'];
# Invalid filter.
if ( $this->mEmpty ) {
$q_conds[] = '0=1';
}
# Filter by moderation status.
if ( $this->mModStatus == self::MS_ACCEPTED ) {
$q_conds['wlc_status'] = 'OK';
} elseif ( $this->mModStatus == self::MS_PENDING ) {
$q_conds['wlc_status'] = 'PENDING';
} elseif ( $this->mModStatus == self::MS_NOTDELETED ) {
$q_conds[] = "wlc_status <> " . $db->addQuotes( 'DELETED' );
} elseif ( $this->mModStatus == self::MS_NOTPENDING ) {
$q_conds[] = "wlc_status <> " . $db->addQuotes( 'PENDING' );
}
# Filter by subject page.
if ( $this->mSubject ) {
if ( $this->mIncludeSubpages ) {
$q_conds['p.page_namespace'] = $this->mSubject->getNamespace();
$q_conds[] = '(p.page_title = ' . $db->addQuotes( $this->mSubject->getDBkey() ) . ' OR p.page_title ' .
$db->buildLike( $this->mSubject->getDBkey() . '/', $db->anyString() ) . ')';
} else {
$q_conds['wlc_post'] = $this->mSubject->getArticleId();
if ( $this->mThread ) {
$q_conds[] = 'wlc_thread ' . $db->buildLike( $this->mThread, $db->anyChar(), $db->anyString() );
}
}
} elseif ( $this->mNamespace !== false ) {
$q_conds['c.page_namespace'] = $this->mNamespace;
}
# Filter by author.
if ( $this->mAuthor ) {
$q_conds['wlc_user_text'] = $this->mAuthor;
}
# Filter by date.
if ( $this->mDate ) {
$q_conds[] = 'wlc_timestamp >= ' . $db->timestamp( $this->mDate->start );
$q_conds[] = 'wlc_timestamp < ' . $db->timestamp( $this->mDate->end );
}
# Sort order and limits
if ( $this->mSort == 'thread' ) {
$dbr = wfGetDB( DB_SLAVE );
$first = $last = $back = false;
if ( $this->mNextCommentId ) {
// Backward navigation: next comment ID is set from the outside.
// Determine first comment ID from it.
if ( $this->mNextCommentId != 'MAX' ) {
$back = $last = $this->getPostThread( $dbr, $this->mNextCommentId );
} else {
// FIXME "Go to the last page" is kludgy anyway with our "not-break-the-thread" idea.
// I.e. "last page" will not be the same as if you navigate forward.
// This can be fixed only by partitioning the full query set into pages at the beginning.
$back = true;
}
} else {
// Forward navigation: first comment ID may be set from the outside.
// Determine real limit from it.
$first = $this->getPostThread( $dbr, $this->mFirstCommentId );
}
list( $first, $last ) = $this->getLimitPostThread( $dbr, $q_tables, $q_conds, $q_options, $q_joins,
$this->mIncludeSubpages ? false : $this->mThread, $first, $last, $back, $this->mLimit );
$q_options['ORDER BY'] = 'wlc_post, wlc_thread, wlc_id';
$q_conds[] = $this->getPostThreadCond( $dbr, $first, $last );
// Save real page boundaries so pager can retrieve them later
if ( $first ) {
$this->mRealFirstCommentId = $first->id;
}
if ( $last ) {
$this->mRealNextCommentId = $last->id;
}
}
return array(
'tables' => $q_tables,
'fields' => $q_fields,
'conds' => $q_conds,
'options' => $q_options,
'join_conds' => $q_joins
);
}
/**
* Determine $first or $last post/thread boundary from another condition
* ($last or $first respectively), query options and a limit.
* This is the function responsive for NOT CUTTING discussion threads in the middle.
* @return array( $newFirst, $newLast )
*/
function getLimitPostThread( $dbr, $tables, $conds, $options, $joins, $parentThread, $first, $last, $backwards, $limit ) {
if ( $limit <= 0 ) {
return false;
}
$tmpConds = $conds;
$tmpConds[] = $this->getPostThreadCond( $dbr, $first, $last );
$tmpOpts = $options;
$tmpOpts['LIMIT'] = 1;
$tmpOpts['OFFSET'] = $limit-1;
$dir = $backwards ? 'DESC' : 'ASC';
$tmpOpts['ORDER BY'] = "wlc_post $dir, wlc_thread $dir, wlc_id $dir";
$other = false;
// Select $limit'th comment, get post and thread from it
$res = $dbr->select( $tables, 'wlc_post, wlc_thread', $tmpConds, __METHOD__, $tmpOpts, $joins );
$row = $res->fetchObject();
if ( $row ) {
$thread = WikilogUtils::decodeVarintArray( $row->wlc_thread );
$parentThread = WikilogUtils::decodeVarintArray( $parentThread );
$p = count( $parentThread );
array_splice( $thread, $p+1 );
if ( !$backwards ) {
// Next child thread number under this parent (for LessThan condition)
$thread[$p]++;
}
$other = (object)array(
'post' => $row->wlc_post,
'thread' => WikilogUtils::encodeVarintArray( $thread ),
);
$tmpConds = $conds;
$tmpConds[] = $this->getPostThreadCond( $dbr, $backwards ? $first : $other, $backwards ? $other : $last );
$tmpOpts['OFFSET'] = 0;
// Get "other" comment id
$res = $dbr->select( $tables, 'wlc_id', $tmpConds, __METHOD__, $tmpOpts, $joins );
$row = $res->fetchObject();
$other->id = $row ? $row->wlc_id : false;
}
return $backwards ? array( $other, $last ) : array( $first, $other );
}
/**
* Get post and thread for comment #$id
*/
function getPostThread( $dbr, $id ) {
if ( !$id ) {
return false;
}
$res = $dbr->select( 'wikilog_comments', 'wlc_post, wlc_thread',
array( 'wlc_id' => $this->mFirstCommentId ), __METHOD__ );
$row = $dbr->fetchObject( $res );
if ( $row ) {
return (object)array(
'id' => $id,
'post' => intval( $row->wlc_post ),
'thread' => $row->wlc_thread,
);
}
return false;
}
/**
* Returns the condition to select comments between (first post, first thread) and (last post, last thread)
* @param object $first (post => post ID, thread => thread)
* @param object $last (post => post ID, thread => thread)
* @return string
*/
function getPostThreadCond( $dbr, $first, $last ) {
if ( !$first && !$last ) {
return '1=1';
}
if ( $first && $last && $first->post == $last->post ) {
return "wlc_post = {$first->post} AND (wlc_thread >= {$first->thread} AND wlc_thread < {$last->thread})";
}
$cond = array();
if ( $first ) {
$cond[] = "wlc_post >= {$first->post} AND (wlc_post > {$first->post}".
" OR wlc_thread >= ".$dbr->addQuotes( $first->thread ).")";
}
if ( $last ) {
$cond[] = "wlc_post <= {$last->post} AND (wlc_post < {$last->post}".
" OR wlc_thread < ".$dbr->addQuotes( $last->thread ).")";
}
return implode( ' AND ', $cond );
}
/**
* Returns the query information as an array suitable to be used to
* construct a URL to Special:WikilogComments with the proper query
* parameters. Used in navigation links.
*/
public function getDefaultQuery() {
$query = array();
//..............
if ( $this->mNamespace !== false ) {
$query['wikilog'] = Title::makeTitle( $this->mNamespace, "*" )->getPrefixedDBKey();
}
if ( $this->mModStatus != self::MS_ALL ) {
$query['show'] = $this->mModStatus;
}
if ( $this->mAuthor ) {
$query['author'] = $this->mAuthor;
}
if ( $this->mDate ) {
$query['year'] = $this->mDate->year;
$query['month'] = $this->mDate->month;
$query['day'] = $this->mDate->day;
}
return $query;
}
}