MediaWiki master
SpecialShortPages.php
Go to the documentation of this file.
1<?php
7namespace MediaWiki\Specials;
8
17use stdClass;
21
28
29 public function __construct(
30 private readonly NamespaceInfo $namespaceInfo,
31 IConnectionProvider $dbProvider,
32 LinkBatchFactory $linkBatchFactory
33 ) {
34 parent::__construct( 'Shortpages' );
35 $this->setDatabaseProvider( $dbProvider );
36 $this->setLinkBatchFactory( $linkBatchFactory );
37 }
38
40 public function isSyndicated() {
41 return false;
42 }
43
45 public function getQueryInfo() {
46 $config = $this->getConfig();
47 $tables = [ 'page' ];
48 $conds = [
49 'page_namespace' => array_diff(
50 $this->namespaceInfo->getContentNamespaces(),
52 ),
53 'page_is_redirect' => 0
54 ];
55 $joinConds = [];
56 $options = [ 'USE INDEX' => [ 'page' => 'page_redirect_namespace_len' ] ];
57
58 // Allow extensions to modify the query
59 $this->getHookRunner()->onShortPagesQuery( $tables, $conds, $joinConds, $options );
60
61 return [
62 'tables' => $tables,
63 'fields' => [
64 'namespace' => 'page_namespace',
65 'title' => 'page_title',
66 'value' => 'page_len'
67 ],
68 'conds' => $conds,
69 'join_conds' => $joinConds,
70 'options' => $options
71 ];
72 }
73
75 public function reallyDoQuery( $limit, $offset = false ) {
76 $fname = static::class . '::reallyDoQuery';
77 $dbr = $this->getRecacheDB();
78 $query = $this->getQueryInfo();
79 $conds = isset( $query['conds'] ) ? (array)$query['conds'] : [];
80 $namespaces = $conds['page_namespace'];
81 unset( $conds['page_namespace'] );
82
83 if ( count( $namespaces ) === 1 || !$dbr->unionSupportsOrderAndLimit() ) {
84 return parent::reallyDoQuery( $limit, $offset );
85 }
86
87 // Optimization: Fix slow query on MySQL in the case of multiple content namespaces,
88 // by rewriting this as a UNION of separate single namespace queries (T168010).
89 $sqb = $dbr->newSelectQueryBuilder()
90 ->select( isset( $query['fields'] ) ? (array)$query['fields'] : [] )
91 ->tables( isset( $query['tables'] ) ? (array)$query['tables'] : [] )
92 ->where( $conds )
93 ->options( isset( $query['options'] ) ? (array)$query['options'] : [] )
94 ->joinConds( isset( $query['join_conds'] ) ? (array)$query['join_conds'] : [] );
95 if ( $limit !== false ) {
96 if ( $offset !== false ) {
97 // We need to increase the limit by the offset rather than
98 // using the offset directly, otherwise it'll skip incorrectly
99 // in the subqueries.
100 $sqb->limit( intval( $limit ) + intval( $offset ) );
101 } else {
102 $sqb->limit( intval( $limit ) );
103 }
104 }
105
106 $order = $this->getOrderFields();
107 if ( $this->sortDescending() ) {
108 foreach ( $order as &$field ) {
109 $field .= ' DESC';
110 }
111 }
112
113 $uqb = $dbr->newUnionQueryBuilder()->all();
114 foreach ( $namespaces as $namespace ) {
115 $nsSqb = clone $sqb;
116 $nsSqb->orderBy( $order );
117 $nsSqb->andWhere( [ 'page_namespace' => $namespace ] );
118 $uqb->add( $nsSqb );
119 }
120
121 if ( $limit !== false ) {
122 $uqb->limit( intval( $limit ) );
123 }
124 if ( $offset !== false ) {
125 $uqb->offset( intval( $offset ) );
126 }
127 $orderBy = 'value';
128 if ( $this->sortDescending() ) {
129 $orderBy .= ' DESC';
130 }
131 $uqb->orderBy( $orderBy );
132 return $uqb->caller( $fname )->fetchResultSet();
133 }
134
136 protected function getOrderFields() {
137 return [ 'page_len' ];
138 }
139
144 public function preprocessResults( $db, $res ) {
145 $this->executeLBFromResultWrapper( $res );
146 }
147
149 protected function sortDescending() {
150 return false;
151 }
152
158 public function formatResult( $skin, $result ) {
159 $title = Title::makeTitleSafe( $result->namespace, $result->title );
160 if ( !$title ) {
161 return Html::element( 'span', [ 'class' => 'mw-invalidtitle' ],
162 Linker::getInvalidTitleDescription( $this->getContext(), $result->namespace, $result->title ) );
163 }
164
165 $linkRenderer = $this->getLinkRenderer();
166 $hlink = $linkRenderer->makeKnownLink(
167 $title,
168 $this->msg( 'hist' )->text(),
169 [],
170 [ 'action' => 'history' ]
171 );
172 $hlinkInParentheses = $this->msg( 'parentheses' )->rawParams( $hlink )->escaped();
173
174 if ( $this->isCached() ) {
175 $plink = $linkRenderer->makeLink( $title );
176 $exists = $title->exists();
177 } else {
178 $plink = $linkRenderer->makeKnownLink( $title );
179 $exists = true;
180 }
181 $contentLanguage = $this->getContentLanguage();
182 $bdiAttrs = [
183 'dir' => $contentLanguage->getDir(),
184 'lang' => $contentLanguage->getHtmlCode(),
185 ];
186 $plink = Html::rawElement( 'bdi', $bdiAttrs, $plink );
187 $size = $this->msg( 'nbytes' )->numParams( $result->value )->escaped();
188 $result = "{$hlinkInParentheses} {$plink} [{$size}]";
189
190 return $exists ? $result : Html::rawElement( 'del', [], $result );
191 }
192
194 protected function getGroupName() {
195 return 'maintenance';
196 }
197}
198
203class_alias( SpecialShortPages::class, 'SpecialShortPages' );
This class is a collection of static functions that serve two purposes:
Definition Html.php:44
Some internal bits split of from Skin.php.
Definition Linker.php:47
A class containing constants representing the names of configuration variables.
const ShortPagesNamespaceExclusions
Name constant for the ShortPagesNamespaceExclusions setting, for use with Config::get()
Factory for LinkBatch objects to batch query page metadata.
The base class for all skins.
Definition Skin.php:53
This is a class for doing query pages; since they're almost all the same, we factor out some of the f...
Definition QueryPage.php:77
setDatabaseProvider(IConnectionProvider $databaseProvider)
int $offset
The offset and limit in use, as passed to the query() function.
Definition QueryPage.php:82
isCached()
Whether or not the output of the page in question is retrieved from the database cache.
executeLBFromResultWrapper(IResultWrapper $res, $ns=null)
Creates a new LinkBatch object, adds all pages from the passed result wrapper (MUST include title and...
getRecacheDB()
Get a DB connection to be used for slow recache queries.
setLinkBatchFactory(LinkBatchFactory $linkBatchFactory)
getConfig()
Shortcut to get main config object.
getContext()
Gets the context this SpecialPage is executed in.
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
getContentLanguage()
Shortcut to get content language.
List of the shortest pages in the database.
getQueryInfo()
Subclasses return an SQL query here, formatted as an array with the following keys: tables => Table(s...
__construct(private readonly NamespaceInfo $namespaceInfo, IConnectionProvider $dbProvider, LinkBatchFactory $linkBatchFactory)
reallyDoQuery( $limit, $offset=false)
Run the query and return the result.to override IResultWrapper 1.18
getOrderFields()
Subclasses return an array of fields to order by here.Don't append DESC to the field names,...
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
sortDescending()
Override to sort by increasing values.to override bool
isSyndicated()
Sometimes we don't want to build rss / atom feeds.to override bool
This is a utility class for dealing with namespaces that encodes all the "magic" behaviors of them ba...
Represents a title within MediaWiki.
Definition Title.php:69
Provide primary and replica IDatabase connections.
A database connection without write operations.
Result wrapper for grabbing data queried from an IDatabase object.
element(SerializerNode $parent, SerializerNode $node, $contents)