MediaWiki  REL1_24
SpecialAllMessages.php
Go to the documentation of this file.
00001 <?php
00030 class SpecialAllMessages extends SpecialPage {
00034     protected $table;
00035 
00039     public function __construct() {
00040         parent::__construct( 'Allmessages' );
00041     }
00042 
00048     public function execute( $par ) {
00049         $request = $this->getRequest();
00050         $out = $this->getOutput();
00051 
00052         $this->setHeaders();
00053 
00054         if ( !$this->getConfig()->get( 'UseDatabaseMessages' ) ) {
00055             $out->addWikiMsg( 'allmessagesnotsupportedDB' );
00056 
00057             return;
00058         }
00059 
00060         $this->outputHeader( 'allmessagestext' );
00061         $out->addModuleStyles( 'mediawiki.special' );
00062 
00063         $this->table = new AllmessagesTablePager(
00064             $this,
00065             array(),
00066             wfGetLangObj( $request->getVal( 'lang', $par ) )
00067         );
00068 
00069         $this->langcode = $this->table->lang->getCode();
00070 
00071         $out->addHTML( $this->table->buildForm() );
00072         $out->addParserOutputContent( $this->table->getFullOutput() );
00073     }
00074 
00075     protected function getGroupName() {
00076         return 'wiki';
00077     }
00078 }
00079 
00084 class AllMessagesTablePager extends TablePager {
00085     protected $filter, $prefix, $langcode, $displayPrefix;
00086 
00087     public $mLimitsShown;
00088 
00092     public $lang;
00093 
00097     public $custom;
00098 
00099     function __construct( $page, $conds, $langObj = null ) {
00100         parent::__construct( $page->getContext() );
00101         $this->mIndexField = 'am_title';
00102         $this->mPage = $page;
00103         $this->mConds = $conds;
00104         // FIXME: Why does this need to be set to DIR_DESCENDING to produce ascending ordering?
00105         $this->mDefaultDirection = IndexPager::DIR_DESCENDING;
00106         $this->mLimitsShown = array( 20, 50, 100, 250, 500, 5000 );
00107 
00108         global $wgContLang;
00109 
00110         $this->talk = $this->msg( 'talkpagelinktext' )->escaped();
00111 
00112         $this->lang = ( $langObj ? $langObj : $wgContLang );
00113         $this->langcode = $this->lang->getCode();
00114         $this->foreign = $this->langcode !== $wgContLang->getCode();
00115 
00116         $request = $this->getRequest();
00117 
00118         $this->filter = $request->getVal( 'filter', 'all' );
00119         if ( $this->filter === 'all' ) {
00120             $this->custom = null; // So won't match in either case
00121         } else {
00122             $this->custom = ( $this->filter === 'unmodified' );
00123         }
00124 
00125         $prefix = $this->getLanguage()->ucfirst( $request->getVal( 'prefix', '' ) );
00126         $prefix = $prefix !== '' ?
00127             Title::makeTitleSafe( NS_MEDIAWIKI, $request->getVal( 'prefix', null ) ) :
00128             null;
00129 
00130         if ( $prefix !== null ) {
00131             $this->displayPrefix = $prefix->getDBkey();
00132             $this->prefix = '/^' . preg_quote( $this->displayPrefix ) . '/i';
00133         } else {
00134             $this->displayPrefix = false;
00135             $this->prefix = false;
00136         }
00137 
00138         // The suffix that may be needed for message names if we're in a
00139         // different language (eg [[MediaWiki:Foo/fr]]: $suffix = '/fr'
00140         if ( $this->foreign ) {
00141             $this->suffix = '/' . $this->langcode;
00142         } else {
00143             $this->suffix = '';
00144         }
00145     }
00146 
00147     function buildForm() {
00148         $attrs = array( 'id' => 'mw-allmessages-form-lang', 'name' => 'lang' );
00149         $msg = wfMessage( 'allmessages-language' );
00150         $langSelect = Xml::languageSelector( $this->langcode, false, null, $attrs, $msg );
00151 
00152         $out = Xml::openElement( 'form', array(
00153                 'method' => 'get',
00154                 'action' => $this->getConfig()->get( 'Script' ),
00155                 'id' => 'mw-allmessages-form'
00156             ) ) .
00157             Xml::fieldset( $this->msg( 'allmessages-filter-legend' )->text() ) .
00158             Html::hidden( 'title', $this->getTitle()->getPrefixedText() ) .
00159             Xml::openElement( 'table', array( 'class' => 'mw-allmessages-table' ) ) . "\n" .
00160             '<tr>
00161                 <td class="mw-label">' .
00162             Xml::label( $this->msg( 'allmessages-prefix' )->text(), 'mw-allmessages-form-prefix' ) .
00163             "</td>\n
00164             <td class=\"mw-input\">" .
00165             Xml::input(
00166                 'prefix',
00167                 20,
00168                 str_replace( '_', ' ', $this->displayPrefix ),
00169                 array( 'id' => 'mw-allmessages-form-prefix' )
00170             ) .
00171             "</td>\n
00172             </tr>
00173             <tr>\n
00174             <td class='mw-label'>" .
00175             $this->msg( 'allmessages-filter' )->escaped() .
00176             "</td>\n
00177                 <td class='mw-input'>" .
00178             Xml::radioLabel( $this->msg( 'allmessages-filter-unmodified' )->text(),
00179                 'filter',
00180                 'unmodified',
00181                 'mw-allmessages-form-filter-unmodified',
00182                 ( $this->filter === 'unmodified' )
00183             ) .
00184             Xml::radioLabel( $this->msg( 'allmessages-filter-all' )->text(),
00185                 'filter',
00186                 'all',
00187                 'mw-allmessages-form-filter-all',
00188                 ( $this->filter === 'all' )
00189             ) .
00190             Xml::radioLabel( $this->msg( 'allmessages-filter-modified' )->text(),
00191                 'filter',
00192                 'modified',
00193                 'mw-allmessages-form-filter-modified',
00194                 ( $this->filter === 'modified' )
00195             ) .
00196             "</td>\n
00197             </tr>
00198             <tr>\n
00199                 <td class=\"mw-label\">" . $langSelect[0] . "</td>\n
00200                 <td class=\"mw-input\">" . $langSelect[1] . "</td>\n
00201             </tr>" .
00202 
00203             '<tr>
00204                 <td class="mw-label">' .
00205             Xml::label( $this->msg( 'table_pager_limit_label' )->text(), 'mw-table_pager_limit_label' ) .
00206             '</td>
00207             <td class="mw-input">' .
00208             $this->getLimitSelect() .
00209             '</td>
00210             <tr>
00211                 <td></td>
00212                 <td>' .
00213             Xml::submitButton( $this->msg( 'allmessages-filter-submit' )->text() ) .
00214             "</td>\n
00215             </tr>" .
00216 
00217             Xml::closeElement( 'table' ) .
00218             $this->getHiddenFields( array( 'title', 'prefix', 'filter', 'lang', 'limit' ) ) .
00219             Xml::closeElement( 'fieldset' ) .
00220             Xml::closeElement( 'form' );
00221 
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 
00239         return $messageNames;
00240     }
00241 
00253     public static function getCustomisedStatuses( $messageNames, $langcode = 'en', $foreign = false ) {
00254         // FIXME: This function should be moved to Language:: or something.
00255         wfProfileIn( __METHOD__ . '-db' );
00256 
00257         $dbr = wfGetDB( DB_SLAVE );
00258         $res = $dbr->select( 'page',
00259             array( 'page_namespace', 'page_title' ),
00260             array( 'page_namespace' => array( NS_MEDIAWIKI, NS_MEDIAWIKI_TALK ) ),
00261             __METHOD__,
00262             array( 'USE INDEX' => 'name_title' )
00263         );
00264         $xNames = array_flip( $messageNames );
00265 
00266         $pageFlags = $talkFlags = array();
00267 
00268         foreach ( $res as $s ) {
00269             $exists = false;
00270 
00271             if ( $foreign ) {
00272                 $titleParts = explode( '/', $s->page_title );
00273                 if ( count( $titleParts ) === 2 &&
00274                     $langcode === $titleParts[1] &&
00275                     isset( $xNames[$titleParts[0]] )
00276                 ) {
00277                     $exists = $titleParts[0];
00278                 }
00279             } elseif ( isset( $xNames[$s->page_title] ) ) {
00280                 $exists = $s->page_title;
00281             }
00282 
00283             $title = Title::newFromRow( $s );
00284             if ( $exists && $title->inNamespace( NS_MEDIAWIKI ) ) {
00285                 $pageFlags[$exists] = true;
00286             } elseif ( $exists && $title->inNamespace( NS_MEDIAWIKI_TALK ) ) {
00287                 $talkFlags[$exists] = true;
00288             }
00289         }
00290 
00291         wfProfileOut( __METHOD__ . '-db' );
00292 
00293         return array( 'pages' => $pageFlags, 'talks' => $talkFlags );
00294     }
00295 
00304     function reallyDoQuery( $offset, $limit, $descending ) {
00305         $result = new FakeResultWrapper( array() );
00306 
00307         $messageNames = $this->getAllMessages( $descending );
00308         $statuses = self::getCustomisedStatuses( $messageNames, $this->langcode, $this->foreign );
00309 
00310         $count = 0;
00311         foreach ( $messageNames as $key ) {
00312             $customised = isset( $statuses['pages'][$key] );
00313             if ( $customised !== $this->custom &&
00314                 ( $descending && ( $key < $offset || !$offset ) || !$descending && $key > $offset ) &&
00315                 ( ( $this->prefix && preg_match( $this->prefix, $key ) ) || $this->prefix === false )
00316             ) {
00317                 $actual = wfMessage( $key )->inLanguage( $this->langcode )->plain();
00318                 $default = wfMessage( $key )->inLanguage( $this->langcode )->useDatabase( false )->plain();
00319                 $result->result[] = array(
00320                     'am_title' => $key,
00321                     'am_actual' => $actual,
00322                     'am_default' => $default,
00323                     'am_customised' => $customised,
00324                     'am_talk_exists' => isset( $statuses['talks'][$key] )
00325                 );
00326                 $count++;
00327             }
00328 
00329             if ( $count === $limit ) {
00330                 break;
00331             }
00332         }
00333 
00334         return $result;
00335     }
00336 
00337     function getStartBody() {
00338         $tableClass = $this->getTableClass();
00339         return Xml::openElement( 'table', array(
00340                 'class' => "mw-datatable $tableClass",
00341                 'id' => 'mw-allmessagestable'
00342             ) ) .
00343             "\n" .
00344             "<thead><tr>
00345                 <th rowspan=\"2\">" .
00346             $this->msg( 'allmessagesname' )->escaped() . "
00347                 </th>
00348                 <th>" .
00349             $this->msg( 'allmessagesdefault' )->escaped() .
00350             "</th>
00351             </tr>\n
00352             <tr>
00353                 <th>" .
00354             $this->msg( 'allmessagescurrent' )->escaped() .
00355             "</th>
00356             </tr></thead><tbody>\n";
00357     }
00358 
00359     function formatValue( $field, $value ) {
00360         switch ( $field ) {
00361             case 'am_title' :
00362                 $title = Title::makeTitle( NS_MEDIAWIKI, $value . $this->suffix );
00363                 $talk = Title::makeTitle( NS_MEDIAWIKI_TALK, $value . $this->suffix );
00364                 $translation = Linker::makeExternalLink(
00365                     'https://translatewiki.net/w/i.php?' . wfArrayToCgi( array(
00366                         'title' => 'Special:SearchTranslations',
00367                         'group' => 'mediawiki',
00368                         'grouppath' => 'mediawiki',
00369                         'query' => 'language:' . $this->getLanguage()->getCode() . '^25 ' .
00370                             'messageid:"MediaWiki:' . $value . '"^10 "' .
00371                             $this->msg( $value )->inLanguage( 'en' )->plain() . '"'
00372                     ) ),
00373                     $this->msg( 'allmessages-filter-translate' )->text()
00374                 );
00375 
00376                 if ( $this->mCurrentRow->am_customised ) {
00377                     $title = Linker::linkKnown( $title, $this->getLanguage()->lcfirst( $value ) );
00378                 } else {
00379                     $title = Linker::link(
00380                         $title,
00381                         $this->getLanguage()->lcfirst( $value ),
00382                         array(),
00383                         array(),
00384                         array( 'broken' )
00385                     );
00386                 }
00387                 if ( $this->mCurrentRow->am_talk_exists ) {
00388                     $talk = Linker::linkKnown( $talk, $this->talk );
00389                 } else {
00390                     $talk = Linker::link(
00391                         $talk,
00392                         $this->talk,
00393                         array(),
00394                         array(),
00395                         array( 'broken' )
00396                     );
00397                 }
00398 
00399                 return $title . ' '
00400                 . $this->msg( 'parentheses' )->rawParams( $talk )->escaped()
00401                 . ' '
00402                 . $this->msg( 'parentheses' )->rawParams( $translation )->escaped();
00403 
00404             case 'am_default' :
00405             case 'am_actual' :
00406                 return Sanitizer::escapeHtmlAllowEntities( $value, ENT_QUOTES );
00407         }
00408 
00409         return '';
00410     }
00411 
00412     function formatRow( $row ) {
00413         // Do all the normal stuff
00414         $s = parent::formatRow( $row );
00415 
00416         // But if there's a customised message, add that too.
00417         if ( $row->am_customised ) {
00418             $s .= Xml::openElement( 'tr', $this->getRowAttrs( $row, true ) );
00419             $formatted = strval( $this->formatValue( 'am_actual', $row->am_actual ) );
00420 
00421             if ( $formatted === '' ) {
00422                 $formatted = '&#160;';
00423             }
00424 
00425             $s .= Xml::tags( 'td', $this->getCellAttrs( 'am_actual', $row->am_actual ), $formatted )
00426                 . "</tr>\n";
00427         }
00428 
00429         return $s;
00430     }
00431 
00432     function getRowAttrs( $row, $isSecond = false ) {
00433         $arr = array();
00434 
00435         if ( $row->am_customised ) {
00436             $arr['class'] = 'allmessages-customised';
00437         }
00438 
00439         if ( !$isSecond ) {
00440             $arr['id'] = Sanitizer::escapeId( 'msg_' . $this->getLanguage()->lcfirst( $row->am_title ) );
00441         }
00442 
00443         return $arr;
00444     }
00445 
00446     function getCellAttrs( $field, $value ) {
00447         if ( $this->mCurrentRow->am_customised && $field === 'am_title' ) {
00448             return array( 'rowspan' => '2', 'class' => $field );
00449         } elseif ( $field === 'am_title' ) {
00450             return array( 'class' => $field );
00451         } else {
00452             return array( 'lang' => $this->langcode, 'dir' => $this->lang->getDir(), 'class' => $field );
00453         }
00454     }
00455 
00456     // This is not actually used, as getStartBody is overridden above
00457     function getFieldNames() {
00458         return array(
00459             'am_title' => $this->msg( 'allmessagesname' )->text(),
00460             'am_default' => $this->msg( 'allmessagesdefault' )->text()
00461         );
00462     }
00463 
00464     function getTitle() {
00465         return SpecialPage::getTitleFor( 'Allmessages', false );
00466     }
00467 
00468     function isFieldSortable( $x ) {
00469         return false;
00470     }
00471 
00472     function getDefaultSort() {
00473         return '';
00474     }
00475 
00476     function getQueryInfo() {
00477         return '';
00478     }
00479 }