AllMessagesTablePager.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. <?php
  2. /**
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along
  14. * with this program; if not, write to the Free Software Foundation, Inc.,
  15. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. * http://www.gnu.org/copyleft/gpl.html
  17. *
  18. * @file
  19. * @ingroup Pager
  20. */
  21. use MediaWiki\MediaWikiServices;
  22. use MediaWiki\Linker\LinkRenderer;
  23. use Wikimedia\Rdbms\FakeResultWrapper;
  24. /**
  25. * Use TablePager for prettified output. We have to pretend that we're
  26. * getting data from a table when in fact not all of it comes from the database.
  27. *
  28. * @ingroup Pager
  29. */
  30. class AllMessagesTablePager extends TablePager {
  31. /**
  32. * @var string
  33. */
  34. protected $langcode;
  35. /**
  36. * @var bool
  37. */
  38. protected $foreign;
  39. /**
  40. * @var string
  41. */
  42. protected $prefix;
  43. /**
  44. * @var string
  45. */
  46. protected $suffix;
  47. /**
  48. * @var Language
  49. */
  50. public $lang;
  51. /**
  52. * @var null|bool
  53. */
  54. public $custom;
  55. /**
  56. * @param IContextSource|null $context
  57. * @param FormOptions $opts
  58. * @param LinkRenderer $linkRenderer
  59. */
  60. public function __construct( IContextSource $context = null, FormOptions $opts,
  61. LinkRenderer $linkRenderer
  62. ) {
  63. parent::__construct( $context, $linkRenderer );
  64. $this->mIndexField = 'am_title';
  65. // FIXME: Why does this need to be set to DIR_DESCENDING to produce ascending ordering?
  66. $this->mDefaultDirection = IndexPager::DIR_DESCENDING;
  67. $contLang = MediaWikiServices::getInstance()->getContentLanguage();
  68. $this->lang = wfGetLangObj( $opts->getValue( 'lang' ) );
  69. $this->langcode = $this->lang->getCode();
  70. $this->foreign = !$this->lang->equals( $contLang );
  71. $filter = $opts->getValue( 'filter' );
  72. if ( $filter === 'all' ) {
  73. $this->custom = null; // So won't match in either case
  74. } else {
  75. $this->custom = ( $filter === 'unmodified' );
  76. }
  77. $prefix = $this->getLanguage()->ucfirst( $opts->getValue( 'prefix' ) );
  78. $prefix = $prefix !== '' ?
  79. Title::makeTitleSafe( NS_MEDIAWIKI, $opts->getValue( 'prefix' ) ) :
  80. null;
  81. if ( $prefix !== null ) {
  82. $displayPrefix = $prefix->getDBkey();
  83. $this->prefix = '/^' . preg_quote( $displayPrefix, '/' ) . '/i';
  84. } else {
  85. $this->prefix = false;
  86. }
  87. // The suffix that may be needed for message names if we're in a
  88. // different language (eg [[MediaWiki:Foo/fr]]: $suffix = '/fr'
  89. if ( $this->foreign ) {
  90. $this->suffix = '/' . $this->langcode;
  91. } else {
  92. $this->suffix = '';
  93. }
  94. }
  95. function getAllMessages( $descending ) {
  96. $messageNames = Language::getLocalisationCache()->getSubitemList( 'en', 'messages' );
  97. // Normalise message names so they look like page titles and sort correctly - T86139
  98. $messageNames = array_map( [ $this->lang, 'ucfirst' ], $messageNames );
  99. if ( $descending ) {
  100. rsort( $messageNames );
  101. } else {
  102. asort( $messageNames );
  103. }
  104. return $messageNames;
  105. }
  106. /**
  107. * Determine which of the MediaWiki and MediaWiki_talk namespace pages exist.
  108. * Returns [ 'pages' => ..., 'talks' => ... ], where the subarrays have
  109. * an entry for each existing page, with the key being the message name and
  110. * value arbitrary.
  111. *
  112. * @param array $messageNames
  113. * @param string $langcode What language code
  114. * @param bool $foreign Whether the $langcode is not the content language
  115. * @return array A 'pages' and 'talks' array with the keys of existing pages
  116. */
  117. public static function getCustomisedStatuses( $messageNames, $langcode = 'en', $foreign = false ) {
  118. // FIXME: This function should be moved to Language:: or something.
  119. $dbr = wfGetDB( DB_REPLICA );
  120. $res = $dbr->select( 'page',
  121. [ 'page_namespace', 'page_title' ],
  122. [ 'page_namespace' => [ NS_MEDIAWIKI, NS_MEDIAWIKI_TALK ] ],
  123. __METHOD__,
  124. [ 'USE INDEX' => 'name_title' ]
  125. );
  126. $xNames = array_flip( $messageNames );
  127. $pageFlags = $talkFlags = [];
  128. foreach ( $res as $s ) {
  129. $exists = false;
  130. if ( $foreign ) {
  131. $titleParts = explode( '/', $s->page_title );
  132. if ( count( $titleParts ) === 2 &&
  133. $langcode === $titleParts[1] &&
  134. isset( $xNames[$titleParts[0]] )
  135. ) {
  136. $exists = $titleParts[0];
  137. }
  138. } elseif ( isset( $xNames[$s->page_title] ) ) {
  139. $exists = $s->page_title;
  140. }
  141. $title = Title::newFromRow( $s );
  142. if ( $exists && $title->inNamespace( NS_MEDIAWIKI ) ) {
  143. $pageFlags[$exists] = true;
  144. } elseif ( $exists && $title->inNamespace( NS_MEDIAWIKI_TALK ) ) {
  145. $talkFlags[$exists] = true;
  146. }
  147. }
  148. return [ 'pages' => $pageFlags, 'talks' => $talkFlags ];
  149. }
  150. /**
  151. * This function normally does a database query to get the results; we need
  152. * to make a pretend result using a FakeResultWrapper.
  153. * @param string $offset
  154. * @param int $limit
  155. * @param bool $order
  156. * @return FakeResultWrapper
  157. */
  158. function reallyDoQuery( $offset, $limit, $order ) {
  159. $asc = ( $order === self::QUERY_ASCENDING );
  160. $messageNames = $this->getAllMessages( $order );
  161. $statuses = self::getCustomisedStatuses( $messageNames, $this->langcode, $this->foreign );
  162. $rows = [];
  163. $count = 0;
  164. foreach ( $messageNames as $key ) {
  165. $customised = isset( $statuses['pages'][$key] );
  166. if ( $customised !== $this->custom &&
  167. ( $asc && ( $key < $offset || !$offset ) || !$asc && $key > $offset ) &&
  168. ( ( $this->prefix && preg_match( $this->prefix, $key ) ) || $this->prefix === false )
  169. ) {
  170. $actual = $this->msg( $key )->inLanguage( $this->lang )->plain();
  171. $default = $this->msg( $key )->inLanguage( $this->lang )->useDatabase( false )->plain();
  172. $rows[] = [
  173. 'am_title' => $key,
  174. 'am_actual' => $actual,
  175. 'am_default' => $default,
  176. 'am_customised' => $customised,
  177. 'am_talk_exists' => isset( $statuses['talks'][$key] )
  178. ];
  179. $count++;
  180. }
  181. if ( $count === $limit ) {
  182. break;
  183. }
  184. }
  185. return new FakeResultWrapper( $rows );
  186. }
  187. protected function getStartBody() {
  188. $tableClass = $this->getTableClass();
  189. return Xml::openElement( 'table', [
  190. 'class' => "mw-datatable $tableClass",
  191. 'id' => 'mw-allmessagestable'
  192. ] ) .
  193. "\n" .
  194. "<thead><tr>
  195. <th rowspan=\"2\">" .
  196. $this->msg( 'allmessagesname' )->escaped() . "
  197. </th>
  198. <th>" .
  199. $this->msg( 'allmessagesdefault' )->escaped() .
  200. "</th>
  201. </tr>\n
  202. <tr>
  203. <th>" .
  204. $this->msg( 'allmessagescurrent' )->escaped() .
  205. "</th>
  206. </tr></thead>\n";
  207. }
  208. function getEndBody() {
  209. return Html::closeElement( 'table' );
  210. }
  211. function formatValue( $field, $value ) {
  212. $linkRenderer = $this->getLinkRenderer();
  213. switch ( $field ) {
  214. case 'am_title' :
  215. $title = Title::makeTitle( NS_MEDIAWIKI, $value . $this->suffix );
  216. $talk = Title::makeTitle( NS_MEDIAWIKI_TALK, $value . $this->suffix );
  217. $translation = Linker::makeExternalLink(
  218. 'https://translatewiki.net/w/i.php?' . wfArrayToCgi( [
  219. 'title' => 'Special:SearchTranslations',
  220. 'group' => 'mediawiki',
  221. 'grouppath' => 'mediawiki',
  222. 'language' => $this->getLanguage()->getCode(),
  223. 'query' => $value . ' ' . $this->msg( $value )->plain()
  224. ] ),
  225. $this->msg( 'allmessages-filter-translate' )->text()
  226. );
  227. $talkLink = $this->msg( 'talkpagelinktext' )->escaped();
  228. if ( $this->mCurrentRow->am_customised ) {
  229. $title = $linkRenderer->makeKnownLink( $title, $this->getLanguage()->lcfirst( $value ) );
  230. } else {
  231. $title = $linkRenderer->makeBrokenLink(
  232. $title, $this->getLanguage()->lcfirst( $value )
  233. );
  234. }
  235. if ( $this->mCurrentRow->am_talk_exists ) {
  236. $talk = $linkRenderer->makeKnownLink( $talk, $talkLink );
  237. } else {
  238. $talk = $linkRenderer->makeBrokenLink(
  239. $talk,
  240. $talkLink
  241. );
  242. }
  243. return $title . ' ' .
  244. $this->msg( 'parentheses' )->rawParams( $talk )->escaped() .
  245. ' ' .
  246. $this->msg( 'parentheses' )->rawParams( $translation )->escaped();
  247. case 'am_default' :
  248. case 'am_actual' :
  249. return Sanitizer::escapeHtmlAllowEntities( $value );
  250. }
  251. return '';
  252. }
  253. /**
  254. * @param stdClass $row
  255. * @return string HTML
  256. */
  257. function formatRow( $row ) {
  258. // Do all the normal stuff
  259. $s = parent::formatRow( $row );
  260. // But if there's a customised message, add that too.
  261. if ( $row->am_customised ) {
  262. $s .= Html::openElement( 'tr', $this->getRowAttrs( $row, true ) );
  263. $formatted = strval( $this->formatValue( 'am_actual', $row->am_actual ) );
  264. if ( $formatted === '' ) {
  265. $formatted = "\u{00A0}";
  266. }
  267. $s .= Html::element( 'td', $this->getCellAttrs( 'am_actual', $row->am_actual ), $formatted )
  268. . Html::closeElement( 'tr' );
  269. }
  270. return Html::rawElement( 'tbody', [], $s );
  271. }
  272. function getRowAttrs( $row ) {
  273. return [];
  274. }
  275. /**
  276. * @param string $field
  277. * @param string $value
  278. * @return array HTML attributes
  279. */
  280. function getCellAttrs( $field, $value ) {
  281. $attr = [];
  282. if ( $field === 'am_title' ) {
  283. if ( $this->mCurrentRow->am_customised ) {
  284. $attr += [ 'rowspan' => '2' ];
  285. }
  286. } else {
  287. $attr += [
  288. 'lang' => $this->lang->getHtmlCode(),
  289. 'dir' => $this->lang->getDir(),
  290. ];
  291. if ( $this->mCurrentRow->am_customised ) {
  292. // CSS class: am_default, am_actual
  293. $attr += [ 'class' => $field ];
  294. }
  295. }
  296. return $attr;
  297. }
  298. // This is not actually used, as getStartBody is overridden above
  299. function getFieldNames() {
  300. return [
  301. 'am_title' => $this->msg( 'allmessagesname' )->text(),
  302. 'am_default' => $this->msg( 'allmessagesdefault' )->text()
  303. ];
  304. }
  305. function getTitle() {
  306. return SpecialPage::getTitleFor( 'Allmessages', false );
  307. }
  308. function isFieldSortable( $x ) {
  309. return false;
  310. }
  311. function getDefaultSort() {
  312. return '';
  313. }
  314. function getQueryInfo() {
  315. return [];
  316. }
  317. }