Nighttime commit when I'm tired and want to get this thing checked in and go to bed...
[lhc/web/wiklou.git] / includes / SpecialCategories.php
1 <?php
2 /**
3 *
4 * @addtogroup SpecialPage
5 */
6
7 function wfSpecialCategories() {
8 global $wgOut;
9
10 $cap = new CategoryPager();
11 $wgOut->addHTML(
12 wfMsgExt( 'categoriespagetext', array( 'parse' ) ) .
13 $cap->getNavigationBar()
14 . '<ul>' . $cap->getBody() . '</ul>' .
15 $cap->getNavigationBar()
16 );
17 }
18
19 /**
20 * @addtogroup SpecialPage
21 * @addtogroup Pager
22 */
23 class CategoryPager extends AlphabeticPager {
24 private $mOrderType = 'abc';
25
26 public function __construct() {
27 parent::__construct();
28 if( $this->mRequest->getText( 'order' ) == 'count' ) {
29 $this->mOrderType = 'count';
30 }
31 if( $this->mRequest->getText( 'direction' ) == 'asc' ) {
32 $this->mDefaultDirection = false;
33 } elseif( $this->mRequest->getText( 'direction' ) == 'desc'
34 || $this->mOrderType == 'count' ) {
35 $this->mDefaultDirection = true;
36 }
37 }
38
39 function getQueryInfo() {
40 global $wgRequest;
41 return array(
42 'tables' => array( 'category' ),
43 'fields' => array( 'cat_title','cat_pages' ),
44 'conds' => array( 'cat_pages > 0' )
45 );
46 }
47
48 function getIndexField() {
49 # We can't use mOrderType here, since this is called from the parent
50 # constructor. Hmm.
51 if( $this->mRequest->getText( 'order' ) == 'count' ) {
52 return 'cat_pages';
53 } else {
54 return "cat_title";
55 }
56 }
57
58 /* Override getBody to apply LinksBatch on resultset before actually outputting anything. */
59 public function getBody() {
60 if (!$this->mQueryDone) {
61 $this->doQuery();
62 }
63 $batch = new LinkBatch;
64
65 $this->mResult->rewind();
66
67 while ( $row = $this->mResult->fetchObject() ) {
68 $batch->addObj( Title::makeTitleSafe( NS_CATEGORY, $row->cat_title ) );
69 }
70 $batch->execute();
71 $this->mResult->rewind();
72 return parent::getBody();
73 }
74
75 function formatRow($result) {
76 global $wgLang;
77 $title = Title::makeTitle( NS_CATEGORY, $result->cat_title );
78 $titleText = $this->getSkin()->makeLinkObj( $title, htmlspecialchars( $title->getText() ) );
79 $count = wfMsgExt( 'nmembers', array( 'parsemag', 'escape' ),
80 $wgLang->formatNum( $result->cat_pages ) );
81 return Xml::tags('li', null, "$titleText ($count)" ) . "\n";
82 }
83
84 /** Override this to order by count */
85 public function getNavigationBar() {
86 $nav = parent::getNavigationBar() . ' (';
87 if( $this->mOrderType == 'abc' ) {
88 $nav .= $this->makeLink(
89 wfMsgHTML( 'special-categories-sort-count' ),
90 array( 'order' => 'count' )
91 );
92 } else {
93 $nav .= $this->makeLink(
94 wfMsgHTML( 'special-categories-sort-abc' ),
95 array( 'order' => 'abc' )
96 );
97 }
98 $nav .= ') (';
99 # FIXME, these are stupid query names. "order" and "dir" are already
100 # used.
101 if( $this->mDefaultDirection ) {
102 # Descending
103 $nav .= $this->makeLink(
104 wfMsgHTML( 'special-categories-sort-asc' ),
105 array( 'direction' => 'asc' )
106 );
107 } else {
108 $nav .= $this->makeLink(
109 wfMsgHTML( 'special-categories-sort-desc' ),
110 array( 'direction' => 'desc' )
111 );
112 }
113 $nav .= ')';
114 return $nav;
115 }
116 }
117
118