MediaWiki  master
AllMessagesTablePager.php
Go to the documentation of this file.
1 <?php
30 
38 
42  protected $langcode;
43 
47  protected $foreign;
48 
52  protected $prefix;
53 
57  protected $suffix;
58 
62  public $lang;
63 
67  public $custom;
68 
70  private $localisationCache;
71 
82  public function __construct(
83  IContextSource $context,
84  Language $contentLanguage,
85  LanguageFactory $languageFactory,
86  LanguageNameUtils $languageNameUtils,
87  LinkRenderer $linkRenderer,
88  ILoadBalancer $loadBalancer,
89  LocalisationCache $localisationCache,
90  FormOptions $opts
91  ) {
92  // Set database before parent constructor to avoid setting it there with wfGetDB
93  $this->mDb = $loadBalancer->getConnectionRef( ILoadBalancer::DB_REPLICA );
94  parent::__construct( $context, $linkRenderer );
95  $this->localisationCache = $localisationCache;
96 
97  $this->mIndexField = 'am_title';
98  // FIXME: Why does this need to be set to DIR_DESCENDING to produce ascending ordering?
99  $this->mDefaultDirection = IndexPager::DIR_DESCENDING;
100 
101  $lang = $opts->getValue( 'lang' );
102  $this->lang = $languageNameUtils->isKnownLanguageTag( $lang ) ?
103  $languageFactory->getRawLanguage( $lang ) :
104  $contentLanguage;
105 
106  $this->langcode = $this->lang->getCode();
107  $this->foreign = !$this->lang->equals( $contentLanguage );
108 
109  $filter = $opts->getValue( 'filter' );
110  if ( $filter === 'all' ) {
111  $this->custom = null; // So won't match in either case
112  } else {
113  $this->custom = ( $filter === 'unmodified' );
114  }
115 
116  $prefix = $this->getLanguage()->ucfirst( $opts->getValue( 'prefix' ) );
117  $prefix = $prefix !== '' ?
118  Title::makeTitleSafe( NS_MEDIAWIKI, $opts->getValue( 'prefix' ) ) :
119  null;
120 
121  if ( $prefix !== null ) {
122  $displayPrefix = $prefix->getDBkey();
123  $this->prefix = '/^' . preg_quote( $displayPrefix, '/' ) . '/i';
124  } else {
125  $this->prefix = false;
126  }
127 
128  // The suffix that may be needed for message names if we're in a
129  // different language (eg [[MediaWiki:Foo/fr]]: $suffix = '/fr'
130  if ( $this->foreign ) {
131  $this->suffix = '/' . $this->langcode;
132  } else {
133  $this->suffix = '';
134  }
135  }
136 
137  private function getAllMessages( $descending ) {
138  $messageNames = $this->localisationCache->getSubitemList( 'en', 'messages' );
139 
140  // Normalise message names so they look like page titles and sort correctly - T86139
141  $messageNames = array_map( [ $this->lang, 'ucfirst' ], $messageNames );
142 
143  if ( $descending ) {
144  rsort( $messageNames );
145  } else {
146  asort( $messageNames );
147  }
148 
149  return $messageNames;
150  }
151 
166  public static function getCustomisedStatuses(
167  $messageNames,
168  $langcode = 'en',
169  $foreign = false,
170  IDatabase $dbr = null
171  ) {
172  // FIXME: This function should be moved to Language:: or something.
173  // Fallback to global state, if not provided
174  $dbr ??= wfGetDB( DB_REPLICA );
175  $res = $dbr->select( 'page',
176  [ 'page_namespace', 'page_title' ],
177  [ 'page_namespace' => [ NS_MEDIAWIKI, NS_MEDIAWIKI_TALK ] ],
178  __METHOD__,
179  [ 'USE INDEX' => 'page_name_title' ]
180  );
181  $xNames = array_fill_keys( $messageNames, true );
182 
183  $pageFlags = $talkFlags = [];
184 
185  foreach ( $res as $s ) {
186  $exists = false;
187 
188  if ( $foreign ) {
189  $titleParts = explode( '/', $s->page_title );
190  if ( count( $titleParts ) === 2 &&
191  $langcode === $titleParts[1] &&
192  isset( $xNames[$titleParts[0]] )
193  ) {
194  $exists = $titleParts[0];
195  }
196  } elseif ( isset( $xNames[$s->page_title] ) ) {
197  $exists = $s->page_title;
198  }
199 
201  if ( $exists && $title->inNamespace( NS_MEDIAWIKI ) ) {
202  $pageFlags[$exists] = true;
203  } elseif ( $exists && $title->inNamespace( NS_MEDIAWIKI_TALK ) ) {
204  $talkFlags[$exists] = true;
205  }
206  }
207 
208  return [ 'pages' => $pageFlags, 'talks' => $talkFlags ];
209  }
210 
219  public function reallyDoQuery( $offset, $limit, $order ) {
220  $asc = ( $order === self::QUERY_ASCENDING );
221 
222  $messageNames = $this->getAllMessages( $order );
223  $statuses = self::getCustomisedStatuses(
224  $messageNames,
225  $this->langcode,
226  $this->foreign,
227  $this->getDatabase()
228  );
229 
230  $rows = [];
231  $count = 0;
232  foreach ( $messageNames as $key ) {
233  $customised = isset( $statuses['pages'][$key] );
234  if ( $customised !== $this->custom &&
235  ( $asc && ( $key < $offset || !$offset ) || !$asc && $key > $offset ) &&
236  ( ( $this->prefix && preg_match( $this->prefix, $key ) ) || $this->prefix === false )
237  ) {
238  $actual = $this->msg( $key )->inLanguage( $this->lang )->plain();
239  $default = $this->msg( $key )->inLanguage( $this->lang )->useDatabase( false )->plain();
240  $rows[] = [
241  'am_title' => $key,
242  'am_actual' => $actual,
243  'am_default' => $default,
244  'am_customised' => $customised,
245  'am_talk_exists' => isset( $statuses['talks'][$key] )
246  ];
247  $count++;
248  }
249 
250  if ( $count === $limit ) {
251  break;
252  }
253  }
254 
255  return new FakeResultWrapper( $rows );
256  }
257 
258  protected function getStartBody() {
259  return Xml::openElement( 'table', [
260  'class' => $this->getTableClass(),
261  'id' => 'mw-allmessagestable'
262  ] ) .
263  "\n" .
264  "<thead><tr>
265  <th rowspan=\"2\">" .
266  $this->msg( 'allmessagesname' )->escaped() . "
267  </th>
268  <th>" .
269  $this->msg( 'allmessagesdefault' )->escaped() .
270  "</th>
271  </tr>\n
272  <tr>
273  <th>" .
274  $this->msg( 'allmessagescurrent' )->escaped() .
275  "</th>
276  </tr></thead>\n";
277  }
278 
279  protected function getEndBody() {
280  return Html::closeElement( 'table' );
281  }
282 
288  public function formatValue( $field, $value ) {
289  $linkRenderer = $this->getLinkRenderer();
290  switch ( $field ) {
291  case 'am_title':
292  $title = Title::makeTitle( NS_MEDIAWIKI, $value . $this->suffix );
293  $talk = Title::makeTitle( NS_MEDIAWIKI_TALK, $value . $this->suffix );
294  $translation = Linker::makeExternalLink(
295  'https://translatewiki.net/w/i.php?' . wfArrayToCgi( [
296  'title' => 'Special:SearchTranslations',
297  'group' => 'mediawiki',
298  'grouppath' => 'mediawiki',
299  'language' => $this->getLanguage()->getCode(),
300  'query' => $value . ' ' . $this->msg( $value )->plain()
301  ] ),
302  $this->msg( 'allmessages-filter-translate' )->text()
303  );
304  $talkLink = $this->msg( 'talkpagelinktext' )->text();
305 
306  if ( $this->mCurrentRow->am_customised ) {
307  $title = $linkRenderer->makeKnownLink( $title, $this->getLanguage()->lcfirst( $value ) );
308  } else {
309  $title = $linkRenderer->makeBrokenLink(
310  $title, $this->getLanguage()->lcfirst( $value )
311  );
312  }
313  if ( $this->mCurrentRow->am_talk_exists ) {
314  $talk = $linkRenderer->makeKnownLink( $talk, $talkLink );
315  } else {
316  $talk = $linkRenderer->makeBrokenLink(
317  $talk,
318  $talkLink
319  );
320  }
321 
322  return $title . ' ' .
323  $this->msg( 'parentheses' )->rawParams( $talk )->escaped() .
324  ' ' .
325  $this->msg( 'parentheses' )->rawParams( $translation )->escaped();
326 
327  case 'am_default':
328  case 'am_actual':
329  return Sanitizer::escapeHtmlAllowEntities( $value );
330  }
331 
332  return '';
333  }
334 
339  public function formatRow( $row ) {
340  // Do all the normal stuff
341  $s = parent::formatRow( $row );
342 
343  // But if there's a customised message, add that too.
344  if ( $row->am_customised ) {
345  $s .= Html::openElement( 'tr', $this->getRowAttrs( $row ) );
346  $formatted = strval( $this->formatValue( 'am_actual', $row->am_actual ) );
347 
348  if ( $formatted === '' ) {
349  $formatted = "\u{00A0}";
350  }
351 
352  $s .= Html::rawElement( 'td', $this->getCellAttrs( 'am_actual', $row->am_actual ), $formatted )
353  . Html::closeElement( 'tr' );
354  }
355 
356  return Html::rawElement( 'tbody', [], $s );
357  }
358 
359  protected function getRowAttrs( $row ) {
360  return [];
361  }
362 
368  protected function getCellAttrs( $field, $value ) {
369  $attr = [];
370  if ( $field === 'am_title' ) {
371  if ( $this->mCurrentRow->am_customised ) {
372  $attr += [ 'rowspan' => '2' ];
373  }
374  } else {
375  $attr += [
376  'lang' => $this->lang->getHtmlCode(),
377  'dir' => $this->lang->getDir(),
378  ];
379  if ( $this->mCurrentRow->am_customised ) {
380  // CSS class: am_default, am_actual
381  $attr += [ 'class' => $field ];
382  }
383  }
384  return $attr;
385  }
386 
387  // This is not actually used, as getStartBody is overridden above
388  protected function getFieldNames() {
389  return [
390  'am_title' => $this->msg( 'allmessagesname' )->text(),
391  'am_default' => $this->msg( 'allmessagesdefault' )->text()
392  ];
393  }
394 
395  public function getTitle() {
396  return SpecialPage::getTitleFor( 'Allmessages', false );
397  }
398 
399  protected function isFieldSortable( $x ) {
400  return false;
401  }
402 
403  public function getDefaultSort() {
404  return '';
405  }
406 
407  public function getQueryInfo() {
408  return [];
409  }
410 
411 }
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.
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, ILoadBalancer $loadBalancer, LocalisationCache $localisationCache, FormOptions $opts)
static getCustomisedStatuses( $messageNames, $langcode='en', $foreign=false, IDatabase $dbr=null)
Determine which of the MediaWiki and MediaWiki_talk namespace pages exist.
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()
Helper class to keep track of options when mixing links and form elements.
Definition: FormOptions.php:35
getValue( $name)
Get the value for the given option name.
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:214
static openElement( $element, $attribs=[])
Identical to rawElement(), but has no third parameter and omits the end tag (and the self-closing '/'...
Definition: Html.php:256
static closeElement( $element)
Returns "</$element>".
Definition: Html.php:320
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:56
getCode()
Get the internal language code for this language object.
Definition: Language.php:4359
Caching for the contents of localisation files.
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 anchor link elements for pages.
Some internal bits split of from Skin.php.
Definition: Linker.php:65
static escapeHtmlAllowEntities( $html)
Given HTML input, escape with htmlspecialchars but un-escape entities.
Definition: Sanitizer.php:1122
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:29
getTableClass()
TablePager relies on mw-datatable for styling, see T214208.
Definition: TablePager.php:272
static makeTitleSafe( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:667
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:641
static newFromRow( $row)
Make a Title object from a DB row.
Definition: Title.php:576
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:112
Interface for objects which can provide a MediaWiki context on request.
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:40
This class is a delegate to ILBFactory for a given database cluster.
getConnectionRef( $i, $groups=[], $domain=false, $flags=0)
Result wrapper for grabbing data queried from an IDatabase object.
foreach( $mmfl['setupFiles'] as $fileName) if( $queue) if(empty( $mmfl['quiet'])) $s
const DB_REPLICA
Definition: defines.php:26