PageRenderTime 57ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/specials/SpecialAllmessages.php

https://github.com/daevid/MWFork
PHP | 441 lines | 314 code | 59 blank | 68 comment | 48 complexity | a6022a34a490481b409552f26286b2ec MD5 | raw file
  1. <?php
  2. /**
  3. * Implements Special:Allmessages
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. * @ingroup SpecialPage
  22. */
  23. /**
  24. * Use this special page to get a list of the MediaWiki system messages.
  25. *
  26. * @file
  27. * @ingroup SpecialPage
  28. */
  29. class SpecialAllmessages extends SpecialPage {
  30. /**
  31. * @var AllmessagesTablePager
  32. */
  33. protected $table;
  34. /**
  35. * Constructor
  36. */
  37. public function __construct() {
  38. parent::__construct( 'Allmessages' );
  39. }
  40. /**
  41. * Show the special page
  42. *
  43. * @param $par Mixed: parameter passed to the page or null
  44. */
  45. public function execute( $par ) {
  46. $request = $this->getRequest();
  47. $out = $this->getOutput();
  48. $this->setHeaders();
  49. global $wgUseDatabaseMessages;
  50. if( !$wgUseDatabaseMessages ) {
  51. $out->addWikiMsg( 'allmessagesnotsupportedDB' );
  52. return;
  53. } else {
  54. $this->outputHeader( 'allmessagestext' );
  55. }
  56. $out->addModuleStyles( 'mediawiki.special' );
  57. $this->filter = $request->getVal( 'filter', 'all' );
  58. $this->prefix = $request->getVal( 'prefix', '' );
  59. $this->table = new AllmessagesTablePager(
  60. $this,
  61. array(),
  62. wfGetLangObj( $request->getVal( 'lang', $par ) )
  63. );
  64. $this->langcode = $this->table->lang->getCode();
  65. $out->addHTML( $this->table->buildForm() .
  66. $this->table->getNavigationBar() .
  67. $this->table->getBody() .
  68. $this->table->getNavigationBar() );
  69. }
  70. }
  71. /**
  72. * Use TablePager for prettified output. We have to pretend that we're
  73. * getting data from a table when in fact not all of it comes from the database.
  74. */
  75. class AllmessagesTablePager extends TablePager {
  76. protected $filter, $prefix, $langcode, $displayPrefix;
  77. public $mLimitsShown;
  78. /**
  79. * @var Language
  80. */
  81. public $lang;
  82. /**
  83. * @var null|bool
  84. */
  85. public $custom;
  86. function __construct( $page, $conds, $langObj = null ) {
  87. parent::__construct();
  88. $this->mIndexField = 'am_title';
  89. $this->mPage = $page;
  90. $this->mConds = $conds;
  91. $this->mDefaultDirection = true; // always sort ascending
  92. $this->mLimitsShown = array( 20, 50, 100, 250, 500, 5000 );
  93. global $wgLang, $wgContLang, $wgRequest;
  94. $this->talk = htmlspecialchars( wfMsg( 'talkpagelinktext' ) );
  95. $this->lang = ( $langObj ? $langObj : $wgContLang );
  96. $this->langcode = $this->lang->getCode();
  97. $this->foreign = $this->langcode != $wgContLang->getCode();
  98. if( $wgRequest->getVal( 'filter', 'all' ) === 'all' ){
  99. $this->custom = null; // So won't match in either case
  100. } else {
  101. $this->custom = ($wgRequest->getVal( 'filter' ) == 'unmodified');
  102. }
  103. $prefix = $wgLang->ucfirst( $wgRequest->getVal( 'prefix', '' ) );
  104. $prefix = $prefix != '' ? Title::makeTitleSafe( NS_MEDIAWIKI, $wgRequest->getVal( 'prefix', null ) ) : null;
  105. if( $prefix !== null ){
  106. $this->displayPrefix = $prefix->getDBkey();
  107. $this->prefix = '/^' . preg_quote( $this->displayPrefix ) . '/i';
  108. } else {
  109. $this->displayPrefix = false;
  110. $this->prefix = false;
  111. }
  112. // The suffix that may be needed for message names if we're in a
  113. // different language (eg [[MediaWiki:Foo/fr]]: $suffix = '/fr'
  114. if( $this->foreign ) {
  115. $this->suffix = '/' . $this->langcode;
  116. } else {
  117. $this->suffix = '';
  118. }
  119. }
  120. function buildForm() {
  121. global $wgScript;
  122. $languages = Language::getLanguageNames( false );
  123. ksort( $languages );
  124. $out = Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript, 'id' => 'mw-allmessages-form' ) ) .
  125. Xml::fieldset( wfMsg( 'allmessages-filter-legend' ) ) .
  126. Html::hidden( 'title', $this->getTitle()->getPrefixedText() ) .
  127. Xml::openElement( 'table', array( 'class' => 'mw-allmessages-table' ) ) . "\n" .
  128. '<tr>
  129. <td class="mw-label">' .
  130. Xml::label( wfMsg( 'allmessages-prefix' ), 'mw-allmessages-form-prefix' ) .
  131. "</td>\n
  132. <td class=\"mw-input\">" .
  133. Xml::input( 'prefix', 20, str_replace( '_', ' ', $this->displayPrefix ), array( 'id' => 'mw-allmessages-form-prefix' ) ) .
  134. "</td>\n
  135. </tr>
  136. <tr>\n
  137. <td class='mw-label'>" .
  138. wfMsg( 'allmessages-filter' ) .
  139. "</td>\n
  140. <td class='mw-input'>" .
  141. Xml::radioLabel( wfMsg( 'allmessages-filter-unmodified' ),
  142. 'filter',
  143. 'unmodified',
  144. 'mw-allmessages-form-filter-unmodified',
  145. ( $this->filter == 'unmodified' )
  146. ) .
  147. Xml::radioLabel( wfMsg( 'allmessages-filter-all' ),
  148. 'filter',
  149. 'all',
  150. 'mw-allmessages-form-filter-all',
  151. ( $this->filter == 'all' )
  152. ) .
  153. Xml::radioLabel( wfMsg( 'allmessages-filter-modified' ),
  154. 'filter',
  155. 'modified',
  156. 'mw-allmessages-form-filter-modified',
  157. ( $this->filter == 'modified' )
  158. ) .
  159. "</td>\n
  160. </tr>
  161. <tr>\n
  162. <td class=\"mw-label\">" .
  163. Xml::label( wfMsg( 'allmessages-language' ), 'mw-allmessages-form-lang' ) .
  164. "</td>\n
  165. <td class=\"mw-input\">" .
  166. Xml::openElement( 'select', array( 'id' => 'mw-allmessages-form-lang', 'name' => 'lang' ) );
  167. foreach( $languages as $lang => $name ) {
  168. $selected = $lang == $this->langcode;
  169. $out .= Xml::option( $lang . ' - ' . $name, $lang, $selected ) . "\n";
  170. }
  171. $out .= Xml::closeElement( 'select' ) .
  172. "</td>\n
  173. </tr>" .
  174. '<tr>
  175. <td class="mw-label">' .
  176. Xml::label( wfMsg( 'table_pager_limit_label'), 'mw-table_pager_limit_label' ) .
  177. '</td>
  178. <td class="mw-input">' .
  179. $this->getLimitSelect() .
  180. '</td>
  181. <tr>
  182. <td></td>
  183. <td>' .
  184. Xml::submitButton( wfMsg( 'allmessages-filter-submit' ) ) .
  185. "</td>\n
  186. </tr>" .
  187. Xml::closeElement( 'table' ) .
  188. $this->getHiddenFields( array( 'title', 'prefix', 'filter', 'lang', 'limit' ) ) .
  189. Xml::closeElement( 'fieldset' ) .
  190. Xml::closeElement( 'form' );
  191. return $out;
  192. }
  193. function getAllMessages( $descending ) {
  194. wfProfileIn( __METHOD__ );
  195. $messageNames = Language::getLocalisationCache()->getSubitemList( 'en', 'messages' );
  196. if( $descending ){
  197. rsort( $messageNames );
  198. } else {
  199. asort( $messageNames );
  200. }
  201. // Normalise message names so they look like page titles
  202. $messageNames = array_map( array( $this->lang, 'ucfirst' ), $messageNames );
  203. wfProfileOut( __METHOD__ );
  204. return $messageNames;
  205. }
  206. /**
  207. * Determine which of the MediaWiki and MediaWiki_talk namespace pages exist.
  208. * Returns array( 'pages' => ..., 'talks' => ... ), where the subarrays have
  209. * an entry for each existing page, with the key being the message name and
  210. * value arbitrary.
  211. *
  212. * @param array $messageNames
  213. * @param string $langcode What language code
  214. * @param bool $foreign Whether the $langcode is not the content language
  215. */
  216. public static function getCustomisedStatuses( $messageNames, $langcode = 'en', $foreign = false ) {
  217. wfProfileIn( __METHOD__ . '-db' );
  218. $dbr = wfGetDB( DB_SLAVE );
  219. $res = $dbr->select( 'page',
  220. array( 'page_namespace', 'page_title' ),
  221. array( 'page_namespace' => array( NS_MEDIAWIKI, NS_MEDIAWIKI_TALK ) ),
  222. __METHOD__,
  223. array( 'USE INDEX' => 'name_title' )
  224. );
  225. $xNames = array_flip( $messageNames );
  226. $pageFlags = $talkFlags = array();
  227. foreach ( $res as $s ) {
  228. if( $s->page_namespace == NS_MEDIAWIKI ) {
  229. if( $foreign ) {
  230. $title = explode( '/', $s->page_title );
  231. if( count( $title ) === 2 && $langcode == $title[1]
  232. && isset( $xNames[$title[0]] ) ) {
  233. $pageFlags["{$title[0]}"] = true;
  234. }
  235. } elseif( isset( $xNames[$s->page_title] ) ) {
  236. $pageFlags[$s->page_title] = true;
  237. }
  238. } elseif( $s->page_namespace == NS_MEDIAWIKI_TALK ){
  239. $talkFlags[$s->page_title] = true;
  240. }
  241. }
  242. wfProfileOut( __METHOD__ . '-db' );
  243. return array( 'pages' => $pageFlags, 'talks' => $talkFlags );
  244. }
  245. /**
  246. * This function normally does a database query to get the results; we need
  247. * to make a pretend result using a FakeResultWrapper.
  248. */
  249. function reallyDoQuery( $offset, $limit, $descending ) {
  250. $result = new FakeResultWrapper( array() );
  251. $messageNames = $this->getAllMessages( $descending );
  252. $statuses = self::getCustomisedStatuses( $messageNames, $this->langcode, $this->foreign );
  253. $count = 0;
  254. foreach( $messageNames as $key ) {
  255. $customised = isset( $statuses['pages'][$key] );
  256. if( $customised !== $this->custom &&
  257. ( $descending && ( $key < $offset || !$offset ) || !$descending && $key > $offset ) &&
  258. ( ( $this->prefix && preg_match( $this->prefix, $key ) ) || $this->prefix === false )
  259. ) {
  260. $actual = wfMessage( $key )->inLanguage( $this->langcode )->plain();
  261. $default = wfMessage( $key )->inLanguage( $this->langcode )->useDatabase( false )->plain();
  262. $result->result[] = array(
  263. 'am_title' => $key,
  264. 'am_actual' => $actual,
  265. 'am_default' => $default,
  266. 'am_customised' => $customised,
  267. 'am_talk_exists' => isset( $statuses['talks'][$key] )
  268. );
  269. $count++;
  270. }
  271. if( $count == $limit ) {
  272. break;
  273. }
  274. }
  275. return $result;
  276. }
  277. function getStartBody() {
  278. return Xml::openElement( 'table', array( 'class' => 'TablePager', 'id' => 'mw-allmessagestable' ) ) . "\n" .
  279. "<thead><tr>
  280. <th rowspan=\"2\">" .
  281. wfMsg( 'allmessagesname' ) . "
  282. </th>
  283. <th>" .
  284. wfMsg( 'allmessagesdefault' ) .
  285. "</th>
  286. </tr>\n
  287. <tr>
  288. <th>" .
  289. wfMsg( 'allmessagescurrent' ) .
  290. "</th>
  291. </tr></thead><tbody>\n";
  292. }
  293. function formatValue( $field, $value ){
  294. global $wgLang;
  295. switch( $field ){
  296. case 'am_title' :
  297. $title = Title::makeTitle( NS_MEDIAWIKI, $value . $this->suffix );
  298. $talk = Title::makeTitle( NS_MEDIAWIKI_TALK, $value . $this->suffix );
  299. if( $this->mCurrentRow->am_customised ){
  300. $title = Linker::linkKnown( $title, $wgLang->lcfirst( $value ) );
  301. } else {
  302. $title = Linker::link(
  303. $title,
  304. $wgLang->lcfirst( $value ),
  305. array(),
  306. array(),
  307. array( 'broken' )
  308. );
  309. }
  310. if ( $this->mCurrentRow->am_talk_exists ) {
  311. $talk = Linker::linkKnown( $talk , $this->talk );
  312. } else {
  313. $talk = Linker::link(
  314. $talk,
  315. $this->talk,
  316. array(),
  317. array(),
  318. array( 'broken' )
  319. );
  320. }
  321. return $title . ' (' . $talk . ')';
  322. case 'am_default' :
  323. case 'am_actual' :
  324. return Sanitizer::escapeHtmlAllowEntities( $value, ENT_QUOTES );
  325. }
  326. return '';
  327. }
  328. function formatRow( $row ){
  329. // Do all the normal stuff
  330. $s = parent::formatRow( $row );
  331. // But if there's a customised message, add that too.
  332. if( $row->am_customised ){
  333. $s .= Xml::openElement( 'tr', $this->getRowAttrs( $row, true ) );
  334. $formatted = strval( $this->formatValue( 'am_actual', $row->am_actual ) );
  335. if ( $formatted == '' ) {
  336. $formatted = '&#160;';
  337. }
  338. $s .= Xml::tags( 'td', $this->getCellAttrs( 'am_actual', $row->am_actual ), $formatted )
  339. . "</tr>\n";
  340. }
  341. return $s;
  342. }
  343. function getRowAttrs( $row, $isSecond = false ){
  344. $arr = array();
  345. global $wgLang;
  346. if( $row->am_customised ){
  347. $arr['class'] = 'allmessages-customised';
  348. }
  349. if( !$isSecond ){
  350. $arr['id'] = Sanitizer::escapeId( 'msg_' . $wgLang->lcfirst( $row->am_title ) );
  351. }
  352. return $arr;
  353. }
  354. function getCellAttrs( $field, $value ){
  355. if( $field != 'am_title' ) {
  356. return array( 'lang' => $this->langcode, 'dir' => $this->lang->getDir() );
  357. } elseif( $this->mCurrentRow->am_customised && $field == 'am_title' ) {
  358. return array( 'rowspan' => '2', 'class' => $field );
  359. } else {
  360. return array( 'class' => $field );
  361. }
  362. }
  363. // This is not actually used, as getStartBody is overridden above
  364. function getFieldNames() {
  365. return array(
  366. 'am_title' => wfMsg( 'allmessagesname' ),
  367. 'am_default' => wfMsg( 'allmessagesdefault' )
  368. );
  369. }
  370. function getTitle() {
  371. return SpecialPage::getTitleFor( 'Allmessages', false );
  372. }
  373. function isFieldSortable( $x ){
  374. return false;
  375. }
  376. function getDefaultSort(){
  377. return '';
  378. }
  379. function getQueryInfo(){
  380. return '';
  381. }
  382. }