MediaWiki master
CategoryPager.php
Go to the documentation of this file.
1<?php
9
10use MediaWiki\Cache\LinkBatchFactory;
19
24
25 private LinkBatchFactory $linkBatchFactory;
26
27 public function __construct(
28 IContextSource $context,
29 LinkBatchFactory $linkBatchFactory,
30 LinkRenderer $linkRenderer,
31 IConnectionProvider $dbProvider,
32 string $from
33 ) {
34 // Set database before parent constructor to avoid setting it there
35 $this->mDb = $dbProvider->getReplicaDatabase();
36 parent::__construct( $context, $linkRenderer );
37 $this->linkBatchFactory = $linkBatchFactory;
38 $from = str_replace( ' ', '_', $from );
39 if ( $from !== '' ) {
40 $from = Title::capitalize( $from, NS_CATEGORY );
41 $this->setOffset( $from );
42 $this->setIncludeOffset( true );
43 }
44 }
45
47 public function getQueryInfo() {
48 return [
49 'tables' => [ 'category' ],
50 'fields' => [ 'cat_title', 'cat_pages' ],
51 'options' => [ 'USE INDEX' => 'cat_title' ],
52 ];
53 }
54
56 public function getIndexField() {
57 return 'cat_title';
58 }
59
61 public function getDefaultQuery() {
62 parent::getDefaultQuery();
63 unset( $this->mDefaultQuery['from'] );
64
66 }
67
72 public function getBody() {
73 $batch = $this->linkBatchFactory->newLinkBatch();
74
75 $this->mResult->rewind();
76
77 foreach ( $this->mResult as $row ) {
78 $batch->add( NS_CATEGORY, $row->cat_title );
79 }
80 $batch->execute();
81 $this->mResult->rewind();
82
83 return parent::getBody();
84 }
85
87 public function formatRow( $result ) {
88 $title = new TitleValue( NS_CATEGORY, $result->cat_title );
89 $text = $title->getText();
90 $link = $this->getLinkRenderer()->makeLink( $title, $text );
91
92 $count = $this->msg( 'nmembers' )->numParams( $result->cat_pages )->escaped();
93 return Html::rawElement( 'li', [], $this->getLanguage()->specialList( $link, $count ) ) . "\n";
94 }
95
97 public function getStartForm( $from ) {
98 $formDescriptor = [
99 'from' => [
100 'type' => 'title',
101 'namespace' => NS_CATEGORY,
102 'relative' => true,
103 'label-message' => 'categoriesfrom',
104 'name' => 'from',
105 'id' => 'from',
106 'size' => 20,
107 'default' => $from,
108 ],
109 ];
110
111 $htmlForm = HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() )
112 ->setSubmitTextMsg( 'categories-submit' )
113 ->setWrapperLegendMsg( 'categories' )
114 ->setMethod( 'get' );
115 return $htmlForm->prepareForm()->getHTML( false );
116 }
117
118}
119
124class_alias( CategoryPager::class, 'CategoryPager' );
125
127class_alias( CategoryPager::class, 'MediaWiki\\Pager\\CategoryPager' );
const NS_CATEGORY
Definition Defines.php:65
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
getContext()
Get the base IContextSource object.
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition HTMLForm.php:195
This class is a collection of static functions that serve two purposes:
Definition Html.php:43
Class that generates HTML for internal links.
IndexPager with an alphabetic list and a formatted navigation bar.
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.
getQueryInfo()
Provides all parameters needed for the main paged query.It returns an associative array with the foll...
__construct(IContextSource $context, LinkBatchFactory $linkBatchFactory, LinkRenderer $linkRenderer, IConnectionProvider $dbProvider, string $from)
getIndexField()
Returns the name of the index field.If the pager supports multiple orders, it may return an array of ...
getDefaultQuery()
Get an array of query parameters that should be put into self-links.By default, all parameters passed...
formatRow( $result)
Returns an HTML string representing the result row $row.Rows will be concatenated and returned by get...
getBody()
Override getBody to apply LinksBatch on resultset before actually outputting anything.
Represents the target of a wiki link.
Represents a title within MediaWiki.
Definition Title.php:69
Interface for objects which can provide a MediaWiki context on request.
Provide primary and replica IDatabase connections.
getReplicaDatabase(string|false $domain=false, $group=null)
Get connection to a replica database.