MediaWiki master
SpecialShortPages.php
Go to the documentation of this file.
1<?php
7namespace MediaWiki\Specials;
8
9use MediaWiki\Cache\LinkBatchFactory;
17use stdClass;
21
28
29 private NamespaceInfo $namespaceInfo;
30
31 public function __construct(
32 NamespaceInfo $namespaceInfo,
33 IConnectionProvider $dbProvider,
34 LinkBatchFactory $linkBatchFactory
35 ) {
36 parent::__construct( 'Shortpages' );
37 $this->namespaceInfo = $namespaceInfo;
38 $this->setDatabaseProvider( $dbProvider );
39 $this->setLinkBatchFactory( $linkBatchFactory );
40 }
41
43 public function isSyndicated() {
44 return false;
45 }
46
48 public function getQueryInfo() {
49 $config = $this->getConfig();
50 $tables = [ 'page' ];
51 $conds = [
52 'page_namespace' => array_diff(
53 $this->namespaceInfo->getContentNamespaces(),
55 ),
56 'page_is_redirect' => 0
57 ];
58 $joinConds = [];
59 $options = [ 'USE INDEX' => [ 'page' => 'page_redirect_namespace_len' ] ];
60
61 // Allow extensions to modify the query
62 $this->getHookRunner()->onShortPagesQuery( $tables, $conds, $joinConds, $options );
63
64 return [
65 'tables' => $tables,
66 'fields' => [
67 'namespace' => 'page_namespace',
68 'title' => 'page_title',
69 'value' => 'page_len'
70 ],
71 'conds' => $conds,
72 'join_conds' => $joinConds,
73 'options' => $options
74 ];
75 }
76
78 public function reallyDoQuery( $limit, $offset = false ) {
79 $fname = static::class . '::reallyDoQuery';
80 $dbr = $this->getRecacheDB();
81 $query = $this->getQueryInfo();
82 $conds = isset( $query['conds'] ) ? (array)$query['conds'] : [];
83 $namespaces = $conds['page_namespace'];
84 unset( $conds['page_namespace'] );
85
86 if ( count( $namespaces ) === 1 || !$dbr->unionSupportsOrderAndLimit() ) {
87 return parent::reallyDoQuery( $limit, $offset );
88 }
89
90 // Optimization: Fix slow query on MySQL in the case of multiple content namespaces,
91 // by rewriting this as a UNION of separate single namespace queries (T168010).
92 $sqb = $dbr->newSelectQueryBuilder()
93 ->select( isset( $query['fields'] ) ? (array)$query['fields'] : [] )
94 ->tables( isset( $query['tables'] ) ? (array)$query['tables'] : [] )
95 ->where( $conds )
96 ->options( isset( $query['options'] ) ? (array)$query['options'] : [] )
97 ->joinConds( isset( $query['join_conds'] ) ? (array)$query['join_conds'] : [] );
98 if ( $limit !== false ) {
99 if ( $offset !== false ) {
100 // We need to increase the limit by the offset rather than
101 // using the offset directly, otherwise it'll skip incorrectly
102 // in the subqueries.
103 $sqb->limit( intval( $limit ) + intval( $offset ) );
104 } else {
105 $sqb->limit( intval( $limit ) );
106 }
107 }
108
109 $order = $this->getOrderFields();
110 if ( $this->sortDescending() ) {
111 foreach ( $order as &$field ) {
112 $field .= ' DESC';
113 }
114 }
115
116 $uqb = $dbr->newUnionQueryBuilder()->all();
117 foreach ( $namespaces as $namespace ) {
118 $nsSqb = clone $sqb;
119 $nsSqb->orderBy( $order );
120 $nsSqb->andWhere( [ 'page_namespace' => $namespace ] );
121 $uqb->add( $nsSqb );
122 }
123
124 if ( $limit !== false ) {
125 $uqb->limit( intval( $limit ) );
126 }
127 if ( $offset !== false ) {
128 $uqb->offset( intval( $offset ) );
129 }
130 $orderBy = 'value';
131 if ( $this->sortDescending() ) {
132 $orderBy .= ' DESC';
133 }
134 $uqb->orderBy( $orderBy );
135 return $uqb->caller( $fname )->fetchResultSet();
136 }
137
139 protected function getOrderFields() {
140 return [ 'page_len' ];
141 }
142
147 public function preprocessResults( $db, $res ) {
148 $this->executeLBFromResultWrapper( $res );
149 }
150
152 protected function sortDescending() {
153 return false;
154 }
155
161 public function formatResult( $skin, $result ) {
162 $title = Title::makeTitleSafe( $result->namespace, $result->title );
163 if ( !$title ) {
164 return Html::element( 'span', [ 'class' => 'mw-invalidtitle' ],
165 Linker::getInvalidTitleDescription( $this->getContext(), $result->namespace, $result->title ) );
166 }
167
168 $linkRenderer = $this->getLinkRenderer();
169 $hlink = $linkRenderer->makeKnownLink(
170 $title,
171 $this->msg( 'hist' )->text(),
172 [],
173 [ 'action' => 'history' ]
174 );
175 $hlinkInParentheses = $this->msg( 'parentheses' )->rawParams( $hlink )->escaped();
176
177 if ( $this->isCached() ) {
178 $plink = $linkRenderer->makeLink( $title );
179 $exists = $title->exists();
180 } else {
181 $plink = $linkRenderer->makeKnownLink( $title );
182 $exists = true;
183 }
184 $contentLanguage = $this->getContentLanguage();
185 $bdiAttrs = [
186 'dir' => $contentLanguage->getDir(),
187 'lang' => $contentLanguage->getHtmlCode(),
188 ];
189 $plink = Html::rawElement( 'bdi', $bdiAttrs, $plink );
190 $size = $this->msg( 'nbytes' )->numParams( $result->value )->escaped();
191 $result = "{$hlinkInParentheses} {$plink} [{$size}]";
192
193 return $exists ? $result : Html::rawElement( 'del', [], $result );
194 }
195
197 protected function getGroupName() {
198 return 'maintenance';
199 }
200}
201
206class_alias( SpecialShortPages::class, 'SpecialShortPages' );
This class is a collection of static functions that serve two purposes:
Definition Html.php:43
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()
The base class for all skins.
Definition Skin.php:52
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(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)