MediaWiki  REL1_24
TitleValue.php
Go to the documentation of this file.
00001 <?php
00036 class TitleValue {
00040     protected $namespace;
00041 
00045     protected $dbkey;
00046 
00050     protected $fragment;
00051 
00068     public function __construct( $namespace, $dbkey, $fragment = '' ) {
00069         if ( !is_int( $namespace ) ) {
00070             throw new InvalidArgumentException( '$namespace must be an integer' );
00071         }
00072 
00073         if ( !is_string( $dbkey ) ) {
00074             throw new InvalidArgumentException( '$dbkey must be a string' );
00075         }
00076 
00077         // Sanity check, no full validation or normalization applied here!
00078         if ( preg_match( '/^_|[ \r\n\t]|_$/', $dbkey ) ) {
00079             throw new InvalidArgumentException( '$dbkey must be a valid DB key: ' . $dbkey );
00080         }
00081 
00082         if ( !is_string( $fragment ) ) {
00083             throw new InvalidArgumentException( '$fragment must be a string' );
00084         }
00085 
00086         if ( $dbkey === '' ) {
00087             throw new InvalidArgumentException( '$dbkey must not be empty' );
00088         }
00089 
00090         $this->namespace = $namespace;
00091         $this->dbkey = $dbkey;
00092         $this->fragment = $fragment;
00093     }
00094 
00098     public function getNamespace() {
00099         return $this->namespace;
00100     }
00101 
00105     public function getFragment() {
00106         return $this->fragment;
00107     }
00108 
00115     public function getDBkey() {
00116         return $this->dbkey;
00117     }
00118 
00130     public function getText() {
00131         return str_replace( '_', ' ', $this->getDBkey() );
00132     }
00133 
00141     public function createFragmentTitle( $fragment ) {
00142         return new TitleValue( $this->namespace, $this->dbkey, $fragment );
00143     }
00144 
00152     public function __toString() {
00153         $name = $this->namespace . ':' . $this->dbkey;
00154 
00155         if ( $this->fragment !== '' ) {
00156             $name .= '#' . $this->fragment;
00157         }
00158 
00159         return $name;
00160     }
00161 }