22c18702d79bb71af43ebbe45b574070adfc66c4
10 $wgTitleInterwikiCache = array();
11 define ( 'GAID_FOR_UPDATE', 1 );
15 * - Represents a title, which may contain an interwiki designation or namespace
16 * - Can fetch various kinds of data from the database, albeit inefficiently.
18 * @todo migrate comments to phpdoc format
21 # All member variables should be considered private
22 # Please use the accessor functions
24 var $mTextform; # Text form (spaces not underscores) of the main part
25 var $mUrlform; # URL-encoded form of the main part
26 var $mDbkeyform; # Main part with underscores
27 var $mNamespace; # Namespace index, i.e. one of the NS_xxxx constants
28 var $mInterwiki; # Interwiki prefix (or null string)
29 var $mFragment; # Title fragment (i.e. the bit after the #)
30 var $mArticleID; # Article ID, fetched from the link cache on demand
31 var $mRestrictions; # Array of groups allowed to edit this article
32 # Only null or "sysop" are supported
33 var $mRestrictionsLoaded; # Boolean for initialisation on demand
34 var $mPrefixedText; # Text form including namespace/interwiki, initialised on demand
35 var $mDefaultNamespace; # Namespace index when there is no namespace
36 # Zero except in {{transclusion}} tags
38 #----------------------------------------------------------------------------
40 #----------------------------------------------------------------------------
42 /* private */ function Title() {
43 $this->mInterwiki
= $this->mUrlform
=
44 $this->mTextform
= $this->mDbkeyform
= '';
45 $this->mArticleID
= -1;
46 $this->mNamespace
= 0;
47 $this->mRestrictionsLoaded
= false;
48 $this->mRestrictions
= array();
49 $this->mDefaultNamespace
= 0;
52 # From a prefixed DB key
53 /* static */ function newFromDBkey( $key ) {
55 $t->mDbkeyform
= $key;
56 if( $t->secureAndSplit() )
62 # From text, such as what you would find in a link
63 /* static */ function newFromText( $text, $defaultNamespace = 0 ) {
64 $fname = 'Title::newFromText';
65 wfProfileIn( $fname );
67 if( is_object( $text ) ) {
68 wfDebugDieBacktrace( 'Called with object instead of string.' );
70 global $wgInputEncoding;
71 $text = do_html_entity_decode( $text, ENT_COMPAT
, $wgInputEncoding );
73 $text = wfMungeToUtf8( $text );
76 # What was this for? TS 2004-03-03
77 # $text = urldecode( $text );
80 $t->mDbkeyform
= str_replace( ' ', '_', $text );
81 $t->mDefaultNamespace
= $defaultNamespace;
83 wfProfileOut( $fname );
84 if ( !is_object( $t ) ) {
85 var_dump( debug_backtrace() );
87 if( $t->secureAndSplit() ) {
94 # From a URL-encoded title
95 /* static */ function newFromURL( $url ) {
96 global $wgLang, $wgServer;
99 # For compatibility with old buggy URLs. "+" is not valid in titles,
100 # but some URLs used it as a space replacement and they still come
101 # from some external search tools.
102 $s = str_replace( '+', ' ', $url );
104 # For links that came from outside, check for alternate/legacy
105 # character encoding.
106 wfDebug( "Servr: $wgServer\n" );
107 if( empty( $_SERVER['HTTP_REFERER'] ) ||
108 strncmp($wgServer, $_SERVER['HTTP_REFERER'], strlen( $wgServer ) ) )
110 $s = $wgLang->checkTitleEncoding( $s );
112 wfDebug( "Refer: {$_SERVER['HTTP_REFERER']}\n" );
115 $t->mDbkeyform
= str_replace( ' ', '_', $s );
116 if( $t->secureAndSplit() ) {
117 # check that length of title is < cur_title size
118 $dbr =& wfGetDB( DB_SLAVE
);
119 $maxSize = $dbr->textFieldSize( 'cur', 'cur_title' );
120 if ( $maxSize != -1 && strlen( $t->mDbkeyform
) > $maxSize ) {
131 # This is inefficiently implemented, the cur row is requested but not
132 # used for anything else
133 /* static */ function newFromID( $id ) {
134 $fname = 'Title::newFromID';
135 $dbr =& wfGetDB( DB_SLAVE
);
136 $row = $dbr->getArray( 'cur', array( 'cur_namespace', 'cur_title' ),
137 array( 'cur_id' => $id ), $fname );
138 if ( $row !== false ) {
139 $title = Title
::makeTitle( $row->cur_namespace
, $row->cur_title
);
146 # From a namespace index and a DB key.
147 # It's assumed that $ns and $title are *valid*, for instance when
148 # they came directly from the database or a special page name.
149 /* static */ function &makeTitle( $ns, $title ) {
153 $t->mNamespace
= $ns;
154 $t->mDbkeyform
= $title;
155 $t->mArticleID
= ( $ns >= 0 ) ?
0 : -1;
156 $t->mUrlform
= wfUrlencode( $title );
157 $t->mTextform
= str_replace( '_', ' ', $title );
161 # From a namespace index and a DB key.
162 # These will be checked for validity, which is a bit slower
163 # than makeTitle() but safer for user-provided data.
164 /* static */ function makeTitleSafe( $ns, $title ) {
166 $t->mDbkeyform
= Title
::makeName( $ns, $title );
167 if( $t->secureAndSplit() ) {
174 /* static */ function newMainPage() {
175 return Title
::newFromText( wfMsg( 'mainpage' ) );
178 # Get the title object for a redirect
179 # Returns NULL if the text is not a valid redirect
180 /* static */ function newFromRedirect( $text ) {
183 if ( $wgMwRedir->matchStart( $text ) ) {
184 if ( preg_match( '/\\[\\[([^\\]\\|]+)[\\]\\|]/', $text, $m ) ) {
185 # categories are escaped using : for example one can enter:
186 # #REDIRECT [[:Category:Music]]. Need to remove it.
187 if ( substr($m[1],0,1) == ':') {
188 # We don't want to keep the ':'
189 $m[1] = substr( $m[1], 1 );
192 $rt = Title
::newFromText( $m[1] );
198 #----------------------------------------------------------------------------
200 #----------------------------------------------------------------------------
202 # Get the prefixed DB key associated with an ID
203 /* static */ function nameOf( $id ) {
204 $fname = 'Title::nameOf';
205 $dbr =& wfGetDB( DB_SLAVE
);
207 $s = $dbr->getArray( 'cur', array( 'cur_namespace','cur_title' ), array( 'cur_id' => $id ), $fname );
208 if ( $s === false ) { return NULL; }
210 $n = Title
::makeName( $s->cur_namespace
, $s->cur_title
);
214 # Get a regex character class describing the legal characters in a link
215 /* static */ function legalChars() {
216 # Missing characters:
217 # * []|# Needed for link syntax
218 # * % and + are corrupted by Apache when they appear in the path
220 # % seems to work though
222 # The problem with % is that URLs are double-unescaped: once by Apache's
223 # path conversion code, and again by PHP. So %253F, for example, becomes "?".
224 # Our code does not double-escape to compensate for this, indeed double escaping
225 # would break if the double-escaped title was passed in the query string
226 # rather than the path. This is a minor security issue because articles can be
227 # created such that they are hard to view or edit. -- TS
229 # Theoretically 0x80-0x9F of ISO 8859-1 should be disallowed, but
230 # this breaks interlanguage links
232 $set = " %!\"$&'()*,\\-.\\/0-9:;=?@A-Z\\\\^_`a-z{}~\\x80-\\xFF";
236 # Returns a stripped-down a title string ready for the search index
237 # Takes a namespace index and a text-form main part
238 /* static */ function indexTitle( $ns, $title ) {
239 global $wgDBminWordLen, $wgLang;
240 require_once( 'SearchEngine.php' );
242 $lc = SearchEngine
::legalSearchChars() . '&#;';
243 $t = $wgLang->stripForSearch( $title );
244 $t = preg_replace( "/[^{$lc}]+/", ' ', $t );
245 $t = strtolower( $t );
248 $t = preg_replace( "/([{$lc}]+)'s( |$)/", "\\1 \\1's ", $t );
249 $t = preg_replace( "/([{$lc}]+)s'( |$)/", "\\1s ", $t );
251 $t = preg_replace( "/\\s+/", ' ', $t );
253 if ( $ns == Namespace::getImage() ) {
254 $t = preg_replace( "/ (png|gif|jpg|jpeg|ogg)$/", "", $t );
259 # Make a prefixed DB key from a DB key and a namespace index
260 /* static */ function makeName( $ns, $title ) {
263 $n = $wgLang->getNsText( $ns );
264 if ( '' == $n ) { return $title; }
265 else { return $n.':'.$title; }
269 # Returns the URL associated with an interwiki prefix
270 # The URL contains $1, which is replaced by the title
271 function getInterwikiLink( $key ) {
272 global $wgMemc, $wgDBname, $wgInterwikiExpiry, $wgTitleInterwikiCache;
273 $fname = 'Title::getInterwikiLink';
274 $k = $wgDBname.':interwiki:'.$key;
276 if( array_key_exists( $k, $wgTitleInterwikiCache ) )
277 return $wgTitleInterwikiCache[$k]->iw_url
;
279 $s = $wgMemc->get( $k );
280 # Ignore old keys with no iw_local
281 if( $s && isset( $s->iw_local
) ) {
282 $wgTitleInterwikiCache[$k] = $s;
285 $dbr =& wfGetDB( DB_SLAVE
);
286 $res = $dbr->select( 'interwiki', array( 'iw_url', 'iw_local' ), array( 'iw_prefix' => $key ), $fname );
289 $s = $dbr->fetchObject( $res );
291 # Cache non-existence: create a blank object and save it to memcached
296 $wgMemc->set( $k, $s, $wgInterwikiExpiry );
297 $wgTitleInterwikiCache[$k] = $s;
302 global $wgTitleInterwikiCache, $wgDBname;
304 if ( $this->mInterwiki
!= '' ) {
305 # Make sure key is loaded into cache
306 $this->getInterwikiLink( $this->mInterwiki
);
307 $k = $wgDBname.':interwiki:' . $this->mInterwiki
;
308 return (bool)($wgTitleInterwikiCache[$k]->iw_local
);
314 # Update the cur_touched field for an array of title objects
315 # Inefficient unless the IDs are already loaded into the link cache
316 /* static */ function touchArray( $titles, $timestamp = '' ) {
317 if ( count( $titles ) == 0 ) {
320 if ( $timestamp == '' ) {
321 $timestamp = wfTimestampNow();
323 $dbw =& wfGetDB( DB_MASTER
);
324 $cur = $dbw->tableName( 'cur' );
325 $sql = "UPDATE $cur SET cur_touched='{$timestamp}' WHERE cur_id IN (";
328 foreach ( $titles as $title ) {
333 $sql .= $title->getArticleID();
337 $dbw->query( $sql, 'Title::touchArray' );
341 #----------------------------------------------------------------------------
343 #----------------------------------------------------------------------------
346 # See the definitions at the top of this file
348 function getText() { return $this->mTextform
; }
349 function getPartialURL() { return $this->mUrlform
; }
350 function getDBkey() { return $this->mDbkeyform
; }
351 function getNamespace() { return $this->mNamespace
; }
352 function setNamespace( $n ) { $this->mNamespace
= $n; }
353 function getInterwiki() { return $this->mInterwiki
; }
354 function getFragment() { return $this->mFragment
; }
355 function getDefaultNamespace() { return $this->mDefaultNamespace
; }
357 # Get title for search index
358 function getIndexTitle() {
359 return Title
::indexTitle( $this->mNamespace
, $this->mTextform
);
362 # Get prefixed title with underscores
363 function getPrefixedDBkey() {
364 $s = $this->prefix( $this->mDbkeyform
);
365 $s = str_replace( ' ', '_', $s );
369 # Get prefixed title with spaces
370 # This is the form usually used for display
371 function getPrefixedText() {
372 if ( empty( $this->mPrefixedText
) ) {
373 $s = $this->prefix( $this->mTextform
);
374 $s = str_replace( '_', ' ', $s );
375 $this->mPrefixedText
= $s;
377 return $this->mPrefixedText
;
380 # As getPrefixedText(), plus fragment.
381 function getFullText() {
382 $text = $this->getPrefixedText();
383 if( '' != $this->mFragment
) {
384 $text .= '#' . $this->mFragment
;
389 # Get a URL-encoded title (not an actual URL) including interwiki
390 function getPrefixedURL() {
391 $s = $this->prefix( $this->mDbkeyform
);
392 $s = str_replace( ' ', '_', $s );
394 $s = wfUrlencode ( $s ) ;
396 # Cleaning up URL to make it look nice -- is this safe?
397 $s = preg_replace( '/%3[Aa]/', ':', $s );
398 $s = preg_replace( '/%2[Ff]/', '/', $s );
399 $s = str_replace( '%28', '(', $s );
400 $s = str_replace( '%29', ')', $s );
405 # Get a real URL referring to this title, with interwiki link and fragment
406 function getFullURL( $query = '' ) {
407 global $wgLang, $wgArticlePath, $wgServer, $wgScript;
409 if ( '' == $this->mInterwiki
) {
411 return $wgServer . $this->getLocalUrl( $query );
413 $baseUrl = $this->getInterwikiLink( $this->mInterwiki
);
414 $namespace = $wgLang->getNsText( $this->mNamespace
);
415 if ( '' != $namespace ) {
416 # Can this actually happen? Interwikis shouldn't be parsed.
419 $url = str_replace( '$1', $namespace . $this->mUrlform
, $baseUrl );
420 if ( '' != $this->mFragment
) {
421 $url .= '#' . $this->mFragment
;
427 # Get a URL with an optional query string, no fragment
428 # * If $query=="", it will use $wgArticlePath
429 # * Returns a full for an interwiki link, loses any query string
430 # * Optionally adds the server and escapes for HTML
431 # * Setting $query to "-" makes an old-style URL with nothing in the
432 # query except a title
435 die( 'Call to obsolete obsolete function Title::getURL()' );
438 function getLocalURL( $query = '' ) {
439 global $wgLang, $wgArticlePath, $wgScript;
441 if ( $this->isExternal() ) {
442 return $this->getFullURL();
445 $dbkey = wfUrlencode( $this->getPrefixedDBkey() );
446 if ( $query == '' ) {
447 $url = str_replace( '$1', $dbkey, $wgArticlePath );
449 if ( $query == '-' ) {
452 if ( $wgScript != '' ) {
453 $url = "{$wgScript}?title={$dbkey}&{$query}";
456 $url = "/{$dbkey}?{$query}";
462 function escapeLocalURL( $query = '' ) {
463 return htmlspecialchars( $this->getLocalURL( $query ) );
466 function escapeFullURL( $query = '' ) {
467 return htmlspecialchars( $this->getFullURL( $query ) );
470 function getInternalURL( $query = '' ) {
471 # Used in various Squid-related code, in case we have a different
472 # internal hostname for the server than the exposed one.
473 global $wgInternalServer;
474 return $wgInternalServer . $this->getLocalURL( $query );
477 # Get the edit URL, or a null string if it is an interwiki link
478 function getEditURL() {
479 global $wgServer, $wgScript;
481 if ( '' != $this->mInterwiki
) { return ''; }
482 $s = $this->getLocalURL( 'action=edit' );
487 # Get HTML-escaped displayable text
488 # For the title field in <a> tags
489 function getEscapedText() {
490 return htmlspecialchars( $this->getPrefixedText() );
493 # Is the title interwiki?
494 function isExternal() { return ( '' != $this->mInterwiki
); }
496 # Does the title correspond to a protected article?
497 function isProtected() {
498 if ( -1 == $this->mNamespace
) { return true; }
499 $a = $this->getRestrictions();
500 if ( in_array( 'sysop', $a ) ) { return true; }
504 # Is the page a log page, i.e. one where the history is messed up by
505 # LogPage.php? This used to be used for suppressing diff links in recent
506 # changes, but now that's done by setting a flag in the recentchanges
507 # table. Hence, this probably is no longer used.
509 if ( $this->mNamespace
!= Namespace::getWikipedia() ) {
512 if ( ( 0 == strcmp( wfMsg( 'uploadlogpage' ), $this->mDbkeyform
) ) ||
513 ( 0 == strcmp( wfMsg( 'dellogpage' ), $this->mDbkeyform
) ) ) {
519 # Is $wgUser is watching this page?
520 function userIsWatching() {
523 if ( -1 == $this->mNamespace
) { return false; }
524 if ( 0 == $wgUser->getID() ) { return false; }
526 return $wgUser->isWatched( $this );
529 # Can $wgUser edit this page?
530 function userCanEdit() {
532 if ( -1 == $this->mNamespace
) { return false; }
533 if ( NS_MEDIAWIKI
== $this->mNamespace
&& !$wgUser->isSysop() ) { return false; }
534 # if ( 0 == $this->getArticleID() ) { return false; }
535 if ( $this->mDbkeyform
== '_' ) { return false; }
536 # protect global styles and js
537 if ( NS_MEDIAWIKI
== $this->mNamespace
538 && preg_match("/\\.(css|js)$/", $this->mTextform
)
539 && !$wgUser->isSysop() )
541 //if ( $this->isCssJsSubpage() and !$this->userCanEditCssJsSubpage() ) { return false; }
542 # protect css/js subpages of user pages
543 # XXX: this might be better using restrictions
544 # XXX: Find a way to work around the php bug that prevents using $this->userCanEditCssJsSubpage() from working
545 if( Namespace::getUser() == $this->mNamespace
546 and preg_match("/\\.(css|js)$/", $this->mTextform
)
547 and !$wgUser->isSysop()
548 and !preg_match('/^'.preg_quote($wgUser->getName(), '/').'/', $this->mTextform
) )
550 $ur = $wgUser->getRights();
551 foreach ( $this->getRestrictions() as $r ) {
552 if ( '' != $r && ( ! in_array( $r, $ur ) ) ) {
559 function userCanRead() {
561 global $wgWhitelistRead;
563 if( 0 != $wgUser->getID() ) return true;
564 if( !is_array( $wgWhitelistRead ) ) return true;
566 $name = $this->getPrefixedText();
567 if( in_array( $name, $wgWhitelistRead ) ) return true;
569 # Compatibility with old settings
570 if( $this->getNamespace() == NS_MAIN
) {
571 if( in_array( ':' . $name, $wgWhitelistRead ) ) return true;
576 function isCssJsSubpage() {
577 return ( Namespace::getUser() == $this->mNamespace
and preg_match("/\\.(css|js)$/", $this->mTextform
) );
579 function isCssSubpage() {
580 return ( Namespace::getUser() == $this->mNamespace
and preg_match("/\\.css$/", $this->mTextform
) );
582 function isJsSubpage() {
583 return ( Namespace::getUser() == $this->mNamespace
and preg_match("/\\.js$/", $this->mTextform
) );
585 function userCanEditCssJsSubpage() {
586 # protect css/js subpages of user pages
587 # XXX: this might be better using restrictions
589 return ( $wgUser->isSysop() or preg_match('/^'.preg_quote($wgUser->getName()).'/', $this->mTextform
) );
592 # Accessor/initialisation for mRestrictions
593 function getRestrictions() {
594 $id = $this->getArticleID();
595 if ( 0 == $id ) { return array(); }
597 if ( ! $this->mRestrictionsLoaded
) {
598 $dbr =& wfGetDB( DB_SLAVE
);
599 $res = $dbr->getField( 'cur', 'cur_restrictions', 'cur_id='.$id );
600 $this->mRestrictions
= explode( ',', trim( $res ) );
601 $this->mRestrictionsLoaded
= true;
603 return $this->mRestrictions
;
606 # Is there a version of this page in the deletion archive?
607 # Returns the number of archived revisions
608 function isDeleted() {
609 $fname = 'Title::isDeleted';
610 $dbr =& wfGetDB( DB_SLAVE
);
611 $n = $dbr->getField( 'archive', 'COUNT(*)', array( 'ar_namespace' => $this->getNamespace(),
612 'ar_title' => $this->getDBkey() ), $fname );
616 # Get the article ID from the link cache
617 # $flags is a bit field, may be GAID_FOR_UPDATE to select for update
618 function getArticleID( $flags = 0 ) {
621 if ( $flags & GAID_FOR_UPDATE
) {
622 $oldUpdate = $wgLinkCache->forUpdate( true );
623 $this->mArticleID
= $wgLinkCache->addLinkObj( $this );
624 $wgLinkCache->forUpdate( $oldUpdate );
626 if ( -1 == $this->mArticleID
) {
627 $this->mArticleID
= $wgLinkCache->addLinkObj( $this );
630 return $this->mArticleID
;
633 # This clears some fields in this object, and clears any associated keys in the
634 # "bad links" section of $wgLinkCache. This is called from Article::insertNewArticle()
635 # to allow loading of the new cur_id. It's also called from Article::doDeleteArticle()
636 function resetArticleID( $newid ) {
638 $wgLinkCache->clearBadLink( $this->getPrefixedDBkey() );
640 if ( 0 == $newid ) { $this->mArticleID
= -1; }
641 else { $this->mArticleID
= $newid; }
642 $this->mRestrictionsLoaded
= false;
643 $this->mRestrictions
= array();
646 # Updates cur_touched
647 # Called from LinksUpdate.php
648 function invalidateCache() {
649 $now = wfTimestampNow();
650 $dbw =& wfGetDB( DB_MASTER
);
651 $success = $dbw->updateArray( 'cur',
653 'cur_touched' => wfTimestampNow()
654 ), array( /* WHERE */
655 'cur_namespace' => $this->getNamespace() ,
656 'cur_title' => $this->getDBkey()
657 ), 'Title::invalidateCache'
662 # Prefixes some arbitrary text with the namespace or interwiki prefix of this object
663 /* private */ function prefix( $name ) {
667 if ( '' != $this->mInterwiki
) {
668 $p = $this->mInterwiki
. ':';
670 if ( 0 != $this->mNamespace
) {
671 $p .= $wgLang->getNsText( $this->mNamespace
) . ':';
676 # Secure and split - main initialisation function for this object
678 # Assumes that mDbkeyform has been set, and is urldecoded
679 # and uses underscores, but not otherwise munged. This function
680 # removes illegal characters, splits off the interwiki and
681 # namespace prefixes, sets the other forms, and canonicalizes
684 /* private */ function secureAndSplit()
686 global $wgLang, $wgLocalInterwiki, $wgCapitalLinks;
687 $fname = 'Title::secureAndSplit';
688 wfProfileIn( $fname );
690 static $imgpre = false;
691 static $rxTc = false;
694 if ( $imgpre === false ) {
695 $imgpre = ':' . $wgLang->getNsText( Namespace::getImage() ) . ':';
696 # % is needed as well
697 $rxTc = '/[^' . Title
::legalChars() . ']/';
700 $this->mInterwiki
= $this->mFragment
= '';
701 $this->mNamespace
= $this->mDefaultNamespace
; # Usually NS_MAIN
703 # Clean up whitespace
705 $t = preg_replace( "/[\\s_]+/", '_', $this->mDbkeyform
);
706 $t = preg_replace( '/^_*(.*?)_*$/', '$1', $t );
709 wfProfileOut( $fname );
713 $this->mDbkeyform
= $t;
717 if ( 0 == strncasecmp( $imgpre, $t, strlen( $imgpre ) ) ) {
718 $t = substr( $t, 1 );
721 # Initial colon indicating main namespace
722 if ( ':' == $t{0} ) {
723 $r = substr( $t, 1 );
724 $this->mNamespace
= NS_MAIN
;
726 # Namespace or interwiki prefix
727 if ( preg_match( "/^(.+?)_*:_*(.*)$/", $t, $m ) ) {
728 #$p = strtolower( $m[1] );
730 $lowerNs = strtolower( $p );
731 if ( $ns = Namespace::getCanonicalIndex( $lowerNs ) ) {
732 # Canonical namespace
734 $this->mNamespace
= $ns;
735 } elseif ( $ns = $wgLang->getNsIndex( $lowerNs )) {
738 $this->mNamespace
= $ns;
739 } elseif ( $this->getInterwikiLink( $p ) ) {
742 $this->mInterwiki
= $p;
744 if ( !preg_match( "/^([A-Za-z0-9_\\x80-\\xff]+):(.*)$/", $t, $m ) ) {
746 } elseif($this->mInterwiki
!= $wgLocalInterwiki) {
754 # Redundant interwiki prefix to the local wiki
755 if ( 0 == strcmp( $this->mInterwiki
, $wgLocalInterwiki ) ) {
756 $this->mInterwiki
= '';
758 # We already know that some pages won't be in the database!
760 if ( '' != $this->mInterwiki ||
-1 == $this->mNamespace
) {
761 $this->mArticleID
= 0;
763 $f = strstr( $r, '#' );
764 if ( false !== $f ) {
765 $this->mFragment
= substr( $f, 1 );
766 $r = substr( $r, 0, strlen( $r ) - strlen( $f ) );
769 # Reject illegal characters.
771 if( preg_match( $rxTc, $r ) ) {
772 wfProfileOut( $fname );
776 # "." and ".." conflict with the directories of those namesa
777 if ( strpos( $r, '.' ) !== false &&
778 ( $r === '.' ||
$r === '..' ||
779 strpos( $r, './' ) === 0 ||
780 strpos( $r, '../' ) === 0 ||
781 strpos( $r, '/./' ) !== false ||
782 strpos( $r, '/../' ) !== false ) )
784 wfProfileOut( $fname );
788 # Initial capital letter
789 if( $wgCapitalLinks && $this->mInterwiki
== '') {
790 $t = $wgLang->ucfirst( $r );
796 $this->mDbkeyform
= $t;
797 $this->mUrlform
= wfUrlencode( $t );
799 $this->mTextform
= str_replace( '_', ' ', $t );
801 wfProfileOut( $fname );
805 # Get a title object associated with the talk page of this article
806 function getTalkPage() {
807 return Title
::makeTitle( Namespace::getTalk( $this->getNamespace() ), $this->getDBkey() );
810 # Get a title object associated with the subject page of this talk page
811 function getSubjectPage() {
812 return Title
::makeTitle( Namespace::getSubject( $this->getNamespace() ), $this->getDBkey() );
815 # Get an array of Title objects linking to this title
816 # Also stores the IDs in the link cache
817 # $options may be FOR UPDATE
818 function getLinksTo( $options = '' ) {
820 $id = $this->getArticleID();
823 $db =& wfGetDB( DB_MASTER
);
825 $db =& wfGetDB( DB_SLAVE
);
827 $cur = $db->tableName( 'cur' );
828 $links = $db->tableName( 'links' );
830 $sql = "SELECT cur_namespace,cur_title,cur_id FROM $cur,$links WHERE l_from=cur_id AND l_to={$id} $options";
831 $res = $db->query( $sql, 'Title::getLinksTo' );
833 if ( $db->numRows( $res ) ) {
834 while ( $row = $db->fetchObject( $res ) ) {
835 if ( $titleObj = Title
::makeTitle( $row->cur_namespace
, $row->cur_title
) ) {
836 $wgLinkCache->addGoodLink( $row->cur_id
, $titleObj->getPrefixedDBkey() );
837 $retVal[] = $titleObj;
841 $db->freeResult( $res );
845 # Get an array of Title objects linking to this non-existent title
846 # Also stores the IDs in the link cache
847 function getBrokenLinksTo( $options = '' ) {
851 $db =& wfGetDB( DB_MASTER
);
853 $db =& wfGetDB( DB_SLAVE
);
855 $cur = $db->tableName( 'cur' );
856 $brokenlinks = $db->tableName( 'brokenlinks' );
857 $encTitle = $db->strencode( $this->getPrefixedDBkey() );
859 $sql = "SELECT cur_namespace,cur_title,cur_id FROM $brokenlinks,$cur " .
860 "WHERE bl_from=cur_id AND bl_to='$encTitle' $options";
861 $res = $db->query( $sql, "Title::getBrokenLinksTo" );
863 if ( $db->numRows( $res ) ) {
864 while ( $row = $db->fetchObject( $res ) ) {
865 $titleObj = Title
::makeTitle( $row->cur_namespace
, $row->cur_title
);
866 $wgLinkCache->addGoodLink( $row->cur_id
, $titleObj->getPrefixedDBkey() );
867 $retVal[] = $titleObj;
870 $db->freeResult( $res );
874 function getSquidURLs() {
876 $this->getInternalURL(),
877 $this->getInternalURL( 'action=history' )
881 function moveNoAuth( &$nt ) {
882 return $this->moveTo( $nt, false );
885 # Move a title to a new location
886 # Returns true on success, message name on failure
887 # auth indicates whether wgUser's permissions should be checked
888 function moveTo( &$nt, $auth = true ) {
889 if( !$this or !$nt ) {
890 return 'badtitletext';
893 $fname = 'Title::move';
894 $oldid = $this->getArticleID();
895 $newid = $nt->getArticleID();
897 if ( strlen( $nt->getDBkey() ) < 1 ) {
898 return 'articleexists';
900 if ( ( ! Namespace::isMovable( $this->getNamespace() ) ) ||
901 ( '' == $this->getDBkey() ) ||
902 ( '' != $this->getInterwiki() ) ||
904 ( ! Namespace::isMovable( $nt->getNamespace() ) ) ||
905 ( '' == $nt->getDBkey() ) ||
906 ( '' != $nt->getInterwiki() ) ) {
907 return 'badarticleerror';
910 if ( $auth && ( !$this->userCanEdit() ||
!$nt->userCanEdit() ) ) {
911 return 'protectedpage';
914 # The move is allowed only if (1) the target doesn't exist, or
915 # (2) the target is a redirect to the source, and has no history
916 # (so we can undo bad moves right after they're done).
918 if ( 0 != $newid ) { # Target exists; check for validity
919 if ( ! $this->isValidMoveTarget( $nt ) ) {
920 return 'articleexists';
922 $this->moveOverExistingRedirect( $nt );
923 } else { # Target didn't exist, do normal move.
924 $this->moveToNewTitle( $nt, $newid );
927 # Fixing category links (those without piped 'alternate' names) to be sorted under the new title
929 $dbw =& wfGetDB( DB_MASTER
);
930 $sql = "UPDATE categorylinks SET cl_sortkey=" . $dbw->addQuotes( $nt->getPrefixedText() ) .
931 " WHERE cl_from=" . $dbw->addQuotes( $this->getArticleID() ) .
932 " AND cl_sortkey=" . $dbw->addQuotes( $this->getPrefixedText() );
933 $dbw->query( $sql, 'SpecialMovepage::doSubmit' );
937 $oldnamespace = $this->getNamespace() & ~
1;
938 $newnamespace = $nt->getNamespace() & ~
1;
939 $oldtitle = $this->getDBkey();
940 $newtitle = $nt->getDBkey();
942 if( $oldnamespace != $newnamespace ||
$oldtitle != $newtitle ) {
943 WatchedItem
::duplicateEntries( $this, $nt );
946 # Update search engine
947 $u = new SearchUpdate( $oldid, $nt->getPrefixedDBkey() );
949 $u = new SearchUpdate( $newid, $this->getPrefixedDBkey(), '' );
955 # Move page to title which is presently a redirect to the source page
957 /* private */ function moveOverExistingRedirect( &$nt ) {
958 global $wgUser, $wgLinkCache, $wgUseSquid, $wgMwRedir;
959 $fname = 'Title::moveOverExistingRedirect';
960 $comment = wfMsg( '1movedto2', $this->getPrefixedText(), $nt->getPrefixedText() );
962 $now = wfTimestampNow();
963 $won = wfInvertTimestamp( $now );
964 $newid = $nt->getArticleID();
965 $oldid = $this->getArticleID();
966 $dbw =& wfGetDB( DB_MASTER
);
967 $links = $dbw->tableName( 'links' );
969 # Change the name of the target page:
970 $dbw->updateArray( 'cur',
972 'cur_touched' => $now,
973 'cur_namespace' => $nt->getNamespace(),
974 'cur_title' => $nt->getDBkey()
976 /* WHERE */ array( 'cur_id' => $oldid ),
979 $wgLinkCache->clearLink( $nt->getPrefixedDBkey() );
981 # Repurpose the old redirect. We don't save it to history since
982 # by definition if we've got here it's rather uninteresting.
984 $redirectText = $wgMwRedir->getSynonym( 0 ) . ' [[' . $nt->getPrefixedText() . "]]\n";
985 $dbw->updateArray( 'cur',
987 'cur_touched' => $now,
988 'cur_timestamp' => $now,
989 'inverse_timestamp' => $won,
990 'cur_namespace' => $this->getNamespace(),
991 'cur_title' => $this->getDBkey(),
992 'cur_text' => $wgMwRedir->getSynonym( 0 ) . ' [[' . $nt->getPrefixedText() . "]]\n",
993 'cur_comment' => $comment,
994 'cur_user' => $wgUser->getID(),
995 'cur_minor_edit' => 0,
997 'cur_restrictions' => '',
998 'cur_user_text' => $wgUser->getName(),
999 'cur_is_redirect' => 1,
1002 /* WHERE */ array( 'cur_id' => $newid ),
1006 $wgLinkCache->clearLink( $this->getPrefixedDBkey() );
1008 # Fix the redundant names for the past revisions of the target page.
1009 # The redirect should have no old revisions.
1013 'old_namespace' => $nt->getNamespace(),
1014 'old_title' => $nt->getDBkey(),
1017 'old_namespace' => $this->getNamespace(),
1018 'old_title' => $this->getDBkey(),
1023 RecentChange
::notifyMoveOverRedirect( $now, $this, $nt, $wgUser, $comment );
1027 # Load titles and IDs
1028 $linksToOld = $this->getLinksTo( 'FOR UPDATE' );
1029 $linksToNew = $nt->getLinksTo( 'FOR UPDATE' );
1032 $sql = "DELETE FROM $links WHERE l_to=$oldid OR l_to=$newid";
1033 $dbw->query( $sql, $fname );
1036 if ( count( $linksToOld ) ||
count( $linksToNew )) {
1037 $sql = "INSERT INTO $links (l_from,l_to) VALUES ";
1040 # Insert links to old title
1041 foreach ( $linksToOld as $linkTitle ) {
1047 $id = $linkTitle->getArticleID();
1048 $sql .= "($id,$newid)";
1051 # Insert links to new title
1052 foreach ( $linksToNew as $linkTitle ) {
1058 $id = $linkTitle->getArticleID();
1059 $sql .= "($id, $oldid)";
1062 $dbw->query( $sql, DB_MASTER
, $fname );
1065 # Now, we record the link from the redirect to the new title.
1066 # It should have no other outgoing links...
1067 $dbw->delete( 'links', array( 'l_from' => $newid ) );
1068 $dbw->insertArray( 'links', array( 'l_from' => $newid, 'l_to' => $oldid ) );
1071 LinkCache
::linksccClearLinksTo( $oldid );
1072 LinkCache
::linksccClearLinksTo( $newid );
1075 if ( $wgUseSquid ) {
1076 $urls = array_merge( $nt->getSquidURLs(), $this->getSquidURLs() );
1077 $u = new SquidUpdate( $urls );
1082 # Move page to non-existing title.
1083 # Sets $newid to be the new article ID
1085 /* private */ function moveToNewTitle( &$nt, &$newid ) {
1086 global $wgUser, $wgLinkCache, $wgUseSquid;
1087 $fname = 'MovePageForm::moveToNewTitle';
1088 $comment = wfMsg( '1movedto2', $this->getPrefixedText(), $nt->getPrefixedText() );
1090 $now = wfTimestampNow();
1091 $won = wfInvertTimestamp( $now );
1092 $newid = $nt->getArticleID();
1093 $oldid = $this->getArticleID();
1094 $dbw =& wfGetDB( DB_MASTER
);
1097 $dbw->updateArray( 'cur',
1099 'cur_touched' => $now,
1100 'cur_namespace' => $nt->getNamespace(),
1101 'cur_title' => $nt->getDBkey()
1103 /* WHERE */ array( 'cur_id' => $oldid ),
1107 $wgLinkCache->clearLink( $nt->getPrefixedDBkey() );
1110 $dbw->insertArray( 'cur', array(
1111 'cur_namespace' => $this->getNamespace(),
1112 'cur_title' => $this->getDBkey(),
1113 'cur_comment' => $comment,
1114 'cur_user' => $wgUser->getID(),
1115 'cur_user_text' => $wgUser->getName(),
1116 'cur_timestamp' => $now,
1117 'inverse_timestamp' => $won,
1118 'cur_touched' => $now,
1119 'cur_is_redirect' => 1,
1121 'cur_text' => "#REDIRECT [[" . $nt->getPrefixedText() . "]]\n" ), $fname
1123 $newid = $dbw->insertId();
1124 $wgLinkCache->clearLink( $this->getPrefixedDBkey() );
1126 # Rename old entries
1130 'old_namespace' => $nt->getNamespace(),
1131 'old_title' => $nt->getDBkey()
1134 'old_namespace' => $this->getNamespace(),
1135 'old_title' => $this->getDBkey()
1140 RecentChange
::notifyMoveToNew( $now, $this, $nt, $wgUser, $comment );
1142 # Purge squid and linkscc as per article creation
1143 Article
::onArticleCreate( $nt );
1145 # Any text links to the old title must be reassigned to the redirect
1146 $dbw->updateArray( 'links', array( 'l_to' => $newid ), array( 'l_to' => $oldid ), $fname );
1147 LinkCache
::linksccClearLinksTo( $oldid );
1149 # Record the just-created redirect's linking to the page
1150 $dbw->insertArray( 'links', array( 'l_from' => $newid, 'l_to' => $oldid ), $fname );
1152 # Non-existent target may have had broken links to it; these must
1153 # now be removed and made into good links.
1154 $update = new LinksUpdate( $oldid, $nt->getPrefixedDBkey() );
1155 $update->fixBrokenLinks();
1157 # Purge old title from squid
1158 # The new title, and links to the new title, are purged in Article::onArticleCreate()
1159 $titles = $nt->getLinksTo();
1160 if ( $wgUseSquid ) {
1161 $urls = $this->getSquidURLs();
1162 foreach ( $titles as $linkTitle ) {
1163 $urls[] = $linkTitle->getInternalURL();
1165 $u = new SquidUpdate( $urls );
1170 # Checks if $this can be moved to $nt
1171 # Selects for update, so don't call it unless you mean business
1172 function isValidMoveTarget( $nt ) {
1173 $fname = 'Title::isValidMoveTarget';
1174 $dbw =& wfGetDB( DB_MASTER
);
1177 $id = $nt->getArticleID();
1178 $obj = $dbw->getArray( 'cur', array( 'cur_is_redirect','cur_text' ),
1179 array( 'cur_id' => $id ), $fname, 'FOR UPDATE' );
1181 if ( !$obj ||
0 == $obj->cur_is_redirect
) {
1186 # Does the redirect point to the source?
1187 if ( preg_match( "/\\[\\[\\s*([^\\]\\|]*)]]/", $obj->cur_text
, $m ) ) {
1188 $redirTitle = Title
::newFromText( $m[1] );
1189 if( !is_object( $redirTitle ) ||
1190 $redirTitle->getPrefixedDBkey() != $this->getPrefixedDBkey() ) {
1195 # Does the article have a history?
1196 $row = $dbw->getArray( 'old', array( 'old_id' ),
1198 'old_namespace' => $nt->getNamespace(),
1199 'old_title' => $nt->getDBkey()
1200 ), $fname, 'FOR UPDATE'
1203 # Return true if there was no history
1204 return $row === false;
1207 # Create a redirect, fails if the title already exists, does not notify RC
1209 function createRedirect( $dest, $comment ) {
1211 if ( $this->getArticleID() ) {
1215 $fname = 'Title::createRedirect';
1216 $dbw =& wfGetDB( DB_MASTER
);
1217 $now = wfTimestampNow();
1218 $won = wfInvertTimestamp( $now );
1219 $seqVal = $dbw->nextSequenceValue( 'cur_cur_id_seq' );
1221 $dbw->insertArray( 'cur', array(
1222 'cur_id' => $seqVal,
1223 'cur_namespace' => $this->getNamespace(),
1224 'cur_title' => $this->getDBkey(),
1225 'cur_comment' => $comment,
1226 'cur_user' => $wgUser->getID(),
1227 'cur_user_text' => $wgUser->getName(),
1228 'cur_timestamp' => $now,
1229 'inverse_timestamp' => $won,
1230 'cur_touched' => $now,
1231 'cur_is_redirect' => 1,
1233 'cur_text' => "#REDIRECT [[" . $dest->getPrefixedText() . "]]\n"
1235 $newid = $dbw->insertId();
1236 $this->resetArticleID( $newid );
1239 if ( $dest->getArticleID() ) {
1240 $dbw->insertArray( 'links',
1242 'l_to' => $dest->getArticleID(),
1247 $dbw->insertArray( 'brokenlinks',
1249 'bl_to' => $dest->getPrefixedDBkey(),
1255 Article
::onArticleCreate( $this );
1259 # Get categories to wich belong this title and return an array of
1261 # Return an array of parents in the form:
1262 # $parent => $currentarticle
1263 function getParentCategories() {
1264 global $wgLang,$wgUser;
1266 $titlekey = $this->getArticleId();
1267 $sk =& $wgUser->getSkin();
1269 $dbr =& wfGetDB( DB_SLAVE
);
1270 $cur = $dbr->tableName( 'cur' );
1271 $categorylinks = $dbr->tableName( 'categorylinks' );
1274 $sql = "SELECT * FROM categorylinks"
1275 ." WHERE cl_from='$titlekey'"
1276 ." AND cl_from <> '0'"
1277 ." ORDER BY cl_sortkey";
1279 $res = $dbr->query ( $sql ) ;
1281 if($dbr->numRows($res) > 0) {
1282 while ( $x = $dbr->fetchObject ( $res ) )
1283 //$data[] = Title::newFromText($wgLang->getNSText ( NS_CATEGORY ).':'.$x->cl_to);
1284 $data[$wgLang->getNSText ( NS_CATEGORY
).':'.$x->cl_to
] = $this->getFullText();
1285 $dbr->freeResult ( $res ) ;
1292 # Go through all parents
1293 function getCategorieBrowser() {
1294 $parents = $this->getParentCategories();
1296 if($parents != '') {
1297 foreach($parents as $parent => $current)
1299 $nt = Title
::newFromText($parent);
1300 $stack[$parent] = $nt->getCategorieBrowser();
1309 # Returns an associative array for selecting this title from cur
1310 function curCond() {
1311 return array( 'cur_namespace' => $this->mNamespace
, 'cur_title' => $this->mDbkeyform
);
1314 function oldCond() {
1315 return array( 'old_namespace' => $this->mNamespace
, 'old_title' => $this->mDbkeyform
);