TranslateMoveJob.php

Go to the documentation of this file.
00001 <?php
00017 class TranslateMoveJob extends Job {
00025     public static function newJob( Title $source, Title $target, array $params,
00026         /*User*/$performer
00027     ) {
00028         $job = new self( $source );
00029         $job->setUser( FuzzyBot::getUser() );
00030         $job->setTarget( $target->getPrefixedText() );
00031         $summary = wfMessage( 'pt-movepage-logreason', $params['base-source'] );
00032         $summary = $summary->inContentLanguage()->text();
00033         $job->setSummary( $summary );
00034         $job->setParams( $params );
00035         $job->setPerformer( $performer );
00036         $job->lock();
00037 
00038         return $job;
00039     }
00040 
00041     function __construct( $title, $params = array(), $id = 0 ) {
00042         parent::__construct( __CLASS__, $title, $params, $id );
00043     }
00044 
00045     function run() {
00046         // Unfortunately the global is needed until bug is fixed:
00047         // https://bugzilla.wikimedia.org/show_bug.cgi?id=49086
00048         global $wgUser;
00049 
00050         // Initialization
00051         $title = $this->title;
00052         // Other stuff
00053         $user = $this->getUser();
00054         $summary = $this->getSummary();
00055         $target = $this->getTarget();
00056         $base = $this->params['base-source'];
00057         $doer = User::newFromName( $this->getPerformer() );
00058 
00059         PageTranslationHooks::$allowTargetEdit = true;
00060         $oldUser = $wgUser;
00061         $wgUser = $user;
00062         self::forceRedirects( false );
00063 
00064         // Don't check perms, don't leave a redirect
00065         $ok = $title->moveTo( $target, false, $summary, false );
00066         if ( !$ok ) {
00067             $params = array(
00068                 'target' => $target->getPrefixedText(),
00069                 'error' => $ok,
00070             );
00071 
00072             $entry = new ManualLogEntry( 'pagetranslation', 'movenok' );
00073             $entry->setPerformer( $doer );
00074             $entry->setTarget( $title );
00075             $entry->setParameters( $params );
00076             $logid = $entry->insert();
00077             $entry->publish( $logid );
00078         }
00079 
00080         self::forceRedirects( true );
00081         PageTranslationHooks::$allowTargetEdit = false;
00082 
00083         $this->unlock();
00084 
00085         $cache = wfGetCache( CACHE_ANYTHING );
00086         $key = wfMemcKey( 'translate-pt-move', $base );
00087 
00088         $count = $cache->decr( $key );
00089         $last = strval( $count ) === '0';
00090 
00091         if ( $last ) {
00092             $cache->delete( $key );
00093 
00094             $params = array(
00095                 'target' => $this->params['base-target'],
00096             );
00097 
00098             $entry = new ManualLogEntry( 'pagetranslation', 'moveok' );
00099             $entry->setPerformer( $doer );
00100             $entry->setParameters( $params );
00101             $entry->setTarget( Title::newFromText( $base ) );
00102             $logid = $entry->insert();
00103             $entry->publish( $logid );
00104         }
00105 
00106         $wgUser = $oldUser;
00107 
00108         return true;
00109     }
00110 
00111     public function setSummary( $summary ) {
00112         $this->params['summary'] = $summary;
00113     }
00114 
00115     public function getSummary() {
00116         return $this->params['summary'];
00117     }
00118 
00119     public function setPerformer( $performer ) {
00120         if ( is_object( $performer ) ) {
00121             $this->params['performer'] = $performer->getName();
00122         } else {
00123             $this->params['performer'] = $performer;
00124         }
00125     }
00126 
00127     public function getPerformer() {
00128         return $this->params['performer'];
00129     }
00130 
00131     public function setTarget( $target ) {
00132         if ( $target instanceof Title ) {
00133             $this->params['target'] = $target->getPrefixedText();
00134         } else {
00135             $this->params['target'] = $target;
00136         }
00137     }
00138 
00139     public function getTarget() {
00140         return Title::newFromText( $this->params['target'] );
00141     }
00142 
00143     public function setUser( $user ) {
00144         if ( is_object( $user ) ) {
00145             $this->params['user'] = $user->getName();
00146         } else {
00147             $this->params['user'] = $user;
00148         }
00149     }
00150 
00155     public function getUser() {
00156         return User::newFromName( $this->params['user'], false );
00157     }
00158 
00159     public function setParams( array $params ) {
00160         foreach ( $params as $k => $v ) {
00161             $this->params[$k] = $v;
00162         }
00163     }
00164 
00165     public function lock() {
00166         $cache = wfGetCache( CACHE_ANYTHING );
00167         $cache->set( wfMemcKey( 'pt-lock', sha1( $this->title->getPrefixedText() ) ), true );
00168         $cache->set( wfMemcKey( 'pt-lock', sha1( $this->getTarget()->getPrefixedText() ) ), true );
00169     }
00170 
00171     public function unlock() {
00172         $cache = wfGetCache( CACHE_ANYTHING );
00173         $cache->delete( wfMemcKey( 'pt-lock', sha1( $this->title->getPrefixedText() ) ) );
00174         $cache->delete( wfMemcKey( 'pt-lock', sha1( $this->getTarget()->getPrefixedText() ) ) );
00175     }
00176 
00181     public static function forceRedirects( $end = false ) {
00182         static $suppressCount = 0;
00183         static $originalLevel = null;
00184 
00185         global $wgGroupPermissions;
00186         global $wgUser;
00187 
00188         if ( $end ) {
00189             if ( $suppressCount ) {
00190                 --$suppressCount;
00191                 if ( !$suppressCount ) {
00192                     if ( $originalLevel === null ) {
00193                         unset( $wgGroupPermissions['*']['suppressredirect'] );
00194                     } else {
00195                         $wgGroupPermissions['*']['suppressredirect'] = $originalLevel;
00196                     }
00197                 }
00198             }
00199         } else {
00200             if ( !$suppressCount ) {
00201                 $originalLevel = isset( $wgGroupPermissions['*']['suppressredirect'] ) ?
00202                     $wgGroupPermissions['*']['suppressredirect'] :
00203                     null;
00204                 $wgGroupPermissions['*']['suppressredirect'] = true;
00205             }
00206             ++$suppressCount;
00207         }
00208         $wgUser->clearInstanceCache();
00209     }
00210 }
Generated on Tue Oct 29 00:00:24 2013 for MediaWiki Translate Extension by  doxygen 1.6.3