[ Index ]

PHP Cross Reference of MediaWiki-1.24.0

title

Body

[close]

/includes/specials/ -> SpecialAllMessages.php (source)

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


Generated: Fri Nov 28 14:03:12 2014 Cross-referenced by PHPXref 0.7.1