10 require_once( 'normal/UtfNormal.php' );
15 $wgTitleInterwikiCache = array();
16 define ( 'GAID_FOR_UPDATE', 1 );
20 * - Represents a title, which may contain an interwiki designation or namespace
21 * - Can fetch various kinds of data from the database, albeit inefficiently.
23 * @todo migrate comments to phpdoc format
27 # All member variables should be considered private
28 # Please use the accessor functions
30 var $mTextform; # Text form (spaces not underscores) of the main part
31 var $mUrlform; # URL-encoded form of the main part
32 var $mDbkeyform; # Main part with underscores
33 var $mNamespace; # Namespace index, i.e. one of the NS_xxxx constants
34 var $mInterwiki; # Interwiki prefix (or null string)
35 var $mFragment; # Title fragment (i.e. the bit after the #)
36 var $mArticleID; # Article ID, fetched from the link cache on demand
37 var $mRestrictions; # Array of groups allowed to edit this article
38 # Only null or "sysop" are supported
39 var $mRestrictionsLoaded; # Boolean for initialisation on demand
40 var $mPrefixedText; # Text form including namespace/interwiki, initialised on demand
41 var $mDefaultNamespace; # Namespace index when there is no namespace
42 # Zero except in {{transclusion}} tags
44 #----------------------------------------------------------------------------
46 #----------------------------------------------------------------------------
48 /* private */ function Title() {
49 $this->mInterwiki
= $this->mUrlform
=
50 $this->mTextform
= $this->mDbkeyform
= '';
51 $this->mArticleID
= -1;
52 $this->mNamespace
= 0;
53 $this->mRestrictionsLoaded
= false;
54 $this->mRestrictions
= array();
55 $this->mDefaultNamespace
= 0;
58 # From a prefixed DB key
59 /* static */ function newFromDBkey( $key ) {
61 $t->mDbkeyform
= $key;
62 if( $t->secureAndSplit() )
68 # From text, such as what you would find in a link
69 /* static */ function newFromText( $text, $defaultNamespace = 0 ) {
70 $fname = 'Title::newFromText';
71 wfProfileIn( $fname );
73 if( is_object( $text ) ) {
74 wfDebugDieBacktrace( 'Called with object instead of string.' );
76 global $wgInputEncoding;
77 $text = do_html_entity_decode( $text, ENT_COMPAT
, $wgInputEncoding );
79 $text = wfMungeToUtf8( $text );
82 # What was this for? TS 2004-03-03
83 # $text = urldecode( $text );
86 $t->mDbkeyform
= str_replace( ' ', '_', $text );
87 $t->mDefaultNamespace
= $defaultNamespace;
89 wfProfileOut( $fname );
90 if ( !is_object( $t ) ) {
91 var_dump( debug_backtrace() );
93 if( $t->secureAndSplit() ) {
100 # From a URL-encoded title
101 /* static */ function newFromURL( $url ) {
102 global $wgLang, $wgServer;
105 # For compatibility with old buggy URLs. "+" is not valid in titles,
106 # but some URLs used it as a space replacement and they still come
107 # from some external search tools.
108 $s = str_replace( '+', ' ', $url );
110 $t->mDbkeyform
= str_replace( ' ', '_', $s );
111 if( $t->secureAndSplit() ) {
112 # check that length of title is < cur_title size
113 $dbr =& wfGetDB( DB_SLAVE
);
114 $maxSize = $dbr->textFieldSize( 'cur', 'cur_title' );
115 if ( $maxSize != -1 && strlen( $t->mDbkeyform
) > $maxSize ) {
126 # This is inefficiently implemented, the cur row is requested but not
127 # used for anything else
128 /* static */ function newFromID( $id ) {
129 $fname = 'Title::newFromID';
130 $dbr =& wfGetDB( DB_SLAVE
);
131 $row = $dbr->getArray( 'cur', array( 'cur_namespace', 'cur_title' ),
132 array( 'cur_id' => $id ), $fname );
133 if ( $row !== false ) {
134 $title = Title
::makeTitle( $row->cur_namespace
, $row->cur_title
);
141 # From a namespace index and a DB key.
142 # It's assumed that $ns and $title are *valid*, for instance when
143 # they came directly from the database or a special page name.
144 /* static */ function &makeTitle( $ns, $title ) {
148 $t->mNamespace
= $ns;
149 $t->mDbkeyform
= $title;
150 $t->mArticleID
= ( $ns >= 0 ) ?
0 : -1;
151 $t->mUrlform
= wfUrlencode( $title );
152 $t->mTextform
= str_replace( '_', ' ', $title );
156 # From a namespace index and a DB key.
157 # These will be checked for validity, which is a bit slower
158 # than makeTitle() but safer for user-provided data.
159 /* static */ function makeTitleSafe( $ns, $title ) {
161 $t->mDbkeyform
= Title
::makeName( $ns, $title );
162 if( $t->secureAndSplit() ) {
169 /* static */ function newMainPage() {
170 return Title
::newFromText( wfMsg( 'mainpage' ) );
173 # Get the title object for a redirect
174 # Returns NULL if the text is not a valid redirect
175 /* static */ function newFromRedirect( $text ) {
178 if ( $wgMwRedir->matchStart( $text ) ) {
179 if ( preg_match( '/\\[\\[([^\\]\\|]+)[\\]\\|]/', $text, $m ) ) {
180 # categories are escaped using : for example one can enter:
181 # #REDIRECT [[:Category:Music]]. Need to remove it.
182 if ( substr($m[1],0,1) == ':') {
183 # We don't want to keep the ':'
184 $m[1] = substr( $m[1], 1 );
187 $rt = Title
::newFromText( $m[1] );
193 #----------------------------------------------------------------------------
195 #----------------------------------------------------------------------------
197 # Get the prefixed DB key associated with an ID
198 /* static */ function nameOf( $id ) {
199 $fname = 'Title::nameOf';
200 $dbr =& wfGetDB( DB_SLAVE
);
202 $s = $dbr->getArray( 'cur', array( 'cur_namespace','cur_title' ), array( 'cur_id' => $id ), $fname );
203 if ( $s === false ) { return NULL; }
205 $n = Title
::makeName( $s->cur_namespace
, $s->cur_title
);
209 # Get a regex character class describing the legal characters in a link
210 /* static */ function legalChars() {
211 # Missing characters:
212 # * []|# Needed for link syntax
213 # * % and + are corrupted by Apache when they appear in the path
215 # % seems to work though
217 # The problem with % is that URLs are double-unescaped: once by Apache's
218 # path conversion code, and again by PHP. So %253F, for example, becomes "?".
219 # Our code does not double-escape to compensate for this, indeed double escaping
220 # would break if the double-escaped title was passed in the query string
221 # rather than the path. This is a minor security issue because articles can be
222 # created such that they are hard to view or edit. -- TS
224 # Theoretically 0x80-0x9F of ISO 8859-1 should be disallowed, but
225 # this breaks interlanguage links
227 $set = " %!\"$&'()*,\\-.\\/0-9:;=?@A-Z\\\\^_`a-z{}~\\x80-\\xFF";
231 # Returns a stripped-down a title string ready for the search index
232 # Takes a namespace index and a text-form main part
233 /* static */ function indexTitle( $ns, $title ) {
234 global $wgDBminWordLen, $wgLang;
235 require_once( 'SearchEngine.php' );
237 $lc = SearchEngine
::legalSearchChars() . '&#;';
238 $t = $wgLang->stripForSearch( $title );
239 $t = preg_replace( "/[^{$lc}]+/", ' ', $t );
240 $t = strtolower( $t );
243 $t = preg_replace( "/([{$lc}]+)'s( |$)/", "\\1 \\1's ", $t );
244 $t = preg_replace( "/([{$lc}]+)s'( |$)/", "\\1s ", $t );
246 $t = preg_replace( "/\\s+/", ' ', $t );
248 if ( $ns == Namespace::getImage() ) {
249 $t = preg_replace( "/ (png|gif|jpg|jpeg|ogg)$/", "", $t );
254 # Make a prefixed DB key from a DB key and a namespace index
255 /* static */ function makeName( $ns, $title ) {
258 $n = $wgLang->getNsText( $ns );
259 if ( '' == $n ) { return $title; }
260 else { return $n.':'.$title; }
264 # Returns the URL associated with an interwiki prefix
265 # The URL contains $1, which is replaced by the title
266 function getInterwikiLink( $key ) {
267 global $wgMemc, $wgDBname, $wgInterwikiExpiry, $wgTitleInterwikiCache;
268 $fname = 'Title::getInterwikiLink';
269 $k = $wgDBname.':interwiki:'.$key;
271 if( array_key_exists( $k, $wgTitleInterwikiCache ) )
272 return $wgTitleInterwikiCache[$k]->iw_url
;
274 $s = $wgMemc->get( $k );
275 # Ignore old keys with no iw_local
276 if( $s && isset( $s->iw_local
) ) {
277 $wgTitleInterwikiCache[$k] = $s;
280 $dbr =& wfGetDB( DB_SLAVE
);
281 $res = $dbr->select( 'interwiki', array( 'iw_url', 'iw_local' ), array( 'iw_prefix' => $key ), $fname );
284 $s = $dbr->fetchObject( $res );
286 # Cache non-existence: create a blank object and save it to memcached
291 $wgMemc->set( $k, $s, $wgInterwikiExpiry );
292 $wgTitleInterwikiCache[$k] = $s;
297 global $wgTitleInterwikiCache, $wgDBname;
299 if ( $this->mInterwiki
!= '' ) {
300 # Make sure key is loaded into cache
301 $this->getInterwikiLink( $this->mInterwiki
);
302 $k = $wgDBname.':interwiki:' . $this->mInterwiki
;
303 return (bool)($wgTitleInterwikiCache[$k]->iw_local
);
309 # Update the cur_touched field for an array of title objects
310 # Inefficient unless the IDs are already loaded into the link cache
311 /* static */ function touchArray( $titles, $timestamp = '' ) {
312 if ( count( $titles ) == 0 ) {
315 $dbw =& wfGetDB( DB_MASTER
);
316 if ( $timestamp == '' ) {
317 $timestamp = $dbw->timestamp();
319 $cur = $dbw->tableName( 'cur' );
320 $sql = "UPDATE $cur SET cur_touched='{$timestamp}' WHERE cur_id IN (";
323 foreach ( $titles as $title ) {
328 $sql .= $title->getArticleID();
332 $dbw->query( $sql, 'Title::touchArray' );
336 #----------------------------------------------------------------------------
338 #----------------------------------------------------------------------------
341 # See the definitions at the top of this file
343 function getText() { return $this->mTextform
; }
344 function getPartialURL() { return $this->mUrlform
; }
345 function getDBkey() { return $this->mDbkeyform
; }
346 function getNamespace() { return $this->mNamespace
; }
347 function setNamespace( $n ) { $this->mNamespace
= $n; }
348 function getInterwiki() { return $this->mInterwiki
; }
349 function getFragment() { return $this->mFragment
; }
350 function getDefaultNamespace() { return $this->mDefaultNamespace
; }
352 # Get title for search index
353 function getIndexTitle() {
354 return Title
::indexTitle( $this->mNamespace
, $this->mTextform
);
357 # Get prefixed title with underscores
358 function getPrefixedDBkey() {
359 $s = $this->prefix( $this->mDbkeyform
);
360 $s = str_replace( ' ', '_', $s );
364 # Get prefixed title with spaces
365 # This is the form usually used for display
366 function getPrefixedText() {
367 if ( empty( $this->mPrefixedText
) ) {
368 $s = $this->prefix( $this->mTextform
);
369 $s = str_replace( '_', ' ', $s );
370 $this->mPrefixedText
= $s;
372 return $this->mPrefixedText
;
375 # As getPrefixedText(), plus fragment.
376 function getFullText() {
377 $text = $this->getPrefixedText();
378 if( '' != $this->mFragment
) {
379 $text .= '#' . $this->mFragment
;
384 # Get a URL-encoded title (not an actual URL) including interwiki
385 function getPrefixedURL() {
386 $s = $this->prefix( $this->mDbkeyform
);
387 $s = str_replace( ' ', '_', $s );
389 $s = wfUrlencode ( $s ) ;
391 # Cleaning up URL to make it look nice -- is this safe?
392 $s = preg_replace( '/%3[Aa]/', ':', $s );
393 $s = preg_replace( '/%2[Ff]/', '/', $s );
394 $s = str_replace( '%28', '(', $s );
395 $s = str_replace( '%29', ')', $s );
400 # Get a real URL referring to this title, with interwiki link and fragment
401 function getFullURL( $query = '' ) {
402 global $wgLang, $wgArticlePath, $wgServer, $wgScript;
404 if ( '' == $this->mInterwiki
) {
406 return $wgServer . $this->getLocalUrl( $query );
408 $baseUrl = $this->getInterwikiLink( $this->mInterwiki
);
409 $namespace = $wgLang->getNsText( $this->mNamespace
);
410 if ( '' != $namespace ) {
411 # Can this actually happen? Interwikis shouldn't be parsed.
414 $url = str_replace( '$1', $namespace . $this->mUrlform
, $baseUrl );
415 if ( '' != $this->mFragment
) {
416 $url .= '#' . $this->mFragment
;
422 # Get a URL with an optional query string, no fragment
423 # * If $query=="", it will use $wgArticlePath
424 # * Returns a full for an interwiki link, loses any query string
425 # * Optionally adds the server and escapes for HTML
426 # * Setting $query to "-" makes an old-style URL with nothing in the
427 # query except a title
430 die( 'Call to obsolete obsolete function Title::getURL()' );
433 function getLocalURL( $query = '' ) {
434 global $wgLang, $wgArticlePath, $wgScript;
436 if ( $this->isExternal() ) {
437 return $this->getFullURL();
440 $dbkey = wfUrlencode( $this->getPrefixedDBkey() );
441 if ( $query == '' ) {
442 $url = str_replace( '$1', $dbkey, $wgArticlePath );
444 if ( $query == '-' ) {
447 if ( $wgScript != '' ) {
448 $url = "{$wgScript}?title={$dbkey}&{$query}";
451 $url = "/{$dbkey}?{$query}";
457 function escapeLocalURL( $query = '' ) {
458 return htmlspecialchars( $this->getLocalURL( $query ) );
461 function escapeFullURL( $query = '' ) {
462 return htmlspecialchars( $this->getFullURL( $query ) );
465 function getInternalURL( $query = '' ) {
466 # Used in various Squid-related code, in case we have a different
467 # internal hostname for the server than the exposed one.
468 global $wgInternalServer;
469 return $wgInternalServer . $this->getLocalURL( $query );
472 # Get the edit URL, or a null string if it is an interwiki link
473 function getEditURL() {
474 global $wgServer, $wgScript;
476 if ( '' != $this->mInterwiki
) { return ''; }
477 $s = $this->getLocalURL( 'action=edit' );
482 # Get HTML-escaped displayable text
483 # For the title field in <a> tags
484 function getEscapedText() {
485 return htmlspecialchars( $this->getPrefixedText() );
488 # Is the title interwiki?
489 function isExternal() { return ( '' != $this->mInterwiki
); }
491 # Does the title correspond to a protected article?
492 function isProtected() {
493 if ( -1 == $this->mNamespace
) { return true; }
494 $a = $this->getRestrictions();
495 if ( in_array( 'sysop', $a ) ) { return true; }
499 # Is the page a log page, i.e. one where the history is messed up by
500 # LogPage.php? This used to be used for suppressing diff links in recent
501 # changes, but now that's done by setting a flag in the recentchanges
502 # table. Hence, this probably is no longer used.
504 if ( $this->mNamespace
!= Namespace::getWikipedia() ) {
507 if ( ( 0 == strcmp( wfMsg( 'uploadlogpage' ), $this->mDbkeyform
) ) ||
508 ( 0 == strcmp( wfMsg( 'dellogpage' ), $this->mDbkeyform
) ) ) {
514 # Is $wgUser is watching this page?
515 function userIsWatching() {
518 if ( -1 == $this->mNamespace
) { return false; }
519 if ( 0 == $wgUser->getID() ) { return false; }
521 return $wgUser->isWatched( $this );
524 # Can $wgUser edit this page?
525 function userCanEdit() {
527 if ( -1 == $this->mNamespace
) { return false; }
528 if ( NS_MEDIAWIKI
== $this->mNamespace
&& !$wgUser->isSysop() ) { return false; }
529 # if ( 0 == $this->getArticleID() ) { return false; }
530 if ( $this->mDbkeyform
== '_' ) { return false; }
531 # protect global styles and js
532 if ( NS_MEDIAWIKI
== $this->mNamespace
533 && preg_match("/\\.(css|js)$/", $this->mTextform
)
534 && !$wgUser->isSysop() )
536 //if ( $this->isCssJsSubpage() and !$this->userCanEditCssJsSubpage() ) { return false; }
537 # protect css/js subpages of user pages
538 # XXX: this might be better using restrictions
539 # XXX: Find a way to work around the php bug that prevents using $this->userCanEditCssJsSubpage() from working
540 if( Namespace::getUser() == $this->mNamespace
541 and preg_match("/\\.(css|js)$/", $this->mTextform
)
542 and !$wgUser->isSysop()
543 and !preg_match('/^'.preg_quote($wgUser->getName(), '/').'/', $this->mTextform
) )
545 $ur = $wgUser->getRights();
546 foreach ( $this->getRestrictions() as $r ) {
547 if ( '' != $r && ( ! in_array( $r, $ur ) ) ) {
554 function userCanRead() {
556 global $wgWhitelistRead;
558 if( 0 != $wgUser->getID() ) return true;
559 if( !is_array( $wgWhitelistRead ) ) return true;
561 $name = $this->getPrefixedText();
562 if( in_array( $name, $wgWhitelistRead ) ) return true;
564 # Compatibility with old settings
565 if( $this->getNamespace() == NS_MAIN
) {
566 if( in_array( ':' . $name, $wgWhitelistRead ) ) return true;
571 function isCssJsSubpage() {
572 return ( Namespace::getUser() == $this->mNamespace
and preg_match("/\\.(css|js)$/", $this->mTextform
) );
574 function isCssSubpage() {
575 return ( Namespace::getUser() == $this->mNamespace
and preg_match("/\\.css$/", $this->mTextform
) );
577 function isJsSubpage() {
578 return ( Namespace::getUser() == $this->mNamespace
and preg_match("/\\.js$/", $this->mTextform
) );
580 function userCanEditCssJsSubpage() {
581 # protect css/js subpages of user pages
582 # XXX: this might be better using restrictions
584 return ( $wgUser->isSysop() or preg_match('/^'.preg_quote($wgUser->getName()).'/', $this->mTextform
) );
587 # Accessor/initialisation for mRestrictions
588 function getRestrictions() {
589 $id = $this->getArticleID();
590 if ( 0 == $id ) { return array(); }
592 if ( ! $this->mRestrictionsLoaded
) {
593 $dbr =& wfGetDB( DB_SLAVE
);
594 $res = $dbr->getField( 'cur', 'cur_restrictions', 'cur_id='.$id );
595 $this->mRestrictions
= explode( ',', trim( $res ) );
596 $this->mRestrictionsLoaded
= true;
598 return $this->mRestrictions
;
601 # Is there a version of this page in the deletion archive?
602 # Returns the number of archived revisions
603 function isDeleted() {
604 $fname = 'Title::isDeleted';
605 $dbr =& wfGetDB( DB_SLAVE
);
606 $n = $dbr->getField( 'archive', 'COUNT(*)', array( 'ar_namespace' => $this->getNamespace(),
607 'ar_title' => $this->getDBkey() ), $fname );
611 # Get the article ID from the link cache
612 # $flags is a bit field, may be GAID_FOR_UPDATE to select for update
613 function getArticleID( $flags = 0 ) {
616 if ( $flags & GAID_FOR_UPDATE
) {
617 $oldUpdate = $wgLinkCache->forUpdate( true );
618 $this->mArticleID
= $wgLinkCache->addLinkObj( $this );
619 $wgLinkCache->forUpdate( $oldUpdate );
621 if ( -1 == $this->mArticleID
) {
622 $this->mArticleID
= $wgLinkCache->addLinkObj( $this );
625 return $this->mArticleID
;
628 # This clears some fields in this object, and clears any associated keys in the
629 # "bad links" section of $wgLinkCache. This is called from Article::insertNewArticle()
630 # to allow loading of the new cur_id. It's also called from Article::doDeleteArticle()
631 function resetArticleID( $newid ) {
633 $wgLinkCache->clearBadLink( $this->getPrefixedDBkey() );
635 if ( 0 == $newid ) { $this->mArticleID
= -1; }
636 else { $this->mArticleID
= $newid; }
637 $this->mRestrictionsLoaded
= false;
638 $this->mRestrictions
= array();
641 # Updates cur_touched
642 # Called from LinksUpdate.php
643 function invalidateCache() {
644 $now = wfTimestampNow();
645 $dbw =& wfGetDB( DB_MASTER
);
646 $success = $dbw->updateArray( 'cur',
648 'cur_touched' => $dbw->timestamp()
649 ), array( /* WHERE */
650 'cur_namespace' => $this->getNamespace() ,
651 'cur_title' => $this->getDBkey()
652 ), 'Title::invalidateCache'
657 # Prefixes some arbitrary text with the namespace or interwiki prefix of this object
658 /* private */ function prefix( $name ) {
662 if ( '' != $this->mInterwiki
) {
663 $p = $this->mInterwiki
. ':';
665 if ( 0 != $this->mNamespace
) {
666 $p .= $wgLang->getNsText( $this->mNamespace
) . ':';
671 # Secure and split - main initialisation function for this object
673 # Assumes that mDbkeyform has been set, and is urldecoded
674 # and uses underscores, but not otherwise munged. This function
675 # removes illegal characters, splits off the interwiki and
676 # namespace prefixes, sets the other forms, and canonicalizes
679 /* private */ function secureAndSplit()
681 global $wgLang, $wgLocalInterwiki, $wgCapitalLinks;
682 $fname = 'Title::secureAndSplit';
683 wfProfileIn( $fname );
685 static $imgpre = false;
686 static $rxTc = false;
689 if ( $imgpre === false ) {
690 $imgpre = ':' . $wgLang->getNsText( Namespace::getImage() ) . ':';
691 # % is needed as well
692 $rxTc = '/[^' . Title
::legalChars() . ']/';
695 $this->mInterwiki
= $this->mFragment
= '';
696 $this->mNamespace
= $this->mDefaultNamespace
; # Usually NS_MAIN
698 # Clean up whitespace
700 $t = preg_replace( "/[\\s_]+/", '_', $this->mDbkeyform
);
701 $t = preg_replace( '/^_*(.*?)_*$/', '$1', $t );
704 wfProfileOut( $fname );
709 if( !$wgUseLatin1 && false !== strpos( $t, UTF8_REPLACEMENT
) ) {
710 # Contained illegal UTF-8 sequences or forbidden Unicode chars.
711 wfProfileOut( $fname );
715 $this->mDbkeyform
= $t;
719 if ( 0 == strncasecmp( $imgpre, $t, strlen( $imgpre ) ) ) {
720 $t = substr( $t, 1 );
723 # Initial colon indicating main namespace
724 if ( ':' == $t{0} ) {
725 $r = substr( $t, 1 );
726 $this->mNamespace
= NS_MAIN
;
728 # Namespace or interwiki prefix
729 if ( preg_match( "/^(.+?)_*:_*(.*)$/", $t, $m ) ) {
730 #$p = strtolower( $m[1] );
732 $lowerNs = strtolower( $p );
733 if ( $ns = Namespace::getCanonicalIndex( $lowerNs ) ) {
734 # Canonical namespace
736 $this->mNamespace
= $ns;
737 } elseif ( $ns = $wgLang->getNsIndex( $lowerNs )) {
740 $this->mNamespace
= $ns;
741 } elseif ( $this->getInterwikiLink( $p ) ) {
744 $this->mInterwiki
= $p;
746 if ( !preg_match( "/^([A-Za-z0-9_\\x80-\\xff]+):(.*)$/", $t, $m ) ) {
748 } elseif($this->mInterwiki
!= $wgLocalInterwiki) {
756 # Redundant interwiki prefix to the local wiki
757 if ( 0 == strcmp( $this->mInterwiki
, $wgLocalInterwiki ) ) {
758 $this->mInterwiki
= '';
760 # We already know that some pages won't be in the database!
762 if ( '' != $this->mInterwiki ||
-1 == $this->mNamespace
) {
763 $this->mArticleID
= 0;
765 $f = strstr( $r, '#' );
766 if ( false !== $f ) {
767 $this->mFragment
= substr( $f, 1 );
768 $r = substr( $r, 0, strlen( $r ) - strlen( $f ) );
771 # Reject illegal characters.
773 if( preg_match( $rxTc, $r ) ) {
774 wfProfileOut( $fname );
778 # "." and ".." conflict with the directories of those namesa
779 if ( strpos( $r, '.' ) !== false &&
780 ( $r === '.' ||
$r === '..' ||
781 strpos( $r, './' ) === 0 ||
782 strpos( $r, '../' ) === 0 ||
783 strpos( $r, '/./' ) !== false ||
784 strpos( $r, '/../' ) !== false ) )
786 wfProfileOut( $fname );
790 # Initial capital letter
791 if( $wgCapitalLinks && $this->mInterwiki
== '') {
792 $t = $wgLang->ucfirst( $r );
798 $this->mDbkeyform
= $t;
799 $this->mUrlform
= wfUrlencode( $t );
801 $this->mTextform
= str_replace( '_', ' ', $t );
803 wfProfileOut( $fname );
807 # Get a title object associated with the talk page of this article
808 function getTalkPage() {
809 return Title
::makeTitle( Namespace::getTalk( $this->getNamespace() ), $this->getDBkey() );
812 # Get a title object associated with the subject page of this talk page
813 function getSubjectPage() {
814 return Title
::makeTitle( Namespace::getSubject( $this->getNamespace() ), $this->getDBkey() );
817 # Get an array of Title objects linking to this title
818 # Also stores the IDs in the link cache
819 # $options may be FOR UPDATE
820 function getLinksTo( $options = '' ) {
822 $id = $this->getArticleID();
825 $db =& wfGetDB( DB_MASTER
);
827 $db =& wfGetDB( DB_SLAVE
);
829 $cur = $db->tableName( 'cur' );
830 $links = $db->tableName( 'links' );
832 $sql = "SELECT cur_namespace,cur_title,cur_id FROM $cur,$links WHERE l_from=cur_id AND l_to={$id} $options";
833 $res = $db->query( $sql, 'Title::getLinksTo' );
835 if ( $db->numRows( $res ) ) {
836 while ( $row = $db->fetchObject( $res ) ) {
837 if ( $titleObj = Title
::makeTitle( $row->cur_namespace
, $row->cur_title
) ) {
838 $wgLinkCache->addGoodLink( $row->cur_id
, $titleObj->getPrefixedDBkey() );
839 $retVal[] = $titleObj;
843 $db->freeResult( $res );
847 # Get an array of Title objects linking to this non-existent title
848 # Also stores the IDs in the link cache
849 function getBrokenLinksTo( $options = '' ) {
853 $db =& wfGetDB( DB_MASTER
);
855 $db =& wfGetDB( DB_SLAVE
);
857 $cur = $db->tableName( 'cur' );
858 $brokenlinks = $db->tableName( 'brokenlinks' );
859 $encTitle = $db->strencode( $this->getPrefixedDBkey() );
861 $sql = "SELECT cur_namespace,cur_title,cur_id FROM $brokenlinks,$cur " .
862 "WHERE bl_from=cur_id AND bl_to='$encTitle' $options";
863 $res = $db->query( $sql, "Title::getBrokenLinksTo" );
865 if ( $db->numRows( $res ) ) {
866 while ( $row = $db->fetchObject( $res ) ) {
867 $titleObj = Title
::makeTitle( $row->cur_namespace
, $row->cur_title
);
868 $wgLinkCache->addGoodLink( $row->cur_id
, $titleObj->getPrefixedDBkey() );
869 $retVal[] = $titleObj;
872 $db->freeResult( $res );
876 function getSquidURLs() {
878 $this->getInternalURL(),
879 $this->getInternalURL( 'action=history' )
883 function moveNoAuth( &$nt ) {
884 return $this->moveTo( $nt, false );
887 # Move a title to a new location
888 # Returns true on success, message name on failure
889 # auth indicates whether wgUser's permissions should be checked
890 function moveTo( &$nt, $auth = true ) {
891 if( !$this or !$nt ) {
892 return 'badtitletext';
895 $fname = 'Title::move';
896 $oldid = $this->getArticleID();
897 $newid = $nt->getArticleID();
899 if ( strlen( $nt->getDBkey() ) < 1 ) {
900 return 'articleexists';
902 if ( ( ! Namespace::isMovable( $this->getNamespace() ) ) ||
903 ( '' == $this->getDBkey() ) ||
904 ( '' != $this->getInterwiki() ) ||
906 ( ! Namespace::isMovable( $nt->getNamespace() ) ) ||
907 ( '' == $nt->getDBkey() ) ||
908 ( '' != $nt->getInterwiki() ) ) {
909 return 'badarticleerror';
912 if ( $auth && ( !$this->userCanEdit() ||
!$nt->userCanEdit() ) ) {
913 return 'protectedpage';
916 # The move is allowed only if (1) the target doesn't exist, or
917 # (2) the target is a redirect to the source, and has no history
918 # (so we can undo bad moves right after they're done).
920 if ( 0 != $newid ) { # Target exists; check for validity
921 if ( ! $this->isValidMoveTarget( $nt ) ) {
922 return 'articleexists';
924 $this->moveOverExistingRedirect( $nt );
925 } else { # Target didn't exist, do normal move.
926 $this->moveToNewTitle( $nt, $newid );
929 # Fixing category links (those without piped 'alternate' names) to be sorted under the new title
931 $dbw =& wfGetDB( DB_MASTER
);
932 $sql = "UPDATE categorylinks SET cl_sortkey=" . $dbw->addQuotes( $nt->getPrefixedText() ) .
933 " WHERE cl_from=" . $dbw->addQuotes( $this->getArticleID() ) .
934 " AND cl_sortkey=" . $dbw->addQuotes( $this->getPrefixedText() );
935 $dbw->query( $sql, 'SpecialMovepage::doSubmit' );
939 $oldnamespace = $this->getNamespace() & ~
1;
940 $newnamespace = $nt->getNamespace() & ~
1;
941 $oldtitle = $this->getDBkey();
942 $newtitle = $nt->getDBkey();
944 if( $oldnamespace != $newnamespace ||
$oldtitle != $newtitle ) {
945 WatchedItem
::duplicateEntries( $this, $nt );
948 # Update search engine
949 $u = new SearchUpdate( $oldid, $nt->getPrefixedDBkey() );
951 $u = new SearchUpdate( $newid, $this->getPrefixedDBkey(), '' );
957 # Move page to title which is presently a redirect to the source page
959 /* private */ function moveOverExistingRedirect( &$nt ) {
960 global $wgUser, $wgLinkCache, $wgUseSquid, $wgMwRedir;
961 $fname = 'Title::moveOverExistingRedirect';
962 $comment = wfMsg( '1movedto2', $this->getPrefixedText(), $nt->getPrefixedText() );
964 $now = wfTimestampNow();
965 $won = wfInvertTimestamp( $now );
966 $newid = $nt->getArticleID();
967 $oldid = $this->getArticleID();
968 $dbw =& wfGetDB( DB_MASTER
);
969 $links = $dbw->tableName( 'links' );
971 # Change the name of the target page:
972 $dbw->updateArray( 'cur',
974 'cur_touched' => $dbw->timestamp($now),
975 'cur_namespace' => $nt->getNamespace(),
976 'cur_title' => $nt->getDBkey()
978 /* WHERE */ array( 'cur_id' => $oldid ),
981 $wgLinkCache->clearLink( $nt->getPrefixedDBkey() );
983 # Repurpose the old redirect. We don't save it to history since
984 # by definition if we've got here it's rather uninteresting.
986 $redirectText = $wgMwRedir->getSynonym( 0 ) . ' [[' . $nt->getPrefixedText() . "]]\n";
987 $dbw->updateArray( 'cur',
989 'cur_touched' => $dbw->timestamp($now),
990 'cur_timestamp' => $dbw->timestamp($now),
991 'inverse_timestamp' => $won,
992 'cur_namespace' => $this->getNamespace(),
993 'cur_title' => $this->getDBkey(),
994 'cur_text' => $wgMwRedir->getSynonym( 0 ) . ' [[' . $nt->getPrefixedText() . "]]\n",
995 'cur_comment' => $comment,
996 'cur_user' => $wgUser->getID(),
997 'cur_minor_edit' => 0,
999 'cur_restrictions' => '',
1000 'cur_user_text' => $wgUser->getName(),
1001 'cur_is_redirect' => 1,
1004 /* WHERE */ array( 'cur_id' => $newid ),
1008 $wgLinkCache->clearLink( $this->getPrefixedDBkey() );
1010 # Fix the redundant names for the past revisions of the target page.
1011 # The redirect should have no old revisions.
1015 'old_namespace' => $nt->getNamespace(),
1016 'old_title' => $nt->getDBkey(),
1019 'old_namespace' => $this->getNamespace(),
1020 'old_title' => $this->getDBkey(),
1025 RecentChange
::notifyMoveOverRedirect( $now, $this, $nt, $wgUser, $comment );
1029 # Load titles and IDs
1030 $linksToOld = $this->getLinksTo( 'FOR UPDATE' );
1031 $linksToNew = $nt->getLinksTo( 'FOR UPDATE' );
1034 $sql = "DELETE FROM $links WHERE l_to=$oldid OR l_to=$newid";
1035 $dbw->query( $sql, $fname );
1038 if ( count( $linksToOld ) ||
count( $linksToNew )) {
1039 $sql = "INSERT INTO $links (l_from,l_to) VALUES ";
1042 # Insert links to old title
1043 foreach ( $linksToOld as $linkTitle ) {
1049 $id = $linkTitle->getArticleID();
1050 $sql .= "($id,$newid)";
1053 # Insert links to new title
1054 foreach ( $linksToNew as $linkTitle ) {
1060 $id = $linkTitle->getArticleID();
1061 $sql .= "($id, $oldid)";
1064 $dbw->query( $sql, DB_MASTER
, $fname );
1067 # Now, we record the link from the redirect to the new title.
1068 # It should have no other outgoing links...
1069 $dbw->delete( 'links', array( 'l_from' => $newid ) );
1070 $dbw->insertArray( 'links', array( 'l_from' => $newid, 'l_to' => $oldid ) );
1073 LinkCache
::linksccClearLinksTo( $oldid );
1074 LinkCache
::linksccClearLinksTo( $newid );
1077 if ( $wgUseSquid ) {
1078 $urls = array_merge( $nt->getSquidURLs(), $this->getSquidURLs() );
1079 $u = new SquidUpdate( $urls );
1084 # Move page to non-existing title.
1085 # Sets $newid to be the new article ID
1087 /* private */ function moveToNewTitle( &$nt, &$newid ) {
1088 global $wgUser, $wgLinkCache, $wgUseSquid;
1089 $fname = 'MovePageForm::moveToNewTitle';
1090 $comment = wfMsg( '1movedto2', $this->getPrefixedText(), $nt->getPrefixedText() );
1092 $newid = $nt->getArticleID();
1093 $oldid = $this->getArticleID();
1094 $dbw =& wfGetDB( DB_MASTER
);
1095 $now = $dbw->timestamp();
1096 $won = wfInvertTimestamp( wfTimestamp(TS_MW
,$now) );
1099 $dbw->updateArray( 'cur',
1101 'cur_touched' => $now,
1102 'cur_namespace' => $nt->getNamespace(),
1103 'cur_title' => $nt->getDBkey()
1105 /* WHERE */ array( 'cur_id' => $oldid ),
1109 $wgLinkCache->clearLink( $nt->getPrefixedDBkey() );
1112 $dbw->insertArray( 'cur', array(
1113 'cur_id' => $dbw->nextSequenceValue('cur_cur_id_seq'),
1114 'cur_namespace' => $this->getNamespace(),
1115 'cur_title' => $this->getDBkey(),
1116 'cur_comment' => $comment,
1117 'cur_user' => $wgUser->getID(),
1118 'cur_user_text' => $wgUser->getName(),
1119 'cur_timestamp' => $now,
1120 'inverse_timestamp' => $won,
1121 'cur_touched' => $now,
1122 'cur_is_redirect' => 1,
1124 'cur_text' => "#REDIRECT [[" . $nt->getPrefixedText() . "]]\n" ), $fname
1126 $newid = $dbw->insertId();
1127 $wgLinkCache->clearLink( $this->getPrefixedDBkey() );
1129 # Rename old entries
1133 'old_namespace' => $nt->getNamespace(),
1134 'old_title' => $nt->getDBkey()
1137 'old_namespace' => $this->getNamespace(),
1138 'old_title' => $this->getDBkey()
1143 RecentChange
::notifyMoveToNew( $now, $this, $nt, $wgUser, $comment );
1145 # Purge squid and linkscc as per article creation
1146 Article
::onArticleCreate( $nt );
1148 # Any text links to the old title must be reassigned to the redirect
1149 $dbw->updateArray( 'links', array( 'l_to' => $newid ), array( 'l_to' => $oldid ), $fname );
1150 LinkCache
::linksccClearLinksTo( $oldid );
1152 # Record the just-created redirect's linking to the page
1153 $dbw->insertArray( 'links', array( 'l_from' => $newid, 'l_to' => $oldid ), $fname );
1155 # Non-existent target may have had broken links to it; these must
1156 # now be removed and made into good links.
1157 $update = new LinksUpdate( $oldid, $nt->getPrefixedDBkey() );
1158 $update->fixBrokenLinks();
1160 # Purge old title from squid
1161 # The new title, and links to the new title, are purged in Article::onArticleCreate()
1162 $titles = $nt->getLinksTo();
1163 if ( $wgUseSquid ) {
1164 $urls = $this->getSquidURLs();
1165 foreach ( $titles as $linkTitle ) {
1166 $urls[] = $linkTitle->getInternalURL();
1168 $u = new SquidUpdate( $urls );
1173 # Checks if $this can be moved to $nt
1174 # Selects for update, so don't call it unless you mean business
1175 function isValidMoveTarget( $nt ) {
1176 $fname = 'Title::isValidMoveTarget';
1177 $dbw =& wfGetDB( DB_MASTER
);
1180 $id = $nt->getArticleID();
1181 $obj = $dbw->getArray( 'cur', array( 'cur_is_redirect','cur_text' ),
1182 array( 'cur_id' => $id ), $fname, 'FOR UPDATE' );
1184 if ( !$obj ||
0 == $obj->cur_is_redirect
) {
1189 # Does the redirect point to the source?
1190 if ( preg_match( "/\\[\\[\\s*([^\\]\\|]*)]]/", $obj->cur_text
, $m ) ) {
1191 $redirTitle = Title
::newFromText( $m[1] );
1192 if( !is_object( $redirTitle ) ||
1193 $redirTitle->getPrefixedDBkey() != $this->getPrefixedDBkey() ) {
1198 # Does the article have a history?
1199 $row = $dbw->getArray( 'old', array( 'old_id' ),
1201 'old_namespace' => $nt->getNamespace(),
1202 'old_title' => $nt->getDBkey()
1203 ), $fname, 'FOR UPDATE'
1206 # Return true if there was no history
1207 return $row === false;
1210 # Create a redirect, fails if the title already exists, does not notify RC
1212 function createRedirect( $dest, $comment ) {
1214 if ( $this->getArticleID() ) {
1218 $fname = 'Title::createRedirect';
1219 $dbw =& wfGetDB( DB_MASTER
);
1220 $now = wfTimestampNow();
1221 $won = wfInvertTimestamp( $now );
1222 $seqVal = $dbw->nextSequenceValue( 'cur_cur_id_seq' );
1224 $dbw->insertArray( 'cur', array(
1225 'cur_id' => $seqVal,
1226 'cur_namespace' => $this->getNamespace(),
1227 'cur_title' => $this->getDBkey(),
1228 'cur_comment' => $comment,
1229 'cur_user' => $wgUser->getID(),
1230 'cur_user_text' => $wgUser->getName(),
1231 'cur_timestamp' => $now,
1232 'inverse_timestamp' => $won,
1233 'cur_touched' => $now,
1234 'cur_is_redirect' => 1,
1236 'cur_text' => "#REDIRECT [[" . $dest->getPrefixedText() . "]]\n"
1238 $newid = $dbw->insertId();
1239 $this->resetArticleID( $newid );
1242 if ( $dest->getArticleID() ) {
1243 $dbw->insertArray( 'links',
1245 'l_to' => $dest->getArticleID(),
1250 $dbw->insertArray( 'brokenlinks',
1252 'bl_to' => $dest->getPrefixedDBkey(),
1258 Article
::onArticleCreate( $this );
1262 # Get categories to wich belong this title and return an array of
1264 # Return an array of parents in the form:
1265 # $parent => $currentarticle
1266 function getParentCategories() {
1267 global $wgLang,$wgUser;
1269 $titlekey = $this->getArticleId();
1270 $sk =& $wgUser->getSkin();
1272 $dbr =& wfGetDB( DB_SLAVE
);
1273 $cur = $dbr->tableName( 'cur' );
1274 $categorylinks = $dbr->tableName( 'categorylinks' );
1277 $sql = "SELECT * FROM categorylinks"
1278 ." WHERE cl_from='$titlekey'"
1279 ." AND cl_from <> '0'"
1280 ." ORDER BY cl_sortkey";
1282 $res = $dbr->query ( $sql ) ;
1284 if($dbr->numRows($res) > 0) {
1285 while ( $x = $dbr->fetchObject ( $res ) )
1286 //$data[] = Title::newFromText($wgLang->getNSText ( NS_CATEGORY ).':'.$x->cl_to);
1287 $data[$wgLang->getNSText ( NS_CATEGORY
).':'.$x->cl_to
] = $this->getFullText();
1288 $dbr->freeResult ( $res ) ;
1295 # Go through all parents
1296 function getCategorieBrowser() {
1297 $parents = $this->getParentCategories();
1299 if($parents != '') {
1300 foreach($parents as $parent => $current)
1302 $nt = Title
::newFromText($parent);
1303 $stack[$parent] = $nt->getCategorieBrowser();
1312 # Returns an associative array for selecting this title from cur
1313 function curCond() {
1314 return array( 'cur_namespace' => $this->mNamespace
, 'cur_title' => $this->mDbkeyform
);
1317 function oldCond() {
1318 return array( 'old_namespace' => $this->mNamespace
, 'old_title' => $this->mDbkeyform
);