5 /* private */ var $mTextform, $mUrlform, $mDbkeyform;
6 /* private */ var $mNamespace, $mInterwiki, $mFragment;
7 /* private */ var $mArticleID, $mRestrictions, $mRestrictionsLoaded;
9 /* private */ function Title()
11 $this->mInterwiki
= $this->mUrlform
=
12 $this->mTextform
= $this->mDbkeyform
= "";
13 $this->mArticleID
= -1;
14 $this->mNamespace
= 0;
15 $this->mRestrictionsLoaded
= false;
16 $this->mRestrictions
= array();
19 # Static factory methods
21 function newFromDBkey( $key )
24 $t->mDbkeyform
= $key;
25 if( $t->secureAndSplit() )
32 function newFromText( $text )
34 wfProfileIn( "Title::newFromText" );
36 # Note - mixing latin1 named entities and unicode numbered
37 # ones will result in a bad link.
38 $trans = get_html_translation_table( HTML_ENTITIES
);
39 $trans = array_flip( $trans );
40 $text = strtr( $text, $trans );
42 $text = wfMungeToUtf8( $text );
44 $text = urldecode( $text );
47 $t->mDbkeyform
= str_replace( " ", "_", $text );
48 if( $t->secureAndSplit() ) {
56 function newFromURL( $url )
58 global $wgLang, $wgServer, $HTTP_SERVER_VARS;
61 $s = urldecode( $url ); # This is technically wrong, as anything
62 # we've gotten is already decoded by PHP.
63 # Kept for backwards compatibility with
64 # buggy URLs we had for a while...
66 # For links that came from outside, check for alternate/legacy
68 if( strncmp($wgServer, $HTTP_SERVER_VARS["HTTP_REFERER"], strlen( $wgServer ) ) )
69 $s = $wgLang->checkTitleEncoding( $s );
71 $t->mDbkeyform
= str_replace( " ", "_", $s );
72 if( $t->secureAndSplit() ) {
79 function nameOf( $id )
81 $sql = "SELECT cur_namespace,cur_title FROM cur WHERE " .
83 $res = wfQuery( $sql, DB_READ
, "Article::nameOf" );
84 if ( 0 == wfNumRows( $res ) ) { return NULL; }
86 $s = wfFetchObject( $res );
87 $n = Title
::makeName( $s->cur_namespace
, $s->cur_title
);
94 global $wgInputEncoding;
95 if( $wgInputEncoding == "utf-8" ) {
96 return "-,.()' &;%!?_0-9A-Za-z\\/:\\x80-\\xFF";
98 # ISO 8859-* don't allow 0x80-0x9F
99 #return "-,.()' &;%!?_0-9A-Za-z\\/:\\xA0-\\xFF";
100 # But that breaks interlanguage links at the moment. Temporary:
101 return "-,.()' &;%!?_0-9A-Za-z\\/:\\x80-\\xFF";
105 function getInterwikiLink( $key )
107 global $wgMemc, $wgDBname;
108 $k = "$wgDBname:interwiki:$key";
109 $s = $wgMemc->get( $k );
110 if( $s !== false ) return $s->iw_url
;
112 $dkey = wfStrencode( $key );
113 $query = "SELECT iw_url FROM interwiki WHERE iw_prefix='$dkey'";
114 $res = wfQuery( $query, DB_READ
, "Title::getInterwikiLink" );
117 $s = wfFetchObject( $res );
122 $wgMemc->set( $k, $s );
126 function getText() { return $this->mTextform
; }
127 function getURL() { return $this->mUrlform
; }
128 function getDBkey() { return $this->mDbkeyform
; }
129 function getNamespace() { return $this->mNamespace
; }
130 function setNamespace( $n ) { $this->mNamespace
= $n; }
131 function getInterwiki() { return $this->mInterwiki
; }
132 function getFragment() { return $this->mFragment
; }
134 /* static */ function indexTitle( $ns, $title )
136 global $wgDBminWordLen, $wgLang;
138 $lc = SearchEngine
::legalSearchChars() . "&#;";
139 $t = $wgLang->stripForSearch( $title );
140 $t = preg_replace( "/[^{$lc}]+/", " ", $t );
141 $t = strtolower( $t );
144 $t = preg_replace( "/([{$lc}]+)'s( |$)/", "\\1 \\1's ", $t );
145 $t = preg_replace( "/([{$lc}]+)s'( |$)/", "\\1s ", $t );
147 $t = preg_replace( "/\\s+/", " ", $t );
149 if ( $ns == Namespace::getImage() ) {
150 $t = preg_replace( "/ (png|gif|jpg|jpeg|ogg)$/", "", $t );
155 function getIndexTitle()
157 return Title
::indexTitle( $this->mNamespace
, $this->mTextform
);
160 /* static */ function makeName( $ns, $title )
164 $n = $wgLang->getNsText( $ns );
165 if ( "" == $n ) { return $title; }
166 else { return "{$n}:{$title}"; }
169 /* static */ function makeTitle( $ns, $title )
172 $t->mDbkeyform
= Title
::makeName( $ns, $title );
173 if( $t->secureAndSplit() ) {
180 function getPrefixedDBkey()
182 $s = $this->prefix( $this->mDbkeyform
);
183 $s = str_replace( " ", "_", $s );
187 function getPrefixedText()
189 $s = $this->prefix( $this->mTextform
);
190 $s = str_replace( "_", " ", $s );
194 function getPrefixedURL()
196 $s = $this->prefix( $this->mDbkeyform
);
197 $s = str_replace( " ", "_", $s );
199 $s = urlencode ( $s ) ;
200 # Cleaning up URL to make it look nice -- is this safe?
201 $s = preg_replace( "/%3[Aa]/", ":", $s );
202 $s = preg_replace( "/%2[Ff]/", "/", $s );
203 $s = str_replace( "%28", "(", $s );
204 $s = str_replace( "%29", ")", $s );
208 function getFullURL()
210 global $wgLang, $wgArticlePath;
212 if ( "" == $this->mInterwiki
) {
215 $p = $this->getInterwikiLink( $this->mInterwiki
);
217 $n = $wgLang->getNsText( $this->mNamespace
);
218 if ( "" != $n ) { $n .= ":"; }
219 $u = str_replace( "$1", $n . $this->mUrlform
, $p );
220 if ( "" != $this->mFragment
) {
221 $u .= "#" . $this->mFragment
;
226 function getEditURL()
228 global $wgServer, $wgScript;
230 if ( "" != $this->mInterwiki
) { return ""; }
231 $s = wfLocalUrl( $this->getPrefixedURL(), "action=edit" );
236 function isExternal() { return ( "" != $this->mInterwiki
); }
238 function isProtected()
240 if ( -1 == $this->mNamespace
) { return true; }
241 $a = $this->getRestrictions();
242 if ( in_array( "sysop", $a ) ) { return true; }
248 if ( $this->mNamespace
!= Namespace::getWikipedia() ) {
251 if ( ( 0 == strcmp( wfMsg( "uploadlogpage" ), $this->mDbkeyform
) ) ||
252 ( 0 == strcmp( wfMsg( "dellogpage" ), $this->mDbkeyform
) ) ) {
258 function userIsWatching()
262 if ( -1 == $this->mNamespace
) { return false; }
263 if ( 0 == $wgUser->getID() ) { return false; }
265 return $wgUser->isWatched( $this );
268 function userCanEdit()
272 if ( -1 == $this->mNamespace
) { return false; }
273 # if ( 0 == $this->getArticleID() ) { return false; }
274 if ( $this->mDbkeyform
== "_" ) { return false; }
276 $ur = $wgUser->getRights();
277 foreach ( $this->getRestrictions() as $r ) {
278 if ( "" != $r && ( ! in_array( $r, $ur ) ) ) {
285 function getRestrictions()
287 $id = $this->getArticleID();
288 if ( 0 == $id ) { return array(); }
290 if ( ! $this->mRestrictionsLoaded
) {
291 $res = wfGetSQL( "cur", "cur_restrictions", "cur_id=$id" );
292 $this->mRestrictions
= explode( ",", trim( $res ) );
293 $this->mRestrictionsLoaded
= true;
295 return $this->mRestrictions
;
298 function isDeleted() {
299 $ns = $this->getNamespace();
300 $t = wfStrencode( $this->getDBkey() );
301 $sql = "SELECT COUNT(*) AS n FROM archive WHERE ar_namespace=$ns AND ar_title='$t'";
302 if( $res = wfQuery( $sql, DB_READ
) ) {
303 $s = wfFetchObject( $res );
309 function getArticleID()
313 if ( -1 != $this->mArticleID
) { return $this->mArticleID
; }
314 $this->mArticleID
= $wgLinkCache->addLink(
315 $this->getPrefixedDBkey() );
316 return $this->mArticleID
;
319 function resetArticleID( $newid )
322 $wgLinkCache->clearBadLink( $this->getPrefixedDBkey() );
324 if ( 0 == $newid ) { $this->mArticleID
= -1; }
325 else { $this->mArticleID
= $newid; }
326 $this->mRestrictionsLoaded
= false;
327 $this->mRestrictions
= array();
330 /* private */ function prefix( $name )
335 if ( "" != $this->mInterwiki
) {
336 $p = $this->mInterwiki
. ":";
338 if ( 0 != $this->mNamespace
) {
339 $p .= $wgLang->getNsText( $this->mNamespace
) . ":";
344 # Assumes that mDbkeyform has been set, and is urldecoded
345 # and uses undersocres, but not otherwise munged. This function
346 # removes illegal characters, splits off the winterwiki and
347 # namespace prefixes, sets the other forms, and canonicalizes
348 # everything. This one function is really at the core of
349 # Wiki--don't mess with it unless you're really sure you know
352 /* private */ function secureAndSplit()
354 global $wgLang, $wgLocalInterwiki;
355 wfProfileIn( "Title::secureAndSplit" );
357 $validNamespaces = $wgLang->getNamespaces();
358 unset( $validNamespaces[0] );
360 $this->mInterwiki
= $this->mFragment
= "";
361 $this->mNamespace
= 0;
363 $t = preg_replace( "/[\\s_]+/", "_", $this->mDbkeyform
);
364 if ( "_" == $t{0} ) { $t = substr( $t, 1 ); }
366 if ( $l && ( "_" == $t{$l-1} ) ) { $t = substr( $t, 0, $l-1 ); }
372 $this->mDbkeyform
= $t;
375 $imgpre = ":" . $wgLang->getNsText( Namespace::getImage() ) . ":";
376 if ( 0 == strncasecmp( $imgpre, $t, strlen( $imgpre ) ) ) {
377 $t = substr( $t, 1 );
379 if ( ":" == $t{0} ) {
380 $r = substr( $t, 1 );
382 if ( preg_match( "/^((?:i|x|[a-z]{2,3})(?:-[a-z0-9]+)?|[A-Za-z0-9_\\x80-\\xff]+):(.*)$/", $t, $m ) ) {
383 #$p = strtolower( $m[1] );
385 if ( $ns = $wgLang->getNsIndex( strtolower( $p ) )) {
387 $this->mNamespace
= $ns;
388 } elseif ( $this->getInterwikiLink( $p ) ) {
390 $this->mInterwiki
= $p;
392 if ( preg_match( "/^([A-Za-z0-9_\\x80-\\xff]+):(.*)$/",
394 $p = strtolower( $m[1] );
398 if($this->mInterwiki
!= $wgLocalInterwiki)
404 if ( 0 == strcmp( $this->mInterwiki
, $wgLocalInterwiki ) ) {
405 $this->mInterwiki
= "";
407 # We already know that some pages won't be in the database!
409 if ( "" != $this->mInterwiki ||
-1 == $this->mNamespace
) {
410 $this->mArticleID
= 0;
412 $f = strstr( $r, "#" );
413 if ( false !== $f ) {
414 $this->mFragment
= substr( $f, 1 );
415 $r = substr( $r, 0, strlen( $r ) - strlen( $f ) );
417 # Strip illegal characters.
419 $tc = Title
::legalChars();
420 $t = preg_replace( "/[^{$tc}]/", "", $r );
422 if( $this->mInterwiki
== "") $t = $wgLang->ucfirst( $t );
423 $this->mDbkeyform
= $t;
424 $this->mUrlform
= wfUrlencode( $t );
425 $this->mTextform
= str_replace( "_", " ", $t );