TPParse.php

Go to the documentation of this file.
00001 <?php
00017 class TPParse {
00019     protected $title = null;
00020 
00024     public $sections = array();
00028     public $template = null;
00032     protected $dbSections = null;
00033 
00035     public function __construct( Title $title ) {
00036         $this->title = $title;
00037     }
00038 
00043     public function countSections() {
00044         return count( $this->sections );
00045     }
00046 
00052     public function getTemplate() {
00053         return $this->template;
00054     }
00055 
00062     public function getTemplatePretty() {
00063         $text = $this->template;
00064         $sections = $this->getSectionsForSave();
00065         foreach ( $sections as $ph => $s ) {
00066             $text = str_replace( $ph, "<!--T:{$s->id}-->", $text );
00067         }
00068 
00069         return $text;
00070     }
00071 
00077     public function getSectionsForSave( $highest = 0 ) {
00078         $this->loadFromDatabase();
00079 
00080         $sections = $this->sections;
00081         foreach ( array_keys( $this->dbSections ) as $key ) {
00082             $highest = max( $highest, intval( $key ) );
00083         }
00084 
00085         foreach ( $sections as $_ ) {
00086             $highest = max( $highest, intval( $_->id ) );
00087         }
00088 
00089         foreach ( $sections as $s ) {
00090             $s->type = 'old';
00091 
00092             if ( $s->id === -1 ) {
00093                 $s->type = 'new';
00094                 $s->id = ++$highest;
00095             } else {
00096                 if ( isset( $this->dbSections[$s->id] ) ) {
00097                     $storedText = $this->dbSections[$s->id]->text;
00098                     if ( $s->text !== $storedText ) {
00099                         $s->type = 'changed';
00100                         $s->oldText = $storedText;
00101                     }
00102                 }
00103             }
00104         }
00105 
00106         return $sections;
00107     }
00108 
00113     public function getDeletedSections() {
00114         $sections = $this->getSectionsForSave();
00115         $deleted = $this->dbSections;
00116 
00117         foreach ( $sections as $s ) {
00118             if ( isset( $deleted[$s->id] ) ) {
00119                 unset( $deleted[$s->id] );
00120             }
00121         }
00122 
00123         return $deleted;
00124     }
00125 
00129     protected function loadFromDatabase() {
00130         if ( $this->dbSections !== null ) {
00131             return;
00132         }
00133 
00134         $this->dbSections = array();
00135 
00136         $db = wfGetDB( DB_SLAVE );
00137         $tables = 'translate_sections';
00138         $vars = array( 'trs_key', 'trs_text' );
00139         $conds = array( 'trs_page' => $this->title->getArticleID() );
00140 
00141         $res = $db->select( $tables, $vars, $conds, __METHOD__ );
00142         foreach ( $res as $r ) {
00143             $section = new TPSection;
00144             $section->id = $r->trs_key;
00145             $section->text = $r->trs_text;
00146             $section->type = 'db';
00147             $this->dbSections[$r->trs_key] = $section;
00148         }
00149     }
00150 
00155     public function getSourcePageText() {
00156         $text = $this->template;
00157 
00158         foreach ( $this->sections as $ph => $s ) {
00159             $text = str_replace( $ph, $s->getMarkedText(), $text );
00160         }
00161 
00162         return $text;
00163     }
00164 
00171     public function getTranslationPageText( $collection ) {
00172         $text = $this->template; // The source
00173 
00174         // For finding the messages
00175         $prefix = $this->title->getPrefixedDBKey() . '/';
00176 
00177         if ( $collection instanceof MessageCollection ) {
00178             $collection->loadTranslations( DB_MASTER );
00179             $collection->filter( 'translated', false );
00180         }
00181 
00182         foreach ( $this->sections as $ph => $s ) {
00183             $sectiontext = null;
00184 
00185             if ( isset( $collection[$prefix . $s->id] ) ) {
00189                 $msg = $collection[$prefix . $s->id];
00190                 $sectiontext = $msg->translation();
00191             }
00192 
00193             // Use the original text if no translation is available.
00194 
00195             // For the source language, this will actually be the source, which
00196             // contains variable declarations (tvar) instead of variables ($1).
00197             // The getTextForTrans will convert declarations to normal variables
00198             // for us so that the variable substitutions below will also work
00199             // for the source language.
00200             if ( $sectiontext === null || $sectiontext === $s->getText() ) {
00201                 $sectiontext = $s->getTextForTrans();
00202             }
00203 
00204             // Substitute variables into section text and substitute text into document
00205             $sectiontext = strtr( $sectiontext, $s->getVariables() );
00206             $text = str_replace( $ph, $sectiontext, $text );
00207         }
00208 
00209         $nph = array();
00210         $text = TranslatablePage::armourNowiki( $nph, $text );
00211 
00212         // Remove translation markup from the template to produce final text
00213         $cb = array( __CLASS__, 'replaceTagCb' );
00214         $text = preg_replace_callback( '~(<translate>)(.*)(</translate>)~sU', $cb, $text );
00215         $text = TranslatablePage::unArmourNowiki( $nph, $text );
00216 
00217         return $text;
00218     }
00219 
00226     protected static function replaceTagCb( $matches ) {
00227         return preg_replace( '~^\n|\n\z~', '', $matches[2] );
00228     }
00229 }
Generated on Tue Oct 29 00:00:24 2013 for MediaWiki Translate Extension by  doxygen 1.6.3