TranslateTasks.php

Go to the documentation of this file.
00001 <?php
00017 abstract class TranslateTask {
00019     protected $id = '__BUG__';
00020 
00021     // We need $id here because staticness prevents subclass overriding.
00027     public static function labelForTask( $id ) {
00028         return wfMessage( 'translate-task-' . $id )->text();
00029     }
00030 
00035     public function getId() {
00036         return $this->id;
00037     }
00038 
00045     public function plainOutput() {
00046         return false;
00047     }
00048 
00052     protected $group;
00053 
00057     protected $collection;
00058 
00062     protected $options;
00063 
00067     protected $nondefaults;
00068 
00072     protected $context;
00073 
00077     protected $offsets;
00078 
00086     final public function init( MessageGroup $group, array $options, array $nondefaults,
00087         IContextSource $context
00088     ) {
00089         $this->group = $group;
00090         $this->options = $options;
00091         $this->nondefaults = $nondefaults;
00092         $this->context = $context;
00093     }
00094 
00099     abstract protected function output();
00100 
00102     abstract protected function preinit();
00103 
00105     abstract protected function postinit();
00106 
00112     final public function execute() {
00113         $this->preinit();
00114         $this->doPaging();
00115         $this->postinit();
00116 
00117         return $this->output();
00118     }
00119 
00125     protected function doPaging() {
00126         $total = count( $this->collection );
00127         $offsets = $this->collection->slice(
00128             $this->options['offset'],
00129             $this->options['limit']
00130         );
00131         $left = count( $this->collection );
00132 
00133         $this->offsets = array(
00134             'backwardsOffset' => $offsets[0],
00135             'forwardsOffset' => $offsets[1],
00136             'start' => $offsets[2],
00137             'count' => $left,
00138             'total' => $total,
00139         );
00140     }
00141 
00148     public function isAllowedFor( User $user ) {
00149         return true;
00150     }
00151 }
00152 
00160 class CustomFilteredMessagesTask extends TranslateTask {
00161     protected $id = 'custom';
00162 
00163     protected function preinit() {
00164     }
00165 
00166     protected function postinit() {
00167     }
00168 
00169     protected function doPaging() {
00170     }
00171 
00172     protected function output() {
00173         $table = new TuxMessageTable( $this->context, $this->group, $this->options['language'] );
00174 
00175         return $table->fullTable();
00176     }
00177 }
00178 
00182 class ViewMessagesTask extends TranslateTask {
00183     protected $id = 'view';
00184 
00185     protected function preinit() {
00186         $code = $this->options['language'];
00187         $this->collection = $this->group->initCollection( $code );
00188         $this->collection->filter( 'ignored' );
00189         $this->collection->filter( 'optional' );
00190     }
00191 
00192     protected function postinit() {
00193         $this->collection->loadTranslations();
00194     }
00195 
00196     protected function output() {
00197         $table = MessageTable::newFromContext( $this->context, $this->collection, $this->group );
00198         $table->appendEditLinkParams( 'loadtask', $this->getId() );
00199 
00200         return $table->fullTable( $this->offsets, $this->nondefaults );
00201     }
00202 }
00203 
00207 class ReviewMessagesTask extends ViewMessagesTask {
00208     protected $id = 'review';
00209 
00210     protected function preinit() {
00211         $code = $this->options['language'];
00212         $this->collection = $this->group->initCollection( $code );
00213         $this->collection->filter( 'ignored' );
00214     }
00215 
00216     protected function output() {
00217         $table = MessageTable::newFromContext( $this->context, $this->collection, $this->group );
00218         $table->appendEditLinkParams( 'loadtask', $this->getId() );
00219         $table->setReviewMode();
00220 
00221         return $table->fullTable( $this->offsets, $this->nondefaults );
00222     }
00223 }
00224 
00229 class ViewUntranslatedTask extends ViewMessagesTask {
00230     protected $id = 'untranslated';
00231 
00232     protected function preinit() {
00233         $code = $this->options['language'];
00234         $this->collection = $this->group->initCollection( $code );
00235         $this->collection->filter( 'ignored' );
00236         $this->collection->filter( 'optional' );
00237         $this->collection->filter( 'translated' );
00238     }
00239 }
00240 
00244 class ViewOptionalTask extends ViewMessagesTask {
00245     protected $id = 'optional';
00246 
00247     protected function preinit() {
00248         $code = $this->options['language'];
00249         $this->collection = $this->group->initCollection( $code );
00250         $this->collection->filter( 'ignored' );
00251         $this->collection->filter( 'optional', false );
00252     }
00253 }
00254 
00258 class ReviewAllMessagesTask extends ReviewMessagesTask {
00259     protected $id = 'reviewall';
00260 
00261     protected function preinit() {
00262         parent::preinit();
00263         $this->collection->filter( 'ignored' );
00264         $this->collection->filter( 'hastranslation', false );
00265     }
00266 }
00267 
00271 class AcceptQueueMessagesTask extends ReviewMessagesTask {
00272     protected $id = 'acceptqueue';
00273 
00274     protected function preinit() {
00275         $user = $this->context->getUser();
00276         parent::preinit();
00277         $this->collection->filter( 'ignored' );
00278         $this->collection->filter( 'hastranslation', false );
00279         $this->collection->filter( 'fuzzy' );
00280         $this->collection->filter( 'reviewer', true, $user->getId() );
00281         $this->collection->filter( 'last-translator', true, $user->getId() );
00282     }
00283 
00284     public function isAllowedFor( User $user ) {
00285         return $user->isAllowed( 'translate-messagereview' );
00286     }
00287 }
00288 
00292 class ExportMessagesTask extends ViewMessagesTask {
00293     protected $id = 'export';
00294 
00295     protected function preinit() {
00296         $code = $this->options['language'];
00297         $this->collection = $this->group->initCollection( $code );
00298         // Don't export ignored, unless it is the source language
00299         // or message documentation
00300         global $wgTranslateDocumentationLanguageCode;
00301         if ( $code !== $wgTranslateDocumentationLanguageCode
00302             && $code !== $this->group->getSourceLanguage()
00303         ) {
00304             $this->collection->filter( 'ignored' );
00305         }
00306     }
00307 
00308     // No paging should be done.
00309     protected function doPaging() {
00310     }
00311 
00312     public function output() {
00313         if ( $this->group instanceof MessageGroupBase ) {
00314             $ffs = $this->group->getFFS();
00315             $data = $ffs->writeIntoVariable( $this->collection );
00316         } else {
00317             $writer = $this->group->getWriter();
00318             $data = $writer->webExport( $this->collection );
00319         }
00320 
00321         return Html::element( 'textarea', array( 'id' => 'wpTextbox1', 'rows' => '50' ), $data );
00322     }
00323 }
00324 
00328 class ExportToFileMessagesTask extends ExportMessagesTask {
00329     protected $id = 'export-to-file';
00330 
00331     public function plainOutput() {
00332         return true;
00333     }
00334 
00335     public function output() {
00336         if ( !$this->group instanceof FileBasedMessageGroup ) {
00337             return 'Not supported';
00338         }
00339 
00340         $ffs = $this->group->getFFS();
00341         $data = $ffs->writeIntoVariable( $this->collection );
00342 
00343         $filename = basename( $this->group->getSourceFilePath( $this->collection->getLanguage() ) );
00344         header( "Content-Disposition: attachment; filename=\"$filename\"" );
00345 
00346         return $data;
00347     }
00348 }
00349 
00355 class ExportAsPoMessagesTask extends ExportMessagesTask {
00356     protected $id = 'export-as-po';
00357 
00358     public function plainOutput() {
00359         return true;
00360     }
00361 
00362     public function output() {
00363         if ( MessageGroups::isDynamic( $this->group ) ) {
00364             return 'Not supported';
00365         }
00366 
00367         $ffs = null;
00368         if ( $this->group instanceof FileBasedMessageGroup ) {
00369             $ffs = $this->group->getFFS();
00370         }
00371 
00372         if ( !$ffs instanceof GettextFFS ) {
00373             $group = FileBasedMessageGroup::newFromMessageGroup( $this->group );
00374             $ffs = new GettextFFS( $group );
00375         }
00376 
00377         $ffs->setOfflineMode( 'true' );
00378 
00379         $code = $this->options['language'];
00380         $id = $this->group->getID();
00381         $filename = "${id}_$code.po";
00382         header( "Content-Disposition: attachment; filename=\"$filename\"" );
00383 
00384         return $ffs->writeIntoVariable( $this->collection );
00385     }
00386 }
00387 
00391 class TranslateTasks {
00398     public static function getTasks( $pageTranslation = false ) {
00399         global $wgTranslateTasks;
00400 
00401         // Tasks not to be available in page translation.
00402         $filterTasks = array(
00403             'optional',
00404             'export-to-file',
00405         );
00406 
00407         $allTasks = array_keys( $wgTranslateTasks );
00408 
00409         if ( $pageTranslation ) {
00410             $allTasks = array_diff( $allTasks, $filterTasks );
00411         }
00412 
00413         return $allTasks;
00414     }
00415 
00421     public static function getTask( $id ) {
00422         global $wgTranslateTasks;
00423 
00424         if ( array_key_exists( $id, $wgTranslateTasks ) ) {
00425             if ( is_callable( $wgTranslateTasks[$id] ) ) {
00426                 return call_user_func( $wgTranslateTasks[$id], $id );
00427             }
00428 
00429             return new $wgTranslateTasks[$id];
00430         }
00431 
00432         return null;
00433     }
00434 }
Generated on Tue Oct 29 00:00:25 2013 for MediaWiki Translate Extension by  doxygen 1.6.3