TranslationEditPage.php

Go to the documentation of this file.
00001 <?php
00016 class TranslationEditPage {
00017     // Instance of an Title object
00018     protected $title;
00019     protected $suggestions = 'sync';
00020 
00025     public function __construct( Title $title ) {
00026         $this->setTitle( $title );
00027     }
00028 
00035     public static function newFromRequest( WebRequest $request ) {
00036         $title = Title::newFromText( $request->getText( 'page' ) );
00037 
00038         if ( !$title ) {
00039             return null;
00040         }
00041 
00042         $obj = new self( $title );
00043         $obj->suggestions = $request->getText( 'suggestions' );
00044 
00045         return $obj;
00046     }
00047 
00052     public function setTitle( Title $title ) {
00053         $this->title = $title;
00054     }
00055 
00060     public function getTitle() {
00061         return $this->title;
00062     }
00063 
00068     public function execute() {
00069         global $wgServer, $wgScriptPath;
00070 
00071         $context = RequestContext::getMain();
00072 
00073         $context->getOutput()->disable();
00074 
00075         $data = $this->getEditInfo();
00076         $groupId = $context->getRequest()->getText( 'loadgroup', '' );
00077         $helpers = new TranslationHelpers( $this->getTitle(), $groupId );
00078 
00079         $id = "tm-target-{$helpers->dialogID()}";
00080         $helpers->setTextareaId( $id );
00081 
00082         if ( $this->suggestions === 'only' ) {
00083             echo $helpers->getBoxes( $this->suggestions );
00084 
00085             return;
00086         }
00087 
00088         if ( $this->suggestions === 'checks' ) {
00089             echo $helpers->getBoxes( $this->suggestions );
00090 
00091             return;
00092         }
00093 
00094         $handle = new MessageHandle( $this->getTitle() );
00095         $groupId = MessageIndex::getPrimaryGroupId( $handle );
00096 
00097         $translation = '';
00098         if ( $groupId ) {
00099             $translation = $helpers->getTranslation();
00100         }
00101 
00102         $targetLang = Language::factory( $helpers->getTargetLanguage() );
00103         $textareaParams = array(
00104             'name' => 'text',
00105             'class' => 'mw-translate-edit-area',
00106             'id' => $id,
00107             /* Target language might differ from interface language. Set
00108              * a suitable default direction */
00109             'lang' => $targetLang->getCode(),
00110             'dir' => $targetLang->getDir(),
00111         );
00112 
00113         if ( !$groupId || !$context->getUser()->isAllowed( 'translate' ) ) {
00114             $textareaParams['readonly'] = 'readonly';
00115         }
00116 
00117         $extraInputs = '';
00118         wfRunHooks( 'TranslateGetExtraInputs', array( &$translation, &$extraInputs ) );
00119 
00120         $textarea = Html::element( 'textarea', $textareaParams, $translation );
00121 
00122         $hidden = array();
00123         $hidden[] = Html::hidden( 'title', $this->getTitle()->getPrefixedDbKey() );
00124 
00125         if ( isset( $data['revisions'][0]['timestamp'] ) ) {
00126             $hidden[] = Html::hidden( 'basetimestamp', $data['revisions'][0]['timestamp'] );
00127         }
00128 
00129         $hidden[] = Html::hidden( 'starttimestamp', $data['starttimestamp'] );
00130         if ( isset( $data['edittoken'] ) ) {
00131             $hidden[] = Html::hidden( 'token', $data['edittoken'] );
00132         }
00133         $hidden[] = Html::hidden( 'format', 'json' );
00134         $hidden[] = Html::hidden( 'action', 'edit' );
00135 
00136         $summary = Xml::inputLabel(
00137             $context->msg( 'translate-js-summary' )->text(),
00138             'summary',
00139             'summary',
00140             40
00141         );
00142         $save = Xml::submitButton(
00143             $context->msg( 'translate-js-save' )->text(),
00144             array( 'class' => 'mw-translate-save' )
00145         );
00146         $saveAndNext = Xml::submitButton(
00147             $context->msg( 'translate-js-next' )->text(),
00148             array( 'class' => 'mw-translate-next' )
00149         );
00150         $skip = Html::element( 'input', array(
00151             'class' => 'mw-translate-skip',
00152             'type' => 'button',
00153             'value' => $context->msg( 'translate-js-skip' )->text()
00154         ) );
00155 
00156         if ( $this->getTitle()->exists() ) {
00157             $history = Html::element(
00158                 'input',
00159                 array(
00160                     'class' => 'mw-translate-history',
00161                     'type' => 'button',
00162                     'value' => $context->msg( 'translate-js-history' )->text()
00163                 )
00164             );
00165         } else {
00166             $history = '';
00167         }
00168 
00169         $support = $this->getSupportButton( $this->getTitle() );
00170 
00171         if ( $context->getUser()->isAllowed( 'translate' ) ) {
00172             $bottom = "$summary$save$saveAndNext$skip$history$support";
00173         } else {
00174             $text = $context->msg( 'translate-edit-nopermission' )->escaped();
00175             $button = $this->getPermissionPageButton();
00176             $bottom = "$text $button$skip$history$support";
00177         }
00178 
00179         // Use the api to submit edits
00180         $formParams = array(
00181             'action' => "{$wgServer}{$wgScriptPath}/api.php",
00182             'method' => 'post',
00183         );
00184 
00185         $form = Html::rawElement( 'form', $formParams,
00186             implode( "\n", $hidden ) . "\n" .
00187                 $helpers->getBoxes( $this->suggestions ) . "\n" .
00188                 Html::rawElement(
00189                     'div',
00190                     array( 'class' => 'mw-translate-inputs' ),
00191                     "$textarea\n$extraInputs"
00192                 ) . "\n" .
00193                 Html::rawElement( 'div', array( 'class' => 'mw-translate-bottom' ), $bottom )
00194         );
00195 
00196         echo Html::rawElement( 'div', array( 'class' => 'mw-ajax-dialog' ), $form );
00197     }
00198 
00205     protected function getEditInfo() {
00206         $params = new FauxRequest( array(
00207             'action' => 'query',
00208             'prop' => 'info|revisions',
00209             'intoken' => 'edit',
00210             'titles' => $this->getTitle(),
00211             'rvprop' => 'timestamp',
00212         ) );
00213 
00214         $api = new ApiMain( $params );
00215         $api->execute();
00216         $data = $api->getResultData();
00217 
00218         if ( !isset( $data['query']['pages'] ) ) {
00219             throw new MWException( 'Api query failed' );
00220         }
00221         $data = $data['query']['pages'];
00222         $data = array_shift( $data );
00223 
00224         return $data;
00225     }
00226 
00238     public static function jsEdit( Title $title, $group = "", $type = 'default' ) {
00239         $context = RequestContext::getMain();
00240 
00241         if ( !$context->getUser()->getOption( 'translate-jsedit' ) ) {
00242             return array();
00243         }
00244 
00245         if ( $type === 'default' ) {
00246             $text = 'tqe-anchor-' . substr( sha1( $title->getPrefixedText() ), 0, 12 );
00247             $onclick = "jQuery( '#$text' ).dblclick(); return false;";
00248         } else {
00249             $onclick = Xml::encodeJsCall(
00250                 'return mw.translate.openDialog', array( $title->getPrefixedDbKey(), $group )
00251             );
00252         }
00253 
00254         return array(
00255             'onclick' => $onclick,
00256             'title' => $context->msg( 'translate-edit-title', $title->getPrefixedText() )->text()
00257         );
00258     }
00259 
00260     protected function getSupportButton( $title ) {
00261         global $wgTranslateSupportUrl;
00262         if ( !$wgTranslateSupportUrl ) {
00263             return '';
00264         }
00265 
00266         $supportTitle = Title::newFromText( $wgTranslateSupportUrl['page'] );
00267         if ( !$supportTitle ) {
00268             return '';
00269         }
00270 
00271         $supportParams = $wgTranslateSupportUrl['params'];
00272         foreach ( $supportParams as &$value ) {
00273             $value = str_replace( '%MESSAGE%', $title->getPrefixedText(), $value );
00274         }
00275 
00276         $support = Html::element(
00277             'input',
00278             array(
00279                 'class' => 'mw-translate-support',
00280                 'type' => 'button',
00281                 'value' => wfMessage( 'translate-js-support' )->text(),
00282                 'title' => wfMessage( 'translate-js-support-title' )->text(),
00283                 'data-load-url' => $supportTitle->getLocalUrl( $supportParams ),
00284             )
00285         );
00286 
00287         return $support;
00288     }
00289 
00290     protected function getPermissionPageButton() {
00291         global $wgTranslatePermissionUrl;
00292         if ( !$wgTranslatePermissionUrl ) {
00293             return '';
00294         }
00295 
00296         $title = Title::newFromText( $wgTranslatePermissionUrl );
00297         if ( !$title ) {
00298             return '';
00299         }
00300 
00301         $button = Html::element(
00302             'input',
00303             array(
00304                 'class' => 'mw-translate-askpermission',
00305                 'type' => 'button',
00306                 'value' => wfMessage( 'translate-edit-askpermission' )->text(),
00307                 'data-load-url' => $title->getLocalUrl(),
00308             )
00309         );
00310 
00311         return $button;
00312     }
00313 }
Generated on Tue Oct 29 00:00:26 2013 for MediaWiki Translate Extension by  doxygen 1.6.3