r36001 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r36000‎ | r36001 | r36002 >
Date:19:19, 7 June 2008
Author:nikerabbit
Status:old
Tags:
Comment:
* Namespace support and few cleanups
Modified paths:
  • /trunk/extensions/Translate/CreateMessageIndex.php (modified) (history)
  • /trunk/extensions/Translate/Message.php (modified) (history)
  • /trunk/extensions/Translate/README (modified) (history)
  • /trunk/extensions/Translate/Translate.php (modified) (history)
  • /trunk/extensions/Translate/TranslateEditAddons.php (modified) (history)
  • /trunk/extensions/Translate/TranslateTasks.php (modified) (history)
  • /trunk/extensions/Translate/TranslateUtils.php (modified) (history)
  • /trunk/extensions/Translate/ffs/Simple.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Translate/TranslateEditAddons.php
@@ -12,7 +12,8 @@
1313 const MSG = 'translate-edit-';
1414
1515 static function addTools( $object ) {
16 - if( $object->mTitle->getNamespace() === NS_MEDIAWIKI ) {
 16+ global $wgTranslateMessageNamespaces;
 17+ if( in_array($object->mTitle->getNamespace(), $wgTranslateMessageNamespaces) ) {
1718 $object->editFormTextTop .= self::editBoxes( $object );
1819 }
1920 return true;
@@ -87,7 +88,7 @@
8889 * @param $key The message key we are interested in.
8990 * @return MessageGroup which the key belongs to, or null.
9091 */
91 - private static function getMessageGroup( $key ) {
 92+ private static function getMessageGroup( $namespace, $key ) {
9293 global $wgRequest;
9394 $group = $wgRequest->getText('loadgroup', '' );
9495 $mg = MessageGroups::getGroup( $group );
@@ -95,7 +96,7 @@
9697 # If we were not given group, or the group given was meta...
9798 if ( is_null( $mg ) || $mg->isMeta() ) {
9899 # .. then try harder, because meta groups are *inefficient*
99 - $group = TranslateUtils::messageKeyToGroup( $key );
 100+ $group = TranslateUtils::messageKeyToGroup( $namespace, $key );
100101 if ( $group ) {
101102 $mg = MessageGroups::getGroup( $group );
102103 }
@@ -110,9 +111,11 @@
111112
112113 list( $key, $code ) = self::figureMessage( $object );
113114
114 - $group = self::getMessageGroup( $key );
 115+ $group = self::getMessageGroup( $object->mTitle->getNamespace(), $key );
115116 if ( $group === null ) return;
116117
 118+ list( $nsMain, /* $nsTalk */) = $group->namespaces;
 119+
117120 $en = $group->getMessage( $key, 'en' );
118121 $xx = $group->getMessage( $key, $code );
119122
@@ -141,6 +144,9 @@
142145 $title = Title::makeTitle( NS_MEDIAWIKI, $key . '/' . $wgTranslateDocumentationLanguageCode );
143146 $edit = $wgUser->getSkin()->makeKnownLinkObj( $title, wfMsgHtml( self::MSG . 'contribute' ), 'action=edit' );
144147 $info = TranslateUtils::getMessageContent( $key, $wgTranslateDocumentationLanguageCode );
 148+ if ( !$info ) {
 149+ $info = $group->getMessage( $key, $wgTranslateDocumentationLanguageCode );
 150+ }
145151 $class = 'mw-sp-translate-edit-info';
146152
147153 if ( !$info ) {
Index: trunk/extensions/Translate/Translate.php
@@ -11,7 +11,7 @@
1212 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
1313 */
1414
15 -define( 'TRANSLATE_VERSION', '8.43' );
 15+define( 'TRANSLATE_VERSION', '8.44' );
1616
1717 $wgExtensionCredits['specialpage'][] = array(
1818 'name' => 'Translate',
@@ -82,6 +82,8 @@
8383
8484 $wgTranslateBlacklist = array();
8585
 86+$wgTranslateMessageNamespaces = array( NS_MEDIAWIKI );
 87+
8688 /** AC = Available classes */
8789 $wgTranslateAC = array(
8890 'core' => 'CoreMessageGroup',
Index: trunk/extensions/Translate/CreateMessageIndex.php
@@ -21,11 +21,14 @@
2222 echo "Something wrong with $id... skipping\n";
2323 continue;
2424 }
 25+
 26+ $namespace = $g->namespaces[0];
 27+
2528 foreach ( $messages as $key => $data ) {
2629 # Force all keys to lower case, because the case doesn't matter and it is
2730 # easier to do comparing when the case of first letter is unknown, because
2831 # mediawiki forces it to upper case
29 - $key = strtolower( $key );
 32+ $key = strtolower( "$namespace:$key" );
3033 if ( isset($hugearray[$key]) ) {
3134 echo "Key $key already belongs to $hugearray[$key], conflict with $id\n";
3235 } else {
Index: trunk/extensions/Translate/TranslateTasks.php
@@ -69,7 +69,6 @@
7070 public final function init( MessageGroup $messageGroup, TaskOptions $options ) {
7171 $this->messageGroup = $messageGroup;
7272 $this->options = $options;
73 - $this->messages = new MessageCollection( $this->options->getLanguage() );
7473 }
7574
7675 protected $process = array();
@@ -130,24 +129,8 @@
131130 }
132131
133132 protected function preinit() {
134 - $definitions = $this->messageGroup->getDefinitions();
135 - foreach ( $definitions as $key => $definition ) {
136 - $this->messages->add( new TMessage( $key, $definition ) );
137 - }
138 -
139 - $bools = $this->messageGroup->getBools();
140 - foreach ( $bools['optional'] as $key ) {
141 - if ( isset($this->messages[$key]) ) {
142 - $this->messages[$key]->optional = true;
143 - }
144 - }
145 -
146 - foreach ( $bools['ignored'] as $key ) {
147 - if ( isset($this->messages[$key]) ) {
148 - $this->messages[$key]->ignored = true;
149 - }
150 - }
151 -
 133+ $this->messages =
 134+ $this->messageGroup->initCollection( $this->options->getLanguage() );
152135 }
153136
154137 protected function filterIgnored() {
@@ -167,9 +150,7 @@
168151 }
169152
170153 protected function postinit() {
171 - $this->messages->populatePageExistence();
172 - $this->messages->populateTranslationsFromDatabase();
173 - $this->messageGroup->fill( $this->messages );
 154+ $this->messageGroup->fillCollection( $this->messages );
174155 }
175156
176157 protected function output() {
@@ -180,7 +161,9 @@
181162 $tableheader .
182163 TranslateUtils::makeListing(
183164 $this->messages,
184 - $this->messageGroup->getId()
 165+ $this->messageGroup->getId(),
 166+ false,
 167+ $this->messageGroup->namespaces
185168 ) .
186169 $tablefooter;
187170 }
@@ -268,7 +251,8 @@
269252 TranslateUtils::makeListing(
270253 $this->messages,
271254 $this->messageGroup->getId(),
272 - true /* Review mode */
 255+ true, /* Review mode */
 256+ $this->messageGroup->namespaces
273257 ) .
274258 $tablefooter;
275259 }
Index: trunk/extensions/Translate/README
@@ -45,6 +45,8 @@
4646 * $wgTranslateBlacklist added to prevent edits to certain language/group combinations
4747 * new exporters
4848 * changed the way of adding MediaWiki extensions
 49+* support for having messages in namespaces other than NS_MEDIAWIKi
 50+* try to load qqq from files if not found from the database
4951
5052 == Changes in version 8 ==
5153 * Released 2008-02-06
Index: trunk/extensions/Translate/TranslateUtils.php
@@ -24,113 +24,115 @@
2525 }
2626
2727 /**
28 - * Initializes messages array.
 28+ * Fills the page/talk exists bools according to their existence in the
 29+ * database.
 30+ *
 31+ * @param $messages MessageCollection
 32+ * @param $namespaces Array: two-item 1-d array with namespace numbers
2933 */
30 - public static function initializeMessages( Array $definitions, $sortCallback = null ) {
31 - wfMemIn( __METHOD__ );
32 - $messages = array();
 34+ public static function fillExistence( MessageCollection $messages,
 35+ array $namespaces ) {
 36+ wfMemIn( __METHOD__ ); wfProfileIn( __METHOD__ );
3337
34 - if ( is_callable( $sortCallback ) ) {
35 - call_user_func_array( $sortCallback, array( &$definitions ) );
 38+ // Filter the titles so that we can find the respective pages from the db
 39+ $code = $messages->code;
 40+ $titles = array();
 41+ foreach ( $messages->keys() as $key ) {
 42+ $titles[] = self::title( $key, $code );
3643 }
3744
38 - foreach ( $definitions as $key => $value ) {
39 - $messages[$key]['definition'] = $value; // the very original message
40 - $messages[$key]['database'] = null; // current translation in db
41 - $messages[$key]['author'] = null; // Author of the latest revision
42 - $messages[$key]['infile'] = null; // current translation in file
43 - $messages[$key]['fallback'] = null; // current fallback
44 - $messages[$key]['optional'] = false;
45 - $messages[$key]['ignored'] = false;
46 - $messages[$key]['changed'] = false;
47 - $messages[$key]['pageexists'] = false;
48 - $messages[$key]['talkexists'] = false;
 45+ // Quick return if nothing to search
 46+ if ( !count($titles) ) return;
 47+
 48+ // Fetch from database, we will get existing titles as the result
 49+ $dbr = wfGetDB( DB_SLAVE );
 50+ $rows = $dbr->select(
 51+ 'page',
 52+ array( 'page_namespace', 'page_title' ),
 53+ array(
 54+ 'page_namespace' => $namespaces,
 55+ 'page_title' => $titles,
 56+ ),
 57+ __METHOD__
 58+ );
 59+
 60+ // Now store the pages we found...
 61+ foreach ( $rows as $row ) {
 62+ if ( $row->page_namespace == $namespaces[0] ) {
 63+ $pages[$row->page_title] = true;
 64+ } elseif ( $row->page_namespace == $namespaces[1]) {
 65+ $talks[$row->page_title] = true;
 66+ }
4967 }
 68+ $rows->free();
5069
51 - wfMemOut( __METHOD__ );
52 - return $messages;
53 - }
54 -
55 - /**
56 - * Fills the page/talk exists bools according to their existence in the
57 - * database.
58 - *
59 - * @param $messages Instance of MessageCollection
60 - */
61 - public static function fillExistence( MessageCollection $messages ) {
62 - wfMemIn( __METHOD__ );
63 - self::doExistenceQuery();
 70+ // ... and loop again to populate the collection
6471 foreach ( $messages->keys() as $key ) {
65 - $messages[$key]->pageExists = isset( self::$pageExists[self::title( $key, $messages->code )] );
66 - $messages[$key]->talkExists = isset( self::$talkExists[self::title( $key, $messages->code )] );
 72+ $messages[$key]->pageExists = isset( $pages[self::title( $key, $code )] );
 73+ $messages[$key]->talkExists = isset( $talks[self::title( $key, $code )] );
6774 }
68 - wfMemOut( __METHOD__ );
 75+
 76+ wfProfileOut( __METHOD__ ); wfMemOut( __METHOD__ );
6977 }
7078
7179 /**
7280 * Fills the actual translation from database, if any.
 81+ * TranslateUtils::fillExistence must be called before this to populate page
 82+ * existences.
7383 *
74 - * @param $messages Instance of MessageCollection
 84+ * @param $messages MessageCollection
 85+ * @param $namespaces Array: two-item 1-d array with namespace numbers
7586 */
76 - public static function fillContents( MessageCollection $messages ) {
77 - wfMemIn( __METHOD__ );
 87+ public static function fillContents( MessageCollection $messages,
 88+ array $namespaces ) {
 89+ wfMemIn( __METHOD__ ); wfProfileIn( __METHOD__ );
 90+
7891 $titles = array();
7992 foreach ( $messages->keys() as $key ) {
80 - $titles[self::title( $key, $messages->code )] = null;
 93+ // Only fetch messages for pages that exists
 94+ if ( $messages[$key]->pageExists ) {
 95+ $titles[] = self::title( $key, $messages->code );
 96+ }
8197 }
82 - // Determine for which messages are not fetched already
83 - $missing = array_diff( $titles, self::$contents );
8498
 99+ if ( !count($titles) ) return;
85100
86 - // Don't fetch pages that do not exists
87 - self::doExistenceQuery();
88 - foreach ( array_keys( $missing ) as $message ) {
89 - if ( !isset(self::$pageExists[$message] ) ) {
90 - unset( $missing[$message] );
91 - }
92 - }
 101+ // Fetch contents
 102+ $titles = self::getContents( $titles, $namespaces[0] );
93103
94 - // Fetch contents for the rest
95 - if ( count( $missing ) ) {
96 - self::getContents( array_keys( $missing ) );
97 - }
98 -
99104 foreach ( $messages->keys() as $key ) {
100105 $title = self::title( $key, $messages->code );
101 - if ( isset( self::$contents[$title] ) ) {
102 - $messages[$key]->database = self::$contents[$title];
103 - $messages[$key]->addAuthor( self::$editors[$title] );
 106+ if ( isset($titles[$title]) ) {
 107+ $messages[$key]->database = $titles[$title][0];
 108+ $messages[$key]->addAuthor( $titles[$title][1] );
104109 }
105110 }
106111
107 - self::$contents = array();
108 - self::$editors = array();
109 - wfMemOut( __METHOD__ );
 112+ wfProfileOut( __METHOD__ ); wfMemOut( __METHOD__ );
110113 }
111114
112 - public static function getMessageContent( $key, $language ) {
113 - wfMemIn( __METHOD__ );
114 - wfProfileIn( __METHOD__ );
 115+ public static function getMessageContent( $key, $language,
 116+ $namespace = NS_MEDIAWIKI ) {
 117+
115118 $title = self::title( $key, $language );
116 - if ( !isset(self::$contents[$title]) ) {
117 - self::getContents( array( $title ) );
118 - }
119 - wfProfileOut( __METHOD__ );
120 - wfMemOut( __METHOD__ );
121 - return isset(self::$contents[$title]) ? self::$contents[$title] : null;
 119+ $data = self::getContents( array($title), $namespace );
 120+ return isset($data[$title][0]) ? $data[$title][0] : null;
122121 }
123122
124 - private static $contents = array();
125 - private static $editors = array();
126 - private static function getContents( Array $titles ) {
127 - wfMemIn( __METHOD__ );
128 - wfProfileIn( __METHOD__ );
 123+ /**
 124+ * Fetches contents for titles in given namespace
 125+ *
 126+ * @param $titles Mixed: string or array of titles.
 127+ * @param $namespace Mixed: the number of the namespace to look in for.
 128+ */
 129+ private static function getContents( $titles, $namespace ) {
 130+ wfMemIn( __METHOD__ ); wfProfileIn( __METHOD__ );
129131 $dbr = wfGetDB( DB_SLAVE );
130132 $rows = $dbr->select( array( 'page', 'revision', 'text' ),
131133 array( 'page_title', 'old_text', 'old_flags', 'rev_user_text' ),
132134 array(
133135 'page_is_redirect' => 0,
134 - 'page_namespace' => NS_MEDIAWIKI,
 136+ 'page_namespace' => $namespace,
135137 'page_latest=rev_id',
136138 'rev_text_id=old_id',
137139 'page_title' => $titles
@@ -140,25 +142,20 @@
141143
142144
143145 foreach ( $rows as $row ) {
144 - self::$contents[$row->page_title] = Revision::getRevisionText( $row );
145 - self::$editors[$row->page_title] = $row->rev_user_text;
 146+ $titles[$row->page_title] = array(
 147+ Revision::getRevisionText( $row ),
 148+ $row->rev_user_text
 149+ );
146150 }
 151+ $rows->free();
147152
148 - $rows->free();
149 - wfProfileOut( __METHOD__ );
150 - wfMemOut( __METHOD__ );
 153+ return $titles;
 154+ wfProfileOut( __METHOD__ ); wfMemOut( __METHOD__ );
151155 }
152156
153 - private static $pageExists = null;
154 - private static $talkExists = null;
155 - private static function doExistenceQuery() {
 157+ private static function doExistenceQuery( MessageCollection $message) {
156158 wfMemIn( __METHOD__ );
157159 wfProfileIn( __METHOD__ );
158 - if ( self::$pageExists !== null && self::$talkExists !== null ) {
159 - wfProfileOut( __METHOD__ );
160 - wfMemOut( __METHOD__ );
161 - return;
162 - }
163160
164161 $dbr = wfGetDB( DB_SLAVE );
165162 $rows = $dbr->select(
@@ -232,8 +229,11 @@
233230 return $tableheader;
234231 }
235232
236 - public static function makeListing( MessageCollection $messages, $group, $review = false ) {
237 - wfMemIn( __METHOD__ );
 233+ public static function makeListing( MessageCollection $messages, $group,
 234+ $review = false, array $namespaces ) {
 235+
 236+ wfMemIn( __METHOD__ ); wfProfileIn( __METHOD__ );
 237+
238238 global $wgUser;
239239 $sk = $wgUser->getSkin();
240240 wfLoadExtensionMessages( 'Translate' );
@@ -250,8 +250,8 @@
251251 $title = self::title( $key, $messages->code );
252252 $tools = array();
253253
254 - $page['object'] = Title::makeTitle( NS_MEDIAWIKI, $title );
255 - $talk['object'] = Title::makeTitle( NS_MEDIAWIKI_TALK, $title );
 254+ $page['object'] = Title::makeTitle( $namespaces[0], $title );
 255+ $talk['object'] = Title::makeTitle( $namespaces[1], $title );
256256
257257 $original = $m->definition;
258258 $message = $m->translation ? $m->translation : $original;
@@ -307,7 +307,7 @@
308308
309309 }
310310
311 - wfMemOut( __METHOD__ );
 311+ wfProfileOut( __METHOD__ ); wfMemOut( __METHOD__ );
312312 return $output;
313313 }
314314
@@ -376,8 +376,8 @@
377377 return $selector->getHTML();
378378 }
379379
380 - public static function messageKeyToGroup( $key ) {
381 - $key = strtolower( $key );
 380+ public static function messageKeyToGroup( $namespace, $key ) {
 381+ $key = strtolower( "$namespace:$key" );
382382 $index = self::messageIndex();
383383 return @$index[$key];
384384 }
Index: trunk/extensions/Translate/Message.php
@@ -190,7 +190,6 @@
191191 private $authors = array();
192192
193193 private $infile = null;
194 - private $fallback = null;
195194 private $database = null;
196195
197196 private $optional = false;
@@ -259,7 +258,7 @@
260259 }
261260
262261 private static $callable = array( 'authors', 'changed', 'translated', 'translation', 'fuzzy' );
263 - private static $writable = array( 'infile', 'fallback', 'database', 'pageExists', 'talkExists', 'optional', 'ignored' );
 262+ private static $writable = array( 'infile', 'database', 'pageExists', 'talkExists', 'optional', 'ignored' );
264263
265264 public function __get( $name ) {
266265 if ( property_exists( $this, $name ) ) {
Index: trunk/extensions/Translate/ffs/Simple.php
@@ -218,7 +218,7 @@
219219
220220 protected function exportStaticHeader( $target ) {
221221 if( $this->staticHeader ) {
222 - fwrite( $target, $this->staticHeader );
 222+ fwrite( $target, $this->staticHeader . "\n" );
223223 }
224224 }
225225

Status & tagging log