c921831147b5da9de9b39549fe56d667d950ae72
9 require_once( 'normal/UtfNormal.php' );
11 $wgTitleInterwikiCache = array();
12 $wgTitleCache = array();
14 define ( 'GAID_FOR_UPDATE', 1 );
16 # Title::newFromTitle maintains a cache to avoid
17 # expensive re-normalization of commonly used titles.
18 # On a batch operation this can become a memory leak
19 # if not bounded. After hitting this many titles,
21 define( 'MW_TITLECACHE_MAX', 1000 );
25 * - Represents a title, which may contain an interwiki designation or namespace
26 * - Can fetch various kinds of data from the database, albeit inefficiently.
32 * All member variables should be considered private
33 * Please use the accessor functions
40 var $mTextform; # Text form (spaces not underscores) of the main part
41 var $mUrlform; # URL-encoded form of the main part
42 var $mDbkeyform; # Main part with underscores
43 var $mNamespace; # Namespace index, i.e. one of the NS_xxxx constants
44 var $mInterwiki; # Interwiki prefix (or null string)
45 var $mFragment; # Title fragment (i.e. the bit after the #)
46 var $mArticleID; # Article ID, fetched from the link cache on demand
47 var $mLatestID; # ID of most recent revision
48 var $mRestrictions; # Array of groups allowed to edit this article
49 # Only null or "sysop" are supported
50 var $mRestrictionsLoaded; # Boolean for initialisation on demand
51 var $mPrefixedText; # Text form including namespace/interwiki, initialised on demand
52 var $mDefaultNamespace; # Namespace index when there is no namespace
53 # Zero except in {{transclusion}} tags
54 var $mWatched; # Is $wgUser watching this page? NULL if unfilled, accessed through userIsWatching()
62 /* private */ function Title() {
63 $this->mInterwiki
= $this->mUrlform
=
64 $this->mTextform
= $this->mDbkeyform
= '';
65 $this->mArticleID
= -1;
66 $this->mNamespace
= NS_MAIN
;
67 $this->mRestrictionsLoaded
= false;
68 $this->mRestrictions
= array();
69 # Dont change the following, NS_MAIN is hardcoded in several place
71 $this->mDefaultNamespace
= NS_MAIN
;
72 $this->mWatched
= NULL;
73 $this->mLatestID
= false;
77 * Create a new Title from a prefixed DB key
78 * @param string $key The database key, which has underscores
79 * instead of spaces, possibly including namespace and
81 * @return Title the new object, or NULL on an error
85 /* static */ function newFromDBkey( $key ) {
87 $t->mDbkeyform
= $key;
88 if( $t->secureAndSplit() )
95 * Create a new Title from text, such as what one would
96 * find in a link. Decodes any HTML entities in the text.
98 * @param string $text the link text; spaces, prefixes,
99 * and an initial ':' indicating the main namespace
101 * @param int $defaultNamespace the namespace to use if
102 * none is specified by a prefix
103 * @return Title the new object, or NULL on an error
107 function newFromText( $text, $defaultNamespace = NS_MAIN
) {
108 global $wgTitleCache;
109 $fname = 'Title::newFromText';
110 wfProfileIn( $fname );
112 if( is_object( $text ) ) {
113 wfDebugDieBacktrace( 'Title::newFromText given an object' );
117 * Wiki pages often contain multiple links to the same page.
118 * Title normalization and parsing can become expensive on
119 * pages with many links, so we can save a little time by
122 * In theory these are value objects and won't get changed...
124 if( $defaultNamespace == NS_MAIN
&& isset( $wgTitleCache[$text] ) ) {
125 wfProfileOut( $fname );
126 return $wgTitleCache[$text];
130 * Convert things like é ā or 〗 into real text...
132 $filteredText = Sanitizer
::decodeCharReferences( $text );
135 $t->mDbkeyform
= str_replace( ' ', '_', $filteredText );
136 $t->mDefaultNamespace
= $defaultNamespace;
138 static $cachedcount = 0 ;
139 if( $t->secureAndSplit() ) {
140 if( $defaultNamespace == NS_MAIN
) {
141 if( $cachedcount >= MW_TITLECACHE_MAX
) {
142 # Avoid memory leaks on mass operations...
143 $wgTitleCache = array();
147 $wgTitleCache[$text] =& $t;
149 wfProfileOut( $fname );
152 wfProfileOut( $fname );
159 * Create a new Title from URL-encoded text. Ensures that
160 * the given title's length does not exceed the maximum.
161 * @param string $url the title, as might be taken from a URL
162 * @return Title the new object, or NULL on an error
166 function newFromURL( $url ) {
167 global $wgLegalTitleChars;
170 # For compatibility with old buggy URLs. "+" is usually not valid in titles,
171 # but some URLs used it as a space replacement and they still come
172 # from some external search tools.
173 if ( strpos( $wgLegalTitleChars, '+' ) === false ) {
174 $url = str_replace( '+', ' ', $url );
177 $t->mDbkeyform
= str_replace( ' ', '_', $url );
178 if( $t->secureAndSplit() ) {
186 * Create a new Title from an article ID
188 * @todo This is inefficiently implemented, the page row is requested
189 * but not used for anything else
191 * @param int $id the page_id corresponding to the Title to create
192 * @return Title the new object, or NULL on an error
196 function newFromID( $id ) {
197 $fname = 'Title::newFromID';
198 $dbr =& wfGetDB( DB_SLAVE
);
199 $row = $dbr->selectRow( 'page', array( 'page_namespace', 'page_title' ),
200 array( 'page_id' => $id ), $fname );
201 if ( $row !== false ) {
202 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
210 * Create a new Title from a namespace index and a DB key.
211 * It's assumed that $ns and $title are *valid*, for instance when
212 * they came directly from the database or a special page name.
213 * For convenience, spaces are converted to underscores so that
214 * eg user_text fields can be used directly.
216 * @param int $ns the namespace of the article
217 * @param string $title the unprefixed database key form
218 * @return Title the new object
222 function &makeTitle( $ns, $title ) {
226 $t->mNamespace
= intval( $ns );
227 $t->mDbkeyform
= str_replace( ' ', '_', $title );
228 $t->mArticleID
= ( $ns >= 0 ) ?
-1 : 0;
229 $t->mUrlform
= wfUrlencode( $t->mDbkeyform
);
230 $t->mTextform
= str_replace( '_', ' ', $title );
235 * Create a new Title frrom a namespace index and a DB key.
236 * The parameters will be checked for validity, which is a bit slower
237 * than makeTitle() but safer for user-provided data.
239 * @param int $ns the namespace of the article
240 * @param string $title the database key form
241 * @return Title the new object, or NULL on an error
245 function makeTitleSafe( $ns, $title ) {
247 $t->mDbkeyform
= Title
::makeName( $ns, $title );
248 if( $t->secureAndSplit() ) {
256 * Create a new Title for the Main Page
259 * @return Title the new object
262 function newMainPage() {
263 return Title
::newFromText( wfMsgForContent( 'mainpage' ) );
267 * Create a new Title for a redirect
268 * @param string $text the redirect title text
269 * @return Title the new object, or NULL if the text is not a
274 function newFromRedirect( $text ) {
277 if ( $wgMwRedir->matchStart( $text ) ) {
278 if ( preg_match( '/\[{2}(.*?)(?:\||\]{2})/', $text, $m ) ) {
279 # categories are escaped using : for example one can enter:
280 # #REDIRECT [[:Category:Music]]. Need to remove it.
281 if ( substr($m[1],0,1) == ':') {
282 # We don't want to keep the ':'
283 $m[1] = substr( $m[1], 1 );
286 $rt = Title
::newFromText( $m[1] );
287 # Disallow redirects to Special:Userlogout
288 if ( !is_null($rt) && $rt->getNamespace() == NS_SPECIAL
&& preg_match( '/^Userlogout/i', $rt->getText() ) ) {
296 #----------------------------------------------------------------------------
298 #----------------------------------------------------------------------------
301 * Get the prefixed DB key associated with an ID
302 * @param int $id the page_id of the article
303 * @return Title an object representing the article, or NULL
304 * if no such article was found
308 function nameOf( $id ) {
309 $fname = 'Title::nameOf';
310 $dbr =& wfGetDB( DB_SLAVE
);
312 $s = $dbr->selectRow( 'page', array( 'page_namespace','page_title' ), array( 'page_id' => $id ), $fname );
313 if ( $s === false ) { return NULL; }
315 $n = Title
::makeName( $s->page_namespace
, $s->page_title
);
320 * Get a regex character class describing the legal characters in a link
321 * @return string the list of characters, not delimited
325 function legalChars() {
326 global $wgLegalTitleChars;
327 return $wgLegalTitleChars;
331 * Get a string representation of a title suitable for
332 * including in a search index
334 * @param int $ns a namespace index
335 * @param string $title text-form main part
336 * @return string a stripped-down title string ready for the
339 /* static */ function indexTitle( $ns, $title ) {
341 require_once( 'SearchEngine.php' );
343 $lc = SearchEngine
::legalSearchChars() . '&#;';
344 $t = $wgContLang->stripForSearch( $title );
345 $t = preg_replace( "/[^{$lc}]+/", ' ', $t );
346 $t = strtolower( $t );
349 $t = preg_replace( "/([{$lc}]+)'s( |$)/", "\\1 \\1's ", $t );
350 $t = preg_replace( "/([{$lc}]+)s'( |$)/", "\\1s ", $t );
352 $t = preg_replace( "/\\s+/", ' ', $t );
354 if ( $ns == NS_IMAGE
) {
355 $t = preg_replace( "/ (png|gif|jpg|jpeg|ogg)$/", "", $t );
361 * Make a prefixed DB key from a DB key and a namespace index
362 * @param int $ns numerical representation of the namespace
363 * @param string $title the DB key form the title
364 * @return string the prefixed form of the title
366 /* static */ function makeName( $ns, $title ) {
369 $n = $wgContLang->getNsText( $ns );
370 return $n == '' ?
$title : "$n:$title";
374 * Returns the URL associated with an interwiki prefix
375 * @param string $key the interwiki prefix (e.g. "MeatBall")
376 * @return the associated URL, containing "$1", which should be
377 * replaced by an article title
381 function getInterwikiLink( $key ) {
382 global $wgMemc, $wgDBname, $wgInterwikiExpiry, $wgInterwikiCache;
383 $fname = 'Title::getInterwikiLink';
385 wfProfileIn( $fname );
387 $key = strtolower( $key );
389 $k = $wgDBname.':interwiki:'.$key;
390 if( array_key_exists( $k, $wgTitleInterwikiCache ) ) {
391 wfProfileOut( $fname );
392 return $wgTitleInterwikiCache[$k]->iw_url
;
395 if ($wgInterwikiCache) {
396 wfProfileOut( $fname );
397 return getInterwikiCached( $key );
400 $s = $wgMemc->get( $k );
401 # Ignore old keys with no iw_local
402 if( $s && isset( $s->iw_local
) && isset($s->iw_trans
)) {
403 $wgTitleInterwikiCache[$k] = $s;
404 wfProfileOut( $fname );
408 $dbr =& wfGetDB( DB_SLAVE
);
409 $res = $dbr->select( 'interwiki',
410 array( 'iw_url', 'iw_local', 'iw_trans' ),
411 array( 'iw_prefix' => $key ), $fname );
413 wfProfileOut( $fname );
417 $s = $dbr->fetchObject( $res );
419 # Cache non-existence: create a blank object and save it to memcached
425 $wgMemc->set( $k, $s, $wgInterwikiExpiry );
426 $wgTitleInterwikiCache[$k] = $s;
428 wfProfileOut( $fname );
433 * Fetch interwiki prefix data from local cache in constant database
435 * More logic is explained in DefaultSettings
437 * @return string URL of interwiki site
440 function getInterwikiCached( $key ) {
441 global $wgDBname, $wgInterwikiCache, $wgInterwikiScopes, $wgInterwikiFallbackSite;
442 global $wgTitleInterwikiCache;
445 $db=dba_open($wgInterwikiCache,'r','cdb');
446 /* Resolve site name */
447 if ($wgInterwikiScopes>=3 and !$site) {
448 $site = dba_fetch("__sites:{$wgDBname}", $db);
450 $site = $wgInterwikiFallbackSite;
452 $value = dba_fetch("{$wgDBname}:{$key}", $db);
453 if ($value=='' and $wgInterwikiScopes>=3) {
455 $value = dba_fetch("_{$site}:{$key}", $db);
457 if ($value=='' and $wgInterwikiScopes>=2) {
459 $value = dba_fetch("__globals:{$key}", $db);
468 list($local,$url)=explode(' ',$value,2);
472 $wgTitleInterwikiCache[$wgDBname.':interwiki:'.$key] = $s;
476 * Determine whether the object refers to a page within
479 * @return bool TRUE if this is an in-project interwiki link
480 * or a wikilink, FALSE otherwise
484 global $wgTitleInterwikiCache, $wgDBname;
486 if ( $this->mInterwiki
!= '' ) {
487 # Make sure key is loaded into cache
488 $this->getInterwikiLink( $this->mInterwiki
);
489 $k = $wgDBname.':interwiki:' . $this->mInterwiki
;
490 return (bool)($wgTitleInterwikiCache[$k]->iw_local
);
497 * Determine whether the object refers to a page within
498 * this project and is transcludable.
500 * @return bool TRUE if this is transcludable
504 global $wgTitleInterwikiCache, $wgDBname;
506 if ($this->mInterwiki
== '' ||
!$this->isLocal())
508 # Make sure key is loaded into cache
509 $this->getInterwikiLink( $this->mInterwiki
);
510 $k = $wgDBname.':interwiki:' . $this->mInterwiki
;
511 return (bool)($wgTitleInterwikiCache[$k]->iw_trans
);
515 * Update the page_touched field for an array of title objects
516 * @todo Inefficient unless the IDs are already loaded into the
518 * @param array $titles an array of Title objects to be touched
519 * @param string $timestamp the timestamp to use instead of the
520 * default current time
524 function touchArray( $titles, $timestamp = '' ) {
526 if ( count( $titles ) == 0 ) {
529 $dbw =& wfGetDB( DB_MASTER
);
530 if ( $timestamp == '' ) {
531 $timestamp = $dbw->timestamp();
534 $page = $dbw->tableName( 'page' );
535 $sql = "UPDATE $page SET page_touched='{$timestamp}' WHERE page_id IN (";
538 foreach ( $titles as $title ) {
539 if ( $wgUseFileCache ) {
540 $cm = new CacheManager($title);
541 @unlink($cm->fileCacheName());
548 $sql .= $title->getArticleID();
552 $dbw->query( $sql, 'Title::touchArray' );
555 // hack hack hack -- brion 2005-07-11. this was unfriendly to db.
556 // do them in small chunks:
557 $fname = 'Title::touchArray';
558 foreach( $titles as $title ) {
559 $dbw->update( 'page',
560 array( 'page_touched' => $timestamp ),
562 'page_namespace' => $title->getNamespace(),
563 'page_title' => $title->getDBkey() ),
568 #----------------------------------------------------------------------------
570 #----------------------------------------------------------------------------
572 /** Simple accessors */
574 * Get the text form (spaces not underscores) of the main part
578 function getText() { return $this->mTextform
; }
580 * Get the URL-encoded form of the main part
584 function getPartialURL() { return $this->mUrlform
; }
586 * Get the main part with underscores
590 function getDBkey() { return $this->mDbkeyform
; }
592 * Get the namespace index, i.e. one of the NS_xxxx constants
596 function getNamespace() { return $this->mNamespace
; }
598 * Get the namespace text
602 function getNsText() {
604 return $wgContLang->getNsText( $this->mNamespace
);
607 * Get the namespace text of the subject (rather than talk) page
611 function getSubjectNsText() {
613 return $wgContLang->getNsText( Namespace::getSubject( $this->mNamespace
) );
617 * Get the interwiki prefix (or null string)
621 function getInterwiki() { return $this->mInterwiki
; }
623 * Get the Title fragment (i.e. the bit after the #)
627 function getFragment() { return $this->mFragment
; }
629 * Get the default namespace index, for when there is no namespace
633 function getDefaultNamespace() { return $this->mDefaultNamespace
; }
636 * Get title for search index
637 * @return string a stripped-down title string ready for the
640 function getIndexTitle() {
641 return Title
::indexTitle( $this->mNamespace
, $this->mTextform
);
645 * Get the prefixed database key form
646 * @return string the prefixed title, with underscores and
647 * any interwiki and namespace prefixes
650 function getPrefixedDBkey() {
651 $s = $this->prefix( $this->mDbkeyform
);
652 $s = str_replace( ' ', '_', $s );
657 * Get the prefixed title with spaces.
658 * This is the form usually used for display
659 * @return string the prefixed title, with spaces
662 function getPrefixedText() {
664 if ( empty( $this->mPrefixedText
) ) { // FIXME: bad usage of empty() ?
665 $s = $this->prefix( $this->mTextform
);
666 $s = str_replace( '_', ' ', $s );
667 $this->mPrefixedText
= $s;
669 return $this->mPrefixedText
;
673 * Get the prefixed title with spaces, plus any fragment
674 * (part beginning with '#')
675 * @return string the prefixed title, with spaces and
676 * the fragment, including '#'
679 function getFullText() {
681 $text = $this->getPrefixedText();
682 if( '' != $this->mFragment
) {
683 $text .= '#' . $this->mFragment
;
689 * Get a URL-encoded title (not an actual URL) including interwiki
690 * @return string the URL-encoded form
693 function getPrefixedURL() {
694 $s = $this->prefix( $this->mDbkeyform
);
695 $s = str_replace( ' ', '_', $s );
697 $s = wfUrlencode ( $s ) ;
699 # Cleaning up URL to make it look nice -- is this safe?
700 $s = str_replace( '%28', '(', $s );
701 $s = str_replace( '%29', ')', $s );
707 * Get a real URL referring to this title, with interwiki link and
710 * @param string $query an optional query string, not used
711 * for interwiki links
712 * @return string the URL
715 function getFullURL( $query = '' ) {
716 global $wgContLang, $wgServer, $wgRequest;
718 if ( '' == $this->mInterwiki
) {
719 $url = $this->getLocalUrl( $query );
721 // Ugly quick hack to avoid duplicate prefixes (bug 4571 etc)
722 // Correct fix would be to move the prepending elsewhere.
723 if ($wgRequest->getVal('action') != 'render') {
724 $url = $wgServer . $url;
727 $baseUrl = $this->getInterwikiLink( $this->mInterwiki
);
729 $namespace = $wgContLang->getNsText( $this->mNamespace
);
730 if ( '' != $namespace ) {
731 # Can this actually happen? Interwikis shouldn't be parsed.
734 $url = str_replace( '$1', $namespace . $this->mUrlform
, $baseUrl );
736 if( false === strpos( $url, '?' ) ) {
743 if ( '' != $this->mFragment
) {
744 $url .= '#' . $this->mFragment
;
747 wfRunHooks( 'GetFullURL', array( &$this, &$url, $query ) );
752 * Get a URL with no fragment or server name. If this page is generated
753 * with action=render, $wgServer is prepended.
754 * @param string $query an optional query string; if not specified,
755 * $wgArticlePath will be used.
756 * @return string the URL
759 function getLocalURL( $query = '' ) {
760 global $wgArticlePath, $wgScript, $wgServer, $wgRequest;
762 if ( $this->isExternal() ) {
763 $url = $this->getFullURL();
765 $dbkey = wfUrlencode( $this->getPrefixedDBkey() );
766 if ( $query == '' ) {
767 $url = str_replace( '$1', $dbkey, $wgArticlePath );
769 global $wgActionPaths;
771 if( !empty( $wgActionPaths ) &&
772 preg_match( '/^(.*&|)action=([^&]*)(&(.*)|)$/', $query, $matches ) )
774 $action = urldecode( $matches[2] );
775 if( isset( $wgActionPaths[$action] ) ) {
776 $query = $matches[1];
777 if( isset( $matches[4] ) ) $query .= $matches[4];
778 $url = str_replace( '$1', $dbkey, $wgActionPaths[$action] );
779 if( $query != '' ) $url .= '?' . $query;
782 if ( $url === false ) {
783 if ( $query == '-' ) {
786 $url = "{$wgScript}?title={$dbkey}&{$query}";
790 // FIXME: this causes breakage in various places when we
791 // actually expected a local URL and end up with dupe prefixes.
792 if ($wgRequest->getVal('action') == 'render') {
793 $url = $wgServer . $url;
796 wfRunHooks( 'GetLocalURL', array( &$this, &$url, $query ) );
801 * Get an HTML-escaped version of the URL form, suitable for
802 * using in a link, without a server name or fragment
803 * @param string $query an optional query string
804 * @return string the URL
807 function escapeLocalURL( $query = '' ) {
808 return htmlspecialchars( $this->getLocalURL( $query ) );
812 * Get an HTML-escaped version of the URL form, suitable for
813 * using in a link, including the server name and fragment
815 * @return string the URL
816 * @param string $query an optional query string
819 function escapeFullURL( $query = '' ) {
820 return htmlspecialchars( $this->getFullURL( $query ) );
824 * Get the URL form for an internal link.
825 * - Used in various Squid-related code, in case we have a different
826 * internal hostname for the server from the exposed one.
828 * @param string $query an optional query string
829 * @return string the URL
832 function getInternalURL( $query = '' ) {
833 global $wgInternalServer;
834 $url = $wgInternalServer . $this->getLocalURL( $query );
835 wfRunHooks( 'GetInternalURL', array( &$this, &$url, $query ) );
840 * Get the edit URL for this Title
841 * @return string the URL, or a null string if this is an
845 function getEditURL() {
846 global $wgServer, $wgScript;
848 if ( '' != $this->mInterwiki
) { return ''; }
849 $s = $this->getLocalURL( 'action=edit' );
855 * Get the HTML-escaped displayable text form.
856 * Used for the title field in <a> tags.
857 * @return string the text, including any prefixes
860 function getEscapedText() {
861 return htmlspecialchars( $this->getPrefixedText() );
865 * Is this Title interwiki?
869 function isExternal() { return ( '' != $this->mInterwiki
); }
872 * Does the title correspond to a protected article?
873 * @param string $what the action the page is protected from,
874 * by default checks move and edit
878 function isProtected( $action = '' ) {
879 global $wgRestrictionLevels;
880 if ( -1 == $this->mNamespace
) { return true; }
882 if( $action == 'edit' ||
$action == '' ) {
883 $r = $this->getRestrictions( 'edit' );
884 foreach( $wgRestrictionLevels as $level ) {
885 if( in_array( $level, $r ) && $level != '' ) {
891 if( $action == 'move' ||
$action == '' ) {
892 $r = $this->getRestrictions( 'move' );
893 foreach( $wgRestrictionLevels as $level ) {
894 if( in_array( $level, $r ) && $level != '' ) {
904 * Is $wgUser is watching this page?
908 function userIsWatching() {
911 if ( is_null( $this->mWatched
) ) {
912 if ( -1 == $this->mNamespace ||
0 == $wgUser->getID()) {
913 $this->mWatched
= false;
915 $this->mWatched
= $wgUser->isWatched( $this );
918 return $this->mWatched
;
922 * Can $wgUser perform $action this page?
923 * @param string $action action that permission needs to be checked for
927 function userCan($action) {
928 $fname = 'Title::userCan';
929 wfProfileIn( $fname );
932 if( NS_SPECIAL
== $this->mNamespace
) {
933 wfProfileOut( $fname );
936 // XXX: This is the code that prevents unprotecting a page in NS_MEDIAWIKI
937 // from taking effect -ævar
938 if( NS_MEDIAWIKI
== $this->mNamespace
&&
939 !$wgUser->isAllowed('editinterface') ) {
940 wfProfileOut( $fname );
944 if( $this->mDbkeyform
== '_' ) {
945 # FIXME: Is this necessary? Shouldn't be allowed anyway...
946 wfProfileOut( $fname );
950 # protect global styles and js
951 if ( NS_MEDIAWIKI
== $this->mNamespace
952 && preg_match("/\\.(css|js)$/", $this->mTextform
)
953 && !$wgUser->isAllowed('editinterface') ) {
954 wfProfileOut( $fname );
958 # protect css/js subpages of user pages
959 # XXX: this might be better using restrictions
960 # XXX: Find a way to work around the php bug that prevents using $this->userCanEditCssJsSubpage() from working
961 if( NS_USER
== $this->mNamespace
962 && preg_match("/\\.(css|js)$/", $this->mTextform
)
963 && !$wgUser->isAllowed('editinterface')
964 && !preg_match('/^'.preg_quote($wgUser->getName(), '/').'\//', $this->mTextform
) ) {
965 wfProfileOut( $fname );
969 foreach( $this->getRestrictions($action) as $right ) {
970 // Backwards compatibility, rewrite sysop -> protect
971 if ( $right == 'sysop' ) {
974 if( '' != $right && !$wgUser->isAllowed( $right ) ) {
975 wfProfileOut( $fname );
980 if( $action == 'move' &&
981 !( $this->isMovable() && $wgUser->isAllowed( 'move' ) ) ) {
982 wfProfileOut( $fname );
986 if( $action == 'create' ) {
987 if( ( $this->isTalkPage() && !$wgUser->isAllowed( 'createtalk' ) ) ||
988 ( !$this->isTalkPage() && !$wgUser->isAllowed( 'createpage' ) ) ) {
993 wfProfileOut( $fname );
998 * Can $wgUser edit this page?
1002 function userCanEdit() {
1003 return $this->userCan('edit');
1007 * Can $wgUser move this page?
1011 function userCanMove() {
1012 return $this->userCan('move');
1016 * Would anybody with sufficient privileges be able to move this page?
1017 * Some pages just aren't movable.
1022 function isMovable() {
1023 return Namespace::isMovable( $this->getNamespace() )
1024 && $this->getInterwiki() == '';
1028 * Can $wgUser read this page?
1032 function userCanRead() {
1035 if( $wgUser->isAllowed('read') ) {
1038 global $wgWhitelistRead;
1040 /** If anon users can create an account,
1041 they need to reach the login page first! */
1042 if( $wgUser->isAllowed( 'createaccount' )
1043 && $this->getNamespace() == NS_SPECIAL
1044 && $this->getText() == 'Userlogin' ) {
1048 /** some pages are explicitly allowed */
1049 $name = $this->getPrefixedText();
1050 if( $wgWhitelistRead && in_array( $name, $wgWhitelistRead ) ) {
1054 # Compatibility with old settings
1055 if( $wgWhitelistRead && $this->getNamespace() == NS_MAIN
) {
1056 if( in_array( ':' . $name, $wgWhitelistRead ) ) {
1065 * Is this a talk page of some sort?
1069 function isTalkPage() {
1070 return Namespace::isTalk( $this->getNamespace() );
1074 * Is this a .css or .js subpage of a user page?
1078 function isCssJsSubpage() {
1079 return ( NS_USER
== $this->mNamespace
and preg_match("/\\.(css|js)$/", $this->mTextform
) );
1082 * Is this a .css subpage of a user page?
1086 function isCssSubpage() {
1087 return ( NS_USER
== $this->mNamespace
and preg_match("/\\.css$/", $this->mTextform
) );
1090 * Is this a .js subpage of a user page?
1094 function isJsSubpage() {
1095 return ( NS_USER
== $this->mNamespace
and preg_match("/\\.js$/", $this->mTextform
) );
1098 * Protect css/js subpages of user pages: can $wgUser edit
1102 * @todo XXX: this might be better using restrictions
1105 function userCanEditCssJsSubpage() {
1107 return ( $wgUser->isAllowed('editinterface') or preg_match('/^'.preg_quote($wgUser->getName(), '/').'\//', $this->mTextform
) );
1111 * Loads a string into mRestrictions array
1112 * @param string $res restrictions in string format
1115 function loadRestrictions( $res ) {
1116 foreach( explode( ':', trim( $res ) ) as $restrict ) {
1117 $temp = explode( '=', trim( $restrict ) );
1118 if(count($temp) == 1) {
1119 // old format should be treated as edit/move restriction
1120 $this->mRestrictions
["edit"] = explode( ',', trim( $temp[0] ) );
1121 $this->mRestrictions
["move"] = explode( ',', trim( $temp[0] ) );
1123 $this->mRestrictions
[$temp[0]] = explode( ',', trim( $temp[1] ) );
1126 $this->mRestrictionsLoaded
= true;
1130 * Accessor/initialisation for mRestrictions
1131 * @param string $action action that permission needs to be checked for
1132 * @return array the array of groups allowed to edit this article
1135 function getRestrictions($action) {
1136 $id = $this->getArticleID();
1137 if ( 0 == $id ) { return array(); }
1139 if ( ! $this->mRestrictionsLoaded
) {
1140 $dbr =& wfGetDB( DB_SLAVE
);
1141 $res = $dbr->selectField( 'page', 'page_restrictions', 'page_id='.$id );
1142 $this->loadRestrictions( $res );
1144 if( isset( $this->mRestrictions
[$action] ) ) {
1145 return $this->mRestrictions
[$action];
1151 * Is there a version of this page in the deletion archive?
1152 * @return int the number of archived revisions
1155 function isDeleted() {
1156 $fname = 'Title::isDeleted';
1157 if ( $this->getNamespace() < 0 ) {
1160 $dbr =& wfGetDB( DB_SLAVE
);
1161 $n = $dbr->selectField( 'archive', 'COUNT(*)', array( 'ar_namespace' => $this->getNamespace(),
1162 'ar_title' => $this->getDBkey() ), $fname );
1168 * Get the article ID for this Title from the link cache,
1169 * adding it if necessary
1170 * @param int $flags a bit field; may be GAID_FOR_UPDATE to select
1172 * @return int the ID
1175 function getArticleID( $flags = 0 ) {
1176 $linkCache =& LinkCache
::singleton();
1177 if ( $flags & GAID_FOR_UPDATE
) {
1178 $oldUpdate = $linkCache->forUpdate( true );
1179 $this->mArticleID
= $linkCache->addLinkObj( $this );
1180 $linkCache->forUpdate( $oldUpdate );
1182 if ( -1 == $this->mArticleID
) {
1183 $this->mArticleID
= $linkCache->addLinkObj( $this );
1186 return $this->mArticleID
;
1189 function getLatestRevID() {
1190 if ($this->mLatestID
!== false)
1191 return $this->mLatestID
;
1193 $db =& wfGetDB(DB_SLAVE
);
1194 return $this->mLatestID
= $db->selectField( 'revision',
1196 array('rev_page' => $this->getArticleID()),
1197 'Title::getLatestRevID' );
1201 * This clears some fields in this object, and clears any associated
1202 * keys in the "bad links" section of the link cache.
1204 * - This is called from Article::insertNewArticle() to allow
1205 * loading of the new page_id. It's also called from
1206 * Article::doDeleteArticle()
1208 * @param int $newid the new Article ID
1211 function resetArticleID( $newid ) {
1212 $linkCache =& LinkCache
::singleton();
1213 $linkCache->clearBadLink( $this->getPrefixedDBkey() );
1215 if ( 0 == $newid ) { $this->mArticleID
= -1; }
1216 else { $this->mArticleID
= $newid; }
1217 $this->mRestrictionsLoaded
= false;
1218 $this->mRestrictions
= array();
1222 * Updates page_touched for this page; called from LinksUpdate.php
1223 * @return bool true if the update succeded
1226 function invalidateCache() {
1227 global $wgUseFileCache;
1229 if ( wfReadOnly() ) {
1233 $dbw =& wfGetDB( DB_MASTER
);
1234 $success = $dbw->update( 'page',
1236 'page_touched' => $dbw->timestamp()
1237 ), array( /* WHERE */
1238 'page_namespace' => $this->getNamespace() ,
1239 'page_title' => $this->getDBkey()
1240 ), 'Title::invalidateCache'
1243 if ($wgUseFileCache) {
1244 $cache = new CacheManager($this);
1245 @unlink
($cache->fileCacheName());
1252 * Prefix some arbitrary text with the namespace or interwiki prefix
1255 * @param string $name the text
1256 * @return string the prefixed text
1259 /* private */ function prefix( $name ) {
1263 if ( '' != $this->mInterwiki
) {
1264 $p = $this->mInterwiki
. ':';
1266 if ( 0 != $this->mNamespace
) {
1267 $p .= $wgContLang->getNsText( $this->mNamespace
) . ':';
1273 * Secure and split - main initialisation function for this object
1275 * Assumes that mDbkeyform has been set, and is urldecoded
1276 * and uses underscores, but not otherwise munged. This function
1277 * removes illegal characters, splits off the interwiki and
1278 * namespace prefixes, sets the other forms, and canonicalizes
1280 * @return bool true on success
1283 /* private */ function secureAndSplit() {
1284 global $wgContLang, $wgLocalInterwiki, $wgCapitalLinks;
1285 $fname = 'Title::secureAndSplit';
1286 wfProfileIn( $fname );
1289 static $rxTc = false;
1291 # % is needed as well
1292 $rxTc = '/[^' . Title
::legalChars() . ']|%[0-9A-Fa-f]{2}/S';
1295 $this->mInterwiki
= $this->mFragment
= '';
1296 $this->mNamespace
= $this->mDefaultNamespace
; # Usually NS_MAIN
1298 # Clean up whitespace
1300 $t = preg_replace( '/[ _]+/', '_', $this->mDbkeyform
);
1301 $t = trim( $t, '_' );
1304 wfProfileOut( $fname );
1308 if( false !== strpos( $t, UTF8_REPLACEMENT
) ) {
1309 # Contained illegal UTF-8 sequences or forbidden Unicode chars.
1310 wfProfileOut( $fname );
1314 $this->mDbkeyform
= $t;
1316 # Initial colon indicates main namespace rather than specified default
1317 # but should not create invalid {ns,title} pairs such as {0,Project:Foo}
1318 if ( ':' == $t{0} ) {
1319 $this->mNamespace
= NS_MAIN
;
1320 $t = substr( $t, 1 ); # remove the colon but continue processing
1323 # Namespace or interwiki prefix
1326 if ( preg_match( "/^(.+?)_*:_*(.*)$/S", $t, $m ) ) {
1328 $lowerNs = strtolower( $p );
1329 if ( $ns = Namespace::getCanonicalIndex( $lowerNs ) ) {
1330 # Canonical namespace
1332 $this->mNamespace
= $ns;
1333 } elseif ( $ns = $wgContLang->getNsIndex( $lowerNs )) {
1334 # Ordinary namespace
1336 $this->mNamespace
= $ns;
1337 } elseif( $this->getInterwikiLink( $p ) ) {
1339 # Can't make a local interwiki link to an interwiki link.
1340 # That's just crazy!
1341 wfProfileOut( $fname );
1347 $this->mInterwiki
= strtolower( $p );
1349 # Redundant interwiki prefix to the local wiki
1350 if ( 0 == strcasecmp( $this->mInterwiki
, $wgLocalInterwiki ) ) {
1352 # Can't have an empty self-link
1353 wfProfileOut( $fname );
1356 $this->mInterwiki
= '';
1358 # Do another namespace split...
1362 # If there's no recognized interwiki or namespace,
1363 # then let the colon expression be part of the title.
1369 # We already know that some pages won't be in the database!
1371 if ( '' != $this->mInterwiki ||
-1 == $this->mNamespace
) {
1372 $this->mArticleID
= 0;
1374 $f = strstr( $r, '#' );
1375 if ( false !== $f ) {
1376 $this->mFragment
= substr( $f, 1 );
1377 $r = substr( $r, 0, strlen( $r ) - strlen( $f ) );
1378 # remove whitespace again: prevents "Foo_bar_#"
1379 # becoming "Foo_bar_"
1380 $r = preg_replace( '/_*$/', '', $r );
1383 # Reject illegal characters.
1385 if( preg_match( $rxTc, $r ) ) {
1386 wfProfileOut( $fname );
1391 * Pages with "/./" or "/../" appearing in the URLs will
1392 * often be unreachable due to the way web browsers deal
1393 * with 'relative' URLs. Forbid them explicitly.
1395 if ( strpos( $r, '.' ) !== false &&
1396 ( $r === '.' ||
$r === '..' ||
1397 strpos( $r, './' ) === 0 ||
1398 strpos( $r, '../' ) === 0 ||
1399 strpos( $r, '/./' ) !== false ||
1400 strpos( $r, '/../' ) !== false ) )
1402 wfProfileOut( $fname );
1406 # We shouldn't need to query the DB for the size.
1407 #$maxSize = $dbr->textFieldSize( 'page', 'page_title' );
1408 if ( strlen( $r ) > 255 ) {
1409 wfProfileOut( $fname );
1414 * Normally, all wiki links are forced to have
1415 * an initial capital letter so [[foo]] and [[Foo]]
1416 * point to the same place.
1418 * Don't force it for interwikis, since the other
1419 * site might be case-sensitive.
1421 if( $wgCapitalLinks && $this->mInterwiki
== '') {
1422 $t = $wgContLang->ucfirst( $r );
1428 * Can't make a link to a namespace alone...
1429 * "empty" local links can only be self-links
1430 * with a fragment identifier.
1433 $this->mInterwiki
== '' &&
1434 $this->mNamespace
!= NS_MAIN
) {
1435 wfProfileOut( $fname );
1440 $this->mDbkeyform
= $t;
1441 $this->mUrlform
= wfUrlencode( $t );
1443 $this->mTextform
= str_replace( '_', ' ', $t );
1445 wfProfileOut( $fname );
1450 * Get a Title object associated with the talk page of this article
1451 * @return Title the object for the talk page
1454 function getTalkPage() {
1455 return Title
::makeTitle( Namespace::getTalk( $this->getNamespace() ), $this->getDBkey() );
1459 * Get a title object associated with the subject page of this
1462 * @return Title the object for the subject page
1465 function getSubjectPage() {
1466 return Title
::makeTitle( Namespace::getSubject( $this->getNamespace() ), $this->getDBkey() );
1470 * Get an array of Title objects linking to this Title
1471 * Also stores the IDs in the link cache.
1473 * @param string $options may be FOR UPDATE
1474 * @return array the Title objects linking here
1477 function getLinksTo( $options = '', $table = 'pagelinks', $prefix = 'pl' ) {
1478 $linkCache =& LinkCache
::singleton();
1479 $id = $this->getArticleID();
1482 $db =& wfGetDB( DB_MASTER
);
1484 $db =& wfGetDB( DB_SLAVE
);
1487 $res = $db->select( array( 'page', $table ),
1488 array( 'page_namespace', 'page_title', 'page_id' ),
1490 "{$prefix}_from=page_id",
1491 "{$prefix}_namespace" => $this->getNamespace(),
1492 "{$prefix}_title" => $this->getDbKey() ),
1493 'Title::getLinksTo',
1497 if ( $db->numRows( $res ) ) {
1498 while ( $row = $db->fetchObject( $res ) ) {
1499 if ( $titleObj = Title
::makeTitle( $row->page_namespace
, $row->page_title
) ) {
1500 $linkCache->addGoodLinkObj( $row->page_id
, $titleObj );
1501 $retVal[] = $titleObj;
1505 $db->freeResult( $res );
1510 * Get an array of Title objects using this Title as a template
1511 * Also stores the IDs in the link cache.
1513 * @param string $options may be FOR UPDATE
1514 * @return array the Title objects linking here
1517 function getTemplateLinksTo( $options = '' ) {
1518 return $this->getLinksTo( $options, 'templatelinks', 'tl' );
1522 * Get an array of Title objects referring to non-existent articles linked from this page
1524 * @param string $options may be FOR UPDATE
1525 * @return array the Title objects
1528 function getBrokenLinksFrom( $options = '' ) {
1530 $db =& wfGetDB( DB_MASTER
);
1532 $db =& wfGetDB( DB_SLAVE
);
1535 $res = $db->safeQuery(
1536 "SELECT pl_namespace, pl_title
1539 ON pl_namespace=page_namespace
1540 AND pl_title=page_title
1542 AND page_namespace IS NULL
1544 $db->tableName( 'pagelinks' ),
1545 $db->tableName( 'page' ),
1546 $this->getArticleId(),
1550 if ( $db->numRows( $res ) ) {
1551 while ( $row = $db->fetchObject( $res ) ) {
1552 $retVal[] = Title
::makeTitle( $row->pl_namespace
, $row->pl_title
);
1555 $db->freeResult( $res );
1561 * Get a list of URLs to purge from the Squid cache when this
1564 * @return array the URLs
1567 function getSquidURLs() {
1569 $this->getInternalURL(),
1570 $this->getInternalURL( 'action=history' )
1575 * Move this page without authentication
1576 * @param Title &$nt the new page Title
1579 function moveNoAuth( &$nt ) {
1580 return $this->moveTo( $nt, false );
1584 * Check whether a given move operation would be valid.
1585 * Returns true if ok, or a message key string for an error message
1586 * if invalid. (Scarrrrry ugly interface this.)
1587 * @param Title &$nt the new title
1588 * @param bool $auth indicates whether $wgUser's permissions
1590 * @return mixed true on success, message name on failure
1593 function isValidMoveOperation( &$nt, $auth = true ) {
1595 if( !$this or !$nt ) {
1596 return 'badtitletext';
1598 if( $this->equals( $nt ) ) {
1601 if( !$this->isMovable() ||
!$nt->isMovable() ) {
1602 return 'immobile_namespace';
1605 $oldid = $this->getArticleID();
1606 $newid = $nt->getArticleID();
1608 if ( strlen( $nt->getDBkey() ) < 1 ) {
1609 return 'articleexists';
1611 if ( ( '' == $this->getDBkey() ) ||
1613 ( '' == $nt->getDBkey() ) ) {
1614 return 'badarticleerror';
1618 !$this->userCanEdit() ||
!$nt->userCanEdit() ||
1619 !$this->userCanMove() ||
!$nt->userCanMove() ) ) {
1620 return 'protectedpage';
1623 # The move is allowed only if (1) the target doesn't exist, or
1624 # (2) the target is a redirect to the source, and has no history
1625 # (so we can undo bad moves right after they're done).
1627 if ( 0 != $newid ) { # Target exists; check for validity
1628 if ( ! $this->isValidMoveTarget( $nt ) ) {
1629 return 'articleexists';
1636 * Move a title to a new location
1637 * @param Title &$nt the new title
1638 * @param bool $auth indicates whether $wgUser's permissions
1640 * @return mixed true on success, message name on failure
1643 function moveTo( &$nt, $auth = true, $reason = '' ) {
1644 $err = $this->isValidMoveOperation( $nt, $auth );
1645 if( is_string( $err ) ) {
1649 $pageid = $this->getArticleID();
1650 if( $nt->exists() ) {
1651 $this->moveOverExistingRedirect( $nt, $reason );
1652 $pageCountChange = 0;
1653 } else { # Target didn't exist, do normal move.
1654 $this->moveToNewTitle( $nt, $reason );
1655 $pageCountChange = 1;
1657 $redirid = $this->getArticleID();
1659 # Fixing category links (those without piped 'alternate' names) to be sorted under the new title
1660 $dbw =& wfGetDB( DB_MASTER
);
1661 $categorylinks = $dbw->tableName( 'categorylinks' );
1662 $sql = "UPDATE $categorylinks SET cl_sortkey=" . $dbw->addQuotes( $nt->getPrefixedText() ) .
1663 " WHERE cl_from=" . $dbw->addQuotes( $pageid ) .
1664 " AND cl_sortkey=" . $dbw->addQuotes( $this->getPrefixedText() );
1665 $dbw->query( $sql, 'SpecialMovepage::doSubmit' );
1669 $oldnamespace = $this->getNamespace() & ~
1;
1670 $newnamespace = $nt->getNamespace() & ~
1;
1671 $oldtitle = $this->getDBkey();
1672 $newtitle = $nt->getDBkey();
1674 if( $oldnamespace != $newnamespace ||
$oldtitle != $newtitle ) {
1675 WatchedItem
::duplicateEntries( $this, $nt );
1678 # Update search engine
1679 $u = new SearchUpdate( $pageid, $nt->getPrefixedDBkey() );
1681 $u = new SearchUpdate( $redirid, $this->getPrefixedDBkey(), '' );
1685 if ( $this->getNamespace() == NS_MAIN
and $nt->getNamespace() != NS_MAIN
) {
1686 # Moved out of main namespace
1687 # not viewed, edited, removing
1688 $u = new SiteStatsUpdate( 0, 1, -1, $pageCountChange);
1689 } elseif ( $this->getNamespace() != NS_MAIN
and $nt->getNamespace() == NS_MAIN
) {
1690 # Moved into main namespace
1691 # not viewed, edited, adding
1692 $u = new SiteStatsUpdate( 0, 1, +
1, $pageCountChange );
1693 } elseif ( $pageCountChange ) {
1695 $u = new SiteStatsUpdate( 0, 0, 0, 1 );
1704 wfRunHooks( 'TitleMoveComplete', array( &$this, &$nt, &$wgUser, $pageid, $redirid ) );
1709 * Move page to a title which is at present a redirect to the
1712 * @param Title &$nt the page to move to, which should currently
1716 function moveOverExistingRedirect( &$nt, $reason = '' ) {
1717 global $wgUser, $wgUseSquid, $wgMwRedir;
1718 $fname = 'Title::moveOverExistingRedirect';
1719 $comment = wfMsgForContent( '1movedto2', $this->getPrefixedText(), $nt->getPrefixedText() );
1722 $comment .= ": $reason";
1725 $now = wfTimestampNow();
1727 $newid = $nt->getArticleID();
1728 $oldid = $this->getArticleID();
1729 $dbw =& wfGetDB( DB_MASTER
);
1730 $linkCache =& LinkCache
::singleton();
1732 # Delete the old redirect. We don't save it to history since
1733 # by definition if we've got here it's rather uninteresting.
1734 # We have to remove it so that the next step doesn't trigger
1735 # a conflict on the unique namespace+title index...
1736 $dbw->delete( 'page', array( 'page_id' => $newid ), $fname );
1738 # Save a null revision in the page's history notifying of the move
1739 $nullRevision = Revision
::newNullRevision( $dbw, $oldid, $comment, true );
1740 $nullRevId = $nullRevision->insertOn( $dbw );
1742 # Change the name of the target page:
1743 $dbw->update( 'page',
1745 'page_touched' => $dbw->timestamp($now),
1746 'page_namespace' => $nt->getNamespace(),
1747 'page_title' => $nt->getDBkey(),
1748 'page_latest' => $nullRevId,
1750 /* WHERE */ array( 'page_id' => $oldid ),
1753 $linkCache->clearLink( $nt->getPrefixedDBkey() );
1755 # Recreate the redirect, this time in the other direction.
1756 $redirectText = $wgMwRedir->getSynonym( 0 ) . ' [[' . $nt->getPrefixedText() . "]]\n";
1757 $redirectArticle = new Article( $this );
1758 $newid = $redirectArticle->insertOn( $dbw );
1759 $redirectRevision = new Revision( array(
1761 'comment' => $comment,
1762 'text' => $redirectText ) );
1763 $revid = $redirectRevision->insertOn( $dbw );
1764 $redirectArticle->updateRevisionOn( $dbw, $redirectRevision, 0 );
1765 $linkCache->clearLink( $this->getPrefixedDBkey() );
1768 $log = new LogPage( 'move' );
1769 $log->addEntry( 'move_redir', $this, $reason, array( 1 => $nt->getPrefixedText() ) );
1771 # Now, we record the link from the redirect to the new title.
1772 # It should have no other outgoing links...
1773 $dbw->delete( 'pagelinks', array( 'pl_from' => $newid ), $fname );
1774 $dbw->insert( 'pagelinks',
1776 'pl_from' => $newid,
1777 'pl_namespace' => $nt->getNamespace(),
1778 'pl_title' => $nt->getDbKey() ),
1782 if ( $wgUseSquid ) {
1783 $urls = array_merge( $nt->getSquidURLs(), $this->getSquidURLs() );
1784 $u = new SquidUpdate( $urls );
1790 * Move page to non-existing title.
1791 * @param Title &$nt the new Title
1794 function moveToNewTitle( &$nt, $reason = '' ) {
1795 global $wgUser, $wgUseSquid;
1797 $fname = 'MovePageForm::moveToNewTitle';
1798 $comment = wfMsgForContent( '1movedto2', $this->getPrefixedText(), $nt->getPrefixedText() );
1800 $comment .= ": $reason";
1803 $newid = $nt->getArticleID();
1804 $oldid = $this->getArticleID();
1805 $dbw =& wfGetDB( DB_MASTER
);
1806 $now = $dbw->timestamp();
1808 $linkCache =& LinkCache
::singleton();
1810 # Save a null revision in the page's history notifying of the move
1811 $nullRevision = Revision
::newNullRevision( $dbw, $oldid, $comment, true );
1812 $nullRevId = $nullRevision->insertOn( $dbw );
1815 $dbw->update( 'page',
1817 'page_touched' => $now,
1818 'page_namespace' => $nt->getNamespace(),
1819 'page_title' => $nt->getDBkey(),
1820 'page_latest' => $nullRevId,
1822 /* WHERE */ array( 'page_id' => $oldid ),
1826 $linkCache->clearLink( $nt->getPrefixedDBkey() );
1829 $redirectText = $wgMwRedir->getSynonym( 0 ) . ' [[' . $nt->getPrefixedText() . "]]\n";
1830 $redirectArticle = new Article( $this );
1831 $newid = $redirectArticle->insertOn( $dbw );
1832 $redirectRevision = new Revision( array(
1834 'comment' => $comment,
1835 'text' => $redirectText ) );
1836 $revid = $redirectRevision->insertOn( $dbw );
1837 $redirectArticle->updateRevisionOn( $dbw, $redirectRevision, 0 );
1838 $linkCache->clearLink( $this->getPrefixedDBkey() );
1841 $log = new LogPage( 'move' );
1842 $log->addEntry( 'move', $this, $reason, array( 1 => $nt->getPrefixedText()) );
1844 # Purge caches as per article creation
1845 Article
::onArticleCreate( $nt );
1847 # Record the just-created redirect's linking to the page
1848 $dbw->insert( 'pagelinks',
1850 'pl_from' => $newid,
1851 'pl_namespace' => $nt->getNamespace(),
1852 'pl_title' => $nt->getDBkey() ),
1855 # Non-existent target may have had broken links to it; these must
1856 # now be touched to update link coloring.
1859 # Purge old title from squid
1860 # The new title, and links to the new title, are purged in Article::onArticleCreate()
1861 $titles = $nt->getLinksTo();
1862 if ( $wgUseSquid ) {
1863 $urls = $this->getSquidURLs();
1864 foreach ( $titles as $linkTitle ) {
1865 $urls[] = $linkTitle->getInternalURL();
1867 $u = new SquidUpdate( $urls );
1873 * Checks if $this can be moved to a given Title
1874 * - Selects for update, so don't call it unless you mean business
1876 * @param Title &$nt the new title to check
1879 function isValidMoveTarget( $nt ) {
1881 $fname = 'Title::isValidMoveTarget';
1882 $dbw =& wfGetDB( DB_MASTER
);
1885 $id = $nt->getArticleID();
1886 $obj = $dbw->selectRow( array( 'page', 'revision', 'text'),
1887 array( 'page_is_redirect','old_text','old_flags' ),
1888 array( 'page_id' => $id, 'page_latest=rev_id', 'rev_text_id=old_id' ),
1889 $fname, 'FOR UPDATE' );
1891 if ( !$obj ||
0 == $obj->page_is_redirect
) {
1895 $text = Revision
::getRevisionText( $obj );
1897 # Does the redirect point to the source?
1898 if ( preg_match( "/\\[\\[\\s*([^\\]\\|]*)]]/", $text, $m ) ) {
1899 $redirTitle = Title
::newFromText( $m[1] );
1900 if( !is_object( $redirTitle ) ||
1901 $redirTitle->getPrefixedDBkey() != $this->getPrefixedDBkey() ) {
1909 # Does the article have a history?
1910 $row = $dbw->selectRow( array( 'page', 'revision'),
1912 array( 'page_namespace' => $nt->getNamespace(),
1913 'page_title' => $nt->getDBkey(),
1914 'page_id=rev_page AND page_latest != rev_id'
1915 ), $fname, 'FOR UPDATE'
1918 # Return true if there was no history
1919 return $row === false;
1923 * Create a redirect; fails if the title already exists; does
1926 * @param Title $dest the destination of the redirect
1927 * @param string $comment the comment string describing the move
1928 * @return bool true on success
1931 function createRedirect( $dest, $comment ) {
1933 if ( $this->getArticleID() ) {
1937 $fname = 'Title::createRedirect';
1938 $dbw =& wfGetDB( DB_MASTER
);
1940 $article = new Article( $this );
1941 $newid = $article->insertOn( $dbw );
1942 $revision = new Revision( array(
1944 'comment' => $comment,
1945 'text' => "#REDIRECT [[" . $dest->getPrefixedText() . "]]\n",
1947 $revisionId = $revision->insertOn( $dbw );
1948 $article->updateRevisionOn( $dbw, $revision, 0 );
1951 $dbw->insert( 'pagelinks',
1953 'pl_from' => $newid,
1954 'pl_namespace' => $dest->getNamespace(),
1955 'pl_title' => $dest->getDbKey()
1959 Article
::onArticleCreate( $this );
1964 * Get categories to which this Title belongs and return an array of
1965 * categories' names.
1967 * @return array an array of parents in the form:
1968 * $parent => $currentarticle
1971 function getParentCategories() {
1972 global $wgContLang,$wgUser;
1974 $titlekey = $this->getArticleId();
1975 $dbr =& wfGetDB( DB_SLAVE
);
1976 $categorylinks = $dbr->tableName( 'categorylinks' );
1979 $sql = "SELECT * FROM $categorylinks"
1980 ." WHERE cl_from='$titlekey'"
1981 ." AND cl_from <> '0'"
1982 ." ORDER BY cl_sortkey";
1984 $res = $dbr->query ( $sql ) ;
1986 if($dbr->numRows($res) > 0) {
1987 while ( $x = $dbr->fetchObject ( $res ) )
1988 //$data[] = Title::newFromText($wgContLang->getNSText ( NS_CATEGORY ).':'.$x->cl_to);
1989 $data[$wgContLang->getNSText ( NS_CATEGORY
).':'.$x->cl_to
] = $this->getFullText();
1990 $dbr->freeResult ( $res ) ;
1998 * Get a tree of parent categories
1999 * @param array $children an array with the children in the keys, to check for circular refs
2003 function getParentCategoryTree( $children = array() ) {
2004 $parents = $this->getParentCategories();
2006 if($parents != '') {
2007 foreach($parents as $parent => $current) {
2008 if ( array_key_exists( $parent, $children ) ) {
2009 # Circular reference
2010 $stack[$parent] = array();
2012 $nt = Title
::newFromText($parent);
2013 $stack[$parent] = $nt->getParentCategoryTree( $children +
array($parent => 1) );
2024 * Get an associative array for selecting this title from
2030 function pageCond() {
2031 return array( 'page_namespace' => $this->mNamespace
, 'page_title' => $this->mDbkeyform
);
2035 * Get the revision ID of the previous revision
2037 * @param integer $revision Revision ID. Get the revision that was before this one.
2038 * @return interger $oldrevision|false
2040 function getPreviousRevisionID( $revision ) {
2041 $dbr =& wfGetDB( DB_SLAVE
);
2042 return $dbr->selectField( 'revision', 'rev_id',
2043 'rev_page=' . intval( $this->getArticleId() ) .
2044 ' AND rev_id<' . intval( $revision ) . ' ORDER BY rev_id DESC' );
2048 * Get the revision ID of the next revision
2050 * @param integer $revision Revision ID. Get the revision that was after this one.
2051 * @return interger $oldrevision|false
2053 function getNextRevisionID( $revision ) {
2054 $dbr =& wfGetDB( DB_SLAVE
);
2055 return $dbr->selectField( 'revision', 'rev_id',
2056 'rev_page=' . intval( $this->getArticleId() ) .
2057 ' AND rev_id>' . intval( $revision ) . ' ORDER BY rev_id' );
2061 * Compare with another title.
2063 * @param Title $title
2066 function equals( $title ) {
2067 return $this->getInterwiki() == $title->getInterwiki()
2068 && $this->getNamespace() == $title->getNamespace()
2069 && $this->getDbkey() == $title->getDbkey();
2073 * Check if page exists
2077 return $this->getArticleId() != 0;
2081 * Should a link should be displayed as a known link, just based on its title?
2083 * Currently, a self-link with a fragment and special pages are in
2084 * this category. Special pages never exist in the database.
2086 function isAlwaysKnown() {
2087 return $this->isExternal() ||
( 0 == $this->mNamespace
&& "" == $this->mDbkeyform
)
2088 || NS_SPECIAL
== $this->mNamespace
;
2092 * Update page_touched timestamps on pages linking to this title.
2093 * In principal, this could be backgrounded and could also do squid
2096 function touchLinks() {
2097 $fname = 'Title::touchLinks';
2099 $dbw =& wfGetDB( DB_MASTER
);
2101 $res = $dbw->select( 'pagelinks',
2104 'pl_namespace' => $this->getNamespace(),
2105 'pl_title' => $this->getDbKey() ),
2108 $toucharr = array();
2109 while( $row = $dbw->fetchObject( $res ) ) {
2110 $toucharr[] = $row->pl_from
;
2112 $dbw->freeResult( $res );
2114 if( $this->getNamespace() == NS_CATEGORY
) {
2115 // Categories show up in a separate set of links as well
2116 $res = $dbw->select( 'categorylinks',
2118 array( 'cl_to' => $this->getDbKey() ),
2120 while( $row = $dbw->fetchObject( $res ) ) {
2121 $toucharr[] = $row->cl_from
;
2123 $dbw->freeResult( $res );
2126 if (!count($toucharr))
2128 $dbw->update( 'page', /* SET */ array( 'page_touched' => $dbw->timestamp() ),
2129 /* WHERE */ array( 'page_id' => $toucharr ),$fname);
2132 function trackbackURL() {
2133 global $wgTitle, $wgScriptPath, $wgServer;
2135 return "$wgServer$wgScriptPath/trackback.php?article="
2136 . htmlspecialchars(urlencode($wgTitle->getPrefixedDBkey()));
2139 function trackbackRDF() {
2140 $url = htmlspecialchars($this->getFullURL());
2141 $title = htmlspecialchars($this->getText());
2142 $tburl = $this->trackbackURL();
2145 <rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"
2146 xmlns:dc=\"http://purl.org/dc/elements/1.1/\"
2147 xmlns:trackback=\"http://madskills.com/public/xml/rss/module/trackback/\">
2150 dc:identifier=\"$url\"
2152 trackback:ping=\"$tburl\" />