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