MediaWiki  master
AllMessagesTablePager.php
Go to the documentation of this file.
1 <?php
24 
32 
36  protected $langcode;
37 
41  protected $foreign;
42 
46  protected $prefix;
47 
51  public $lang;
52 
56  public $custom;
57 
62  public function __construct( IContextSource $context = null, FormOptions $opts ) {
63  parent::__construct( $context );
64 
65  $this->mIndexField = 'am_title';
66  // FIXME: Why does this need to be set to DIR_DESCENDING to produce ascending ordering?
67  $this->mDefaultDirection = IndexPager::DIR_DESCENDING;
68 
69  $contLang = MediaWikiServices::getInstance()->getContentLanguage();
70  $this->lang = wfGetLangObj( $opts->getValue( 'lang' ) );
71 
72  $this->langcode = $this->lang->getCode();
73  $this->foreign = !$this->lang->equals( $contLang );
74 
75  $filter = $opts->getValue( 'filter' );
76  if ( $filter === 'all' ) {
77  $this->custom = null; // So won't match in either case
78  } else {
79  $this->custom = ( $filter === 'unmodified' );
80  }
81 
82  $prefix = $this->getLanguage()->ucfirst( $opts->getValue( 'prefix' ) );
83  $prefix = $prefix !== '' ?
84  Title::makeTitleSafe( NS_MEDIAWIKI, $opts->getValue( 'prefix' ) ) :
85  null;
86 
87  if ( $prefix !== null ) {
88  $displayPrefix = $prefix->getDBkey();
89  $this->prefix = '/^' . preg_quote( $displayPrefix, '/' ) . '/i';
90  } else {
91  $this->prefix = false;
92  }
93 
94  // The suffix that may be needed for message names if we're in a
95  // different language (eg [[MediaWiki:Foo/fr]]: $suffix = '/fr'
96  if ( $this->foreign ) {
97  $this->suffix = '/' . $this->langcode;
98  } else {
99  $this->suffix = '';
100  }
101  }
102 
103  function getAllMessages( $descending ) {
104  $messageNames = Language::getLocalisationCache()->getSubitemList( 'en', 'messages' );
105 
106  // Normalise message names so they look like page titles and sort correctly - T86139
107  $messageNames = array_map( [ $this->lang, 'ucfirst' ], $messageNames );
108 
109  if ( $descending ) {
110  rsort( $messageNames );
111  } else {
112  asort( $messageNames );
113  }
114 
115  return $messageNames;
116  }
117 
129  public static function getCustomisedStatuses( $messageNames, $langcode = 'en', $foreign = false ) {
130  // FIXME: This function should be moved to Language:: or something.
131 
132  $dbr = wfGetDB( DB_REPLICA );
133  $res = $dbr->select( 'page',
134  [ 'page_namespace', 'page_title' ],
135  [ 'page_namespace' => [ NS_MEDIAWIKI, NS_MEDIAWIKI_TALK ] ],
136  __METHOD__,
137  [ 'USE INDEX' => 'name_title' ]
138  );
139  $xNames = array_flip( $messageNames );
140 
141  $pageFlags = $talkFlags = [];
142 
143  foreach ( $res as $s ) {
144  $exists = false;
145 
146  if ( $foreign ) {
147  $titleParts = explode( '/', $s->page_title );
148  if ( count( $titleParts ) === 2 &&
149  $langcode === $titleParts[1] &&
150  isset( $xNames[$titleParts[0]] )
151  ) {
152  $exists = $titleParts[0];
153  }
154  } elseif ( isset( $xNames[$s->page_title] ) ) {
155  $exists = $s->page_title;
156  }
157 
158  $title = Title::newFromRow( $s );
159  if ( $exists && $title->inNamespace( NS_MEDIAWIKI ) ) {
160  $pageFlags[$exists] = true;
161  } elseif ( $exists && $title->inNamespace( NS_MEDIAWIKI_TALK ) ) {
162  $talkFlags[$exists] = true;
163  }
164  }
165 
166  return [ 'pages' => $pageFlags, 'talks' => $talkFlags ];
167  }
168 
177  function reallyDoQuery( $offset, $limit, $order ) {
178  $asc = ( $order === self::QUERY_ASCENDING );
179 
180  $messageNames = $this->getAllMessages( $order );
181  $statuses = self::getCustomisedStatuses( $messageNames, $this->langcode, $this->foreign );
182 
183  $rows = [];
184  $count = 0;
185  foreach ( $messageNames as $key ) {
186  $customised = isset( $statuses['pages'][$key] );
187  if ( $customised !== $this->custom &&
188  ( $asc && ( $key < $offset || !$offset ) || !$asc && $key > $offset ) &&
189  ( ( $this->prefix && preg_match( $this->prefix, $key ) ) || $this->prefix === false )
190  ) {
191  $actual = $this->msg( $key )->inLanguage( $this->lang )->plain();
192  $default = $this->msg( $key )->inLanguage( $this->lang )->useDatabase( false )->plain();
193  $rows[] = [
194  'am_title' => $key,
195  'am_actual' => $actual,
196  'am_default' => $default,
197  'am_customised' => $customised,
198  'am_talk_exists' => isset( $statuses['talks'][$key] )
199  ];
200  $count++;
201  }
202 
203  if ( $count === $limit ) {
204  break;
205  }
206  }
207 
208  return new FakeResultWrapper( $rows );
209  }
210 
211  protected function getStartBody() {
212  $tableClass = $this->getTableClass();
213  return Xml::openElement( 'table', [
214  'class' => "mw-datatable $tableClass",
215  'id' => 'mw-allmessagestable'
216  ] ) .
217  "\n" .
218  "<thead><tr>
219  <th rowspan=\"2\">" .
220  $this->msg( 'allmessagesname' )->escaped() . "
221  </th>
222  <th>" .
223  $this->msg( 'allmessagesdefault' )->escaped() .
224  "</th>
225  </tr>\n
226  <tr>
227  <th>" .
228  $this->msg( 'allmessagescurrent' )->escaped() .
229  "</th>
230  </tr></thead>\n";
231  }
232 
233  function getEndBody() {
234  return Html::closeElement( 'table' );
235  }
236 
237  function formatValue( $field, $value ) {
238  $linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
239  switch ( $field ) {
240  case 'am_title' :
241  $title = Title::makeTitle( NS_MEDIAWIKI, $value . $this->suffix );
242  $talk = Title::makeTitle( NS_MEDIAWIKI_TALK, $value . $this->suffix );
243  $translation = Linker::makeExternalLink(
244  'https://translatewiki.net/w/i.php?' . wfArrayToCgi( [
245  'title' => 'Special:SearchTranslations',
246  'group' => 'mediawiki',
247  'grouppath' => 'mediawiki',
248  'language' => $this->getLanguage()->getCode(),
249  'query' => $value . ' ' . $this->msg( $value )->plain()
250  ] ),
251  $this->msg( 'allmessages-filter-translate' )->text()
252  );
253  $talkLink = $this->msg( 'talkpagelinktext' )->escaped();
254 
255  if ( $this->mCurrentRow->am_customised ) {
256  $title = $linkRenderer->makeKnownLink( $title, $this->getLanguage()->lcfirst( $value ) );
257  } else {
258  $title = $linkRenderer->makeBrokenLink(
259  $title,
260  $this->getLanguage()->lcfirst( $value )
261  );
262  }
263  if ( $this->mCurrentRow->am_talk_exists ) {
264  $talk = $linkRenderer->makeKnownLink( $talk, $talkLink );
265  } else {
266  $talk = $linkRenderer->makeBrokenLink(
267  $talk,
268  $talkLink
269  );
270  }
271 
272  return $title . ' ' .
273  $this->msg( 'parentheses' )->rawParams( $talk )->escaped() .
274  ' ' .
275  $this->msg( 'parentheses' )->rawParams( $translation )->escaped();
276 
277  case 'am_default' :
278  case 'am_actual' :
280  }
281 
282  return '';
283  }
284 
289  function formatRow( $row ) {
290  // Do all the normal stuff
291  $s = parent::formatRow( $row );
292 
293  // But if there's a customised message, add that too.
294  if ( $row->am_customised ) {
295  $s .= Html::openElement( 'tr', $this->getRowAttrs( $row, true ) );
296  $formatted = strval( $this->formatValue( 'am_actual', $row->am_actual ) );
297 
298  if ( $formatted === '' ) {
299  $formatted = "\u{00A0}";
300  }
301 
302  $s .= Html::element( 'td', $this->getCellAttrs( 'am_actual', $row->am_actual ), $formatted )
303  . Html::closeElement( 'tr' );
304  }
305 
306  return Html::rawElement( 'tbody', [], $s );
307  }
308 
309  function getRowAttrs( $row ) {
310  return [];
311  }
312 
318  function getCellAttrs( $field, $value ) {
319  $attr = [];
320  if ( $field === 'am_title' ) {
321  if ( $this->mCurrentRow->am_customised ) {
322  $attr += [ 'rowspan' => '2' ];
323  }
324  } else {
325  $attr += [
326  'lang' => $this->lang->getHtmlCode(),
327  'dir' => $this->lang->getDir(),
328  ];
329  if ( $this->mCurrentRow->am_customised ) {
330  // CSS class: am_default, am_actual
331  $attr += [ 'class' => $field ];
332  }
333  }
334  return $attr;
335  }
336 
337  // This is not actually used, as getStartBody is overridden above
338  function getFieldNames() {
339  return [
340  'am_title' => $this->msg( 'allmessagesname' )->text(),
341  'am_default' => $this->msg( 'allmessagesdefault' )->text()
342  ];
343  }
344 
345  function getTitle() {
346  return SpecialPage::getTitleFor( 'Allmessages', false );
347  }
348 
349  function isFieldSortable( $x ) {
350  return false;
351  }
352 
353  function getDefaultSort() {
354  return '';
355  }
356 
357  function getQueryInfo() {
358  return '';
359  }
360 
361 }
const DIR_DESCENDING
Backwards-compatible constant for $mDefaultDirection field (do not change)
Definition: IndexPager.php:75
Helper class to keep track of options when mixing links and form elements.
Definition: FormOptions.php:35
Table-based display with a user-selectable sort order.
Definition: TablePager.php:28
static getLocalisationCache()
Get the LocalisationCache instance.
Definition: Language.php:448
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction $rows
Definition: hooks.txt:2620
static element( $element, $attribs=[], $contents='')
Identical to rawElement(), but HTML-escapes $contents (like Xml::element()).
Definition: Html.php:231
either a plain
Definition: hooks.txt:2032
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses after processing after in associative array form before processing starts Return false to skip default processing and return $ret $linkRenderer
Definition: hooks.txt:1971
static openElement( $element, $attribs=[])
Identical to rawElement(), but has no third parameter and omits the end tag (and the self-closing &#39;/&#39;...
Definition: Html.php:251
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:209
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
$value
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency MediaWikiServices
Definition: injection.txt:23
msg( $key)
Get a Message object with context set Parameters are the same as wfMessage()
static newFromRow( $row)
Make a Title object from a DB row.
Definition: Title.php:520
IContextSource $context
This list may contain false positives That usually means there is additional text with links below the first Each row contains links to the first and second as well as the first line of the second redirect text
static getCustomisedStatuses( $messageNames, $langcode='en', $foreign=false)
Determine which of the MediaWiki and MediaWiki_talk namespace pages exist.
wfGetLangObj( $langcode=false)
Return a Language object from $langcode.
wfArrayToCgi( $array1, $array2=null, $prefix='')
This function takes one or two arrays as input, and returns a CGI-style string, e.g.
static openElement( $element, $attribs=null)
This opens an XML element.
Definition: Xml.php:108
__construct(IContextSource $context=null, FormOptions $opts)
const NS_MEDIAWIKI_TALK
Definition: Defines.php:69
$res
Definition: database.txt:21
reallyDoQuery( $offset, $limit, $order)
This function normally does a database query to get the results; we need to make a pretend result usi...
static closeElement( $element)
Returns "</$element>".
Definition: Html.php:315
static makeExternalLink( $url, $text, $escape=true, $linktype='', $attribs=[], $title=null)
Make an external link.
Definition: Linker.php:844
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition: hooks.txt:767
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:912
$filter
static getTitleFor( $name, $subpage=false, $fragment='')
Get a localised Title object for a specified special page name If you don&#39;t need a full Title object...
Definition: SpecialPage.php:83
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
const NS_MEDIAWIKI
Definition: Defines.php:68
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:618
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:590
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
Overloads the relevant methods of the real ResultsWrapper so it doesn&#39;t go anywhere near an actual da...
static escapeHtmlAllowEntities( $html)
Given HTML input, escape with htmlspecialchars but un-escape entities.
Definition: Sanitizer.php:1433
const DB_REPLICA
Definition: defines.php:25
getValue( $name)
Get the value for the given option name.
Use TablePager for prettified output.