r81469 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r81468‎ | r81469 | r81470 >
Date:17:30, 3 February 2011
Author:ialex
Status:resolved (Comments)
Tags:
Comment:
* Use wfMessage() instead of wfMsgGetKey()
* Don't play with $wgLang, pass the Language object to the Message object
* Bug fix: when 'enableparser' is passed without 'args', $1 is no longer replaced by an empty string (the default value on line 92 of the file was null instead of array(), making wfMsgExt() think that this was the value of $1 and thus this was replaced)
Modified paths:
  • /trunk/phase3/includes/api/ApiQueryAllmessages.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/api/ApiQueryAllmessages.php
@@ -43,12 +43,11 @@
4444 public function execute() {
4545 $params = $this->extractRequestParams();
4646
47 - global $wgLang;
48 -
49 - $oldLang = null;
50 - if ( !is_null( $params['lang'] ) ) {
51 - $oldLang = $wgLang; // Keep $wgLang for restore later
52 - $wgLang = Language::factory( $params['lang'] );
 47+ if ( is_null( $params['lang'] ) ) {
 48+ global $wgLang;
 49+ $langObj = $wgLang;
 50+ } else {
 51+ $langObj = Language::factory( $params['lang'] );
5352 }
5453
5554 $prop = array_flip( (array)$params['prop'] );
@@ -90,32 +89,29 @@
9190
9291 if ( !$skip ) {
9392 $a = array( 'name' => $message );
94 - $args = null;
 93+ $args = array();
9594 if ( isset( $params['args'] ) && count( $params['args'] ) != 0 ) {
9695 $args = $params['args'];
9796 }
98 - // Check if the parser is enabled:
99 - if ( $params['enableparser'] ) {
100 - $msg = wfMsgExt( $message, array( 'parsemag' ), $args );
101 - } elseif ( $args ) {
102 - $msgString = wfMsgGetKey( $message, true, false, false );
103 - $msg = wfMsgReplaceArgs( $msgString, $args );
104 - } else {
105 - $msg = wfMsgGetKey( $message, true, false, false );
106 - }
10797
108 - if ( wfEmptyMsg( $message, $msg ) ) {
 98+ $msg = wfMessage( $message, $args )->inLanguage( $langObj );
 99+
 100+ if ( !$msg->exists() ) {
109101 $a['missing'] = '';
110102 } else {
111 - ApiResult::setContent( $a, $msg );
 103+ // Check if the parser is enabled:
 104+ if ( $params['enableparser'] ) {
 105+ $msgString = $msg->text();
 106+ } else {
 107+ $msgString = $msg->plain();
 108+ }
 109+ ApiResult::setContent( $a, $msgString );
112110 if ( isset( $prop['default'] ) ) {
113 - $default = wfMsgGetKey( $message, false, false, false );
114 - if ( $default !== $msg ) {
115 - if ( wfEmptyMsg( $message, $default ) ) {
116 - $a['defaultmissing'] = '';
117 - } else {
118 - $a['default'] = $default;
119 - }
 111+ $default = wfMessage( $message )->inLanguage( $langObj )->useDatabase( false );
 112+ if ( !$default->exists() ) {
 113+ $a['defaultmissing'] = '';
 114+ } elseif ( $default->plain() != $msgString ) {
 115+ $a['default'] = $default->plain();
120116 }
121117 }
122118 }
@@ -127,10 +123,6 @@
128124 }
129125 }
130126 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'message' );
131 -
132 - if ( !is_null( $oldLang ) ) {
133 - $wgLang = $oldLang; // Restore $oldLang
134 - }
135127 }
136128
137129 public function getCacheMode( $params ) {

Follow-up revisions

RevisionCommit summaryAuthorDate
r83673Per Jarry1250 on IRC: fix for r81469: accept StubObject in Message::inLanguag...ialex21:39, 10 March 2011

Comments

#Comment by Jarry1250 (talk | contribs)   18:01, 10 March 2011

I suspect this of breaking api.php?action=query&meta=allmessages, which now dies with the error :

 <error code="internal_api_error_MWException" info="Exception Caught: Message::inLanguage must be passed a String or Language object; object given" xml:space="preserve">
  1. 0 C:\xampp\htdocs\w\includes\api\ApiQueryAllmessages.php(97): Message->inLanguage(Object(StubUserLang))
  2. 1 C:\xampp\htdocs\w\includes\api\ApiQuery.php(268): ApiQueryAllmessages->execute()
  3. 2 C:\xampp\htdocs\w\includes\api\ApiMain.php(666): ApiQuery->execute()
  4. 3 C:\xampp\htdocs\w\includes\api\ApiMain.php(350): ApiMain->executeAction()
  5. 4 C:\xampp\htdocs\w\includes\api\ApiMain.php(334): ApiMain->executeActionWithErrorHandling()
  6. 5 C:\xampp\htdocs\w\api.php(116): ApiMain->execute()
  7. 6 {main}
#Comment by Jarry1250 (talk | contribs)   19:21, 10 March 2011

Replacing $langObj = $wgLang; with $langObj = Language::factory($wgLang->getCode()); fixed it: but I don't know if there's a better way of doing this, or indeed I've just misunderstood completely and there's no issue with this commit at all / it's all just me being noobish :).

#Comment by Nikerabbit (talk | contribs)   20:23, 10 March 2011

Fro the error essage I would guess that it excepts Language object but it gets Stub*Language object instead. It should accept those as well.

Status & tagging log