MediaWiki  master
CategoryPager.php
Go to the documentation of this file.
1 <?php
22 namespace MediaWiki\Pager;
23 
24 use HTMLForm;
25 use IContextSource;
32 
37 
38  private LinkBatchFactory $linkBatchFactory;
39 
47  public function __construct(
48  IContextSource $context,
49  LinkBatchFactory $linkBatchFactory,
50  LinkRenderer $linkRenderer,
51  IConnectionProvider $dbProvider,
52  $from
53  ) {
54  // Set database before parent constructor to avoid setting it there with wfGetDB
55  $this->mDb = $dbProvider->getReplicaDatabase();
56  parent::__construct( $context, $linkRenderer );
57  $this->linkBatchFactory = $linkBatchFactory;
58  $from = str_replace( ' ', '_', $from );
59  if ( $from !== '' ) {
60  $from = Title::capitalize( $from, NS_CATEGORY );
61  $this->setOffset( $from );
62  $this->setIncludeOffset( true );
63  }
64  }
65 
66  public function getQueryInfo() {
67  return [
68  'tables' => [ 'category' ],
69  'fields' => [ 'cat_title', 'cat_pages' ],
70  'options' => [ 'USE INDEX' => 'cat_title' ],
71  ];
72  }
73 
74  public function getIndexField() {
75  return 'cat_title';
76  }
77 
78  public function getDefaultQuery() {
79  parent::getDefaultQuery();
80  unset( $this->mDefaultQuery['from'] );
81 
82  return $this->mDefaultQuery;
83  }
84 
85  /* Override getBody to apply LinksBatch on resultset before actually outputting anything. */
86  public function getBody() {
87  $batch = $this->linkBatchFactory->newLinkBatch();
88 
89  $this->mResult->rewind();
90 
91  foreach ( $this->mResult as $row ) {
92  $batch->add( NS_CATEGORY, $row->cat_title );
93  }
94  $batch->execute();
95  $this->mResult->rewind();
96 
97  return parent::getBody();
98  }
99 
100  public function formatRow( $result ) {
101  $title = new TitleValue( NS_CATEGORY, $result->cat_title );
102  $text = $title->getText();
103  $link = $this->getLinkRenderer()->makeLink( $title, $text );
104 
105  $count = $this->msg( 'nmembers' )->numParams( $result->cat_pages )->escaped();
106  return Html::rawElement( 'li', [], $this->getLanguage()->specialList( $link, $count ) ) . "\n";
107  }
108 
109  public function getStartForm( $from ) {
110  $formDescriptor = [
111  'from' => [
112  'type' => 'title',
113  'namespace' => NS_CATEGORY,
114  'relative' => true,
115  'label-message' => 'categoriesfrom',
116  'name' => 'from',
117  'id' => 'from',
118  'size' => 20,
119  'default' => $from,
120  ],
121  ];
122 
123  $htmlForm = HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() )
124  ->setSubmitTextMsg( 'categories-submit' )
125  ->setWrapperLegendMsg( 'categories' )
126  ->setMethod( 'get' );
127  return $htmlForm->prepareForm()->getHTML( false );
128  }
129 
130 }
131 
136 class_alias( CategoryPager::class, 'CategoryPager' );
const NS_CATEGORY
Definition: Defines.php:78
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:158
static factory( $displayFormat, $descriptor, IContextSource $context, $messagePrefix='')
Construct a HTMLForm object for given display type.
Definition: HTMLForm.php:360
This class is a collection of static functions that serve two purposes:
Definition: Html.php:57
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:235
Class that generates HTML for internal links.
IndexPager with an alphabetic list and a formatted navigation bar.
getIndexField()
Returns the name of the index field.
getDefaultQuery()
Get an array of query parameters that should be put into self-links.
formatRow( $result)
Returns an HTML string representing the result row $row.
getQueryInfo()
Provides all parameters needed for the main paged query.
getBody()
Get the formatted result list.
__construct(IContextSource $context, LinkBatchFactory $linkBatchFactory, LinkRenderer $linkRenderer, IConnectionProvider $dbProvider, $from)
setOffset( $offset)
Set the offset from an other source than the request.
Definition: IndexPager.php:330
setIncludeOffset( $include)
Set whether a row matching exactly the offset should be also included in the result or not.
Definition: IndexPager.php:370
Represents the target of a wiki link.
Definition: TitleValue.php:44
Represents a title within MediaWiki.
Definition: Title.php:76
static capitalize( $text, $ns=NS_MAIN)
Capitalize a text string for a title if it belongs to a namespace that capitalizes.
Definition: Title.php:2717
Interface for objects which can provide a MediaWiki context on request.
Provide primary and replica IDatabase connections.
getReplicaDatabase( $domain=false, $group=null)
Get connection to a replica database.