MediaWiki  REL1_24
MediaWikiTitleCodec.php
Go to the documentation of this file.
00001 <?php
00035 class MediaWikiTitleCodec implements TitleFormatter, TitleParser {
00039     protected $language;
00040 
00044     protected $genderCache;
00045 
00049     protected $localInterwikis;
00050 
00056     public function __construct( Language $language, GenderCache $genderCache,
00057         $localInterwikis = array()
00058     ) {
00059         $this->language = $language;
00060         $this->genderCache = $genderCache;
00061         $this->localInterwikis = (array)$localInterwikis;
00062     }
00063 
00073     public function getNamespaceName( $namespace, $text ) {
00074         if ( $this->language->needsGenderDistinction() &&
00075             MWNamespace::hasGenderDistinction( $namespace )
00076         ) {
00077 
00078             //NOTE: we are assuming here that the title text is a user name!
00079             $gender = $this->genderCache->getGenderOf( $text, __METHOD__ );
00080             $name = $this->language->getGenderNsText( $namespace, $gender );
00081         } else {
00082             $name = $this->language->getNsText( $namespace );
00083         }
00084 
00085         if ( $name === false ) {
00086             throw new InvalidArgumentException( 'Unknown namespace ID: ' . $namespace );
00087         }
00088 
00089         return $name;
00090     }
00091 
00103     public function formatTitle( $namespace, $text, $fragment = '' ) {
00104         if ( $namespace !== false ) {
00105             $namespace = $this->getNamespaceName( $namespace, $text );
00106 
00107             if ( $namespace !== '' ) {
00108                 $text = $namespace . ':' . $text;
00109             }
00110         }
00111 
00112         if ( $fragment !== '' ) {
00113             $text = $text . '#' . $fragment;
00114         }
00115 
00116         $text = str_replace( '_', ' ', $text );
00117 
00118         return $text;
00119     }
00120 
00131     public function parseTitle( $text, $defaultNamespace ) {
00132         // NOTE: this is an ugly cludge that allows this class to share the
00133         // code for parsing with the old Title class. The parser code should
00134         // be refactored to avoid this.
00135         $parts = $this->splitTitleString( $text, $defaultNamespace );
00136 
00137         // Interwiki links are not supported by TitleValue
00138         if ( $parts['interwiki'] !== '' ) {
00139             throw new MalformedTitleException( 'Title must not contain an interwiki prefix: ' . $text );
00140         }
00141 
00142         // Relative fragment links are not supported by TitleValue
00143         if ( $parts['dbkey'] === '' ) {
00144             throw new MalformedTitleException( 'Title must not be empty: ' . $text );
00145         }
00146 
00147         return new TitleValue( $parts['namespace'], $parts['dbkey'], $parts['fragment'] );
00148     }
00149 
00157     public function getText( TitleValue $title ) {
00158         return $this->formatTitle( false, $title->getText(), '' );
00159     }
00160 
00168     public function getPrefixedText( TitleValue $title ) {
00169         return $this->formatTitle( $title->getNamespace(), $title->getText(), '' );
00170     }
00171 
00179     public function getFullText( TitleValue $title ) {
00180         return $this->formatTitle( $title->getNamespace(), $title->getText(), $title->getFragment() );
00181     }
00182 
00203     public function splitTitleString( $text, $defaultNamespace = NS_MAIN ) {
00204         $dbkey = str_replace( ' ', '_', $text );
00205 
00206         # Initialisation
00207         $parts = array(
00208             'interwiki' => '',
00209             'local_interwiki' => false,
00210             'fragment' => '',
00211             'namespace' => $defaultNamespace,
00212             'dbkey' => $dbkey,
00213             'user_case_dbkey' => $dbkey,
00214         );
00215 
00216         # Strip Unicode bidi override characters.
00217         # Sometimes they slip into cut-n-pasted page titles, where the
00218         # override chars get included in list displays.
00219         $dbkey = preg_replace( '/\xE2\x80[\x8E\x8F\xAA-\xAE]/S', '', $dbkey );
00220 
00221         # Clean up whitespace
00222         # Note: use of the /u option on preg_replace here will cause
00223         # input with invalid UTF-8 sequences to be nullified out in PHP 5.2.x,
00224         # conveniently disabling them.
00225         $dbkey = preg_replace(
00226             '/[ _\xA0\x{1680}\x{180E}\x{2000}-\x{200A}\x{2028}\x{2029}\x{202F}\x{205F}\x{3000}]+/u',
00227             '_',
00228             $dbkey
00229         );
00230         $dbkey = trim( $dbkey, '_' );
00231 
00232         if ( strpos( $dbkey, UTF8_REPLACEMENT ) !== false ) {
00233             # Contained illegal UTF-8 sequences or forbidden Unicode chars.
00234             throw new MalformedTitleException( 'Bad UTF-8 sequences found in title: ' . $text );
00235         }
00236 
00237         $parts['dbkey'] = $dbkey;
00238 
00239         # Initial colon indicates main namespace rather than specified default
00240         # but should not create invalid {ns,title} pairs such as {0,Project:Foo}
00241         if ( $dbkey !== '' && ':' == $dbkey[0] ) {
00242             $parts['namespace'] = NS_MAIN;
00243             $dbkey = substr( $dbkey, 1 ); # remove the colon but continue processing
00244             $dbkey = trim( $dbkey, '_' ); # remove any subsequent whitespace
00245         }
00246 
00247         if ( $dbkey == '' ) {
00248             throw new MalformedTitleException( 'Empty title: ' . $text );
00249         }
00250 
00251         # Namespace or interwiki prefix
00252         $prefixRegexp = "/^(.+?)_*:_*(.*)$/S";
00253         do {
00254             $m = array();
00255             if ( preg_match( $prefixRegexp, $dbkey, $m ) ) {
00256                 $p = $m[1];
00257                 if ( ( $ns = $this->language->getNsIndex( $p ) ) !== false ) {
00258                     # Ordinary namespace
00259                     $dbkey = $m[2];
00260                     $parts['namespace'] = $ns;
00261                     # For Talk:X pages, check if X has a "namespace" prefix
00262                     if ( $ns == NS_TALK && preg_match( $prefixRegexp, $dbkey, $x ) ) {
00263                         if ( $this->language->getNsIndex( $x[1] ) ) {
00264                             # Disallow Talk:File:x type titles...
00265                             throw new MalformedTitleException( 'Bad namespace prefix: ' . $text );
00266                         } elseif ( Interwiki::isValidInterwiki( $x[1] ) ) {
00267                             //TODO: get rid of global state!
00268                             # Disallow Talk:Interwiki:x type titles...
00269                             throw new MalformedTitleException( 'Interwiki prefix found in title: ' . $text );
00270                         }
00271                     }
00272                 } elseif ( Interwiki::isValidInterwiki( $p ) ) {
00273                     # Interwiki link
00274                     $dbkey = $m[2];
00275                     $parts['interwiki'] = $this->language->lc( $p );
00276 
00277                     # Redundant interwiki prefix to the local wiki
00278                     foreach ( $this->localInterwikis as $localIW ) {
00279                         if ( 0 == strcasecmp( $parts['interwiki'], $localIW ) ) {
00280                             if ( $dbkey == '' ) {
00281                                 # Empty self-links should point to the Main Page, to ensure
00282                                 # compatibility with cross-wiki transclusions and the like.
00283                                 $mainPage = Title::newMainPage();
00284                                 return array(
00285                                     'interwiki' => $mainPage->getInterwiki(),
00286                                     'local_interwiki' => true,
00287                                     'fragment' => $mainPage->getFragment(),
00288                                     'namespace' => $mainPage->getNamespace(),
00289                                     'dbkey' => $mainPage->getDBkey(),
00290                                     'user_case_dbkey' => $mainPage->getUserCaseDBKey()
00291                                 );
00292                             }
00293                             $parts['interwiki'] = '';
00294                             # local interwikis should behave like initial-colon links
00295                             $parts['local_interwiki'] = true;
00296 
00297                             # Do another namespace split...
00298                             continue 2;
00299                         }
00300                     }
00301 
00302                     # If there's an initial colon after the interwiki, that also
00303                     # resets the default namespace
00304                     if ( $dbkey !== '' && $dbkey[0] == ':' ) {
00305                         $parts['namespace'] = NS_MAIN;
00306                         $dbkey = substr( $dbkey, 1 );
00307                     }
00308                 }
00309                 # If there's no recognized interwiki or namespace,
00310                 # then let the colon expression be part of the title.
00311             }
00312             break;
00313         } while ( true );
00314 
00315         $fragment = strstr( $dbkey, '#' );
00316         if ( false !== $fragment ) {
00317             $parts['fragment'] = str_replace( '_', ' ', substr( $fragment, 1 ) );
00318             $dbkey = substr( $dbkey, 0, strlen( $dbkey ) - strlen( $fragment ) );
00319             # remove whitespace again: prevents "Foo_bar_#"
00320             # becoming "Foo_bar_"
00321             $dbkey = preg_replace( '/_*$/', '', $dbkey );
00322         }
00323 
00324         # Reject illegal characters.
00325         $rxTc = Title::getTitleInvalidRegex();
00326         if ( preg_match( $rxTc, $dbkey ) ) {
00327             throw new MalformedTitleException( 'Illegal characters found in title: ' . $text );
00328         }
00329 
00330         # Pages with "/./" or "/../" appearing in the URLs will often be un-
00331         # reachable due to the way web browsers deal with 'relative' URLs.
00332         # Also, they conflict with subpage syntax.  Forbid them explicitly.
00333         if (
00334             strpos( $dbkey, '.' ) !== false &&
00335             (
00336                 $dbkey === '.' || $dbkey === '..' ||
00337                 strpos( $dbkey, './' ) === 0 ||
00338                 strpos( $dbkey, '../' ) === 0 ||
00339                 strpos( $dbkey, '/./' ) !== false ||
00340                 strpos( $dbkey, '/../' ) !== false ||
00341                 substr( $dbkey, -2 ) == '/.' ||
00342                 substr( $dbkey, -3 ) == '/..'
00343             )
00344         ) {
00345             throw new MalformedTitleException( 'Bad title: ' . $text );
00346         }
00347 
00348         # Magic tilde sequences? Nu-uh!
00349         if ( strpos( $dbkey, '~~~' ) !== false ) {
00350             throw new MalformedTitleException( 'Bad title: ' . $text );
00351         }
00352 
00353         # Limit the size of titles to 255 bytes. This is typically the size of the
00354         # underlying database field. We make an exception for special pages, which
00355         # don't need to be stored in the database, and may edge over 255 bytes due
00356         # to subpage syntax for long titles, e.g. [[Special:Block/Long name]]
00357         if (
00358             ( $parts['namespace'] != NS_SPECIAL && strlen( $dbkey ) > 255 )
00359             || strlen( $dbkey ) > 512
00360         ) {
00361             throw new MalformedTitleException( 'Title too long: ' . substr( $dbkey, 0, 255 ) . '...' );
00362         }
00363 
00364         # Normally, all wiki links are forced to have an initial capital letter so [[foo]]
00365         # and [[Foo]] point to the same place.  Don't force it for interwikis, since the
00366         # other site might be case-sensitive.
00367         $parts['user_case_dbkey'] = $dbkey;
00368         if ( $parts['interwiki'] === '' ) {
00369             $dbkey = Title::capitalize( $dbkey, $parts['namespace'] );
00370         }
00371 
00372         # Can't make a link to a namespace alone... "empty" local links can only be
00373         # self-links with a fragment identifier.
00374         if ( $dbkey == '' && $parts['interwiki'] === '' ) {
00375             if ( $parts['namespace'] != NS_MAIN ) {
00376                 throw new MalformedTitleException( 'Empty title: ' . $text );
00377             }
00378         }
00379 
00380         // Allow IPv6 usernames to start with '::' by canonicalizing IPv6 titles.
00381         // IP names are not allowed for accounts, and can only be referring to
00382         // edits from the IP. Given '::' abbreviations and caps/lowercaps,
00383         // there are numerous ways to present the same IP. Having sp:contribs scan
00384         // them all is silly and having some show the edits and others not is
00385         // inconsistent. Same for talk/userpages. Keep them normalized instead.
00386         if ( $parts['namespace'] == NS_USER || $parts['namespace'] == NS_USER_TALK ) {
00387             $dbkey = IP::sanitizeIP( $dbkey );
00388         }
00389 
00390         // Any remaining initial :s are illegal.
00391         if ( $dbkey !== '' && ':' == $dbkey[0] ) {
00392             throw new MalformedTitleException( 'Title must not start with a colon: ' . $text );
00393         }
00394 
00395         # Fill fields
00396         $parts['dbkey'] = $dbkey;
00397 
00398         return $parts;
00399     }
00400 }