PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/includes/specials/pagers/AllMessagesTablePager.php

https://gitlab.com/link233/bootmw
PHP | 424 lines | 309 code | 57 blank | 58 comment | 33 complexity | e6b409963211fba68dce61e1f4221436 MD5 | raw file
  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. /**
  22. * Use TablePager for prettified output. We have to pretend that we're
  23. * getting data from a table when in fact not all of it comes from the database.
  24. *
  25. * @ingroup Pager
  26. */
  27. class AllMessagesTablePager extends TablePager {
  28. protected $filter, $prefix, $langcode, $displayPrefix;
  29. public $mLimitsShown;
  30. /**
  31. * @var Language
  32. */
  33. public $lang;
  34. /**
  35. * @var null|bool
  36. */
  37. public $custom;
  38. function __construct( $page, $conds, $langObj = null ) {
  39. parent::__construct( $page->getContext() );
  40. $this->mIndexField = 'am_title';
  41. $this->mPage = $page;
  42. $this->mConds = $conds;
  43. // FIXME: Why does this need to be set to DIR_DESCENDING to produce ascending ordering?
  44. $this->mDefaultDirection = IndexPager::DIR_DESCENDING;
  45. $this->mLimitsShown = [ 20, 50, 100, 250, 500, 5000 ];
  46. global $wgContLang;
  47. $this->talk = $this->msg( 'talkpagelinktext' )->escaped();
  48. $this->lang = ( $langObj ? $langObj : $wgContLang );
  49. $this->langcode = $this->lang->getCode();
  50. $this->foreign = $this->langcode !== $wgContLang->getCode();
  51. $request = $this->getRequest();
  52. $this->filter = $request->getVal( 'filter', 'all' );
  53. if ( $this->filter === 'all' ) {
  54. $this->custom = null; // So won't match in either case
  55. } else {
  56. $this->custom = ( $this->filter === 'unmodified' );
  57. }
  58. $prefix = $this->getLanguage()->ucfirst( $request->getVal( 'prefix', '' ) );
  59. $prefix = $prefix !== '' ?
  60. Title::makeTitleSafe( NS_MEDIAWIKI, $request->getVal( 'prefix', null ) ) :
  61. null;
  62. if ( $prefix !== null ) {
  63. $this->displayPrefix = $prefix->getDBkey();
  64. $this->prefix = '/^' . preg_quote( $this->displayPrefix, '/' ) . '/i';
  65. } else {
  66. $this->displayPrefix = false;
  67. $this->prefix = false;
  68. }
  69. // The suffix that may be needed for message names if we're in a
  70. // different language (eg [[MediaWiki:Foo/fr]]: $suffix = '/fr'
  71. if ( $this->foreign ) {
  72. $this->suffix = '/' . $this->langcode;
  73. } else {
  74. $this->suffix = '';
  75. }
  76. }
  77. function buildForm() {
  78. $attrs = [ 'id' => 'mw-allmessages-form-lang', 'name' => 'lang' ];
  79. $msg = wfMessage( 'allmessages-language' );
  80. $langSelect = Xml::languageSelector( $this->langcode, false, null, $attrs, $msg );
  81. $out = Xml::openElement( 'form', [
  82. 'method' => 'get',
  83. 'action' => $this->getConfig()->get( 'Script' ),
  84. 'id' => 'mw-allmessages-form'
  85. ] ) .
  86. Xml::fieldset( $this->msg( 'allmessages-filter-legend' )->text() ) .
  87. Html::hidden( 'title', $this->getTitle()->getPrefixedText() ) .
  88. Xml::openElement( 'table', [ 'class' => 'mw-allmessages-table' ] ) . "\n" .
  89. '<tr>
  90. <td class="mw-label">' .
  91. Xml::label( $this->msg( 'allmessages-prefix' )->text(), 'mw-allmessages-form-prefix' ) .
  92. "</td>\n
  93. <td class=\"mw-input\">" .
  94. Xml::input(
  95. 'prefix',
  96. 20,
  97. str_replace( '_', ' ', $this->displayPrefix ),
  98. [ 'id' => 'mw-allmessages-form-prefix' ]
  99. ) .
  100. "</td>\n
  101. </tr>
  102. <tr>\n
  103. <td class='mw-label'>" .
  104. $this->msg( 'allmessages-filter' )->escaped() .
  105. "</td>\n
  106. <td class='mw-input'>" .
  107. Xml::radioLabel( $this->msg( 'allmessages-filter-unmodified' )->text(),
  108. 'filter',
  109. 'unmodified',
  110. 'mw-allmessages-form-filter-unmodified',
  111. ( $this->filter === 'unmodified' )
  112. ) .
  113. Xml::radioLabel( $this->msg( 'allmessages-filter-all' )->text(),
  114. 'filter',
  115. 'all',
  116. 'mw-allmessages-form-filter-all',
  117. ( $this->filter === 'all' )
  118. ) .
  119. Xml::radioLabel( $this->msg( 'allmessages-filter-modified' )->text(),
  120. 'filter',
  121. 'modified',
  122. 'mw-allmessages-form-filter-modified',
  123. ( $this->filter === 'modified' )
  124. ) .
  125. "</td>\n
  126. </tr>
  127. <tr>\n
  128. <td class=\"mw-label\">" . $langSelect[0] . "</td>\n
  129. <td class=\"mw-input\">" . $langSelect[1] . "</td>\n
  130. </tr>" .
  131. '<tr>
  132. <td class="mw-label">' .
  133. Xml::label( $this->msg( 'table_pager_limit_label' )->text(), 'mw-table_pager_limit_label' ) .
  134. '</td>
  135. <td class="mw-input">' .
  136. $this->getLimitSelect( [ 'id' => 'mw-table_pager_limit_label' ] ) .
  137. '</td>
  138. <tr>
  139. <td></td>
  140. <td>' .
  141. Xml::submitButton( $this->msg( 'allmessages-filter-submit' )->text() ) .
  142. "</td>\n
  143. </tr>" .
  144. Xml::closeElement( 'table' ) .
  145. $this->getHiddenFields( [ 'title', 'prefix', 'filter', 'lang', 'limit' ] ) .
  146. Xml::closeElement( 'fieldset' ) .
  147. Xml::closeElement( 'form' );
  148. return $out;
  149. }
  150. function getAllMessages( $descending ) {
  151. $messageNames = Language::getLocalisationCache()->getSubitemList( 'en', 'messages' );
  152. // Normalise message names so they look like page titles and sort correctly - T86139
  153. $messageNames = array_map( [ $this->lang, 'ucfirst' ], $messageNames );
  154. if ( $descending ) {
  155. rsort( $messageNames );
  156. } else {
  157. asort( $messageNames );
  158. }
  159. return $messageNames;
  160. }
  161. /**
  162. * Determine which of the MediaWiki and MediaWiki_talk namespace pages exist.
  163. * Returns array( 'pages' => ..., 'talks' => ... ), where the subarrays have
  164. * an entry for each existing page, with the key being the message name and
  165. * value arbitrary.
  166. *
  167. * @param array $messageNames
  168. * @param string $langcode What language code
  169. * @param bool $foreign Whether the $langcode is not the content language
  170. * @return array A 'pages' and 'talks' array with the keys of existing pages
  171. */
  172. public static function getCustomisedStatuses( $messageNames, $langcode = 'en', $foreign = false ) {
  173. // FIXME: This function should be moved to Language:: or something.
  174. $dbr = wfGetDB( DB_SLAVE );
  175. $res = $dbr->select( 'page',
  176. [ 'page_namespace', 'page_title' ],
  177. [ 'page_namespace' => [ NS_MEDIAWIKI, NS_MEDIAWIKI_TALK ] ],
  178. __METHOD__,
  179. [ 'USE INDEX' => 'name_title' ]
  180. );
  181. $xNames = array_flip( $messageNames );
  182. $pageFlags = $talkFlags = [];
  183. foreach ( $res as $s ) {
  184. $exists = false;
  185. if ( $foreign ) {
  186. $titleParts = explode( '/', $s->page_title );
  187. if ( count( $titleParts ) === 2 &&
  188. $langcode === $titleParts[1] &&
  189. isset( $xNames[$titleParts[0]] )
  190. ) {
  191. $exists = $titleParts[0];
  192. }
  193. } elseif ( isset( $xNames[$s->page_title] ) ) {
  194. $exists = $s->page_title;
  195. }
  196. $title = Title::newFromRow( $s );
  197. if ( $exists && $title->inNamespace( NS_MEDIAWIKI ) ) {
  198. $pageFlags[$exists] = true;
  199. } elseif ( $exists && $title->inNamespace( NS_MEDIAWIKI_TALK ) ) {
  200. $talkFlags[$exists] = true;
  201. }
  202. }
  203. return [ 'pages' => $pageFlags, 'talks' => $talkFlags ];
  204. }
  205. /**
  206. * This function normally does a database query to get the results; we need
  207. * to make a pretend result using a FakeResultWrapper.
  208. * @param string $offset
  209. * @param int $limit
  210. * @param bool $descending
  211. * @return FakeResultWrapper
  212. */
  213. function reallyDoQuery( $offset, $limit, $descending ) {
  214. $result = new FakeResultWrapper( [] );
  215. $messageNames = $this->getAllMessages( $descending );
  216. $statuses = self::getCustomisedStatuses( $messageNames, $this->langcode, $this->foreign );
  217. $count = 0;
  218. foreach ( $messageNames as $key ) {
  219. $customised = isset( $statuses['pages'][$key] );
  220. if ( $customised !== $this->custom &&
  221. ( $descending && ( $key < $offset || !$offset ) || !$descending && $key > $offset ) &&
  222. ( ( $this->prefix && preg_match( $this->prefix, $key ) ) || $this->prefix === false )
  223. ) {
  224. $actual = wfMessage( $key )->inLanguage( $this->langcode )->plain();
  225. $default = wfMessage( $key )->inLanguage( $this->langcode )->useDatabase( false )->plain();
  226. $result->result[] = [
  227. 'am_title' => $key,
  228. 'am_actual' => $actual,
  229. 'am_default' => $default,
  230. 'am_customised' => $customised,
  231. 'am_talk_exists' => isset( $statuses['talks'][$key] )
  232. ];
  233. $count++;
  234. }
  235. if ( $count === $limit ) {
  236. break;
  237. }
  238. }
  239. return $result;
  240. }
  241. function getStartBody() {
  242. $tableClass = $this->getTableClass();
  243. return Xml::openElement( 'table', [
  244. 'class' => "mw-datatable $tableClass",
  245. 'id' => 'mw-allmessagestable'
  246. ] ) .
  247. "\n" .
  248. "<thead><tr>
  249. <th rowspan=\"2\">" .
  250. $this->msg( 'allmessagesname' )->escaped() . "
  251. </th>
  252. <th>" .
  253. $this->msg( 'allmessagesdefault' )->escaped() .
  254. "</th>
  255. </tr>\n
  256. <tr>
  257. <th>" .
  258. $this->msg( 'allmessagescurrent' )->escaped() .
  259. "</th>
  260. </tr></thead><tbody>\n";
  261. }
  262. function formatValue( $field, $value ) {
  263. switch ( $field ) {
  264. case 'am_title' :
  265. $title = Title::makeTitle( NS_MEDIAWIKI, $value . $this->suffix );
  266. $talk = Title::makeTitle( NS_MEDIAWIKI_TALK, $value . $this->suffix );
  267. $translation = Linker::makeExternalLink(
  268. 'https://translatewiki.net/w/i.php?' . wfArrayToCgi( [
  269. 'title' => 'Special:SearchTranslations',
  270. 'group' => 'mediawiki',
  271. 'grouppath' => 'mediawiki',
  272. 'query' => 'language:' . $this->getLanguage()->getCode() . '^25 ' .
  273. 'messageid:"MediaWiki:' . $value . '"^10 "' .
  274. $this->msg( $value )->inLanguage( 'en' )->plain() . '"'
  275. ] ),
  276. $this->msg( 'allmessages-filter-translate' )->text()
  277. );
  278. if ( $this->mCurrentRow->am_customised ) {
  279. $title = Linker::linkKnown( $title, $this->getLanguage()->lcfirst( $value ) );
  280. } else {
  281. $title = Linker::link(
  282. $title,
  283. $this->getLanguage()->lcfirst( $value ),
  284. [],
  285. [],
  286. [ 'broken' ]
  287. );
  288. }
  289. if ( $this->mCurrentRow->am_talk_exists ) {
  290. $talk = Linker::linkKnown( $talk, $this->talk );
  291. } else {
  292. $talk = Linker::link(
  293. $talk,
  294. $this->talk,
  295. [],
  296. [],
  297. [ 'broken' ]
  298. );
  299. }
  300. return $title . ' ' .
  301. $this->msg( 'parentheses' )->rawParams( $talk )->escaped() .
  302. ' ' .
  303. $this->msg( 'parentheses' )->rawParams( $translation )->escaped();
  304. case 'am_default' :
  305. case 'am_actual' :
  306. return Sanitizer::escapeHtmlAllowEntities( $value );
  307. }
  308. return '';
  309. }
  310. function formatRow( $row ) {
  311. // Do all the normal stuff
  312. $s = parent::formatRow( $row );
  313. // But if there's a customised message, add that too.
  314. if ( $row->am_customised ) {
  315. $s .= Xml::openElement( 'tr', $this->getRowAttrs( $row, true ) );
  316. $formatted = strval( $this->formatValue( 'am_actual', $row->am_actual ) );
  317. if ( $formatted === '' ) {
  318. $formatted = '&#160;';
  319. }
  320. $s .= Xml::tags( 'td', $this->getCellAttrs( 'am_actual', $row->am_actual ), $formatted )
  321. . "</tr>\n";
  322. }
  323. return $s;
  324. }
  325. function getRowAttrs( $row, $isSecond = false ) {
  326. $arr = [];
  327. if ( $row->am_customised ) {
  328. $arr['class'] = 'allmessages-customised';
  329. }
  330. if ( !$isSecond ) {
  331. $arr['id'] = Sanitizer::escapeId( 'msg_' . $this->getLanguage()->lcfirst( $row->am_title ) );
  332. }
  333. return $arr;
  334. }
  335. function getCellAttrs( $field, $value ) {
  336. if ( $this->mCurrentRow->am_customised && $field === 'am_title' ) {
  337. return [ 'rowspan' => '2', 'class' => $field ];
  338. } elseif ( $field === 'am_title' ) {
  339. return [ 'class' => $field ];
  340. } else {
  341. return [
  342. 'lang' => $this->lang->getHtmlCode(),
  343. 'dir' => $this->lang->getDir(),
  344. 'class' => $field
  345. ];
  346. }
  347. }
  348. // This is not actually used, as getStartBody is overridden above
  349. function getFieldNames() {
  350. return [
  351. 'am_title' => $this->msg( 'allmessagesname' )->text(),
  352. 'am_default' => $this->msg( 'allmessagesdefault' )->text()
  353. ];
  354. }
  355. function getTitle() {
  356. return SpecialPage::getTitleFor( 'Allmessages', false );
  357. }
  358. function isFieldSortable( $x ) {
  359. return false;
  360. }
  361. function getDefaultSort() {
  362. return '';
  363. }
  364. function getQueryInfo() {
  365. return '';
  366. }
  367. }