MediaWiki  master
AllMessagesTablePager.php
Go to the documentation of this file.
1 <?php
33 
41 
45  protected $langcode;
46 
50  protected $foreign;
51 
55  protected $prefix;
56 
60  protected $suffix;
61 
65  public $lang;
66 
70  public $custom;
71 
73  private $localisationCache;
74 
85  public function __construct(
86  IContextSource $context,
87  Language $contentLanguage,
88  LanguageFactory $languageFactory,
89  LanguageNameUtils $languageNameUtils,
90  LinkRenderer $linkRenderer,
91  IConnectionProvider $dbProvider,
92  LocalisationCache $localisationCache,
93  FormOptions $opts
94  ) {
95  // Set database before parent constructor to avoid setting it there with wfGetDB
96  $this->mDb = $dbProvider->getReplicaDatabase();
97  parent::__construct( $context, $linkRenderer );
98  $this->localisationCache = $localisationCache;
99 
100  $this->mIndexField = 'am_title';
101  // FIXME: Why does this need to be set to DIR_DESCENDING to produce ascending ordering?
102  $this->mDefaultDirection = IndexPager::DIR_DESCENDING;
103 
104  $lang = $opts->getValue( 'lang' );
105  $this->lang = $languageNameUtils->isKnownLanguageTag( $lang ) ?
106  $languageFactory->getRawLanguage( $lang ) :
107  $contentLanguage;
108 
109  $this->langcode = $this->lang->getCode();
110  $this->foreign = !$this->lang->equals( $contentLanguage );
111 
112  $filter = $opts->getValue( 'filter' );
113  if ( $filter === 'all' ) {
114  $this->custom = null; // So won't match in either case
115  } else {
116  $this->custom = ( $filter === 'unmodified' );
117  }
118 
119  $prefix = $this->getLanguage()->ucfirst( $opts->getValue( 'prefix' ) );
120  $prefix = $prefix !== '' ?
121  Title::makeTitleSafe( NS_MEDIAWIKI, $opts->getValue( 'prefix' ) ) :
122  null;
123 
124  if ( $prefix !== null ) {
125  $displayPrefix = $prefix->getDBkey();
126  $this->prefix = '/^' . preg_quote( $displayPrefix, '/' ) . '/i';
127  } else {
128  $this->prefix = false;
129  }
130 
131  // The suffix that may be needed for message names if we're in a
132  // different language (eg [[MediaWiki:Foo/fr]]: $suffix = '/fr'
133  if ( $this->foreign ) {
134  $this->suffix = '/' . $this->langcode;
135  } else {
136  $this->suffix = '';
137  }
138  }
139 
140  private function getAllMessages( $descending ) {
141  $messageNames = $this->localisationCache->getSubitemList( 'en', 'messages' );
142 
143  // Normalise message names so they look like page titles and sort correctly - T86139
144  $messageNames = array_map( [ $this->lang, 'ucfirst' ], $messageNames );
145 
146  if ( $descending ) {
147  rsort( $messageNames );
148  } else {
149  asort( $messageNames );
150  }
151 
152  return $messageNames;
153  }
154 
169  public static function getCustomisedStatuses(
170  $messageNames,
171  $langcode = 'en',
172  $foreign = false,
173  IReadableDatabase $dbr = null
174  ) {
175  // FIXME: This function should be moved to Language:: or something.
176  // Fallback to global state, if not provided
177  $dbr ??= wfGetDB( DB_REPLICA );
178  $res = $dbr->select( 'page',
179  [ 'page_namespace', 'page_title' ],
180  [ 'page_namespace' => [ NS_MEDIAWIKI, NS_MEDIAWIKI_TALK ] ],
181  __METHOD__,
182  [ 'USE INDEX' => 'page_name_title' ]
183  );
184  $xNames = array_fill_keys( $messageNames, true );
185 
186  $pageFlags = $talkFlags = [];
187 
188  foreach ( $res as $s ) {
189  $exists = false;
190 
191  if ( $foreign ) {
192  $titleParts = explode( '/', $s->page_title );
193  if ( count( $titleParts ) === 2 &&
194  $langcode === $titleParts[1] &&
195  isset( $xNames[$titleParts[0]] )
196  ) {
197  $exists = $titleParts[0];
198  }
199  } elseif ( isset( $xNames[$s->page_title] ) ) {
200  $exists = $s->page_title;
201  }
202 
203  $title = Title::newFromRow( $s );
204  if ( $exists && $title->inNamespace( NS_MEDIAWIKI ) ) {
205  $pageFlags[$exists] = true;
206  } elseif ( $exists && $title->inNamespace( NS_MEDIAWIKI_TALK ) ) {
207  $talkFlags[$exists] = true;
208  }
209  }
210 
211  return [ 'pages' => $pageFlags, 'talks' => $talkFlags ];
212  }
213 
222  public function reallyDoQuery( $offset, $limit, $order ) {
223  $asc = ( $order === self::QUERY_ASCENDING );
224 
225  $messageNames = $this->getAllMessages( $order );
226  $statuses = self::getCustomisedStatuses(
227  $messageNames,
228  $this->langcode,
229  $this->foreign,
230  $this->getDatabase()
231  );
232 
233  $rows = [];
234  $count = 0;
235  foreach ( $messageNames as $key ) {
236  $customised = isset( $statuses['pages'][$key] );
237  if ( $customised !== $this->custom &&
238  ( $asc && ( $key < $offset || !$offset ) || !$asc && $key > $offset ) &&
239  ( ( $this->prefix && preg_match( $this->prefix, $key ) ) || $this->prefix === false )
240  ) {
241  $actual = $this->msg( $key )->inLanguage( $this->lang )->plain();
242  $default = $this->msg( $key )->inLanguage( $this->lang )->useDatabase( false )->plain();
243  $rows[] = [
244  'am_title' => $key,
245  'am_actual' => $actual,
246  'am_default' => $default,
247  'am_customised' => $customised,
248  'am_talk_exists' => isset( $statuses['talks'][$key] )
249  ];
250  $count++;
251  }
252 
253  if ( $count === $limit ) {
254  break;
255  }
256  }
257 
258  return new FakeResultWrapper( $rows );
259  }
260 
261  protected function getStartBody() {
262  return Xml::openElement( 'table', [
263  'class' => $this->getTableClass(),
264  'id' => 'mw-allmessagestable'
265  ] ) .
266  "\n" .
267  "<thead><tr>
268  <th rowspan=\"2\">" .
269  $this->msg( 'allmessagesname' )->escaped() . "
270  </th>
271  <th>" .
272  $this->msg( 'allmessagesdefault' )->escaped() .
273  "</th>
274  </tr>\n
275  <tr>
276  <th>" .
277  $this->msg( 'allmessagescurrent' )->escaped() .
278  "</th>
279  </tr></thead>\n";
280  }
281 
282  protected function getEndBody() {
283  return Html::closeElement( 'table' );
284  }
285 
291  public function formatValue( $field, $value ) {
292  $linkRenderer = $this->getLinkRenderer();
293  switch ( $field ) {
294  case 'am_title':
295  $title = Title::makeTitle( NS_MEDIAWIKI, $value . $this->suffix );
296  $talk = Title::makeTitle( NS_MEDIAWIKI_TALK, $value . $this->suffix );
297  $translation = Linker::makeExternalLink(
298  'https://translatewiki.net/w/i.php?' . wfArrayToCgi( [
299  'title' => 'Special:SearchTranslations',
300  'group' => 'mediawiki',
301  'grouppath' => 'mediawiki',
302  'language' => $this->getLanguage()->getCode(),
303  'query' => $value . ' ' . $this->msg( $value )->plain()
304  ] ),
305  $this->msg( 'allmessages-filter-translate' )->text()
306  );
307  $talkLink = $this->msg( 'talkpagelinktext' )->text();
308 
309  if ( $this->mCurrentRow->am_customised ) {
310  $title = $linkRenderer->makeKnownLink( $title, $this->getLanguage()->lcfirst( $value ) );
311  } else {
312  $title = $linkRenderer->makeBrokenLink(
313  $title, $this->getLanguage()->lcfirst( $value )
314  );
315  }
316  if ( $this->mCurrentRow->am_talk_exists ) {
317  $talk = $linkRenderer->makeKnownLink( $talk, $talkLink );
318  } else {
319  $talk = $linkRenderer->makeBrokenLink(
320  $talk,
321  $talkLink
322  );
323  }
324 
325  return $title . ' ' .
326  $this->msg( 'parentheses' )->rawParams( $talk )->escaped() .
327  ' ' .
328  $this->msg( 'parentheses' )->rawParams( $translation )->escaped();
329 
330  case 'am_default':
331  case 'am_actual':
332  return Sanitizer::escapeHtmlAllowEntities( $value );
333  }
334 
335  return '';
336  }
337 
342  public function formatRow( $row ) {
343  // Do all the normal stuff
344  $s = parent::formatRow( $row );
345 
346  // But if there's a customised message, add that too.
347  if ( $row->am_customised ) {
348  $s .= Html::openElement( 'tr', $this->getRowAttrs( $row ) );
349  $formatted = strval( $this->formatValue( 'am_actual', $row->am_actual ) );
350 
351  if ( $formatted === '' ) {
352  $formatted = "\u{00A0}";
353  }
354 
355  $s .= Html::rawElement( 'td', $this->getCellAttrs( 'am_actual', $row->am_actual ), $formatted )
356  . Html::closeElement( 'tr' );
357  }
358 
359  return Html::rawElement( 'tbody', [], $s );
360  }
361 
362  protected function getRowAttrs( $row ) {
363  return [];
364  }
365 
371  protected function getCellAttrs( $field, $value ) {
372  $attr = [];
373  if ( $field === 'am_title' ) {
374  if ( $this->mCurrentRow->am_customised ) {
375  $attr += [ 'rowspan' => '2' ];
376  }
377  } else {
378  $attr += [
379  'lang' => $this->lang->getHtmlCode(),
380  'dir' => $this->lang->getDir(),
381  ];
382  if ( $this->mCurrentRow->am_customised ) {
383  // CSS class: am_default, am_actual
384  $attr += [ 'class' => $field ];
385  }
386  }
387  return $attr;
388  }
389 
390  // This is not actually used, as getStartBody is overridden above
391  protected function getFieldNames() {
392  return [
393  'am_title' => $this->msg( 'allmessagesname' )->text(),
394  'am_default' => $this->msg( 'allmessagesdefault' )->text()
395  ];
396  }
397 
398  public function getTitle() {
399  return SpecialPage::getTitleFor( 'Allmessages', false );
400  }
401 
402  protected function isFieldSortable( $x ) {
403  return false;
404  }
405 
406  public function getDefaultSort() {
407  return '';
408  }
409 
410  public function getQueryInfo() {
411  return [];
412  }
413 
414 }
const NS_MEDIAWIKI_TALK
Definition: Defines.php:73
const NS_MEDIAWIKI
Definition: Defines.php:72
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
wfArrayToCgi( $array1, $array2=null, $prefix='')
This function takes one or two arrays as input, and returns a CGI-style string, e....
Use TablePager for prettified output.
getQueryInfo()
Provides all parameters needed for the main paged query.
static getCustomisedStatuses( $messageNames, $langcode='en', $foreign=false, IReadableDatabase $dbr=null)
Determine which of the MediaWiki and MediaWiki_talk namespace pages exist.
getRowAttrs( $row)
Get attributes to be applied to the given row.
getDefaultSort()
The database field name used as a default sort order.
isFieldSortable( $x)
Return true if the named field should be sortable by the UI, false otherwise.
reallyDoQuery( $offset, $limit, $order)
This function normally does a database query to get the results; we need to make a pretend result usi...
__construct(IContextSource $context, Language $contentLanguage, LanguageFactory $languageFactory, LanguageNameUtils $languageNameUtils, LinkRenderer $linkRenderer, IConnectionProvider $dbProvider, LocalisationCache $localisationCache, FormOptions $opts)
getFieldNames()
An array mapping database field names to a textual description of the field name, for use in the tabl...
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
getDatabase()
Get the Database object in use.
Definition: IndexPager.php:248
const DIR_DESCENDING
Backwards-compatible constant for $mDefaultDirection field (do not change)
Definition: IndexPager.php:80
const QUERY_ASCENDING
Backwards-compatible constant for reallyDoQuery() (do not change)
Definition: IndexPager.php:83
Base class for language-specific code.
Definition: Language.php:57
getCode()
Get the internal language code for this language object.
Definition: Language.php:4338
Caching for the contents of localisation files.
Helper class to keep track of options when mixing links and form elements.
Definition: FormOptions.php:41
getValue( $name)
Get the value for the given option name.
This class is a collection of static functions that serve two purposes:
Definition: Html.php:55
Internationalisation code See https://www.mediawiki.org/wiki/Special:MyLanguage/Localisation for more...
getRawLanguage( $code)
Get a cached or new language object for a given language code without normalization of the language c...
A service that provides utilities to do with language names and codes.
isKnownLanguageTag(string $tag)
Returns true if a language code is an IETF tag known to MediaWiki.
Class that generates HTML for internal links.
Some internal bits split of from Skin.php.
Definition: Linker.php:67
Represents a title within MediaWiki.
Definition: Title.php:82
static escapeHtmlAllowEntities( $html)
Given HTML input, escape with htmlspecialchars but un-escape entities.
Definition: Sanitizer.php:1123
static getTitleFor( $name, $subpage=false, $fragment='')
Get a localised Title object for a specified special page name If you don't need a full Title object,...
Table-based display with a user-selectable sort order.
Definition: TablePager.php:30
getTableClass()
TablePager relies on mw-datatable for styling, see T214208.
Definition: TablePager.php:273
Overloads the relevant methods of the real ResultWrapper so it doesn't go anywhere near an actual dat...
static openElement( $element, $attribs=null)
This opens an XML element.
Definition: Xml.php:109
Interface for objects which can provide a MediaWiki context on request.
Provide primary and replica IDatabase connections.
getReplicaDatabase( $domain=false, $group=null)
Get connection to a replica database.
A database connection without write operations.
Result wrapper for grabbing data queried from an IDatabase object.
const DB_REPLICA
Definition: defines.php:26