00001 <?php
00018 class TranslateEditAddons {
00023 static function addNavigationTabs( Skin $skin, array &$tabs ) {
00024 $title = $skin->getTitle();
00025 $handle = new MessageHandle( $title );
00026
00027 if ( !$handle->isValid() ) {
00028 return true;
00029 }
00030
00031 $group = $handle->getGroup();
00032
00033 if ( !$group ) {
00034 return true;
00035 }
00036
00037 $index = $next = $prev = $key = null;
00038 if ( $skin->getUser()->isAllowed( 'translate' ) ) {
00039 self::figureNextPrevMessages( $handle, $key, $index, $next, $prev );
00040 }
00041
00042 $id = $group->getId();
00043 $code = $handle->getCode();
00044
00045 $translate = SpecialPage::getTitleFor( 'Translate' );
00046 $fragment = htmlspecialchars( "#msg_$key" );
00047
00048 $nav_params = array();
00049 $nav_params['loadgroup'] = $id;
00050 $nav_params['action'] = $skin->getRequest()->getText( 'action', 'edit' );
00051
00052 $tabindex = 2;
00053
00057 if ( $prev !== null ) {
00058 $data = array(
00059 'text' => wfMessage( 'translate-edit-tab-prev' )->text(),
00060 'href' => $prev->getLocalUrl( $nav_params ),
00061 );
00062 self::addTab( $skin, $tabs, 'prev', $data, $tabindex );
00063 }
00064
00065 $params = array(
00066 'group' => $id,
00067 'language' => $code,
00068 'task' => 'view',
00069 'offset' => max( 0, $index - 250 ),
00070 'limit' => 500,
00071 );
00072 $data = array(
00073 'text' => wfMessage( 'translate-edit-tab-list' )->text(),
00074 'href' => $translate->getLocalUrl( $params ) . $fragment,
00075 );
00076 self::addTab( $skin, $tabs, 'list', $data, $tabindex );
00077
00081 if ( $next !== null ) {
00082 $data = array(
00083 'text' => wfMessage( 'translate-edit-tab-next' )->text(),
00084 'href' => $next->getLocalUrl( $nav_params ),
00085 );
00086 self::addTab( $skin, $tabs, 'next', $data, $tabindex );
00087 }
00088
00089 return true;
00090 }
00091
00092 protected static function addTab( $skin, &$tabs, $name, $data, &$index ) {
00093
00094 if ( $skin instanceof SkinVector || $skin instanceof SkinChihuahua ) {
00095 $data['class'] = false;
00096 $tabs['namespaces'][$name] = $data;
00097 } else {
00098 array_splice( $tabs, $index++, 0, array( $name => $data ) );
00099 }
00100 }
00101
00113 protected static function figureNextPrevMessages( MessageHandle $handle, &$key,
00114 &$index, &$next, &$prev
00115 ) {
00116 wfProfileIn( __METHOD__ );
00117 $group = $handle->getGroup();
00118
00119 if ( $group instanceof MessageGroupBase ) {
00120 $keys = $group->getKeys();
00121 } else {
00122 $keys = array_keys( $group->getDefinitions() );
00123 }
00124
00125 $key = $handle->getKey();
00126 $key = strtolower( strtr( $key, ' ', '_' ) );
00127 $match = -100;
00128 foreach ( $keys as $index => $tkey ) {
00129
00130 if ( $key === strtolower( strtr( $tkey, ' ', '_' ) ) ) {
00131
00132 $key = $tkey;
00133 $match = $index;
00134 break;
00135 }
00136 }
00137
00138 $ns = $handle->getTitle()->getNamespace();
00139 $code = $handle->getCode();
00140
00141 if ( isset( $keys[$match - 1] ) ) {
00142 $mkey = $keys[$match - 1];
00143 $prev = Title::makeTitleSafe( $ns, "$mkey/$code" );
00144 }
00145 if ( isset( $keys[$match + 1] ) ) {
00146 $mkey = $keys[$match + 1];
00147 $next = Title::makeTitleSafe( $ns, "$mkey/$code" );
00148 }
00149 wfProfileOut( __METHOD__ );
00150 }
00151
00156 public static function intro( EditPage $editpage ) {
00157 $handle = new MessageHandle( $editpage->getTitle() );
00158 if ( $handle->isValid() ) {
00159 $editpage->suppressIntro = true;
00160 $group = $handle->getGroup();
00161 $languages = $group->getTranslatableLanguages();
00162 if ( $handle->getCode() && $languages !== null && !isset( $languages[$handle->getCode()] ) ) {
00163 $editpage->getArticle()->getContext()->getOutput()->wrapWikiMsg(
00164 "<div class='error'>$1</div>", 'translate-language-disabled'
00165 );
00166
00167 return false;
00168 }
00169
00170 return true;
00171 }
00172 $msg = wfMessage( 'translate-edit-tag-warning' )->inContentLanguage();
00173
00174 if ( !$msg->isDisabled() &&
00175 TranslatablePage::isSourcePage( $editpage->getTitle() )
00176 ) {
00177 $editpage->editFormTextTop .= $editpage->getArticle()->getContext()
00178 ->getOutput()->parse( $msg->plain() );
00179 }
00180
00181 return true;
00182 }
00183
00188 static function addTools( EditPage $object ) {
00189 $handle = new MessageHandle( $object->getTitle() );
00190 if ( !$handle->isValid() ) {
00191 return true;
00192 }
00193
00194 $object->editFormTextTop .= self::editBoxes( $object );
00195
00196 return true;
00197 }
00198
00204 static function buttonHack( EditPage $editpage, &$buttons, $tabindex ) {
00205 $handle = new MessageHandle( $editpage->getTitle() );
00206 if ( !$handle->isValid() ) {
00207 return true;
00208 }
00209
00210 $context = $editpage->getArticle()->getContext();
00211
00212 if ( $handle->isDoc() ) {
00213 $langCode = $context->getLanguage()->getCode();
00214 $name = TranslateUtils::getLanguageName( $handle->getCode(), $langCode );
00215 $accessKey = $context->msg( 'accesskey-save' )->plain();
00216 $temp = array(
00217 'id' => 'wpSave',
00218 'name' => 'wpSave',
00219 'type' => 'submit',
00220 'tabindex' => ++$tabindex,
00221 'value' => $context->msg( 'translate-save', $name )->text(),
00222 'accesskey' => $accessKey,
00223 'title' => $context->msg( 'tooltip-save' )->text() . ' [' . $accessKey . ']',
00224 );
00225 $buttons['save'] = Xml::element( 'input', $temp, '' );
00226 }
00227
00228 global $wgTranslateSupportUrl;
00229 if ( !$wgTranslateSupportUrl ) {
00230 return true;
00231 }
00232
00233 $supportTitle = Title::newFromText( $wgTranslateSupportUrl['page'] );
00234 if ( !$supportTitle ) {
00235 return true;
00236 }
00237
00238 $supportParams = $wgTranslateSupportUrl['params'];
00239 foreach ( $supportParams as &$value ) {
00240 $value = str_replace( '%MESSAGE%', $handle->getTitle()->getPrefixedText(), $value );
00241 }
00242
00243 $temp = array(
00244 'id' => 'wpSupport',
00245 'name' => 'wpSupport',
00246 'type' => 'button',
00247 'tabindex' => ++$tabindex,
00248 'value' => $context->msg( 'translate-js-support' )->text(),
00249 'title' => $context->msg( 'translate-js-support-title' )->text(),
00250 'data-load-url' => $supportTitle->getLocalUrl( $supportParams ),
00251 'onclick' => "window.open( jQuery(this).attr('data-load-url') );",
00252 );
00253 $buttons['ask'] = Html::element( 'input', $temp, '' );
00254
00255 return true;
00256 }
00257
00262 private static function editBoxes( EditPage $editpage ) {
00263 $context = $editpage->getArticle()->getContext();
00264 $request = $context->getRequest();
00265
00266 $groupId = $request->getText( 'loadgroup', '' );
00267 $th = new TranslationHelpers( $editpage->getTitle(), $groupId );
00268
00269 if ( $editpage->firsttime &&
00270 !$request->getCheck( 'oldid' ) &&
00271 !$request->getCheck( 'undo' )
00272 ) {
00273 $editpage->textbox1 = (string)$th->getTranslation();
00274 } else {
00275 $th->setTranslation( $editpage->textbox1 );
00276 }
00277
00278 TranslationHelpers::addModules( $context->getOutput() );
00279
00280 return $th->getBoxes();
00281 }
00282
00289 public static function hasFuzzyString( $text ) {
00290 # wfDeprecated( __METHOD__, '1.19' );
00291 return MessageHandle::hasFuzzyString( $text );
00292 }
00293
00299 public static function isFuzzy( Title $title ) {
00300 # wfDeprecated( __METHOD__, '1.19' );
00301 $handle = new MessageHandle( $title );
00302
00303 return $handle->isFuzzy();
00304 }
00305
00309 public static function keepFields( EditPage $editpage, OutputPage $out ) {
00310 $request = $editpage->getArticle()->getContext()->getRequest();
00311
00312 $out->addHTML(
00313 "\n" .
00314 Html::hidden( 'loadgroup', $request->getText( 'loadgroup' ) ) .
00315 Html::hidden( 'loadtask', $request->getText( 'loadtask' ) ) .
00316 "\n"
00317 );
00318
00319 return true;
00320 }
00321
00329 public static function onSave( $wikiPage, $user, $content, $summary,
00330 $minor, $_, $_, $flags, $revision
00331 ) {
00332
00333 if ( $content instanceof TextContent ) {
00334 $text = $content->getNativeData();
00335 } elseif ( is_string( $content ) ) {
00336
00337 $text = $content;
00338 } else {
00339
00340 return true;
00341 }
00342
00343 $title = $wikiPage->getTitle();
00344 $handle = new MessageHandle( $title );
00345
00346 if ( !$handle->isValid() ) {
00347 return true;
00348 }
00349
00350
00351 if ( $revision === null ) {
00352 $rev = $wikiPage->getTitle()->getLatestRevId();
00353 } else {
00354 $rev = $revision->getID();
00355 }
00356
00357 $fuzzy = self::checkNeedsFuzzy( $handle, $text );
00358 self::updateFuzzyTag( $title, $rev, $fuzzy );
00359 wfRunHooks( 'TranslateEventTranslationEdit', array( $handle ) );
00360
00361 if ( $fuzzy === false ) {
00362 wfRunHooks( 'Translate:newTranslation', array( $handle, $rev, $text, $user ) );
00363 }
00364
00365 TTMServer::onChange( $handle, $text, $fuzzy );
00366
00367 return true;
00368 }
00369
00375 protected static function checkNeedsFuzzy( MessageHandle $handle, $text ) {
00376
00377 $fuzzy = self::hasFuzzyString( $text );
00378
00379
00380 if ( $handle->isDoc() ) {
00381 return $fuzzy;
00382 }
00383
00384
00385 $group = $handle->getGroup();
00386 $checker = $group->getChecker();
00387 if ( !$checker ) {
00388 return $fuzzy;
00389 }
00390
00391 $code = $handle->getCode();
00392 $key = $handle->getKey();
00393 $en = $group->getMessage( $key, $group->getSourceLanguage() );
00394 $message = new FatMessage( $key, $en );
00395
00396 $message->setTranslation( $text );
00397
00398 $checks = $checker->checkMessage( $message, $code );
00399 if ( count( $checks ) ) {
00400 $fuzzy = true;
00401 }
00402
00403 return $fuzzy;
00404 }
00405
00411 protected static function updateFuzzyTag( Title $title, $revision, $fuzzy ) {
00412 $dbw = wfGetDB( DB_MASTER );
00413
00414 $conds = array(
00415 'rt_page' => $title->getArticleID(),
00416 'rt_type' => RevTag::getType( 'fuzzy' ),
00417 'rt_revision' => $revision
00418 );
00419
00420
00421 if ( $fuzzy !== false ) {
00422 $index = array_keys( $conds );
00423 $dbw->replace( 'revtag', array( $index ), $conds, __METHOD__ );
00424 } else {
00425 $dbw->delete( 'revtag', $conds, __METHOD__ );
00426 }
00427 }
00428
00440 public static function updateTransverTag( MessageHandle $handle, $revision,
00441 $text, User $user
00442 ) {
00443 if ( $user->isAllowed( 'bot' ) ) {
00444 return false;
00445 }
00446
00447 $group = $handle->getGroup();
00448
00449 $title = $handle->getTitle();
00450 $name = $handle->getKey() . '/' . $group->getSourceLanguage();
00451 $definitionTitle = Title::makeTitleSafe( $title->getNamespace(), $name );
00452 if ( !$definitionTitle || !$definitionTitle->exists() ) {
00453 return true;
00454 }
00455
00456 $definitionRevision = $definitionTitle->getLatestRevID();
00457
00458 $dbw = wfGetDB( DB_MASTER );
00459
00460 $conds = array(
00461 'rt_page' => $title->getArticleID(),
00462 'rt_type' => RevTag::getType( 'tp:transver' ),
00463 'rt_revision' => $revision,
00464 'rt_value' => $definitionRevision,
00465 );
00466 $index = array( 'rt_type', 'rt_page', 'rt_revision' );
00467 $dbw->replace( 'revtag', array( $index ), $conds, __METHOD__ );
00468
00469 return true;
00470 }
00471
00478 public static function disablePreSaveTransform( $wikiPage, ParserOptions $popts ) {
00479 global $wgTranslateUsePreSaveTransform;
00480
00481 if ( !$wgTranslateUsePreSaveTransform ) {
00482 $handle = new MessageHandle( $wikiPage->getTitle() );
00483 if ( $handle->isMessageNamespace() && !$handle->isDoc() ) {
00484 $popts->setPreSaveTransform( false );
00485 }
00486 }
00487
00488 return true;
00489 }
00490
00494 public static function displayOnDiff( DifferenceEngine $de, OutputPage $out ) {
00495 $title = $de->getTitle();
00496 $handle = new MessageHandle( $title );
00497
00498 if ( !$handle->isValid() ) {
00499 return true;
00500 }
00501
00502 $de->loadNewText();
00503 $out->setRevisionId( $de->mNewRev->getId() );
00504
00505 $th = new TranslationHelpers( $title, false );
00506 $th->setEditMode( false );
00507
00508 if ( isset( $de->mNewContent ) && $de->mNewContent instanceof TextContent ) {
00509 $th->setTranslation( $de->mNewContent->getNativeData() );
00510 } elseif ( isset( $de->mNewtext ) ) {
00511
00512 $th->setTranslation( $de->mNewtext );
00513 } else {
00514
00515 return true;
00516 }
00517 TranslationHelpers::addModules( $out );
00518
00519 $boxes = array();
00520 $boxes[] = $th->callBox( 'documentation', array( $th, 'getDocumentationBox' ) );
00521 $boxes[] = $th->callBox( 'definition', array( $th, 'getDefinitionBox' ) );
00522 $boxes[] = $th->callBox( 'translation', array( $th, 'getTranslationDisplayBox' ) );
00523
00524 $output = implode( "\n", $boxes );
00525 $output = Html::rawElement(
00526 'div',
00527 array( 'class' => 'mw-sp-translate-edit-fields' ),
00528 $output
00529 );
00530 $out->addHtml( $output );
00531
00532 return true;
00533 }
00534 }