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