Add back the slower version of Title::makeTitle as Title::makeTitleSafe()
authorBrion Vibber <brion@users.mediawiki.org>
Sun, 22 Aug 2004 23:55:36 +0000 (23:55 +0000)
committerBrion Vibber <brion@users.mediawiki.org>
Sun, 22 Aug 2004 23:55:36 +0000 (23:55 +0000)
and use it in a few places where user names, images, or messages are being
used to create titles.

includes/DifferenceEngine.php
includes/Image.php
includes/OutputPage.php
includes/Skin.php
includes/SpecialBooksources.php
includes/Title.php
index.php

index 744dbe7..1bad821 100644 (file)
@@ -60,10 +60,10 @@ class DifferenceEngine {
                $this->mOldComment = $sk->formatComment($this->mOldComment);
                $this->mNewComment = $sk->formatComment($this->mNewComment);
 
-               $oldUserLink = $sk->makeLinkObj( Title::makeTitle( NS_USER, $this->mOldUser ), $this->mOldUser );
-               $newUserLink = $sk->makeLinkObj( Title::makeTitle( NS_USER, $this->mNewUser ), $this->mNewUser );
-               $oldUTLink = $sk->makeLinkObj( Title::makeTitle( NS_USER_TALK, $this->mOldUser ), $talk );
-               $newUTLink = $sk->makeLinkObj( Title::makeTitle( NS_USER_TALK, $this->mNewUser ), $talk );
+               $oldUserLink = $sk->makeLinkObj( Title::makeTitleSafe( NS_USER, $this->mOldUser ), $this->mOldUser );
+               $newUserLink = $sk->makeLinkObj( Title::makeTitleSafe( NS_USER, $this->mNewUser ), $this->mNewUser );
+               $oldUTLink = $sk->makeLinkObj( Title::makeTitleSafe( NS_USER_TALK, $this->mOldUser ), $talk );
+               $newUTLink = $sk->makeLinkObj( Title::makeTitleSafe( NS_USER_TALK, $this->mNewUser ), $talk );
                $oldContribs = $sk->makeKnownLinkObj( Title::makeTitle( NS_SPECIAL, 'Contributions' ), $contribs,
                        'target=' . urlencode($this->mOldUser) );
                $newContribs = $sk->makeKnownLinkObj( Title::makeTitle( NS_SPECIAL, 'Contributions' ), $contribs,
index ae2ca8e..3b9ff5d 100644 (file)
@@ -26,7 +26,7 @@ class Image
                global $wgUploadDirectory;
 
                $this->name      = $name;
-               $this->title     = Title::makeTitle( Namespace::getImage(), $this->name );
+               $this->title     = Title::makeTitleSafe( NS_IMAGE, $this->name );
                //$this->imagePath = wfImagePath( $name );
                $hash            = md5( $this->title->getDBkey() );
                $this->imagePath = $wgUploadDirectory . '/' . $hash{0} . '/' .substr( $hash, 0, 2 ) . "/{$name}";
@@ -377,7 +377,7 @@ function wfRecordUpload( $name, $oldver, $size, $desc, $copyStatus = "", $source
                        'img_user_text' => $wgUser->getName(),
                ), $fname, 'IGNORE'
        );
-       $descTitle = Title::makeTitle( NS_IMAGE, $name );
+       $descTitle = Title::makeTitleSafe( NS_IMAGE, $name );
 
        if ( $dbw->affectedRows() ) {
                # Successfully inserted, this is a new image
index bfa3989..6e3cc1b 100644 (file)
@@ -829,7 +829,7 @@ class OutputPage {
                        foreach ( $namespaces as $key => $val ) {
                                # Make title object
                                $dbk = $dbkeys[$key];
-                               $title = $titles[$key] = Title::makeTitle( $val, $dbk );
+                               $title = $titles[$key] = Title::makeTitleSafe( $val, $dbk );
 
                                # Skip invalid entries.
                                # Result will be ugly, but prevents crash.
index a042e7e..1ee2524 100644 (file)
@@ -1696,7 +1696,7 @@ class Skin {
        }
        # this can be passed the NS number as defined in Language.php
        /*static*/ function makeNSUrl( $name, $urlaction='', $namespace=0 ) {
-               $title = Title::makeTitle( $namespace, $name );
+               $title = Title::makeTitleSafe( $namespace, $name );
                $this->checkTitle($title, $name);
                return $title->getLocalURL( $urlaction );
        }
@@ -1770,7 +1770,7 @@ class Skin {
        }
 
        function makeImageLink( $name, $url, $alt = '' ) {
-               $nt = Title::makeTitle( Namespace::getImage(), $name );
+               $nt = Title::makeTitleSafe( NS_IMAGE, $name );
                return $this->makeImageLinkObj( $nt, $alt );
        }
 
@@ -1903,7 +1903,7 @@ class Skin {
        # $img is an Image object
        function makeThumbLinkObj( $img, $label = '', $align = 'right', $boxwidth = 180, $boxheight=false, $framed=false , $manual_thumb = "" ) {
                global $wgStylePath, $wgLang;
-               # $image = Title::makeTitle( Namespace::getImage(), $name );
+               # $image = Title::makeTitleSafe( NS_IMAGE, $name );
                $url  = $img->getURL();
 
                #$label = htmlspecialchars( $label );
@@ -1945,7 +1945,7 @@ class Skin {
 
                if ( $manual_thumb != '' ) # Use manually specified thumbnail
                {
-                       $manual_title = Title::makeTitle( Namespace::getImage(), $manual_thumb ); #new Title ( $manual_thumb ) ;
+                       $manual_title = Title::makeTitleSafe( NS_IMAGE, $manual_thumb ); #new Title ( $manual_thumb ) ;
                        $manual_img = Image::newFromTitle( $manual_title );
                        $thumbUrl = $manual_img->getURL();
                        if ( $manual_img->exists() )
@@ -1986,7 +1986,7 @@ class Skin {
        }
 
        function makeMediaLink( $name, $url, $alt = "" ) {
-               $nt = Title::makeTitle( Namespace::getMedia(), $name );
+               $nt = Title::makeTitleSafe( Namespace::getMedia(), $name );
                return $this->makeMediaLinkObj( $nt, $alt );
        }
 
index 13994c4..b193067 100644 (file)
@@ -41,7 +41,7 @@ class BookSourceList {
                
                # First, see if we have a custom list setup in
                # [[Wikipedia:Book sources]] or equivalent.
-               $bstitle = Title::makeTitle( NS_WIKIPEDIA, wfMsg( "booksources" ) );
+               $bstitle = Title::makeTitleSafe( NS_WIKIPEDIA, wfMsg( "booksources" ) );
                $dbr =& wfGetDB( DB_SLAVE );
                $bstext = $dbr->selectField( 'cur', 'cur_text', $bstitle->curCond(), $fname );
                if( $bstext ) { 
index 37873a4..f08d91e 100644 (file)
@@ -138,8 +138,8 @@ class Title {
        }
        
        # From a namespace index and a DB key.
-       # It's assumed that $ns and $title are *valid*, for instance
-       # when they came directly from the database.
+       # It's assumed that $ns and $title are *valid*, for instance when
+       # they came directly from the database or a special page name.
        /* static */ function &makeTitle( $ns, $title ) {
                $t =& new Title();
                $t->mInterwiki = '';
@@ -151,6 +151,19 @@ class Title {
                $t->mTextform = str_replace( '_', ' ', $title );
                return $t;
        }
+       
+       # From a namespace index and a DB key.
+       # These will be checked for validity, which is a bit slower
+       # than makeTitle() but safer for user-provided data.
+       /* static */ function makeTitleSafe( $ns, $title ) {
+               $t = new Title();
+               $t->mDbkeyform = Title::makeName( $ns, $title );
+               if( $t->secureAndSplit() ) {
+                       return $t;
+               } else {
+                       return NULL;
+               }
+       }
 
        /* static */ function newMainPage() {
                return Title::newFromText( wfMsg( 'mainpage' ) );
index ad36e52..9f8fa52 100644 (file)
--- a/index.php
+++ b/index.php
@@ -96,7 +96,7 @@ if( $search = $wgRequest->getText( 'search' ) ) {
        SpecialPage::executePath( $wgTitle );
 } else {
        if ( Namespace::getMedia() == $wgTitle->getNamespace() ) {
-               $wgTitle = Title::makeTitle( Namespace::getImage(), $wgTitle->getDBkey() );
+               $wgTitle = Title::makeTitle( NS_IMAGE, $wgTitle->getDBkey() );
        }
 
        switch( $wgTitle->getNamespace() ) {