MediaWiki master
SpecialShortPages.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\Specials;
22
31use stdClass;
35
42
43 private NamespaceInfo $namespaceInfo;
44
45 public function __construct(
46 NamespaceInfo $namespaceInfo,
47 IConnectionProvider $dbProvider,
48 LinkBatchFactory $linkBatchFactory
49 ) {
50 parent::__construct( 'Shortpages' );
51 $this->namespaceInfo = $namespaceInfo;
52 $this->setDatabaseProvider( $dbProvider );
53 $this->setLinkBatchFactory( $linkBatchFactory );
54 }
55
56 public function isSyndicated() {
57 return false;
58 }
59
60 public function getQueryInfo() {
61 $config = $this->getConfig();
62 $tables = [ 'page' ];
63 $conds = [
64 'page_namespace' => array_diff(
65 $this->namespaceInfo->getContentNamespaces(),
67 ),
68 'page_is_redirect' => 0
69 ];
70 $joinConds = [];
71 $options = [ 'USE INDEX' => [ 'page' => 'page_redirect_namespace_len' ] ];
72
73 // Allow extensions to modify the query
74 $this->getHookRunner()->onShortPagesQuery( $tables, $conds, $joinConds, $options );
75
76 return [
77 'tables' => $tables,
78 'fields' => [
79 'namespace' => 'page_namespace',
80 'title' => 'page_title',
81 'value' => 'page_len'
82 ],
83 'conds' => $conds,
84 'join_conds' => $joinConds,
85 'options' => $options
86 ];
87 }
88
89 public function reallyDoQuery( $limit, $offset = false ) {
90 $fname = static::class . '::reallyDoQuery';
91 $dbr = $this->getRecacheDB();
92 $query = $this->getQueryInfo();
93 $conds = isset( $query['conds'] ) ? (array)$query['conds'] : [];
94 $namespaces = $conds['page_namespace'];
95 unset( $conds['page_namespace'] );
96
97 if ( count( $namespaces ) === 1 || !$dbr->unionSupportsOrderAndLimit() ) {
98 return parent::reallyDoQuery( $limit, $offset );
99 }
100
101 // Optimization: Fix slow query on MySQL the case of multiple content namespaces,
102 // by rewriting this as a UNION of separate single namespace queries (T168010).
103 $sqb = $dbr->newSelectQueryBuilder()
104 ->select( isset( $query['fields'] ) ? (array)$query['fields'] : [] )
105 ->tables( isset( $query['tables'] ) ? (array)$query['tables'] : [] )
106 ->where( $conds )
107 ->options( isset( $query['options'] ) ? (array)$query['options'] : [] )
108 ->joinConds( isset( $query['join_conds'] ) ? (array)$query['join_conds'] : [] );
109 if ( $limit !== false ) {
110 if ( $offset !== false ) {
111 // We need to increase the limit by the offset rather than
112 // using the offset directly, otherwise it'll skip incorrectly
113 // in the subqueries.
114 $sqb->limit( intval( $limit ) + intval( $offset ) );
115 } else {
116 $sqb->limit( intval( $limit ) );
117 }
118 }
119
120 $order = $this->getOrderFields();
121 if ( $this->sortDescending() ) {
122 foreach ( $order as &$field ) {
123 $field .= ' DESC';
124 }
125 }
126
127 $uqb = $dbr->newUnionQueryBuilder()->all();
128 foreach ( $namespaces as $namespace ) {
129 $nsSqb = clone $sqb;
130 $nsSqb->orderBy( $order );
131 $nsSqb->andWhere( [ 'page_namespace' => $namespace ] );
132 $uqb->add( $nsSqb );
133 }
134
135 if ( $limit !== false ) {
136 $uqb->limit( intval( $limit ) );
137 }
138 if ( $offset !== false ) {
139 $uqb->offset( intval( $offset ) );
140 }
141 $orderBy = 'value';
142 if ( $this->sortDescending() ) {
143 $orderBy .= ' DESC';
144 }
145 $uqb->orderBy( $orderBy );
146 return $uqb->caller( $fname )->fetchResultSet();
147 }
148
149 protected function getOrderFields() {
150 return [ 'page_len' ];
151 }
152
157 public function preprocessResults( $db, $res ) {
158 $this->executeLBFromResultWrapper( $res );
159 }
160
161 protected function sortDescending() {
162 return false;
163 }
164
170 public function formatResult( $skin, $result ) {
171 $title = Title::makeTitleSafe( $result->namespace, $result->title );
172 if ( !$title ) {
173 return Html::element( 'span', [ 'class' => 'mw-invalidtitle' ],
174 Linker::getInvalidTitleDescription( $this->getContext(), $result->namespace, $result->title ) );
175 }
176
177 $linkRenderer = $this->getLinkRenderer();
178 $hlink = $linkRenderer->makeKnownLink(
179 $title,
180 $this->msg( 'hist' )->text(),
181 [],
182 [ 'action' => 'history' ]
183 );
184 $hlinkInParentheses = $this->msg( 'parentheses' )->rawParams( $hlink )->escaped();
185
186 if ( $this->isCached() ) {
187 $plink = $linkRenderer->makeLink( $title );
188 $exists = $title->exists();
189 } else {
190 $plink = $linkRenderer->makeKnownLink( $title );
191 $exists = true;
192 }
193 $contentLanguage = $this->getContentLanguage();
194 $bdiAttrs = [
195 'dir' => $contentLanguage->getDir(),
196 'lang' => $contentLanguage->getHtmlCode(),
197 ];
198 $plink = Html::rawElement( 'bdi', $bdiAttrs, $plink );
199 $size = $this->msg( 'nbytes' )->numParams( $result->value )->escaped();
200 $result = "{$hlinkInParentheses} {$plink} [{$size}]";
201
202 return $exists ? $result : Html::rawElement( 'del', [], $result );
203 }
204
205 protected function getGroupName() {
206 return 'maintenance';
207 }
208}
209
214class_alias( SpecialShortPages::class, 'SpecialShortPages' );
This class is a collection of static functions that serve two purposes:
Definition Html.php:57
Some internal bits split of from Skin.php.
Definition Linker.php:61
A class containing constants representing the names of configuration variables.
const ShortPagesNamespaceExclusions
Name constant for the ShortPagesNamespaceExclusions setting, for use with Config::get()
The base class for all skins.
Definition Skin.php:58
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:87
setDatabaseProvider(IConnectionProvider $databaseProvider)
int $offset
The offset and limit in use, as passed to the query() function.
Definition QueryPage.php:92
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(NamespaceInfo $namespaceInfo, IConnectionProvider $dbProvider, LinkBatchFactory $linkBatchFactory)
reallyDoQuery( $limit, $offset=false)
Run the query and return the result.
getOrderFields()
Subclasses return an array of fields to order by here.
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
sortDescending()
Override to sort by increasing values.
isSyndicated()
Sometimes we don't want to build rss / atom feeds.
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:78
Provide primary and replica IDatabase connections.
Interface to a relational database.
Definition IDatabase.php:45
Result wrapper for grabbing data queried from an IDatabase object.
element(SerializerNode $parent, SerializerNode $node, $contents)