5 /* private */ var $mTextform, $mUrlform, $mDbkeyform;
6 /* private */ var $mNamespace, $mInterwiki, $mFragment;
7 /* private */ var $mArticleID, $mRestrictions, $mRestrictionsLoaded;
8 /* private */ var $mPrefixedText;
10 /* private */ function Title()
12 $this->mInterwiki
= $this->mUrlform
=
13 $this->mTextform
= $this->mDbkeyform
= "";
14 $this->mArticleID
= -1;
15 $this->mNamespace
= 0;
16 $this->mRestrictionsLoaded
= false;
17 $this->mRestrictions
= array();
20 # Static factory methods
22 function newFromDBkey( $key )
25 $t->mDbkeyform
= $key;
26 if( $t->secureAndSplit() )
32 function newFromText( $text )
34 $fname = "Title::newFromText";
35 wfProfileIn( $fname );
37 # Note - mixing latin1 named entities and unicode numbered
38 # ones will result in a bad link.
39 $trans = get_html_translation_table( HTML_ENTITIES
);
40 $trans = array_flip( $trans );
41 $text = strtr( $text, $trans );
43 $text = wfMungeToUtf8( $text );
45 $text = urldecode( $text );
48 $t->mDbkeyform
= str_replace( " ", "_", $text );
49 wfProfileOut( $fname );
50 if( $t->secureAndSplit() ) {
57 function newFromURL( $url )
59 global $wgLang, $wgServer, $HTTP_SERVER_VARS;
62 $s = urldecode( $url ); # This is technically wrong, as anything
63 # we've gotten is already decoded by PHP.
64 # Kept for backwards compatibility with
65 # buggy URLs we had for a while...
67 # For links that came from outside, check for alternate/legacy
69 if( strncmp($wgServer, $HTTP_SERVER_VARS["HTTP_REFERER"], strlen( $wgServer ) ) )
70 $s = $wgLang->checkTitleEncoding( $s );
72 $t->mDbkeyform
= str_replace( " ", "_", $s );
73 if( $t->secureAndSplit() ) {
80 function nameOf( $id )
82 $sql = "SELECT cur_namespace,cur_title FROM cur WHERE " .
84 $res = wfQuery( $sql, DB_READ
, "Article::nameOf" );
85 if ( 0 == wfNumRows( $res ) ) { return NULL; }
87 $s = wfFetchObject( $res );
88 $n = Title
::makeName( $s->cur_namespace
, $s->cur_title
);
95 global $wgInputEncoding;
96 if( $wgInputEncoding == "utf-8" ) {
97 return "-,.()' &;%!?_0-9A-Za-z\\/:\\x80-\\xFF";
99 # ISO 8859-* don't allow 0x80-0x9F
100 #return "-,.()' &;%!?_0-9A-Za-z\\/:\\xA0-\\xFF";
101 # But that breaks interlanguage links at the moment. Temporary:
102 return "-,.()' &;%!?_0-9A-Za-z\\/:\\x80-\\xFF";
106 function getInterwikiLink( $key )
108 global $wgMemc, $wgDBname;
109 $k = "$wgDBname:interwiki:$key";
110 $s = $wgMemc->get( $k );
111 if( $s !== false ) return $s->iw_url
;
113 $dkey = wfStrencode( $key );
114 $query = "SELECT iw_url FROM interwiki WHERE iw_prefix='$dkey'";
115 $res = wfQuery( $query, DB_READ
, "Title::getInterwikiLink" );
118 $s = wfFetchObject( $res );
123 $wgMemc->set( $k, $s );
127 function getText() { return $this->mTextform
; }
128 function getURL() { return $this->mUrlform
; }
129 function getDBkey() { return $this->mDbkeyform
; }
130 function getNamespace() { return $this->mNamespace
; }
131 function setNamespace( $n ) { $this->mNamespace
= $n; }
132 function getInterwiki() { return $this->mInterwiki
; }
133 function getFragment() { return $this->mFragment
; }
135 /* static */ function indexTitle( $ns, $title )
137 global $wgDBminWordLen, $wgLang;
139 $lc = SearchEngine
::legalSearchChars() . "&#;";
140 $t = $wgLang->stripForSearch( $title );
141 $t = preg_replace( "/[^{$lc}]+/", " ", $t );
142 $t = strtolower( $t );
145 $t = preg_replace( "/([{$lc}]+)'s( |$)/", "\\1 \\1's ", $t );
146 $t = preg_replace( "/([{$lc}]+)s'( |$)/", "\\1s ", $t );
148 $t = preg_replace( "/\\s+/", " ", $t );
150 if ( $ns == Namespace::getImage() ) {
151 $t = preg_replace( "/ (png|gif|jpg|jpeg|ogg)$/", "", $t );
156 function getIndexTitle()
158 return Title
::indexTitle( $this->mNamespace
, $this->mTextform
);
161 /* static */ function makeName( $ns, $title )
165 $n = $wgLang->getNsText( $ns );
166 if ( "" == $n ) { return $title; }
167 else { return "{$n}:{$title}"; }
170 /* static */ function makeTitle( $ns, $title )
173 $t->mDbkeyform
= Title
::makeName( $ns, $title );
174 if( $t->secureAndSplit() ) {
181 function getPrefixedDBkey()
183 $s = $this->prefix( $this->mDbkeyform
);
184 $s = str_replace( " ", "_", $s );
188 function getPrefixedText()
191 if ( empty( $this->mPrefixedText
) ) {
192 $s = $this->prefix( $this->mTextform
);
193 $s = str_replace( "_", " ", $s );
194 $this->mPrefixedText
= $s;
196 return $this->mPrefixedText
;
199 function getPrefixedURL()
201 $s = $this->prefix( $this->mDbkeyform
);
202 $s = str_replace( " ", "_", $s );
204 $s = urlencode ( $s ) ;
205 # Cleaning up URL to make it look nice -- is this safe?
206 $s = preg_replace( "/%3[Aa]/", ":", $s );
207 $s = preg_replace( "/%2[Ff]/", "/", $s );
208 $s = str_replace( "%28", "(", $s );
209 $s = str_replace( "%29", ")", $s );
213 function getFullURL()
215 global $wgLang, $wgArticlePath;
217 if ( "" == $this->mInterwiki
) {
220 $p = $this->getInterwikiLink( $this->mInterwiki
);
222 $n = $wgLang->getNsText( $this->mNamespace
);
223 if ( "" != $n ) { $n .= ":"; }
224 $u = str_replace( "$1", $n . $this->mUrlform
, $p );
225 if ( "" != $this->mFragment
) {
226 $u .= "#" . $this->mFragment
;
231 function getEditURL()
233 global $wgServer, $wgScript;
235 if ( "" != $this->mInterwiki
) { return ""; }
236 $s = wfLocalUrl( $this->getPrefixedURL(), "action=edit" );
241 # For the title field in <a> tags
242 function getEscapedText()
244 return wfEscapeHTML( $this->getPrefixedText() );
247 function isExternal() { return ( "" != $this->mInterwiki
); }
249 function isProtected()
251 if ( -1 == $this->mNamespace
) { return true; }
252 $a = $this->getRestrictions();
253 if ( in_array( "sysop", $a ) ) { return true; }
259 if ( $this->mNamespace
!= Namespace::getWikipedia() ) {
262 if ( ( 0 == strcmp( wfMsg( "uploadlogpage" ), $this->mDbkeyform
) ) ||
263 ( 0 == strcmp( wfMsg( "dellogpage" ), $this->mDbkeyform
) ) ) {
269 function userIsWatching()
273 if ( -1 == $this->mNamespace
) { return false; }
274 if ( 0 == $wgUser->getID() ) { return false; }
276 return $wgUser->isWatched( $this );
279 function userCanEdit()
283 if ( -1 == $this->mNamespace
) { return false; }
284 # if ( 0 == $this->getArticleID() ) { return false; }
285 if ( $this->mDbkeyform
== "_" ) { return false; }
287 $ur = $wgUser->getRights();
288 foreach ( $this->getRestrictions() as $r ) {
289 if ( "" != $r && ( ! in_array( $r, $ur ) ) ) {
296 function getRestrictions()
298 $id = $this->getArticleID();
299 if ( 0 == $id ) { return array(); }
301 if ( ! $this->mRestrictionsLoaded
) {
302 $res = wfGetSQL( "cur", "cur_restrictions", "cur_id=$id" );
303 $this->mRestrictions
= explode( ",", trim( $res ) );
304 $this->mRestrictionsLoaded
= true;
306 return $this->mRestrictions
;
309 function isDeleted() {
310 $ns = $this->getNamespace();
311 $t = wfStrencode( $this->getDBkey() );
312 $sql = "SELECT COUNT(*) AS n FROM archive WHERE ar_namespace=$ns AND ar_title='$t'";
313 if( $res = wfQuery( $sql, DB_READ
) ) {
314 $s = wfFetchObject( $res );
320 function getArticleID()
324 if ( -1 != $this->mArticleID
) { return $this->mArticleID
; }
325 $this->mArticleID
= $wgLinkCache->addLinkObj( $this );
326 return $this->mArticleID
;
329 function resetArticleID( $newid )
332 $wgLinkCache->clearBadLink( $this->getPrefixedDBkey() );
334 if ( 0 == $newid ) { $this->mArticleID
= -1; }
335 else { $this->mArticleID
= $newid; }
336 $this->mRestrictionsLoaded
= false;
337 $this->mRestrictions
= array();
340 function invalidateCache() {
341 $now = wfTimestampNow();
342 $ns = $this->getNamespace();
343 $ti = wfStrencode( $this->getDBkey() );
344 $sql = "UPDATE cur SET cur_touched='$now' WHERE cur_namespace=$ns AND cur_title='$ti'";
345 return wfQuery( $sql, "Title::invalidateCache" );
348 /* private */ function prefix( $name )
353 if ( "" != $this->mInterwiki
) {
354 $p = $this->mInterwiki
. ":";
356 if ( 0 != $this->mNamespace
) {
357 $p .= $wgLang->getNsText( $this->mNamespace
) . ":";
362 # Assumes that mDbkeyform has been set, and is urldecoded
363 # and uses undersocres, but not otherwise munged. This function
364 # removes illegal characters, splits off the winterwiki and
365 # namespace prefixes, sets the other forms, and canonicalizes
368 /* private */ function secureAndSplit()
370 global $wgLang, $wgLocalInterwiki;
371 $fname = "Title::secureAndSplit";
372 wfProfileIn( $fname );
374 static $imgpre = false;
375 static $rxTc = false;
378 if ( $imgpre === false ) {
379 $imgpre = ":" . $wgLang->getNsText( Namespace::getImage() ) . ":";
380 $rxTc = "/[^" . Title
::legalChars() . "]/";
384 $this->mInterwiki
= $this->mFragment
= "";
385 $this->mNamespace
= 0;
387 $t = preg_replace( "/[\\s_]+/", "_", $this->mDbkeyform
);
388 if ( "_" == $t{0} ) {
389 $t = substr( $t, 1 );
392 if ( $l && ( "_" == $t{$l-1} ) ) {
393 $t = substr( $t, 0, $l-1 );
396 wfProfileOut( $fname );
400 $this->mDbkeyform
= $t;
403 if ( 0 == strncasecmp( $imgpre, $t, strlen( $imgpre ) ) ) {
404 $t = substr( $t, 1 );
406 if ( ":" == $t{0} ) {
407 $r = substr( $t, 1 );
409 if ( preg_match( "/^((?:i|x|[a-z]{2,3})(?:-[a-z0-9]+)?|[A-Za-z0-9_\\x80-\\xff]+):_*(.*)$/", $t, $m ) ) {
410 #$p = strtolower( $m[1] );
412 if ( $ns = $wgLang->getNsIndex( strtolower( $p ) )) {
414 $this->mNamespace
= $ns;
415 } elseif ( $this->getInterwikiLink( $p ) ) {
417 $this->mInterwiki
= $p;
419 if ( !preg_match( "/^([A-Za-z0-9_\\x80-\\xff]+):(.*)$/", $t, $m ) ) {
421 } elseif($this->mInterwiki
!= $wgLocalInterwiki) {
428 if ( 0 == strcmp( $this->mInterwiki
, $wgLocalInterwiki ) ) {
429 $this->mInterwiki
= "";
431 # We already know that some pages won't be in the database!
433 if ( "" != $this->mInterwiki ||
-1 == $this->mNamespace
) {
434 $this->mArticleID
= 0;
436 $f = strstr( $r, "#" );
437 if ( false !== $f ) {
438 $this->mFragment
= substr( $f, 1 );
439 $r = substr( $r, 0, strlen( $r ) - strlen( $f ) );
441 # Strip illegal characters.
443 $t = preg_replace( $rxTc, "", $r );
445 if( $this->mInterwiki
== "") $t = $wgLang->ucfirst( $t );
446 $this->mDbkeyform
= $t;
447 $this->mUrlform
= wfUrlencode( $t );
448 $this->mTextform
= str_replace( "_", " ", $t );
450 wfProfileOut( $fname );