MediaWiki  master
CategoryPager.php
Go to the documentation of this file.
1 <?php
27 
32 
34  private $linkBatchFactory;
35 
43  public function __construct(
44  IContextSource $context,
45  LinkBatchFactory $linkBatchFactory,
46  LinkRenderer $linkRenderer,
47  ILoadBalancer $loadBalancer,
48  $from
49  ) {
50  // Set database before parent constructor to avoid setting it there with wfGetDB
51  $this->mDb = $loadBalancer->getConnectionRef( ILoadBalancer::DB_REPLICA );
52  parent::__construct( $context, $linkRenderer );
53  $this->linkBatchFactory = $linkBatchFactory;
54  $from = str_replace( ' ', '_', $from );
55  if ( $from !== '' ) {
56  $from = Title::capitalize( $from, NS_CATEGORY );
57  $this->setOffset( $from );
58  $this->setIncludeOffset( true );
59  }
60  }
61 
62  public function getQueryInfo() {
63  return [
64  'tables' => [ 'category' ],
65  'fields' => [ 'cat_title', 'cat_pages' ],
66  'options' => [ 'USE INDEX' => 'cat_title' ],
67  ];
68  }
69 
70  public function getIndexField() {
71  return 'cat_title';
72  }
73 
74  public function getDefaultQuery() {
75  parent::getDefaultQuery();
76  unset( $this->mDefaultQuery['from'] );
77 
78  return $this->mDefaultQuery;
79  }
80 
81  /* Override getBody to apply LinksBatch on resultset before actually outputting anything. */
82  public function getBody() {
83  $batch = $this->linkBatchFactory->newLinkBatch();
84 
85  $this->mResult->rewind();
86 
87  foreach ( $this->mResult as $row ) {
88  $batch->add( NS_CATEGORY, $row->cat_title );
89  }
90  $batch->execute();
91  $this->mResult->rewind();
92 
93  return parent::getBody();
94  }
95 
96  public function formatRow( $result ) {
97  $title = new TitleValue( NS_CATEGORY, $result->cat_title );
98  $text = $title->getText();
99  $link = $this->getLinkRenderer()->makeLink( $title, $text );
100 
101  $count = $this->msg( 'nmembers' )->numParams( $result->cat_pages )->escaped();
102  return Html::rawElement( 'li', [], $this->getLanguage()->specialList( $link, $count ) ) . "\n";
103  }
104 
105  public function getStartForm( $from ) {
106  $formDescriptor = [
107  'from' => [
108  'type' => 'title',
109  'namespace' => NS_CATEGORY,
110  'relative' => true,
111  'label-message' => 'categoriesfrom',
112  'name' => 'from',
113  'id' => 'from',
114  'size' => 20,
115  'default' => $from,
116  ],
117  ];
118 
119  $htmlForm = HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() )
120  ->setSubmitTextMsg( 'categories-submit' )
121  ->setWrapperLegendMsg( 'categories' )
122  ->setMethod( 'get' );
123  return $htmlForm->prepareForm()->getHTML( false );
124  }
125 
126 }
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.
getStartForm( $from)
__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.
static factory( $displayFormat, $descriptor, IContextSource $context, $messagePrefix='')
Construct a HTMLForm object for given display type.
Definition: HTMLForm.php:352
array $mDefaultQuery
Definition: IndexPager.php:149
setOffset( $offset)
Set the offset from an other source than the request.
Definition: IndexPager.php:322
setIncludeOffset( $include)
Set whether a row matching exactly the offset should be also included in the result or not.
Definition: IndexPager.php:362
This class is a collection of static functions that serve two purposes:
Definition: Html.php:55
Class that generates HTML for internal links.
Represents a title within MediaWiki.
Definition: Title.php:82
Represents a page (or page fragment) title within MediaWiki.
Definition: TitleValue.php:40
Interface for objects which can provide a MediaWiki context on request.
This class is a delegate to ILBFactory for a given database cluster.
getConnectionRef( $i, $groups=[], $domain=false, $flags=0)
const DB_REPLICA
Definition: defines.php:26