MessageHandle.php
Go to the documentation of this file.00001 <?php
00014 class MessageHandle {
00016 protected $title = null;
00018 protected $key = null;
00020 protected $code = null;
00022 protected $groupIds = null;
00024 protected $group = false;
00025
00026 public function __construct( Title $title ) {
00027 $this->title = $title;
00028 }
00029
00034 public function isMessageNamespace() {
00035 global $wgTranslateMessageNamespaces;
00036 $namespace = $this->getTitle()->getNamespace();
00037
00038 return in_array( $namespace, $wgTranslateMessageNamespaces );
00039 }
00040
00045 public function figureMessage() {
00046 if ( $this->key === null ) {
00047 $title = $this->getTitle();
00048
00049 $this->key = $title->getDBKey();
00050 $known = count( MessageIndex::singleton()->getGroupIds( $this ) );
00051
00052 $pos = strrpos( $this->key, '/' );
00053 if ( $known || $pos === false ) {
00054 $this->code = '';
00055 } else {
00056
00057 $this->code = strval( substr( $this->key, $pos + 1 ) );
00058 $this->key = substr( $this->key, 0, $pos );
00059 }
00060 }
00061
00062 return array( $this->key, $this->code );
00063 }
00064
00069 public function getKey() {
00070 $this->figureMessage();
00071
00072 return $this->key;
00073 }
00074
00080 public function getCode() {
00081 $this->figureMessage();
00082
00083 return $this->code;
00084 }
00085
00092 public function getEffectiveLanguageCode() {
00093 global $wgContLang;
00094 $code = $this->getCode();
00095 if ( $code === '' || $this->isDoc() ) {
00096 return $wgContLang->getCode();
00097 }
00098
00099 return $code;
00100 }
00101
00106 public function isDoc() {
00107 global $wgTranslateDocumentationLanguageCode;
00108
00109 return $this->getCode() === $wgTranslateDocumentationLanguageCode;
00110 }
00111
00117 public function isPageTranslation() {
00118 return $this->getTitle()->getNamespace() == NS_TRANSLATIONS;
00119 }
00120
00128 public function getGroupIds() {
00129 if ( $this->groupIds === null ) {
00130 $this->groupIds = MessageIndex::singleton()->getGroupIds( $this );
00131 }
00132
00133 return $this->groupIds;
00134 }
00135
00142 public function getGroup() {
00143 $ids = $this->getGroupIds();
00144 if ( !isset( $ids[0] ) ) {
00145 throw new MWException( 'called before isValid' );
00146 }
00147
00148 return MessageGroups::getGroup( $ids[0] );
00149 }
00150
00156 public function isValid() {
00157 if ( !$this->isMessageNamespace() ) {
00158 return false;
00159 }
00160
00161 $groups = $this->getGroupIds();
00162 if ( !$groups ) {
00163 return false;
00164 }
00165
00166
00167 $group = $this->getGroup();
00168 if ( !$group ) {
00169 $warning = "MessageIndex is out of date – refers to unknown group {$groups[0]}. ";
00170 $warning .= "Doing a rebuild.";
00171 wfWarn( $warning );
00172 MessageIndexRebuildJob::newJob()->run();
00173
00174 return false;
00175 }
00176
00177 return true;
00178 }
00179
00184 public function getTitle() {
00185 return $this->title;
00186 }
00187
00194 public static function hasFuzzyString( $text ) {
00195 return strpos( $text, TRANSLATE_FUZZY ) !== false;
00196 }
00197
00202 public function isFuzzy() {
00203 $dbr = wfGetDB( DB_SLAVE );
00204
00205 $tables = array( 'page', 'revtag' );
00206 $field = 'rt_type';
00207 $conds = array(
00208 'page_namespace' => $this->title->getNamespace(),
00209 'page_title' => $this->title->getDBkey(),
00210 'rt_type' => RevTag::getType( 'fuzzy' ),
00211 'page_id=rt_page',
00212 'page_latest=rt_revision'
00213 );
00214
00215 $res = $dbr->selectField( $tables, $field, $conds, __METHOD__ );
00216
00217 return $res !== false;
00218 }
00219 }