MediaWiki  REL1_19
SpecialAllmessages.php
Go to the documentation of this file.
00001 <?php
00030 class SpecialAllmessages extends SpecialPage {
00031 
00035         protected $table;
00036 
00040         public function __construct() {
00041                 parent::__construct( 'Allmessages' );
00042         }
00043 
00049         public function execute( $par ) {
00050                 $request = $this->getRequest();
00051                 $out = $this->getOutput();
00052 
00053                 $this->setHeaders();
00054 
00055                 global $wgUseDatabaseMessages;
00056                 if( !$wgUseDatabaseMessages ) {
00057                         $out->addWikiMsg( 'allmessagesnotsupportedDB' );
00058                         return;
00059                 } else {
00060                         $this->outputHeader( 'allmessagestext' );
00061                 }
00062 
00063                 $out->addModuleStyles( 'mediawiki.special' );
00064 
00065                 $this->table = new AllmessagesTablePager(
00066                         $this,
00067                         array(),
00068                         wfGetLangObj( $request->getVal( 'lang', $par ) )
00069                 );
00070 
00071                 $this->langcode = $this->table->lang->getCode();
00072 
00073                 $out->addHTML( $this->table->buildForm() .
00074                         $this->table->getNavigationBar() .
00075                         $this->table->getBody() .
00076                         $this->table->getNavigationBar() );
00077 
00078         }
00079 
00080 }
00081 
00086 class AllmessagesTablePager extends TablePager {
00087 
00088         protected $filter, $prefix, $langcode, $displayPrefix;
00089 
00090         public $mLimitsShown;
00091 
00095         public $lang;
00096 
00100         public $custom;
00101 
00102         function __construct( $page, $conds, $langObj = null ) {
00103                 parent::__construct( $page->getContext() );
00104                 $this->mIndexField = 'am_title';
00105                 $this->mPage = $page;
00106                 $this->mConds = $conds;
00107                 $this->mDefaultDirection = true; // always sort ascending
00108                 $this->mLimitsShown = array( 20, 50, 100, 250, 500, 5000 );
00109 
00110                 global $wgContLang;
00111 
00112                 $this->talk = $this->msg( 'talkpagelinktext' )->escaped();
00113 
00114                 $this->lang = ( $langObj ? $langObj : $wgContLang );
00115                 $this->langcode = $this->lang->getCode();
00116                 $this->foreign  = $this->langcode != $wgContLang->getCode();
00117 
00118                 $request = $this->getRequest();
00119 
00120                 $this->filter = $request->getVal( 'filter', 'all' );
00121                 if( $this->filter === 'all' ){
00122                         $this->custom = null; // So won't match in either case
00123                 } else {
00124                         $this->custom = ($this->filter == 'unmodified');
00125                 }
00126 
00127                 $prefix = $this->getLanguage()->ucfirst( $request->getVal( 'prefix', '' ) );
00128                 $prefix = $prefix != '' ? Title::makeTitleSafe( NS_MEDIAWIKI, $request->getVal( 'prefix', null ) ) : null;
00129                 if( $prefix !== null ){
00130                         $this->displayPrefix = $prefix->getDBkey();
00131                         $this->prefix = '/^' . preg_quote( $this->displayPrefix ) . '/i';
00132                 } else {
00133                         $this->displayPrefix = false;
00134                         $this->prefix = false;
00135                 }
00136 
00137                 // The suffix that may be needed for message names if we're in a
00138                 // different language (eg [[MediaWiki:Foo/fr]]: $suffix = '/fr'
00139                 if( $this->foreign ) {
00140                         $this->suffix = '/' . $this->langcode;
00141                 } else {
00142                         $this->suffix = '';
00143                 }
00144         }
00145 
00146         function buildForm() {
00147                 global $wgScript;
00148 
00149                 $languages = Language::getLanguageNames( false );
00150                 ksort( $languages );
00151 
00152                 $out  = Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript, 'id' => 'mw-allmessages-form' ) ) .
00153                         Xml::fieldset( $this->msg( 'allmessages-filter-legend' )->text() ) .
00154                         Html::hidden( 'title', $this->getTitle()->getPrefixedText() ) .
00155                         Xml::openElement( 'table', array( 'class' => 'mw-allmessages-table' ) ) . "\n" .
00156                         '<tr>
00157                                 <td class="mw-label">' .
00158                                         Xml::label( $this->msg( 'allmessages-prefix' )->text(), 'mw-allmessages-form-prefix' ) .
00159                                 "</td>\n
00160                                 <td class=\"mw-input\">" .
00161                                         Xml::input( 'prefix', 20, str_replace( '_', ' ', $this->displayPrefix ), array( 'id' => 'mw-allmessages-form-prefix' ) ) .
00162                                 "</td>\n
00163                         </tr>
00164                         <tr>\n
00165                                 <td class='mw-label'>" .
00166                                         $this->msg( 'allmessages-filter' )->escaped() .
00167                                 "</td>\n
00168                                 <td class='mw-input'>" .
00169                                         Xml::radioLabel( $this->msg( 'allmessages-filter-unmodified' )->text(),
00170                                                 'filter',
00171                                                 'unmodified',
00172                                                 'mw-allmessages-form-filter-unmodified',
00173                                                 ( $this->filter == 'unmodified' )
00174                                         ) .
00175                                         Xml::radioLabel( $this->msg( 'allmessages-filter-all' )->text(),
00176                                                 'filter',
00177                                                 'all',
00178                                                 'mw-allmessages-form-filter-all',
00179                                                 ( $this->filter == 'all' )
00180                                         ) .
00181                                         Xml::radioLabel( $this->msg( 'allmessages-filter-modified' )->text(),
00182                                                 'filter',
00183                                                 'modified',
00184                                                 'mw-allmessages-form-filter-modified',
00185                                         ( $this->filter == 'modified' )
00186                                 ) .
00187                                 "</td>\n
00188                         </tr>
00189                         <tr>\n
00190                                 <td class=\"mw-label\">" .
00191                                         Xml::label( $this->msg( 'allmessages-language' )->text(), 'mw-allmessages-form-lang' ) .
00192                                 "</td>\n
00193                                 <td class=\"mw-input\">" .
00194                                         Xml::openElement( 'select', array( 'id' => 'mw-allmessages-form-lang', 'name' => 'lang' ) );
00195 
00196                 foreach( $languages as $lang => $name ) {
00197                         $selected = $lang == $this->langcode;
00198                         $out .= Xml::option( $lang . ' - ' . $name, $lang, $selected ) . "\n";
00199                 }
00200                 $out .= Xml::closeElement( 'select' ) .
00201                                 "</td>\n
00202                         </tr>" .
00203 
00204                         '<tr>
00205                                 <td class="mw-label">' .
00206                                         Xml::label( $this->msg( 'table_pager_limit_label' )->text(), 'mw-table_pager_limit_label' ) .
00207                                 '</td>
00208                                 <td class="mw-input">' .
00209                                         $this->getLimitSelect() .
00210                                 '</td>
00211                         <tr>
00212                                 <td></td>
00213                                 <td>' .
00214                                         Xml::submitButton( $this->msg( 'allmessages-filter-submit' )->text() ) .
00215                                 "</td>\n
00216                         </tr>" .
00217 
00218                         Xml::closeElement( 'table' ) .
00219                         $this->getHiddenFields( array( 'title', 'prefix', 'filter', 'lang', 'limit' ) ) .
00220                         Xml::closeElement( 'fieldset' ) .
00221                         Xml::closeElement( 'form' );
00222                 return $out;
00223         }
00224 
00225         function getAllMessages( $descending ) {
00226                 wfProfileIn( __METHOD__ );
00227                 $messageNames = Language::getLocalisationCache()->getSubitemList( 'en', 'messages' );
00228                 if( $descending ){
00229                         rsort( $messageNames );
00230                 } else {
00231                         asort( $messageNames );
00232                 }
00233 
00234                 // Normalise message names so they look like page titles
00235                 $messageNames = array_map( array( $this->lang, 'ucfirst' ), $messageNames );
00236 
00237                 wfProfileOut( __METHOD__ );
00238                 return $messageNames;
00239         }
00240 
00252         public static function getCustomisedStatuses( $messageNames, $langcode = 'en', $foreign = false ) {
00253                 // FIXME: This function should be moved to Language:: or something.
00254                 wfProfileIn( __METHOD__ . '-db' );
00255 
00256                 $dbr = wfGetDB( DB_SLAVE );
00257                 $res = $dbr->select( 'page',
00258                         array( 'page_namespace', 'page_title' ),
00259                         array( 'page_namespace' => array( NS_MEDIAWIKI, NS_MEDIAWIKI_TALK ) ),
00260                         __METHOD__,
00261                         array( 'USE INDEX' => 'name_title' )
00262                 );
00263                 $xNames = array_flip( $messageNames );
00264 
00265                 $pageFlags = $talkFlags = array();
00266 
00267                 foreach ( $res as $s ) {
00268                         $exists = false;
00269                         if( $foreign ) {
00270                                 $title = explode( '/', $s->page_title );
00271                                 if( count( $title ) === 2 && $langcode == $title[1]
00272                                         && isset( $xNames[$title[0]] ) ) {
00273                                         $exists = $title[0];
00274                                 }
00275                         } elseif( isset( $xNames[$s->page_title] ) ) {
00276                                 $exists = $s->page_title;
00277                         }
00278                         if( $exists && $s->page_namespace == NS_MEDIAWIKI ) {
00279                                 $pageFlags[$exists] = true;
00280                         } elseif( $exists && $s->page_namespace == NS_MEDIAWIKI_TALK ) {
00281                                 $talkFlags[$exists] = true;
00282                         }
00283                 }
00284 
00285                 wfProfileOut( __METHOD__ . '-db' );
00286 
00287                 return array( 'pages' => $pageFlags, 'talks' => $talkFlags );
00288         }
00289 
00294         function reallyDoQuery( $offset, $limit, $descending ) {
00295                 $result = new FakeResultWrapper( array() );
00296 
00297                 $messageNames = $this->getAllMessages( $descending );
00298                 $statuses = self::getCustomisedStatuses( $messageNames, $this->langcode, $this->foreign );
00299 
00300                 $count = 0;
00301                 foreach( $messageNames as $key ) {
00302                         $customised = isset( $statuses['pages'][$key] );
00303                         if( $customised !== $this->custom &&
00304                                 ( $descending && ( $key < $offset || !$offset ) || !$descending && $key > $offset ) &&
00305                                 ( ( $this->prefix && preg_match( $this->prefix, $key ) ) || $this->prefix === false )
00306                         ) {
00307                                 $actual = wfMessage( $key )->inLanguage( $this->langcode )->plain();
00308                                 $default = wfMessage( $key )->inLanguage( $this->langcode )->useDatabase( false )->plain();
00309                                 $result->result[] = array(
00310                                         'am_title'      => $key,
00311                                         'am_actual'     => $actual,
00312                                         'am_default'    => $default,
00313                                         'am_customised' => $customised,
00314                                         'am_talk_exists' => isset( $statuses['talks'][$key] )
00315                                 );
00316                                 $count++;
00317                         }
00318                         if( $count == $limit ) {
00319                                 break;
00320                         }
00321                 }
00322                 return $result;
00323         }
00324 
00325         function getStartBody() {
00326                 return Xml::openElement( 'table', array( 'class' => 'mw-datatable TablePager', 'id' => 'mw-allmessagestable' ) ) . "\n" .
00327                         "<thead><tr>
00328                                 <th rowspan=\"2\">" .
00329                                         $this->msg( 'allmessagesname' )->escaped() . "
00330                                 </th>
00331                                 <th>" .
00332                                         $this->msg( 'allmessagesdefault' )->escaped() .
00333                                 "</th>
00334                         </tr>\n
00335                         <tr>
00336                                 <th>" .
00337                                         $this->msg( 'allmessagescurrent' )->escaped() .
00338                                 "</th>
00339                         </tr></thead><tbody>\n";
00340         }
00341 
00342         function formatValue( $field, $value ){
00343                 switch( $field ){
00344 
00345                         case 'am_title' :
00346 
00347                                 $title = Title::makeTitle( NS_MEDIAWIKI, $value . $this->suffix );
00348                                 $talk  = Title::makeTitle( NS_MEDIAWIKI_TALK, $value . $this->suffix );
00349 
00350                                 if( $this->mCurrentRow->am_customised ){
00351                                         $title = Linker::linkKnown( $title, $this->getLanguage()->lcfirst( $value ) );
00352                                 } else {
00353                                         $title = Linker::link(
00354                                                 $title,
00355                                                 $this->getLanguage()->lcfirst( $value ),
00356                                                 array(),
00357                                                 array(),
00358                                                 array( 'broken' )
00359                                         );
00360                                 }
00361                                 if ( $this->mCurrentRow->am_talk_exists ) {
00362                                         $talk = Linker::linkKnown( $talk , $this->talk );
00363                                 } else {
00364                                         $talk = Linker::link(
00365                                                 $talk,
00366                                                 $this->talk,
00367                                                 array(),
00368                                                 array(),
00369                                                 array( 'broken' )
00370                                         );
00371                                 }
00372                                 return $title . ' (' . $talk . ')';
00373 
00374                         case 'am_default' :
00375                         case 'am_actual' :
00376                                 return Sanitizer::escapeHtmlAllowEntities( $value, ENT_QUOTES );
00377                 }
00378                 return '';
00379         }
00380 
00381         function formatRow( $row ){
00382                 // Do all the normal stuff
00383                 $s = parent::formatRow( $row );
00384 
00385                 // But if there's a customised message, add that too.
00386                 if( $row->am_customised ){
00387                         $s .= Xml::openElement( 'tr', $this->getRowAttrs( $row, true ) );
00388                         $formatted = strval( $this->formatValue( 'am_actual', $row->am_actual ) );
00389                         if ( $formatted == '' ) {
00390                                 $formatted = '&#160;';
00391                         }
00392                         $s .= Xml::tags( 'td', $this->getCellAttrs( 'am_actual', $row->am_actual ), $formatted )
00393                                 . "</tr>\n";
00394                 }
00395                 return $s;
00396         }
00397 
00398         function getRowAttrs( $row, $isSecond = false ){
00399                 $arr = array();
00400                 if( $row->am_customised ){
00401                         $arr['class'] = 'allmessages-customised';
00402                 }
00403                 if( !$isSecond ){
00404                         $arr['id'] = Sanitizer::escapeId( 'msg_' . $this->getLanguage()->lcfirst( $row->am_title ) );
00405                 }
00406                 return $arr;
00407         }
00408 
00409         function getCellAttrs( $field, $value ){
00410                 if( $this->mCurrentRow->am_customised && $field == 'am_title' ){
00411                         return array( 'rowspan' => '2', 'class' => $field );
00412                 } elseif( $field == 'am_title' ) {
00413                         return array( 'class' => $field );
00414                 } else {
00415                         return array( 'lang' => $this->langcode, 'dir' => $this->lang->getDir(), 'class' => $field );
00416                 }
00417         }
00418 
00419         // This is not actually used, as getStartBody is overridden above
00420         function getFieldNames() {
00421                 return array(
00422                         'am_title' => $this->msg( 'allmessagesname' )->text(),
00423                         'am_default' => $this->msg( 'allmessagesdefault' )->text()
00424                 );
00425         }
00426 
00427         function getTitle() {
00428                 return SpecialPage::getTitleFor( 'Allmessages', false );
00429         }
00430 
00431         function isFieldSortable( $x ){
00432                 return false;
00433         }
00434 
00435         function getDefaultSort(){
00436                 return '';
00437         }
00438 
00439         function getQueryInfo(){
00440                 return '';
00441         }
00442 }
00443