MediaWiki REL1_39
CategoryPager.php
Go to the documentation of this file.
1<?php
25
30
32 private $linkBatchFactory;
33
41 public function __construct(
42 IContextSource $context,
43 LinkBatchFactory $linkBatchFactory,
44 LinkRenderer $linkRenderer,
45 ILoadBalancer $loadBalancer,
46 $from
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 $this->linkBatchFactory = $linkBatchFactory;
52 $from = str_replace( ' ', '_', $from );
53 if ( $from !== '' ) {
54 $from = Title::capitalize( $from, NS_CATEGORY );
55 $this->setOffset( $from );
56 $this->setIncludeOffset( true );
57 }
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', [], $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, LinkBatchFactory $linkBatchFactory, LinkRenderer $linkRenderer, ILoadBalancer $loadBalancer, $from)
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
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.
Class that generates HTML anchor link elements for pages.
Represents a page (or page fragment) title within MediaWiki.
Interface for objects which can provide a MediaWiki context on request.
Create and track the database connections and transactions for a given database cluster.
getConnectionRef( $i, $groups=[], $domain=false, $flags=0)