DB error log
[lhc/web/wiklou.git] / includes / Title.php
index 86262db..35ae3e8 100644 (file)
@@ -88,13 +88,13 @@ class Title {
        # From a URL-encoded title
        /* static */ function newFromURL( $url )
        {
-               global $wgLang, $wgServer;
+               global $wgLang, $wgServer, $wgIsMySQL, $wgIsPg;
                $t = new Title();
-               $s = urldecode( $url ); # This is technically wrong, as anything
-                                                               # we've gotten is already decoded by PHP.
-                                                               # Kept for backwards compatibility with
-                                                               # buggy URLs we had for a while...
-               $s = $url;
+               
+               # For compatibility with old buggy URLs. "+" is not valid in titles,
+               # but some URLs used it as a space replacement and they still come
+               # from some external search tools.
+               $s = str_replace( "+", " ", $url );
                
                # For links that came from outside, check for alternate/legacy
                # character encoding.
@@ -109,14 +109,20 @@ class Title {
                
                $t->mDbkeyform = str_replace( " ", "_", $s );
                if( $t->secureAndSplit() ) {
-
                        # check that lenght of title is < cur_title size
-                       $sql = "SHOW COLUMNS FROM cur LIKE \"cur_title\";";
-                       $cur_title_object = wfFetchObject(wfQuery( $sql, DB_READ ));
+                       if ($wgIsMySQL) {
+                               $sql = "SHOW COLUMNS FROM cur LIKE \"cur_title\";";
+                               $cur_title_object = wfFetchObject(wfQuery( $sql, DB_READ ));
 
-                       preg_match( "/\((.*)\)/", $cur_title_object->Type, $cur_title_size);
+                               preg_match( "/\((.*)\)/", $cur_title_object->Type, $cur_title_type);
+                               $cur_title_size=$cur_title_type[1];
+                       } else {
+                               /* midom:FIXME pg_field_type does not return varchar length
+                                  assume 255 */
+                               $cur_title_size=255;
+                       }
 
-                       if (strlen($t->mDbkeyform) > $cur_title_size[1] ) {
+                       if (strlen($t->mDbkeyform) > $cur_title_size ) {
                                return NULL;
                        }
 
@@ -187,7 +193,7 @@ class Title {
                # Theoretically 0x80-0x9F of ISO 8859-1 should be disallowed, but
                # this breaks interlanguage links
                
-               $set = " %!\"$&'()*,\\-.\\/0-9:;<=>?@A-Z\\\\^_`a-z{}~\\x80-\\xFF";
+               $set = " %!\"$&'()*,\\-.\\/0-9:;=?@A-Z\\\\^_`a-z{}~\\x80-\\xFF";
                return $set;
        }
        
@@ -229,8 +235,7 @@ class Title {
        # The URL contains $1, which is replaced by the title
        function getInterwikiLink( $key )
        {       
-               global $wgMemc, $wgDBname, $wgInterwikiExpiry;
-               static $wgTitleInterwikiCache = array();
+               global $wgMemc, $wgDBname, $wgInterwikiExpiry, $wgTitleInterwikiCache;
 
                $k = "$wgDBname:interwiki:$key";
 
@@ -370,9 +375,6 @@ class Title {
                $n = $wgLang->getNsText( $this->mNamespace );
                if ( "" != $n ) { $n .= ":"; }
                $u = str_replace( "$1", $n . $this->mUrlform, $p );
-               if ( "" != $this->mFragment ) {
-                       $u .= "#" . wfUrlencode( $this->mFragment );
-               }
                return $u;
        }
 
@@ -487,15 +489,20 @@ class Title {
        # Can $wgUser edit this page?
        function userCanEdit()
        {
-
+               global $wgUser;
                if ( -1 == $this->mNamespace ) { return false; }
+               if ( NS_MEDIAWIKI == $this->mNamespace && !$wgUser->isSysop() ) { return false; }
                # if ( 0 == $this->getArticleID() ) { return false; }
                if ( $this->mDbkeyform == "_" ) { return false; }
+               # protect global styles and js
+               if ( NS_MEDIAWIKI == $this->mNamespace 
+                    && preg_match("/\\.(css|js)$/", $this->mTextform )
+                    && !$wgUser->isSysop() )
+               { return false; }
                //if ( $this->isCssJsSubpage() and !$this->userCanEditCssJsSubpage() ) { return false; }
                # protect css/js subpages of user pages
                # XXX: this might be better using restrictions
                # XXX: Find a way to work around the php bug that prevents using $this->userCanEditCssJsSubpage() from working
-               global $wgUser;
                if( Namespace::getUser() == $this->mNamespace
                        and preg_match("/\\.(css|js)$/", $this->mTextform )
                        and !$wgUser->isSysop()
@@ -521,7 +528,7 @@ class Title {
                if( in_array( $name, $wgWhitelistRead ) ) return true;
                
                # Compatibility with old settings
-               if( $this->getNamespace() == NS_ARTICLE ) {
+               if( $this->getNamespace() == NS_MAIN ) {
                        if( in_array( ":" . $name, $wgWhitelistRead ) ) return true;
                }
                return false;
@@ -670,7 +677,7 @@ class Title {
                        $this->mNamespace = NS_MAIN;
                } else {
                        # Namespace or interwiki prefix
-                       if ( preg_match( "/^((?:i|x|[a-z]{2,3})(?:-[a-z0-9]+)?|[A-Za-z0-9_\\x80-\\xff]+?)_*:_*(.*)$/", $t, $m ) ) {
+                       if ( preg_match( "/^(.+?)_*:_*(.*)$/", $t, $m ) ) {
                                #$p = strtolower( $m[1] );
                                $p = $m[1];
                                $lowerNs = strtolower( $p );
@@ -718,8 +725,13 @@ class Title {
                        return false;
                }
                
-               # "." and ".." conflict with the directories of those names
-               if ( $r === "." || $r === ".." ) {
+               # "." and ".." conflict with the directories of those namesa
+               if ( strpos( $r, "." ) !== false &&
+                    ( $r === "." || $r === ".." ||
+                      strpos( $r, "./" ) === 0 ||
+                      strpos( $r, "/./" !== false ) ||
+                      strpos( $r, "/../" !== false ) ) )
+               {
                        return false;
                }
 
@@ -758,9 +770,10 @@ class Title {
                $retVal = array();
                if ( wfNumRows( $res ) ) {
                        while ( $row = wfFetchObject( $res ) ) {
-                               $titleObj = Title::makeTitle( $row->cur_namespace, $row->cur_title );
-                               $wgLinkCache->addGoodLink( $row->cur_id, $titleObj->getPrefixedDBkey() );
-                               $retVal[] = $titleObj;
+                               if ( $titleObj = Title::makeTitle( $row->cur_namespace, $row->cur_title ) ) {
+                                       $wgLinkCache->addGoodLink( $row->cur_id, $titleObj->getPrefixedDBkey() );
+                                       $retVal[] = $titleObj;
+                               }
                        }
                }
                wfFreeResult( $res );
@@ -779,7 +792,7 @@ class Title {
                if ( wfNumRows( $res ) ) {
                        while ( $row = wfFetchObject( $res ) ) {
                                $titleObj = Title::makeTitle( $row->cur_namespace, $row->cur_title );
-                               $wgLinkCache->addGoodLink( $titleObj->getPrefixedDBkey(), $row->cur_id );
+                               $wgLinkCache->addGoodLink( $row->cur_id, $titleObj->getPrefixedDBkey() );
                                $retVal[] = $titleObj;
                        }
                }
@@ -929,7 +942,7 @@ class Title {
                        $fname
                );
                
-               RecentChange::notifyMove( $now, $this, $nt, $wgUser, $comment );
+               RecentChange::notifyMoveOverRedirect( $now, $this, $nt, $wgUser, $comment );
 
                # Swap links
                
@@ -977,7 +990,11 @@ class Title {
                wfQuery( $sql, DB_WRITE, $fname );
                $sql = "INSERT INTO links (l_from,l_to) VALUES ({$newid},{$oldid})";
                wfQuery( $sql, DB_WRITE, $fname );
-
+               
+               # Clear linkscc
+               LinkCache::linksccClearLinksTo( $oldid );
+               LinkCache::linksccClearLinksTo( $newid );
+               
                # Purge squid
                if ( $wgUseSquid ) {
                        $urls = array_merge( $nt->getSquidURLs(), $this->getSquidURLs() );
@@ -1044,14 +1061,16 @@ class Title {
                        ), $fname
                );
                
-               # Miscellaneous updates
+               # Record in RC
+               RecentChange::notifyMoveToNew( $now, $this, $nt, $wgUser, $comment );
 
-               RecentChange::notifyMove( $now, $this, $nt, $wgUser, $comment );
+               # Purge squid and linkscc as per article creation
                Article::onArticleCreate( $nt );
 
                # Any text links to the old title must be reassigned to the redirect
                $sql = "UPDATE links SET l_to={$newid} WHERE l_to={$oldid}";
                wfQuery( $sql, DB_WRITE, $fname );
+               LinkCache::linksccClearLinksTo( $oldid );
 
                # Record the just-created redirect's linking to the page
                $sql = "INSERT INTO links (l_from,l_to) VALUES ({$newid},{$oldid})";
@@ -1155,5 +1174,84 @@ class Title {
                return true;
        }
        
+       # Get categories to wich belong this title and return an array of
+       # categories names.
+       function getParentCategories( )
+       {
+               global $wgLang,$wgUser;
+               
+               #$titlekey = wfStrencode( $this->getArticleID() );
+               $titlekey = $this->getArticleId();
+               $cns = Namespace::getCategory();
+               $sk =& $wgUser->getSkin();
+               $parents = array();
+               
+               # get the parents categories of this title from the database
+               $sql = "SELECT DISTINCT cur_id FROM cur,categorylinks
+                       WHERE cl_from='$titlekey' AND cl_to=cur_title AND cur_namespace='$cns'
+                               ORDER BY cl_sortkey" ;
+               $res = wfQuery ( $sql, DB_READ ) ;
+               
+               if(wfNumRows($res) > 0) {
+                       while ( $x = wfFetchObject ( $res ) ) $data[] = $x ;
+                       wfFreeResult ( $res ) ;
+               } else {
+                       $data = '';
+               }
+               return $data;
+       }
+       
+       # will get the parents and grand-parents
+       # TODO : not sure what's happening when a loop happen like:
+       #       Encyclopedia > Astronomy > Encyclopedia
+       function getAllParentCategories(&$stack)
+       {
+               global $wgUser,$wgLang;
+               $result = '';
+               
+               # getting parents
+               $parents = $this->getParentCategories( );
+
+               if($parents == '')
+               {
+                       # The current element has no more parent so we dump the stack
+                       # and make a clean line of categories
+                       $sk =& $wgUser->getSkin() ;
+
+                       foreach ( array_reverse($stack) as $child => $parent )
+                       {
+                               # make a link of that parent
+                               $result .= $sk->makeLink($wgLang->getNSText ( Namespace::getCategory() ).":".$parent,$parent);
+                               $result .= ' &gt; ';
+                               $lastchild = $child;
+                       }
+                       # append the last child.
+                       # TODO : We should have a last child unless there is an error in the
+                       # "categorylinks" table.
+                       if(isset($lastchild)) { $result .= $lastchild; }
+                       
+                       $result .= "<br/>\n";
+                       
+                       # now we can empty the stack
+                       $stack = array();
+                       
+               } else {
+                       # look at parents of current category
+                       foreach($parents as $parent)
+                       {
+                               # create a title object for the parent
+                               $tpar = Title::newFromID($parent->cur_id);
+                               # add it to the stack
+                               $stack[$this->getText()] = $tpar->getText();
+                               # grab its parents
+                               $result .= $tpar->getAllParentCategories($stack);
+                       }
+               }
+
+               if(isset($result)) { return $result; }
+               else { return ''; };
+       }
+       
+       
 }
 ?>