MediaWiki  master
SpecialShortPages.php
Go to the documentation of this file.
1 <?php
33 
41 
43  private $namespaceInfo;
44 
50  public function __construct(
51  NamespaceInfo $namespaceInfo,
52  IConnectionProvider $dbProvider,
53  LinkBatchFactory $linkBatchFactory
54  ) {
55  parent::__construct( 'Shortpages' );
56  $this->namespaceInfo = $namespaceInfo;
57  $this->setDatabaseProvider( $dbProvider );
58  $this->setLinkBatchFactory( $linkBatchFactory );
59  }
60 
61  public function isSyndicated() {
62  return false;
63  }
64 
65  public function getQueryInfo() {
66  $config = $this->getConfig();
67  $tables = [ 'page' ];
68  $conds = [
69  'page_namespace' => array_diff(
70  $this->namespaceInfo->getContentNamespaces(),
71  $config->get( MainConfigNames::ShortPagesNamespaceExclusions )
72  ),
73  'page_is_redirect' => 0
74  ];
75  $joinConds = [];
76  $options = [ 'USE INDEX' => [ 'page' => 'page_redirect_namespace_len' ] ];
77 
78  // Allow extensions to modify the query
79  $this->getHookRunner()->onShortPagesQuery( $tables, $conds, $joinConds, $options );
80 
81  return [
82  'tables' => $tables,
83  'fields' => [
84  'namespace' => 'page_namespace',
85  'title' => 'page_title',
86  'value' => 'page_len'
87  ],
88  'conds' => $conds,
89  'join_conds' => $joinConds,
90  'options' => $options
91  ];
92  }
93 
94  public function reallyDoQuery( $limit, $offset = false ) {
95  $fname = static::class . '::reallyDoQuery';
96  $dbr = $this->getRecacheDB();
97  $query = $this->getQueryInfo();
98  $order = $this->getOrderFields();
99 
100  if ( $this->sortDescending() ) {
101  foreach ( $order as &$field ) {
102  $field .= ' DESC';
103  }
104  }
105 
106  $tables = isset( $query['tables'] ) ? (array)$query['tables'] : [];
107  $fields = isset( $query['fields'] ) ? (array)$query['fields'] : [];
108  $conds = isset( $query['conds'] ) ? (array)$query['conds'] : [];
109  $options = isset( $query['options'] ) ? (array)$query['options'] : [];
110  $join_conds = isset( $query['join_conds'] ) ? (array)$query['join_conds'] : [];
111 
112  if ( $limit !== false ) {
113  $options['LIMIT'] = intval( $limit );
114  }
115 
116  if ( $offset !== false ) {
117  $options['OFFSET'] = intval( $offset );
118  }
119 
120  $namespaces = $conds['page_namespace'];
121  if ( count( $namespaces ) === 1 ) {
122  $options['ORDER BY'] = $order;
123  $res = $dbr->select( $tables, $fields, $conds, $fname,
124  $options, $join_conds
125  );
126  } else {
127  unset( $conds['page_namespace'] );
128  $options['INNER ORDER BY'] = $order;
129  $options['ORDER BY'] = [ 'value' . ( $this->sortDescending() ? ' DESC' : '' ) ];
130  $sql = $dbr->unionConditionPermutations(
131  $tables,
132  $fields,
133  [ 'page_namespace' => $namespaces ],
134  $conds,
135  $fname,
136  $options,
137  $join_conds
138  );
139  // phpcs:ignore MediaWiki.Usage.DbrQueryUsage.DbrQueryFound
140  $res = $dbr->query( $sql, $fname, ISQLPlatform::QUERY_CHANGE_NONE );
141  }
142 
143  return $res;
144  }
145 
146  protected function getOrderFields() {
147  return [ 'page_len' ];
148  }
149 
154  public function preprocessResults( $db, $res ) {
156  }
157 
158  protected function sortDescending() {
159  return false;
160  }
161 
167  public function formatResult( $skin, $result ) {
168  $dm = $this->getLanguage()->getDirMark();
169 
170  $title = Title::makeTitleSafe( $result->namespace, $result->title );
171  if ( !$title ) {
172  return Html::element( 'span', [ 'class' => 'mw-invalidtitle' ],
173  Linker::getInvalidTitleDescription( $this->getContext(), $result->namespace, $result->title ) );
174  }
175 
176  $linkRenderer = $this->getLinkRenderer();
177  $hlink = $linkRenderer->makeKnownLink(
178  $title,
179  $this->msg( 'hist' )->text(),
180  [],
181  [ 'action' => 'history' ]
182  );
183  $hlinkInParentheses = $this->msg( 'parentheses' )->rawParams( $hlink )->escaped();
184 
185  if ( $this->isCached() ) {
186  $plink = $linkRenderer->makeLink( $title );
187  $exists = $title->exists();
188  } else {
189  $plink = $linkRenderer->makeKnownLink( $title );
190  $exists = true;
191  }
192 
193  $size = $this->msg( 'nbytes' )->numParams( $result->value )->escaped();
194 
195  return $exists
196  ? "{$hlinkInParentheses} {$dm}{$plink} {$dm}[{$size}]"
197  : "<del>{$hlinkInParentheses} {$dm}{$plink} {$dm}[{$size}]</del>";
198  }
199 
200  protected function getGroupName() {
201  return 'maintenance';
202  }
203 }
This class is a collection of static functions that serve two purposes:
Definition: Html.php:55
Some internal bits split of from Skin.php.
Definition: Linker.php:67
A class containing constants representing the names of configuration variables.
Represents a title within MediaWiki.
Definition: Title.php:82
This is a utility class for dealing with namespaces that encodes all the "magic" behaviors of them ba...
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:65
setDatabaseProvider(IConnectionProvider $databaseProvider)
Definition: QueryPage.php:964
executeLBFromResultWrapper(IResultWrapper $res, $ns=null)
Creates a new LinkBatch object, adds all pages from the passed result wrapper (MUST include title and...
Definition: QueryPage.php:925
int $offset
The offset and limit in use, as passed to the query() function.
Definition: QueryPage.php:70
isCached()
Whether or not the output of the page in question is retrieved from the database cache.
Definition: QueryPage.php:337
int $limit
Definition: QueryPage.php:73
setLinkBatchFactory(LinkBatchFactory $linkBatchFactory)
Definition: QueryPage.php:162
getRecacheDB()
Get a DB connection to be used for slow recache queries.
Definition: QueryPage.php:483
getContext()
Gets the context this SpecialPage is executed in.
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
getConfig()
Shortcut to get main config object.
getLanguage()
Shortcut to get user's language.
SpecialShortpages extends QueryPage.
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
reallyDoQuery( $limit, $offset=false)
Run the query and return the result.
getQueryInfo()
Subclasses return an SQL query here, formatted as an array with the following keys: tables => Table(s...
preprocessResults( $db, $res)
sortDescending()
Override to sort by increasing values.
getOrderFields()
Subclasses return an array of fields to order by here.
isSyndicated()
Sometimes we don't want to build rss / atom feeds.
formatResult( $skin, $result)
__construct(NamespaceInfo $namespaceInfo, IConnectionProvider $dbProvider, LinkBatchFactory $linkBatchFactory)
Provide primary and replica IDatabase connections.
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:36
Result wrapper for grabbing data queried from an IDatabase object.
Interface for query language.