SpecialFirstSteps.php

Go to the documentation of this file.
00001 <?php
00019 class SpecialFirstSteps extends UnlistedSpecialPage {
00020     public function __construct() {
00021         parent::__construct( 'FirstSteps' );
00022     }
00023 
00024     public function execute( $params ) {
00025         $out = $this->getOutput();
00026         $out->addWikiMsg( 'translate-fs-intro' );
00027         $step = false;
00028 
00029         $step = $this->showSignup( $step );
00030         $step = $this->showSettings( $step );
00031         $step = $this->showUserpage( $step );
00032         $step = $this->showPermissions( $step );
00033         $step = $this->showTarget( $step );
00034         $step = $this->showEmail( $step );
00035 
00036         if ( $step === 'translate-fs-target-title' ) {
00037             $code = $this->getLanguage()->getCode();
00038             $title = SpecialPage::getTitleFor( 'LanguageStats', $code );
00039             $out->redirect( $title->getLocalUrl() );
00040         }
00041 
00042         $stepText = $this->msg( $step )->plain();
00043         $out->setPageTitle( $this->msg( 'translate-fs-pagetitle', $stepText ) );
00044     }
00045 
00050     protected function showSignup( $step ) {
00051         $step_message = 'translate-fs-signup-title';
00052         $out = $this->getOutput();
00053 
00054         if ( $step ) {
00055             $out->addHtml( $this->getHeader( $step_message, 'inactive' ) );
00056 
00057             return $step;
00058         }
00059 
00060         if ( $this->getUser()->isLoggedIn() ) {
00061             $out->addHtml( $this->getHeader( $step_message, 'done' ) );
00062 
00063             return $step;
00064         }
00065 
00066         // Go straight to create account (or login) page
00067         $create = SpecialPage::getTitleFor( 'Userlogin' );
00068         $returnto = $this->getTitle()->getPrefixedText();
00069         $params = array( 'returnto' => $returnto, 'type' => 'signup' );
00070         $out->redirect( $create->getLocalUrl( $params ) );
00071 
00072         return false;
00073     }
00074 
00075     protected function showSettings( $step ) {
00076         $step_message = 'translate-fs-settings-title';
00077         $out = $this->getOutput();
00078 
00079         if ( $step ) {
00080             $out->addHtml( $this->getHeader( $step_message, 'inactive' ) );
00081 
00082             return $step;
00083         }
00084 
00085         $user = $this->getUser();
00086         $request = $this->getRequest();
00087         if ( true
00088             && $request->wasPosted()
00089             && $user->matchEditToken( $request->getVal( 'token' ) )
00090             && $request->getText( 'step' ) === 'settings'
00091         ) {
00092             $user->setOption( 'language', $request->getVal( 'primary-language' ) );
00093             $user->setOption( 'translate-firststeps', '1' );
00094 
00095             $assistant = array();
00096             for ( $i = 0; $i < 10; $i++ ) {
00097                 $language = $request->getText( "assistant-language-$i", '-' );
00098                 if ( $language === '-' ) {
00099                     continue;
00100                 }
00101                 $assistant[] = $language;
00102             }
00103 
00104             if ( count( $assistant ) ) {
00105                 $user->setOption( 'translate-editlangs', implode( ',', $assistant ) );
00106             }
00107             $user->saveSettings();
00108             // Reload the page if language changed, just in case and this is the easieast way
00109             $out->redirect( $this->getTitle()->getLocalUrl() );
00110         }
00111 
00112         if ( $user->getOption( 'translate-firststeps' ) === '1' ) {
00113             $out->addHtml( $this->getHeader( $step_message, 'done' ) );
00114 
00115             return $step;
00116         }
00117 
00118         $out->addHtml( $this->getHeader( $step_message ) );
00119 
00120         $output = Html::openElement( 'form', array( 'method' => 'post' ) );
00121         $output .= Html::hidden( 'step', 'settings' );
00122         $output .= Html::hidden( 'token', $user->getEditToken() );
00123         $output .= Html::hidden( 'title', $this->getTitle() );
00124         $output .= Html::openElement( 'table' );
00125 
00126         $name = $id = 'primary-language';
00127         $code = $this->getLanguage()->getCode();
00128         $selector = TranslateUtils::getLanguageSelector( $code );
00129         $selector->setDefault( $code );
00130         $selector->setAttribute( 'id', $id );
00131         $selector->setAttribute( 'name', $name );
00132         $text = $this->msg( 'translate-fs-settings-planguage' )->text();
00133         $row = self::wrap( 'td', Xml::label( $text, $id ) );
00134         $row .= self::wrap( 'td', $selector->getHtml() );
00135         $output .= self::wrap( 'tr', $row );
00136 
00137         $desc = $this->msg( 'translate-fs-settings-planguage-desc' )->parse();
00138         $output .= self::wrap( 'tr', self::wrapRow( $desc ) );
00139 
00140         $helpers = $this->getHelpers( $user, $code );
00141 
00142         $labelOption = $this->msg( 'translate-fs-selectlanguage' )->text();
00143         $selector = TranslateUtils::getLanguageSelector( $code, $labelOption );
00144 
00145         $num = max( 2, count( $helpers ) );
00146         for ( $i = 0; $i < $num; $i++ ) {
00147             $id = $name = "assistant-language-$i";
00148             $text = $this->msg( 'translate-fs-settings-slanguage' )->numParams( $i + 1 )->text();
00149             $selector->setDefault( isset( $helpers[$i] ) ? $helpers[$i] : false );
00150             $selector->setAttribute( 'id', $id );
00151             $selector->setAttribute( 'name', $name );
00152 
00153             $row = self::wrap( 'td', Xml::label( $text, $id ) );
00154             $row .= self::wrap( 'td', $selector->getHtml() );
00155             $output .= self::wrap( 'tr', $row );
00156         }
00157 
00158         $desc = $this->msg( 'translate-fs-settings-slanguage-desc' )->parse();
00159         $submit = Xml::submitButton( $this->msg( 'translate-fs-settings-submit' )->text() );
00160         $output .= ''
00161             . self::wrap( 'tr', self::wrapRow( $desc ) )
00162             . self::wrap( 'tr', self::wrapRow( $submit ) )
00163             . Html::closeElement( 'table' )
00164             . Html::closeElement( 'form' );
00165 
00166         $out->addHtml( $output );
00167 
00168         return $step_message;
00169     }
00170 
00171     protected static function wrap( /*string*/ $tag, /*string*/$content ) {
00172         return Html::rawElement( $tag, array(), $content );
00173     }
00174 
00175     protected static function wrapRow( /*string*/$content ) {
00176         return Html::rawElement( 'td', array( 'colspan' => 2 ), $content );
00177     }
00178 
00179     protected function getHelpers( User $user, /*string*/$code ) {
00180         global $wgTranslateLanguageFallbacks;
00181         $helpers = $user->getOption( 'translate-editlangs' );
00182         if ( $helpers === 'default' ) {
00183             if ( isset( $wgTranslateLanguageFallbacks[$code] ) ) {
00184                 $helpers = $wgTranslateLanguageFallbacks[$code];
00185             } else {
00186                 $helpers = array();
00187             }
00188         } else {
00189             $helpers = array_map( 'trim', explode( ',', $helpers ) );
00190         }
00191 
00192         return $helpers;
00193     }
00194 
00195     protected function showUserpage( $step ) {
00196         $textareaId = 'userpagetext';
00197         $step_message = 'translate-fs-userpage-title';
00198         $out = $this->getOutput();
00199 
00200         if ( $step ) {
00201             $out->addHtml( $this->getHeader( $step_message, 'inactive' ) );
00202 
00203             return $step;
00204         }
00205 
00206         $user = $this->getUser();
00207         $request = $this->getRequest();
00208         $userpage = $user->getUserPage();
00209         $preload = "I am [Your Name] and....";
00210 
00211         if ( true
00212             && $request->wasPosted()
00213             && $user->matchEditToken( $request->getVal( 'token' ) )
00214             && $request->getText( 'step' ) === 'userpage'
00215         ) {
00216             $babel = array();
00217             for ( $i = 0; $i < 5; $i++ ) {
00218                 $language = $request->getText( "babel-$i-language", '-' );
00219                 if ( $language === '-' ) {
00220                     continue;
00221                 }
00222                 $level = $request->getText( "babel-$i-level", '-' );
00223                 $babel[$language] = $level;
00224             }
00225 
00226             arsort( $babel );
00227             $babeltext = '{{#babel:';
00228             foreach ( $babel as $language => $level ) {
00229                 if ( $level === 'N' ) {
00230                     $level = '';
00231                 } else {
00232                     $level = "-$level";
00233                 }
00234                 $babeltext .= "$language$level|";
00235             }
00236             $babeltext = trim( $babeltext, '|' );
00237             $babeltext .= "}}\n";
00238 
00239             $article = new Article( $userpage, 0 );
00240             $status = $article->doEdit(
00241                 $babeltext . $request->getText( $textareaId ),
00242                 $this->getTitle()
00243             );
00244 
00245             if ( $status->isOK() ) {
00246                 $out->addHtml( $this->getHeader( $step_message, 'done' ) );
00247                 $out->addWikiMsg( 'translate-fs-userpage-done' );
00248 
00249                 return false;
00250             } else {
00251                 $out->addWikiText( $status->getWikiText() );
00252                 $preload = $request->getText( 'userpagetext' );
00253             }
00254         }
00255 
00256         if ( $userpage->exists() ) {
00257             $revision = Revision::newFromTitle( $userpage );
00258             $text = $revision->getText();
00259             $preload = $text;
00260 
00261             if ( preg_match( '/{{#babel:/i', $text ) ) {
00262                 $out->addHtml( $this->getHeader( $step_message, 'done' ) );
00263 
00264                 return false;
00265             }
00266         }
00267 
00268         $out->addHtml( $this->getHeader( $step_message ) );
00269         $out->addWikiMsg( 'translate-fs-userpage-help' );
00270 
00271         $output = ''
00272             . Html::openElement( 'form', array( 'method' => 'post' ) )
00273             . Html::hidden( 'step', 'userpage' )
00274             . Html::hidden( 'token', $this->getUser()->getEditToken() )
00275             . Html::hidden( 'title', $this->getTitle() );
00276 
00277         $code = $this->getLanguage()->getCode();
00278         $labelOption = $this->msg( 'translate-fs-selectlanguage' )->text();
00279         $selector = TranslateUtils::getLanguageSelector( $code, $labelOption );
00280 
00281         // Building a skill selector
00282         $skill = new XmlSelect();
00283         foreach ( explode( ',', 'N,5,4,3,2,1' ) as $level ) {
00284             // Give grep a chance to find the usages:
00285             // translate-fs-userpage-level-N, translate-fs-userpage-level-5,
00286             // translate-fs-userpage-level-4, translate-fs-userpage-level-3,
00287             // translate-fs-userpage-level-2, translate-fs-userpage-level-1
00288             $skill->addOption(
00289                 $this->msg( "translate-fs-userpage-level-$level" )->text(),
00290                 $level
00291             );
00292         }
00293 
00294         $lang = $this->getLanguage();
00295         for ( $i = 0; $i < 5; $i++ ) {
00296             // Prefill en-2 and [wgLang]-N if [wgLang] != en
00297             if ( $i === 0 ) {
00298                 $skill->setDefault( '2' );
00299                 $selector->setDefault( 'en' );
00300             } elseif ( $i === 1 && $lang->getCode() !== 'en' ) {
00301                 $skill->setDefault( 'N' );
00302                 $selector->setDefault( $lang->getCode() );
00303             } else {
00304                 $skill->setDefault( false );
00305                 $selector->setDefault( false );
00306             }
00307 
00308             // [skill level selector][language selector]
00309             $skill->setAttribute( 'name', "babel-$i-level" );
00310             $selector->setAttribute( 'name', "babel-$i-language" );
00311             $output .= $skill->getHtml() . $selector->getHtml() . '<br />';
00312         }
00313 
00314         $attribs = array(
00315             'rows' => 5,
00316             'name' => $textareaId,
00317             'id' => $textareaId,
00318         );
00319         $output .= Html::element( 'textarea', $attribs, $preload );
00320         $output .= Xml::submitButton( $this->msg( 'translate-fs-userpage-submit' )->text() );
00321         $output .= Html::closeElement( 'form' );
00322 
00323         $out->addHtml( $output );
00324 
00325         return $step_message;
00326     }
00327 
00328     protected function showPermissions( $step ) {
00329         $step_message = 'translate-fs-permissions-title';
00330         $out = $this->getOutput();
00331 
00332         if ( $step ) {
00333             $out->addHtml( $this->getHeader( $step_message, 'inactive' ) );
00334 
00335             return $step;
00336         }
00337 
00338         $request = $this->getRequest();
00339         $user = $this->getUser();
00340 
00341         if ( true
00342             && $request->wasPosted()
00343             && $user->matchEditToken( $request->getVal( 'token' ) )
00344             && $request->getText( 'step' ) === 'permissions'
00345         ) {
00346             // This is ridiculous
00347             global $wgCaptchaTriggers;
00348             $captcha = $wgCaptchaTriggers;
00349             $wgCaptchaTriggers = null;
00350 
00351             $language = $request->getVal( 'primary-language' );
00352             $message = $request->getText( 'message' );
00353             if ( trim( $message ) === '' ) {
00354                 $message = '...';
00355             }
00356             $params = array(
00357                 'action' => 'threadaction',
00358                 'threadaction' => 'newthread',
00359                 'token' => $user->getEditToken(),
00360                 'talkpage' => 'Project:Translator',
00361                 'subject' => "{{LanguageHeader|$language}}",
00362                 'reason' => 'Using Special:FirstSteps',
00363                 'text' => $message,
00364             );
00365             $request = new FauxRequest( $params, true, $_SESSION );
00366             $api = new ApiMain( $request, true );
00367             $api->execute();
00368             $result = $api->getResultData();
00369             $wgCaptchaTriggers = $captcha;
00370             $page = $result['threadaction']['thread']['thread-title'];
00371             $user->setOption( 'translate-firststeps-request', $page );
00372             $user->saveSettings();
00373         }
00374 
00375         $page = $user->getOption( 'translate-firststeps-request' );
00376         if ( $user->isAllowed( 'translate' ) ) {
00377             $out->addHtml( $this->getHeader( $step_message, 'done' ) );
00378 
00379             return $step;
00380         } elseif ( $page ) {
00381             $out->addHtml( $this->getHeader( $step_message, 'pending' ) );
00382             $out->addWikiMsg( 'translate-fs-permissions-pending', $page );
00383 
00384             return $step_message;
00385         }
00386 
00387         $out->addHtml( $this->getHeader( $step_message ) );
00388         $out->addWikiMsg( 'translate-fs-permissions-help' );
00389 
00390         $output = Html::openElement( 'form', array( 'method' => 'post' ) );
00391         $output .= Html::hidden( 'step', 'permissions' );
00392         $output .= Html::hidden( 'token', $user->getEditToken() );
00393         $output .= Html::hidden( 'title', $this->getTitle() );
00394         $name = $id = 'primary-language';
00395 
00396         $code = $this->getLanguage()->getCode();
00397         $selector = TranslateUtils::getLanguageSelector( $code );
00398         $selector->setAttribute( 'id', $id );
00399         $selector->setAttribute( 'name', $name );
00400         $selector->setDefault( $code );
00401 
00402         $text = $this->msg( 'translate-fs-permissions-planguage' )->text();
00403         $output .= Xml::label( $text, $id ) . "&#160;" . $selector->getHtml() . '<br />';
00404         $output .= Html::element( 'textarea', array( 'rows' => 5, 'name' => 'message' ), '' );
00405         $output .= Xml::submitButton( $this->msg( 'translate-fs-permissions-submit' )->text() );
00406         $output .= Html::closeElement( 'form' );
00407 
00408         $out->addHtml( $output );
00409 
00410         return $step_message;
00411     }
00412 
00413     protected function showTarget( $step ) {
00414         $step_message = 'translate-fs-target-title';
00415         $out = $this->getOutput();
00416 
00417         if ( $step ) {
00418             $out->addHtml( $this->getHeader( $step_message, 'inactive' ) );
00419 
00420             return $step;
00421         }
00422 
00423         $out->addHtml( $this->getHeader( $step_message ) );
00424         $code = $this->getLanguage()->getCode();
00425         $out->addWikiMsg( 'translate-fs-target-text', $code );
00426 
00427         return $step_message;
00428     }
00429 
00430     protected function showEmail( $step ) {
00431         $step_message = 'translate-fs-email-title';
00432         $out = $this->getOutput();
00433 
00434         if ( $step &&
00435             ( $step !== 'translate-fs-target-title' && $step !== 'translate-fs-permissions-title' )
00436         ) {
00437             $out->addHtml( $this->getHeader( $step_message, 'inactive' ) );
00438 
00439             return $step;
00440         }
00441 
00442         if ( $this->getUser()->isEmailConfirmed() ) {
00443             $out->addHtml( $this->getHeader( $step_message, 'done' ) );
00444 
00445             return $step; // Start translating step
00446         }
00447 
00448         $out->addHtml( $this->getHeader( $step_message ) );
00449         $out->addWikiMsg( 'translate-fs-email-text' );
00450 
00451         return $step_message;
00452     }
00453 
00460     protected function getHeader( $msg, $options = '' ) {
00461         $text = $this->msg( $msg )->text();
00462         if ( $options === 'done' ) {
00463             $text .= $this->msg( 'translate-fs-pagetitle-done' )->text();
00464         } elseif ( $options === 'pending' ) {
00465             $text .= $this->msg( 'translate-fs-pagetitle-pending' )->text();
00466         }
00467 
00468         $attribs = array();
00469         if ( $options === 'inactive' || $options === 'done' ) {
00470             $attribs['style'] = 'opacity: 0.4';
00471         }
00472 
00473         return Html::element( 'h2', $attribs, $text );
00474     }
00475 }
Generated on Tue Oct 29 00:00:24 2013 for MediaWiki Translate Extension by  doxygen 1.6.3