MediaWiki  master
CategoryPager.php
Go to the documentation of this file.
1 <?php
25 
30 
33 
41  public function __construct(
43  $from,
46  ILoadBalancer $loadBalancer
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  $from = str_replace( ' ', '_', $from );
52  if ( $from !== '' ) {
53  $from = Title::capitalize( $from, NS_CATEGORY );
54  $this->setOffset( $from );
55  $this->setIncludeOffset( true );
56  }
57  $this->linkBatchFactory = $linkBatchFactory;
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 
76  return $this->mDefaultQuery;
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', null, $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 }
ContextSource\$context
IContextSource $context
Definition: ContextSource.php:37
ContextSource\getContext
getContext()
Get the base IContextSource object.
Definition: ContextSource.php:45
CategoryPager\$linkBatchFactory
LinkBatchFactory $linkBatchFactory
Definition: CategoryPager.php:32
MediaWiki\Linker\LinkRenderer
Class that generates HTML links for pages.
Definition: LinkRenderer.php:41
AlphabeticPager
IndexPager with an alphabetic list and a formatted navigation bar Stable to extend.
Definition: AlphabeticPager.php:29
IndexPager\$linkRenderer
LinkRenderer $linkRenderer
Definition: IndexPager.php:167
Wikimedia\Rdbms\ILoadBalancer\getConnectionRef
getConnectionRef( $i, $groups=[], $domain=false, $flags=0)
Get a live database handle reference for a real or virtual (DB_MASTER/DB_REPLICA) server index.
ContextSource\getLanguage
getLanguage()
Definition: ContextSource.php:143
CategoryPager\getBody
getBody()
Get the formatted result list.
Definition: CategoryPager.php:80
CategoryPager\getQueryInfo
getQueryInfo()
Provides all parameters needed for the main paged query.
Definition: CategoryPager.php:60
CategoryPager\getStartForm
getStartForm( $from)
Definition: CategoryPager.php:103
MediaWiki\Cache\LinkBatchFactory
Definition: LinkBatchFactory.php:38
$title
$title
Definition: testCompression.php:38
CategoryPager\getDefaultQuery
getDefaultQuery()
Get an array of query parameters that should be put into self-links.
Definition: CategoryPager.php:72
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
NS_CATEGORY
const NS_CATEGORY
Definition: Defines.php:83
CategoryPager
Definition: CategoryPager.php:29
IndexPager\setIncludeOffset
setIncludeOffset( $include)
Set whether a row matching exactly the offset should be also included in the result or not.
Definition: IndexPager.php:352
ContextSource\msg
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
Definition: ContextSource.php:187
CategoryPager\__construct
__construct(IContextSource $context, $from, LinkRenderer $linkRenderer, LinkBatchFactory $linkBatchFactory, ILoadBalancer $loadBalancer)
Definition: CategoryPager.php:41
IndexPager\setOffset
setOffset( $offset)
Set the offset from an other source than the request.
Definition: IndexPager.php:312
IndexPager\getLinkRenderer
getLinkRenderer()
Definition: IndexPager.php:976
CategoryPager\formatRow
formatRow( $result)
Returns an HTML string representing the result row $row.
Definition: CategoryPager.php:94
IContextSource
Interface for objects which can provide a MediaWiki context on request.
Definition: IContextSource.php:55
Title\capitalize
static capitalize( $text, $ns=NS_MAIN)
Capitalize a text string for a title if it belongs to a namespace that capitalizes.
Definition: Title.php:3298
Html\rawElement
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:212
HTMLForm\factory
static factory( $displayFormat,... $arguments)
Construct a HTMLForm object for given display type.
Definition: HTMLForm.php:316
IndexPager\$mDefaultQuery
array $mDefaultQuery
Definition: IndexPager.php:149
CategoryPager\getIndexField
getIndexField()
Returns the name of the index field.
Definition: CategoryPager.php:68
Wikimedia\Rdbms\ILoadBalancer
Database cluster connection, tracking, load balancing, and transaction manager interface.
Definition: ILoadBalancer.php:81
TitleValue
Represents a page (or page fragment) title within MediaWiki.
Definition: TitleValue.php:39