MediaWiki REL1_37
CategoryPager.php
Go to the documentation of this file.
1<?php
25
30
33
41 public function __construct(
43 $from,
46 ILoadBalancer $loadBalancer
47 ) {
48 // Set database before parent constructor to avoid setting it there with wfGetDB
49 $this->mDb = $loadBalancer->getConnectionRef( ILoadBalancer::DB_REPLICA );
50 parent::__construct( $context, $linkRenderer );
51 $from = str_replace( ' ', '_', $from );
52 if ( $from !== '' ) {
53 $from = Title::capitalize( $from, NS_CATEGORY );
54 $this->setOffset( $from );
55 $this->setIncludeOffset( true );
56 }
57 $this->linkBatchFactory = $linkBatchFactory;
58 }
59
60 public function getQueryInfo() {
61 return [
62 'tables' => [ 'category' ],
63 'fields' => [ 'cat_title', 'cat_pages' ],
64 'options' => [ 'USE INDEX' => 'cat_title' ],
65 ];
66 }
67
68 public function getIndexField() {
69 return 'cat_title';
70 }
71
72 public function getDefaultQuery() {
73 parent::getDefaultQuery();
74 unset( $this->mDefaultQuery['from'] );
75
77 }
78
79 /* Override getBody to apply LinksBatch on resultset before actually outputting anything. */
80 public function getBody() {
81 $batch = $this->linkBatchFactory->newLinkBatch();
82
83 $this->mResult->rewind();
84
85 foreach ( $this->mResult as $row ) {
86 $batch->add( NS_CATEGORY, $row->cat_title );
87 }
88 $batch->execute();
89 $this->mResult->rewind();
90
91 return parent::getBody();
92 }
93
94 public function formatRow( $result ) {
95 $title = new TitleValue( NS_CATEGORY, $result->cat_title );
96 $text = $title->getText();
97 $link = $this->getLinkRenderer()->makeLink( $title, $text );
98
99 $count = $this->msg( 'nmembers' )->numParams( $result->cat_pages )->escaped();
100 return Html::rawElement( 'li', null, $this->getLanguage()->specialList( $link, $count ) ) . "\n";
101 }
102
103 public function getStartForm( $from ) {
104 $formDescriptor = [
105 'from' => [
106 'type' => 'title',
107 'namespace' => NS_CATEGORY,
108 'relative' => true,
109 'label-message' => 'categoriesfrom',
110 'name' => 'from',
111 'id' => 'from',
112 'size' => 20,
113 'default' => $from,
114 ],
115 ];
116
117 $htmlForm = HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() )
118 ->setSubmitTextMsg( 'categories-submit' )
119 ->setWrapperLegendMsg( 'categories' )
120 ->setMethod( 'get' );
121 return $htmlForm->prepareForm()->getHTML( false );
122 }
123
124}
const NS_CATEGORY
Definition Defines.php:78
IndexPager with an alphabetic list and a formatted navigation bar.
getDefaultQuery()
Get an array of query parameters that should be put into self-links.
getBody()
Get the formatted result list.
getIndexField()
Returns the name of the index field.
getQueryInfo()
Provides all parameters needed for the main paged query.
formatRow( $result)
Returns an HTML string representing the result row $row.
__construct(IContextSource $context, $from, LinkRenderer $linkRenderer, LinkBatchFactory $linkBatchFactory, ILoadBalancer $loadBalancer)
LinkBatchFactory $linkBatchFactory
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
IContextSource $context
getContext()
Get the base IContextSource object.
array $mDefaultQuery
setOffset( $offset)
Set the offset from an other source than the request.
setIncludeOffset( $include)
Set whether a row matching exactly the offset should be also included in the result or not.
LinkRenderer $linkRenderer
Class that generates HTML links for pages.
Represents a page (or page fragment) title within MediaWiki.
Interface for objects which can provide a MediaWiki context on request.
Database cluster connection, tracking, load balancing, and transaction manager interface.
getConnectionRef( $i, $groups=[], $domain=false, $flags=0)
Get a live database handle reference for a server index.