MediaWiki master
CategoryPager.php
Go to the documentation of this file.
1<?php
9
19
24
25 public function __construct(
26 IContextSource $context,
27 private readonly LinkBatchFactory $linkBatchFactory,
28 LinkRenderer $linkRenderer,
29 IConnectionProvider $dbProvider,
30 string $from,
31 ) {
32 // Set database before parent constructor to avoid setting it there
33 $this->mDb = $dbProvider->getReplicaDatabase();
34 parent::__construct( $context, $linkRenderer );
35 $from = str_replace( ' ', '_', $from );
36 if ( $from !== '' ) {
37 $from = Title::capitalize( $from, NS_CATEGORY );
38 $this->setOffset( $from );
39 $this->setIncludeOffset( true );
40 }
41 }
42
44 public function getQueryInfo() {
45 return [
46 'tables' => [ 'category' ],
47 'fields' => [ 'cat_title', 'cat_pages' ],
48 'options' => [ 'USE INDEX' => 'cat_title' ],
49 ];
50 }
51
53 public function getIndexField() {
54 return 'cat_title';
55 }
56
58 public function getDefaultQuery() {
59 parent::getDefaultQuery();
60 unset( $this->mDefaultQuery['from'] );
61
63 }
64
69 public function getBody() {
70 $batch = $this->linkBatchFactory->newLinkBatch();
71
72 $this->mResult->rewind();
73
74 foreach ( $this->mResult as $row ) {
75 $batch->add( NS_CATEGORY, $row->cat_title );
76 }
77 $batch->execute();
78 $this->mResult->rewind();
79
80 return parent::getBody();
81 }
82
84 public function formatRow( $result ) {
85 $title = new TitleValue( NS_CATEGORY, $result->cat_title );
86 $text = $title->getText();
87 $link = $this->getLinkRenderer()->makeLink( $title, $text );
88
89 $count = $this->msg( 'nmembers' )->numParams( $result->cat_pages )->escaped();
90 return Html::rawElement( 'li', [], $this->getLanguage()->specialList( $link, $count ) ) . "\n";
91 }
92
94 public function getStartForm( $from ) {
95 $formDescriptor = [
96 'from' => [
97 'type' => 'title',
98 'namespace' => NS_CATEGORY,
99 'relative' => true,
100 'label-message' => 'categoriesfrom',
101 'name' => 'from',
102 'id' => 'from',
103 'size' => 20,
104 'default' => $from,
105 ],
106 ];
107
108 $htmlForm = HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() )
109 ->setSubmitTextMsg( 'categories-submit' )
110 ->setWrapperLegendMsg( 'categories' )
111 ->setMethod( 'get' );
112 return $htmlForm->prepareForm()->getHTML( false );
113 }
114
115}
116
121class_alias( CategoryPager::class, 'CategoryPager' );
122
124class_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:207
This class is a collection of static functions that serve two purposes:
Definition Html.php:44
Class that generates HTML for internal links.
Factory for LinkBatch objects to batch query page metadata.
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, private readonly 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.