| Index: trunk/extensions/Translate/utils/RevTag.php |
| — | — | @@ -0,0 +1,97 @@ |
| | 2 | +<?php |
| | 3 | +/** |
| | 4 | + * Code related to revtag database table |
| | 5 | + * |
| | 6 | + * @file |
| | 7 | + * @author Niklas Laxström |
| | 8 | + * @copyright Copyright © 2011 Niklas Laxström |
| | 9 | + * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later |
| | 10 | + */ |
| | 11 | + |
| | 12 | +/** |
| | 13 | + * Abstraction for revtag table to handle new and old schemas during migration. |
| | 14 | + */ |
| | 15 | +class RevTag { |
| | 16 | + protected static $schema = false; |
| | 17 | + |
| | 18 | + /** |
| | 19 | + * Determines the schema version. |
| | 20 | + */ |
| | 21 | + public static function checkSchema() { |
| | 22 | + if ( self::$schema !== false ) { |
| | 23 | + return self::$schema; |
| | 24 | + } else { |
| | 25 | + $dbr = wfGetDB( DB_SLAVE ); |
| | 26 | + if ( $dbr->tableExists( 'revtag_type' ) ) { |
| | 27 | + return self::$schema = 1; |
| | 28 | + } else { |
| | 29 | + return self::$schema = 2; |
| | 30 | + } |
| | 31 | + } |
| | 32 | + } |
| | 33 | + |
| | 34 | + /** |
| | 35 | + * Returns value suitable for rt_type field. |
| | 36 | + * @param $tag string tag name |
| | 37 | + * return int|string |
| | 38 | + */ |
| | 39 | + public static function getType( $tag ) { |
| | 40 | + if ( self::checkSchema() === 2 ) { |
| | 41 | + return $tag; |
| | 42 | + } |
| | 43 | + |
| | 44 | + $tags = self::loadTags(); |
| | 45 | + |
| | 46 | + if ( isset( $tags[$tag] ) ) { |
| | 47 | + return $tags[$tag]; |
| | 48 | + } else { |
| | 49 | + throw new MWException( "Unknown revtag $tag. Known are " . implode( ', ', array_keys( $tags ) ) ); |
| | 50 | + } |
| | 51 | + } |
| | 52 | + |
| | 53 | + |
| | 54 | + /** |
| | 55 | + * Converts rt_type field back to the tag name. |
| | 56 | + * @param $tag rt_type value |
| | 57 | + * @return string |
| | 58 | + */ |
| | 59 | + public static function typeToTag( $tag ) { |
| | 60 | + if ( self::checkSchema() === 2 ) { |
| | 61 | + return $tag; |
| | 62 | + } |
| | 63 | + |
| | 64 | + $tags = self::loadTags(); |
| | 65 | + $tags = array_flip( $tags ); |
| | 66 | + |
| | 67 | + if ( isset( $tags[$tag] ) ) { |
| | 68 | + return $tags[$tag]; |
| | 69 | + } else { |
| | 70 | + throw new MWException( "Unknown revtag type $tag. Known are " . implode( ', ', array_keys( $tags ) ) ); |
| | 71 | + } |
| | 72 | + } |
| | 73 | + |
| | 74 | + /** |
| | 75 | + * Loads the list of tags from database using the old schema |
| | 76 | + * return array tag names => tag id |
| | 77 | + */ |
| | 78 | + protected static function loadTags() { |
| | 79 | + static $tags = null; |
| | 80 | + if ( $tags === null ) { |
| | 81 | + $tags = array(); |
| | 82 | + |
| | 83 | + $dbr = wfGetDB( DB_SLAVE ); |
| | 84 | + $res = $dbr->select( |
| | 85 | + 'revtag_type', |
| | 86 | + array( 'rtt_name', 'rtt_id' ), |
| | 87 | + array(), |
| | 88 | + __METHOD__ |
| | 89 | + ); |
| | 90 | + |
| | 91 | + foreach ( $res as $row ) { |
| | 92 | + $tags[$row->rtt_name] = $row->rtt_id; |
| | 93 | + } |
| | 94 | + } |
| | 95 | + return $tags; |
| | 96 | + } |
| | 97 | + |
| | 98 | +} |
| Property changes on: trunk/extensions/Translate/utils/RevTag.php |
| ___________________________________________________________________ |
| Added: svn:eol-style |
| 1 | 99 | + native |