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 if( $t->secureAndSplit() ) {
139 if( $defaultNamespace == NS_MAIN
) {
140 if( count( $wgTitleCache ) >= MW_TITLECACHE_MAX
) {
141 # Avoid memory leaks on mass operations...
142 $wgTitleCache = array();
144 $wgTitleCache[$text] =& $t;
146 wfProfileOut( $fname );
149 wfProfileOut( $fname );
156 * Create a new Title from URL-encoded text. Ensures that
157 * the given title's length does not exceed the maximum.
158 * @param string $url the title, as might be taken from a URL
159 * @return Title the new object, or NULL on an error
163 function newFromURL( $url ) {
164 global $wgLegalTitleChars;
167 # For compatibility with old buggy URLs. "+" is usually not valid in titles,
168 # but some URLs used it as a space replacement and they still come
169 # from some external search tools.
170 if ( strpos( $wgLegalTitleChars, '+' ) === false ) {
171 $url = str_replace( '+', ' ', $url );
174 $t->mDbkeyform
= str_replace( ' ', '_', $url );
175 if( $t->secureAndSplit() ) {
183 * Create a new Title from an article ID
185 * @todo This is inefficiently implemented, the page row is requested
186 * but not used for anything else
188 * @param int $id the page_id corresponding to the Title to create
189 * @return Title the new object, or NULL on an error
193 function newFromID( $id ) {
194 $fname = 'Title::newFromID';
195 $dbr =& wfGetDB( DB_SLAVE
);
196 $row = $dbr->selectRow( 'page', array( 'page_namespace', 'page_title' ),
197 array( 'page_id' => $id ), $fname );
198 if ( $row !== false ) {
199 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
207 * Create a new Title from a namespace index and a DB key.
208 * It's assumed that $ns and $title are *valid*, for instance when
209 * they came directly from the database or a special page name.
210 * For convenience, spaces are converted to underscores so that
211 * eg user_text fields can be used directly.
213 * @param int $ns the namespace of the article
214 * @param string $title the unprefixed database key form
215 * @return Title the new object
219 function &makeTitle( $ns, $title ) {
223 $t->mNamespace
= intval( $ns );
224 $t->mDbkeyform
= str_replace( ' ', '_', $title );
225 $t->mArticleID
= ( $ns >= 0 ) ?
-1 : 0;
226 $t->mUrlform
= wfUrlencode( $t->mDbkeyform
);
227 $t->mTextform
= str_replace( '_', ' ', $title );
232 * Create a new Title frrom a namespace index and a DB key.
233 * The parameters will be checked for validity, which is a bit slower
234 * than makeTitle() but safer for user-provided data.
236 * @param int $ns the namespace of the article
237 * @param string $title the database key form
238 * @return Title the new object, or NULL on an error
242 function makeTitleSafe( $ns, $title ) {
244 $t->mDbkeyform
= Title
::makeName( $ns, $title );
245 if( $t->secureAndSplit() ) {
253 * Create a new Title for the Main Page
256 * @return Title the new object
259 function newMainPage() {
260 return Title
::newFromText( wfMsgForContent( 'mainpage' ) );
264 * Create a new Title for a redirect
265 * @param string $text the redirect title text
266 * @return Title the new object, or NULL if the text is not a
271 function newFromRedirect( $text ) {
274 if ( $wgMwRedir->matchStart( $text ) ) {
275 if ( preg_match( '/\[{2}(.*?)(?:\||\]{2})/', $text, $m ) ) {
276 # categories are escaped using : for example one can enter:
277 # #REDIRECT [[:Category:Music]]. Need to remove it.
278 if ( substr($m[1],0,1) == ':') {
279 # We don't want to keep the ':'
280 $m[1] = substr( $m[1], 1 );
283 $rt = Title
::newFromText( $m[1] );
284 # Disallow redirects to Special:Userlogout
285 if ( !is_null($rt) && $rt->getNamespace() == NS_SPECIAL
&& preg_match( '/^Userlogout/i', $rt->getText() ) ) {
293 #----------------------------------------------------------------------------
295 #----------------------------------------------------------------------------
298 * Get the prefixed DB key associated with an ID
299 * @param int $id the page_id of the article
300 * @return Title an object representing the article, or NULL
301 * if no such article was found
305 function nameOf( $id ) {
306 $fname = 'Title::nameOf';
307 $dbr =& wfGetDB( DB_SLAVE
);
309 $s = $dbr->selectRow( 'page', array( 'page_namespace','page_title' ), array( 'page_id' => $id ), $fname );
310 if ( $s === false ) { return NULL; }
312 $n = Title
::makeName( $s->page_namespace
, $s->page_title
);
317 * Get a regex character class describing the legal characters in a link
318 * @return string the list of characters, not delimited
322 function legalChars() {
323 global $wgLegalTitleChars;
324 return $wgLegalTitleChars;
328 * Get a string representation of a title suitable for
329 * including in a search index
331 * @param int $ns a namespace index
332 * @param string $title text-form main part
333 * @return string a stripped-down title string ready for the
336 /* static */ function indexTitle( $ns, $title ) {
338 require_once( 'SearchEngine.php' );
340 $lc = SearchEngine
::legalSearchChars() . '&#;';
341 $t = $wgContLang->stripForSearch( $title );
342 $t = preg_replace( "/[^{$lc}]+/", ' ', $t );
343 $t = strtolower( $t );
346 $t = preg_replace( "/([{$lc}]+)'s( |$)/", "\\1 \\1's ", $t );
347 $t = preg_replace( "/([{$lc}]+)s'( |$)/", "\\1s ", $t );
349 $t = preg_replace( "/\\s+/", ' ', $t );
351 if ( $ns == NS_IMAGE
) {
352 $t = preg_replace( "/ (png|gif|jpg|jpeg|ogg)$/", "", $t );
358 * Make a prefixed DB key from a DB key and a namespace index
359 * @param int $ns numerical representation of the namespace
360 * @param string $title the DB key form the title
361 * @return string the prefixed form of the title
363 /* static */ function makeName( $ns, $title ) {
366 $n = $wgContLang->getNsText( $ns );
367 return $n == '' ?
$title : "$n:$title";
371 * Returns the URL associated with an interwiki prefix
372 * @param string $key the interwiki prefix (e.g. "MeatBall")
373 * @return the associated URL, containing "$1", which should be
374 * replaced by an article title
378 function getInterwikiLink( $key ) {
379 global $wgMemc, $wgDBname, $wgInterwikiExpiry, $wgTitleInterwikiCache;
380 $fname = 'Title::getInterwikiLink';
382 wfProfileIn( $fname );
384 $key = strtolower( $key );
386 $k = $wgDBname.':interwiki:'.$key;
387 if( array_key_exists( $k, $wgTitleInterwikiCache ) ) {
388 wfProfileOut( $fname );
389 return $wgTitleInterwikiCache[$k]->iw_url
;
392 $s = $wgMemc->get( $k );
393 # Ignore old keys with no iw_local
394 if( $s && isset( $s->iw_local
) && isset($s->iw_trans
)) {
395 $wgTitleInterwikiCache[$k] = $s;
396 wfProfileOut( $fname );
400 $dbr =& wfGetDB( DB_SLAVE
);
401 $res = $dbr->select( 'interwiki',
402 array( 'iw_url', 'iw_local', 'iw_trans' ),
403 array( 'iw_prefix' => $key ), $fname );
405 wfProfileOut( $fname );
409 $s = $dbr->fetchObject( $res );
411 # Cache non-existence: create a blank object and save it to memcached
417 $wgMemc->set( $k, $s, $wgInterwikiExpiry );
418 $wgTitleInterwikiCache[$k] = $s;
420 wfProfileOut( $fname );
425 * Determine whether the object refers to a page within
428 * @return bool TRUE if this is an in-project interwiki link
429 * or a wikilink, FALSE otherwise
433 global $wgTitleInterwikiCache, $wgDBname;
435 if ( $this->mInterwiki
!= '' ) {
436 # Make sure key is loaded into cache
437 $this->getInterwikiLink( $this->mInterwiki
);
438 $k = $wgDBname.':interwiki:' . $this->mInterwiki
;
439 return (bool)($wgTitleInterwikiCache[$k]->iw_local
);
446 * Determine whether the object refers to a page within
447 * this project and is transcludable.
449 * @return bool TRUE if this is transcludable
453 global $wgTitleInterwikiCache, $wgDBname;
455 if ($this->mInterwiki
== '' ||
!$this->isLocal())
457 # Make sure key is loaded into cache
458 $this->getInterwikiLink( $this->mInterwiki
);
459 $k = $wgDBname.':interwiki:' . $this->mInterwiki
;
460 return (bool)($wgTitleInterwikiCache[$k]->iw_trans
);
464 * Update the page_touched field for an array of title objects
465 * @todo Inefficient unless the IDs are already loaded into the
467 * @param array $titles an array of Title objects to be touched
468 * @param string $timestamp the timestamp to use instead of the
469 * default current time
473 function touchArray( $titles, $timestamp = '' ) {
475 if ( count( $titles ) == 0 ) {
478 $dbw =& wfGetDB( DB_MASTER
);
479 if ( $timestamp == '' ) {
480 $timestamp = $dbw->timestamp();
483 $page = $dbw->tableName( 'page' );
484 $sql = "UPDATE $page SET page_touched='{$timestamp}' WHERE page_id IN (";
487 foreach ( $titles as $title ) {
488 if ( $wgUseFileCache ) {
489 $cm = new CacheManager($title);
490 @unlink($cm->fileCacheName());
497 $sql .= $title->getArticleID();
501 $dbw->query( $sql, 'Title::touchArray' );
504 // hack hack hack -- brion 2005-07-11. this was unfriendly to db.
505 // do them in small chunks:
506 $fname = 'Title::touchArray';
507 foreach( $titles as $title ) {
508 $dbw->update( 'page',
509 array( 'page_touched' => $timestamp ),
511 'page_namespace' => $title->getNamespace(),
512 'page_title' => $title->getDBkey() ),
517 #----------------------------------------------------------------------------
519 #----------------------------------------------------------------------------
521 /** Simple accessors */
523 * Get the text form (spaces not underscores) of the main part
527 function getText() { return $this->mTextform
; }
529 * Get the URL-encoded form of the main part
533 function getPartialURL() { return $this->mUrlform
; }
535 * Get the main part with underscores
539 function getDBkey() { return $this->mDbkeyform
; }
541 * Get the namespace index, i.e. one of the NS_xxxx constants
545 function getNamespace() { return $this->mNamespace
; }
547 * Get the namespace text
551 function getNsText() {
553 return $wgContLang->getNsText( $this->mNamespace
);
556 * Get the namespace text of the subject (rather than talk) page
560 function getSubjectNsText() {
562 return $wgContLang->getNsText( Namespace::getSubject( $this->mNamespace
) );
566 * Get the interwiki prefix (or null string)
570 function getInterwiki() { return $this->mInterwiki
; }
572 * Get the Title fragment (i.e. the bit after the #)
576 function getFragment() { return $this->mFragment
; }
578 * Get the default namespace index, for when there is no namespace
582 function getDefaultNamespace() { return $this->mDefaultNamespace
; }
585 * Get title for search index
586 * @return string a stripped-down title string ready for the
589 function getIndexTitle() {
590 return Title
::indexTitle( $this->mNamespace
, $this->mTextform
);
594 * Get the prefixed database key form
595 * @return string the prefixed title, with underscores and
596 * any interwiki and namespace prefixes
599 function getPrefixedDBkey() {
600 $s = $this->prefix( $this->mDbkeyform
);
601 $s = str_replace( ' ', '_', $s );
606 * Get the prefixed title with spaces.
607 * This is the form usually used for display
608 * @return string the prefixed title, with spaces
611 function getPrefixedText() {
613 if ( empty( $this->mPrefixedText
) ) { // FIXME: bad usage of empty() ?
614 $s = $this->prefix( $this->mTextform
);
615 $s = str_replace( '_', ' ', $s );
616 $this->mPrefixedText
= $s;
618 return $this->mPrefixedText
;
622 * Get the prefixed title with spaces, plus any fragment
623 * (part beginning with '#')
624 * @return string the prefixed title, with spaces and
625 * the fragment, including '#'
628 function getFullText() {
630 $text = $this->getPrefixedText();
631 if( '' != $this->mFragment
) {
632 $text .= '#' . $this->mFragment
;
638 * Get a URL-encoded title (not an actual URL) including interwiki
639 * @return string the URL-encoded form
642 function getPrefixedURL() {
643 $s = $this->prefix( $this->mDbkeyform
);
644 $s = str_replace( ' ', '_', $s );
646 $s = wfUrlencode ( $s ) ;
648 # Cleaning up URL to make it look nice -- is this safe?
649 $s = str_replace( '%28', '(', $s );
650 $s = str_replace( '%29', ')', $s );
656 * Get a real URL referring to this title, with interwiki link and
659 * @param string $query an optional query string, not used
660 * for interwiki links
661 * @return string the URL
664 function getFullURL( $query = '' ) {
665 global $wgContLang, $wgServer;
667 if ( '' == $this->mInterwiki
) {
668 $url = $wgServer . $this->getLocalUrl( $query );
670 $baseUrl = $this->getInterwikiLink( $this->mInterwiki
);
672 $namespace = $wgContLang->getNsText( $this->mNamespace
);
673 if ( '' != $namespace ) {
674 # Can this actually happen? Interwikis shouldn't be parsed.
677 $url = str_replace( '$1', $namespace . $this->mUrlform
, $baseUrl );
679 if( false === strpos( $url, '?' ) ) {
686 if ( '' != $this->mFragment
) {
687 $url .= '#' . $this->mFragment
;
690 wfRunHooks( 'GetFullURL', array( &$this, &$url, $query ) );
695 * Get a URL with no fragment or server name. If this page is generated
696 * with action=render, $wgServer is prepended.
697 * @param string $query an optional query string; if not specified,
698 * $wgArticlePath will be used.
699 * @return string the URL
702 function getLocalURL( $query = '' ) {
703 global $wgArticlePath, $wgScript, $wgServer, $wgRequest;
705 if ( $this->isExternal() ) {
706 $url = $this->getFullURL();
708 $dbkey = wfUrlencode( $this->getPrefixedDBkey() );
709 if ( $query == '' ) {
710 $url = str_replace( '$1', $dbkey, $wgArticlePath );
712 global $wgActionPaths;
714 if( !empty( $wgActionPaths ) &&
715 preg_match( '/^(.*&|)action=([^&]*)(&(.*)|)$/', $query, $matches ) )
717 $action = urldecode( $matches[2] );
718 if( isset( $wgActionPaths[$action] ) ) {
719 $query = $matches[1];
720 if( isset( $matches[4] ) ) $query .= $matches[4];
721 $url = str_replace( '$1', $dbkey, $wgActionPaths[$action] );
722 if( $query != '' ) $url .= '?' . $query;
725 if ( $url === false ) {
726 if ( $query == '-' ) {
729 $url = "{$wgScript}?title={$dbkey}&{$query}";
733 if ($wgRequest->getText('action') == 'render') {
734 $url = $wgServer . $url;
737 wfRunHooks( 'GetLocalURL', array( &$this, &$url, $query ) );
742 * Get an HTML-escaped version of the URL form, suitable for
743 * using in a link, without a server name or fragment
744 * @param string $query an optional query string
745 * @return string the URL
748 function escapeLocalURL( $query = '' ) {
749 return htmlspecialchars( $this->getLocalURL( $query ) );
753 * Get an HTML-escaped version of the URL form, suitable for
754 * using in a link, including the server name and fragment
756 * @return string the URL
757 * @param string $query an optional query string
760 function escapeFullURL( $query = '' ) {
761 return htmlspecialchars( $this->getFullURL( $query ) );
765 * Get the URL form for an internal link.
766 * - Used in various Squid-related code, in case we have a different
767 * internal hostname for the server from the exposed one.
769 * @param string $query an optional query string
770 * @return string the URL
773 function getInternalURL( $query = '' ) {
774 global $wgInternalServer;
775 $url = $wgInternalServer . $this->getLocalURL( $query );
776 wfRunHooks( 'GetInternalURL', array( &$this, &$url, $query ) );
781 * Get the edit URL for this Title
782 * @return string the URL, or a null string if this is an
786 function getEditURL() {
787 global $wgServer, $wgScript;
789 if ( '' != $this->mInterwiki
) { return ''; }
790 $s = $this->getLocalURL( 'action=edit' );
796 * Get the HTML-escaped displayable text form.
797 * Used for the title field in <a> tags.
798 * @return string the text, including any prefixes
801 function getEscapedText() {
802 return htmlspecialchars( $this->getPrefixedText() );
806 * Is this Title interwiki?
810 function isExternal() { return ( '' != $this->mInterwiki
); }
813 * Does the title correspond to a protected article?
814 * @param string $what the action the page is protected from,
815 * by default checks move and edit
819 function isProtected($action = '') {
820 if ( -1 == $this->mNamespace
) { return true; }
821 if($action == 'edit' ||
$action == '') {
822 $a = $this->getRestrictions("edit");
823 if ( in_array( 'sysop', $a ) ) { return true; }
825 if($action == 'move' ||
$action == '') {
826 $a = $this->getRestrictions("move");
827 if ( in_array( 'sysop', $a ) ) { return true; }
833 * Is $wgUser is watching this page?
837 function userIsWatching() {
840 if ( is_null( $this->mWatched
) ) {
841 if ( -1 == $this->mNamespace ||
0 == $wgUser->getID()) {
842 $this->mWatched
= false;
844 $this->mWatched
= $wgUser->isWatched( $this );
847 return $this->mWatched
;
851 * Can $wgUser perform $action this page?
852 * @param string $action action that permission needs to be checked for
856 function userCan($action) {
857 $fname = 'Title::userCan';
858 wfProfileIn( $fname );
861 if( NS_SPECIAL
== $this->mNamespace
) {
862 wfProfileOut( $fname );
865 // XXX: This is the code that prevents unprotecting a page in NS_MEDIAWIKI
866 // from taking effect -ævar
867 if( NS_MEDIAWIKI
== $this->mNamespace
&&
868 !$wgUser->isAllowed('editinterface') ) {
869 wfProfileOut( $fname );
873 if( $this->mDbkeyform
== '_' ) {
874 # FIXME: Is this necessary? Shouldn't be allowed anyway...
875 wfProfileOut( $fname );
879 # protect global styles and js
880 if ( NS_MEDIAWIKI
== $this->mNamespace
881 && preg_match("/\\.(css|js)$/", $this->mTextform
)
882 && !$wgUser->isAllowed('editinterface') ) {
883 wfProfileOut( $fname );
887 # protect css/js subpages of user pages
888 # XXX: this might be better using restrictions
889 # XXX: Find a way to work around the php bug that prevents using $this->userCanEditCssJsSubpage() from working
890 if( NS_USER
== $this->mNamespace
891 && preg_match("/\\.(css|js)$/", $this->mTextform
)
892 && !$wgUser->isAllowed('editinterface')
893 && !preg_match('/^'.preg_quote($wgUser->getName(), '/').'\//', $this->mTextform
) ) {
894 wfProfileOut( $fname );
898 foreach( $this->getRestrictions($action) as $right ) {
899 // Backwards compatibility, rewrite sysop -> protect
900 if ( $right == 'sysop' ) {
903 if( '' != $right && !$wgUser->isAllowed( $right ) ) {
904 wfProfileOut( $fname );
909 if( $action == 'move' &&
910 !( $this->isMovable() && $wgUser->isAllowed( 'move' ) ) ) {
911 wfProfileOut( $fname );
915 if( $action == 'create' ) {
916 if( ( $this->isTalkPage() && !$wgUser->isAllowed( 'createtalk' ) ) ||
917 ( !$this->isTalkPage() && !$wgUser->isAllowed( 'createpage' ) ) ) {
922 wfProfileOut( $fname );
927 * Can $wgUser edit this page?
931 function userCanEdit() {
932 return $this->userCan('edit');
936 * Can $wgUser move this page?
940 function userCanMove() {
941 return $this->userCan('move');
945 * Would anybody with sufficient privileges be able to move this page?
946 * Some pages just aren't movable.
951 function isMovable() {
952 return Namespace::isMovable( $this->getNamespace() )
953 && $this->getInterwiki() == '';
957 * Can $wgUser read this page?
961 function userCanRead() {
964 if( $wgUser->isAllowed('read') ) {
967 global $wgWhitelistRead;
969 /** If anon users can create an account,
970 they need to reach the login page first! */
971 if( $wgUser->isAllowed( 'createaccount' )
972 && $this->getNamespace() == NS_SPECIAL
973 && $this->getText() == 'Userlogin' ) {
977 /** some pages are explicitly allowed */
978 $name = $this->getPrefixedText();
979 if( $wgWhitelistRead && in_array( $name, $wgWhitelistRead ) ) {
983 # Compatibility with old settings
984 if( $wgWhitelistRead && $this->getNamespace() == NS_MAIN
) {
985 if( in_array( ':' . $name, $wgWhitelistRead ) ) {
994 * Is this a talk page of some sort?
998 function isTalkPage() {
999 return Namespace::isTalk( $this->getNamespace() );
1003 * Is this a .css or .js subpage of a user page?
1007 function isCssJsSubpage() {
1008 return ( NS_USER
== $this->mNamespace
and preg_match("/\\.(css|js)$/", $this->mTextform
) );
1011 * Is this a .css subpage of a user page?
1015 function isCssSubpage() {
1016 return ( NS_USER
== $this->mNamespace
and preg_match("/\\.css$/", $this->mTextform
) );
1019 * Is this a .js subpage of a user page?
1023 function isJsSubpage() {
1024 return ( NS_USER
== $this->mNamespace
and preg_match("/\\.js$/", $this->mTextform
) );
1027 * Protect css/js subpages of user pages: can $wgUser edit
1031 * @todo XXX: this might be better using restrictions
1034 function userCanEditCssJsSubpage() {
1036 return ( $wgUser->isAllowed('editinterface') or preg_match('/^'.preg_quote($wgUser->getName(), '/').'\//', $this->mTextform
) );
1040 * Loads a string into mRestrictions array
1041 * @param string $res restrictions in string format
1044 function loadRestrictions( $res ) {
1045 foreach( explode( ':', trim( $res ) ) as $restrict ) {
1046 $temp = explode( '=', trim( $restrict ) );
1047 if(count($temp) == 1) {
1048 // old format should be treated as edit/move restriction
1049 $this->mRestrictions
["edit"] = explode( ',', trim( $temp[0] ) );
1050 $this->mRestrictions
["move"] = explode( ',', trim( $temp[0] ) );
1052 $this->mRestrictions
[$temp[0]] = explode( ',', trim( $temp[1] ) );
1055 $this->mRestrictionsLoaded
= true;
1059 * Accessor/initialisation for mRestrictions
1060 * @param string $action action that permission needs to be checked for
1061 * @return array the array of groups allowed to edit this article
1064 function getRestrictions($action) {
1065 $id = $this->getArticleID();
1066 if ( 0 == $id ) { return array(); }
1068 if ( ! $this->mRestrictionsLoaded
) {
1069 $dbr =& wfGetDB( DB_SLAVE
);
1070 $res = $dbr->selectField( 'page', 'page_restrictions', 'page_id='.$id );
1071 $this->loadRestrictions( $res );
1073 if( isset( $this->mRestrictions
[$action] ) ) {
1074 return $this->mRestrictions
[$action];
1080 * Is there a version of this page in the deletion archive?
1081 * @return int the number of archived revisions
1084 function isDeleted() {
1085 $fname = 'Title::isDeleted';
1086 if ( $this->getNamespace() < 0 ) {
1089 $dbr =& wfGetDB( DB_SLAVE
);
1090 $n = $dbr->selectField( 'archive', 'COUNT(*)', array( 'ar_namespace' => $this->getNamespace(),
1091 'ar_title' => $this->getDBkey() ), $fname );
1097 * Get the article ID for this Title from the link cache,
1098 * adding it if necessary
1099 * @param int $flags a bit field; may be GAID_FOR_UPDATE to select
1101 * @return int the ID
1104 function getArticleID( $flags = 0 ) {
1105 $linkCache =& LinkCache
::singleton();
1106 if ( $flags & GAID_FOR_UPDATE
) {
1107 $oldUpdate = $linkCache->forUpdate( true );
1108 $this->mArticleID
= $linkCache->addLinkObj( $this );
1109 $linkCache->forUpdate( $oldUpdate );
1111 if ( -1 == $this->mArticleID
) {
1112 $this->mArticleID
= $linkCache->addLinkObj( $this );
1115 return $this->mArticleID
;
1118 function getLatestRevID() {
1119 if ($this->mLatestID
!== false)
1120 return $this->mLatestID
;
1122 $db =& wfGetDB(DB_SLAVE
);
1123 return $this->mLatestID
= $db->selectField( 'revision',
1125 array('rev_page' => $this->getArticleID()),
1126 'Title::getLatestRevID' );
1130 * This clears some fields in this object, and clears any associated
1131 * keys in the "bad links" section of the link cache.
1133 * - This is called from Article::insertNewArticle() to allow
1134 * loading of the new page_id. It's also called from
1135 * Article::doDeleteArticle()
1137 * @param int $newid the new Article ID
1140 function resetArticleID( $newid ) {
1141 $linkCache =& LinkCache
::singleton();
1142 $linkCache->clearBadLink( $this->getPrefixedDBkey() );
1144 if ( 0 == $newid ) { $this->mArticleID
= -1; }
1145 else { $this->mArticleID
= $newid; }
1146 $this->mRestrictionsLoaded
= false;
1147 $this->mRestrictions
= array();
1151 * Updates page_touched for this page; called from LinksUpdate.php
1152 * @return bool true if the update succeded
1155 function invalidateCache() {
1156 global $wgUseFileCache;
1158 if ( wfReadOnly() ) {
1162 $dbw =& wfGetDB( DB_MASTER
);
1163 $success = $dbw->update( 'page',
1165 'page_touched' => $dbw->timestamp()
1166 ), array( /* WHERE */
1167 'page_namespace' => $this->getNamespace() ,
1168 'page_title' => $this->getDBkey()
1169 ), 'Title::invalidateCache'
1172 if ($wgUseFileCache) {
1173 $cache = new CacheManager($this);
1174 @unlink
($cache->fileCacheName());
1181 * Prefix some arbitrary text with the namespace or interwiki prefix
1184 * @param string $name the text
1185 * @return string the prefixed text
1188 /* private */ function prefix( $name ) {
1192 if ( '' != $this->mInterwiki
) {
1193 $p = $this->mInterwiki
. ':';
1195 if ( 0 != $this->mNamespace
) {
1196 $p .= $wgContLang->getNsText( $this->mNamespace
) . ':';
1202 * Secure and split - main initialisation function for this object
1204 * Assumes that mDbkeyform has been set, and is urldecoded
1205 * and uses underscores, but not otherwise munged. This function
1206 * removes illegal characters, splits off the interwiki and
1207 * namespace prefixes, sets the other forms, and canonicalizes
1209 * @return bool true on success
1212 /* private */ function secureAndSplit() {
1213 global $wgContLang, $wgLocalInterwiki, $wgCapitalLinks;
1214 $fname = 'Title::secureAndSplit';
1215 wfProfileIn( $fname );
1218 static $rxTc = false;
1220 # % is needed as well
1221 $rxTc = '/[^' . Title
::legalChars() . ']|%[0-9A-Fa-f]{2}/S';
1224 $this->mInterwiki
= $this->mFragment
= '';
1225 $this->mNamespace
= $this->mDefaultNamespace
; # Usually NS_MAIN
1227 # Clean up whitespace
1229 $t = preg_replace( '/[ _]+/', '_', $this->mDbkeyform
);
1230 $t = trim( $t, '_' );
1233 wfProfileOut( $fname );
1237 if( false !== strpos( $t, UTF8_REPLACEMENT
) ) {
1238 # Contained illegal UTF-8 sequences or forbidden Unicode chars.
1239 wfProfileOut( $fname );
1243 $this->mDbkeyform
= $t;
1245 # Initial colon indicates main namespace rather than specified default
1246 # but should not create invalid {ns,title} pairs such as {0,Project:Foo}
1247 if ( ':' == $t{0} ) {
1248 $this->mNamespace
= NS_MAIN
;
1249 $t = substr( $t, 1 ); # remove the colon but continue processing
1252 # Namespace or interwiki prefix
1255 if ( preg_match( "/^(.+?)_*:_*(.*)$/S", $t, $m ) ) {
1257 $lowerNs = strtolower( $p );
1258 if ( $ns = Namespace::getCanonicalIndex( $lowerNs ) ) {
1259 # Canonical namespace
1261 $this->mNamespace
= $ns;
1262 } elseif ( $ns = $wgContLang->getNsIndex( $lowerNs )) {
1263 # Ordinary namespace
1265 $this->mNamespace
= $ns;
1266 } elseif( $this->getInterwikiLink( $p ) ) {
1268 # Can't make a local interwiki link to an interwiki link.
1269 # That's just crazy!
1270 wfProfileOut( $fname );
1276 $this->mInterwiki
= strtolower( $p );
1278 # Redundant interwiki prefix to the local wiki
1279 if ( 0 == strcasecmp( $this->mInterwiki
, $wgLocalInterwiki ) ) {
1281 # Can't have an empty self-link
1282 wfProfileOut( $fname );
1285 $this->mInterwiki
= '';
1287 # Do another namespace split...
1291 # If there's no recognized interwiki or namespace,
1292 # then let the colon expression be part of the title.
1298 # We already know that some pages won't be in the database!
1300 if ( '' != $this->mInterwiki ||
-1 == $this->mNamespace
) {
1301 $this->mArticleID
= 0;
1303 $f = strstr( $r, '#' );
1304 if ( false !== $f ) {
1305 $this->mFragment
= substr( $f, 1 );
1306 $r = substr( $r, 0, strlen( $r ) - strlen( $f ) );
1307 # remove whitespace again: prevents "Foo_bar_#"
1308 # becoming "Foo_bar_"
1309 $r = preg_replace( '/_*$/', '', $r );
1312 # Reject illegal characters.
1314 if( preg_match( $rxTc, $r ) ) {
1315 wfProfileOut( $fname );
1320 * Pages with "/./" or "/../" appearing in the URLs will
1321 * often be unreachable due to the way web browsers deal
1322 * with 'relative' URLs. Forbid them explicitly.
1324 if ( strpos( $r, '.' ) !== false &&
1325 ( $r === '.' ||
$r === '..' ||
1326 strpos( $r, './' ) === 0 ||
1327 strpos( $r, '../' ) === 0 ||
1328 strpos( $r, '/./' ) !== false ||
1329 strpos( $r, '/../' ) !== false ) )
1331 wfProfileOut( $fname );
1335 # We shouldn't need to query the DB for the size.
1336 #$maxSize = $dbr->textFieldSize( 'page', 'page_title' );
1337 if ( strlen( $r ) > 255 ) {
1338 wfProfileOut( $fname );
1343 * Normally, all wiki links are forced to have
1344 * an initial capital letter so [[foo]] and [[Foo]]
1345 * point to the same place.
1347 * Don't force it for interwikis, since the other
1348 * site might be case-sensitive.
1350 if( $wgCapitalLinks && $this->mInterwiki
== '') {
1351 $t = $wgContLang->ucfirst( $r );
1357 * Can't make a link to a namespace alone...
1358 * "empty" local links can only be self-links
1359 * with a fragment identifier.
1362 $this->mInterwiki
== '' &&
1363 $this->mNamespace
!= NS_MAIN
) {
1364 wfProfileOut( $fname );
1369 $this->mDbkeyform
= $t;
1370 $this->mUrlform
= wfUrlencode( $t );
1372 $this->mTextform
= str_replace( '_', ' ', $t );
1374 wfProfileOut( $fname );
1379 * Get a Title object associated with the talk page of this article
1380 * @return Title the object for the talk page
1383 function getTalkPage() {
1384 return Title
::makeTitle( Namespace::getTalk( $this->getNamespace() ), $this->getDBkey() );
1388 * Get a title object associated with the subject page of this
1391 * @return Title the object for the subject page
1394 function getSubjectPage() {
1395 return Title
::makeTitle( Namespace::getSubject( $this->getNamespace() ), $this->getDBkey() );
1399 * Get an array of Title objects linking to this Title
1400 * Also stores the IDs in the link cache.
1402 * @param string $options may be FOR UPDATE
1403 * @return array the Title objects linking here
1406 function getLinksTo( $options = '', $table = 'pagelinks', $prefix = 'pl' ) {
1407 $linkCache =& LinkCache
::singleton();
1408 $id = $this->getArticleID();
1411 $db =& wfGetDB( DB_MASTER
);
1413 $db =& wfGetDB( DB_SLAVE
);
1416 $res = $db->select( array( 'page', $table ),
1417 array( 'page_namespace', 'page_title', 'page_id' ),
1419 "{$prefix}_from=page_id",
1420 "{$prefix}_namespace" => $this->getNamespace(),
1421 "{$prefix}_title" => $this->getDbKey() ),
1422 'Title::getLinksTo',
1426 if ( $db->numRows( $res ) ) {
1427 while ( $row = $db->fetchObject( $res ) ) {
1428 if ( $titleObj = Title
::makeTitle( $row->page_namespace
, $row->page_title
) ) {
1429 $linkCache->addGoodLinkObj( $row->page_id
, $titleObj );
1430 $retVal[] = $titleObj;
1434 $db->freeResult( $res );
1439 * Get an array of Title objects using this Title as a template
1440 * Also stores the IDs in the link cache.
1442 * @param string $options may be FOR UPDATE
1443 * @return array the Title objects linking here
1446 function getTemplateLinksTo( $options = '' ) {
1447 return $this->getLinksTo( $options, 'templatelinks', 'tl' );
1451 * Get an array of Title objects referring to non-existent articles linked from this page
1453 * @param string $options may be FOR UPDATE
1454 * @return array the Title objects
1457 function getBrokenLinksFrom( $options = '' ) {
1459 $db =& wfGetDB( DB_MASTER
);
1461 $db =& wfGetDB( DB_SLAVE
);
1464 $res = $db->safeQuery(
1465 "SELECT pl_namespace, pl_title
1468 ON pl_namespace=page_namespace
1469 AND pl_title=page_title
1471 AND page_namespace IS NULL
1473 $db->tableName( 'pagelinks' ),
1474 $db->tableName( 'page' ),
1475 $this->getArticleId(),
1479 if ( $db->numRows( $res ) ) {
1480 while ( $row = $db->fetchObject( $res ) ) {
1481 $retVal[] = Title
::makeTitle( $row->pl_namespace
, $row->pl_title
);
1484 $db->freeResult( $res );
1490 * Get a list of URLs to purge from the Squid cache when this
1493 * @return array the URLs
1496 function getSquidURLs() {
1498 $this->getInternalURL(),
1499 $this->getInternalURL( 'action=history' )
1504 * Move this page without authentication
1505 * @param Title &$nt the new page Title
1508 function moveNoAuth( &$nt ) {
1509 return $this->moveTo( $nt, false );
1513 * Check whether a given move operation would be valid.
1514 * Returns true if ok, or a message key string for an error message
1515 * if invalid. (Scarrrrry ugly interface this.)
1516 * @param Title &$nt the new title
1517 * @param bool $auth indicates whether $wgUser's permissions
1519 * @return mixed true on success, message name on failure
1522 function isValidMoveOperation( &$nt, $auth = true ) {
1524 if( !$this or !$nt ) {
1525 return 'badtitletext';
1527 if( $this->equals( $nt ) ) {
1530 if( !$this->isMovable() ||
!$nt->isMovable() ) {
1531 return 'immobile_namespace';
1534 $oldid = $this->getArticleID();
1535 $newid = $nt->getArticleID();
1537 if ( strlen( $nt->getDBkey() ) < 1 ) {
1538 return 'articleexists';
1540 if ( ( '' == $this->getDBkey() ) ||
1542 ( '' == $nt->getDBkey() ) ) {
1543 return 'badarticleerror';
1547 !$this->userCanEdit() ||
!$nt->userCanEdit() ||
1548 !$this->userCanMove() ||
!$nt->userCanMove() ) ) {
1549 return 'protectedpage';
1552 # The move is allowed only if (1) the target doesn't exist, or
1553 # (2) the target is a redirect to the source, and has no history
1554 # (so we can undo bad moves right after they're done).
1556 if ( 0 != $newid ) { # Target exists; check for validity
1557 if ( ! $this->isValidMoveTarget( $nt ) ) {
1558 return 'articleexists';
1565 * Move a title to a new location
1566 * @param Title &$nt the new title
1567 * @param bool $auth indicates whether $wgUser's permissions
1569 * @return mixed true on success, message name on failure
1572 function moveTo( &$nt, $auth = true, $reason = '' ) {
1573 $err = $this->isValidMoveOperation( $nt, $auth );
1574 if( is_string( $err ) ) {
1578 $pageid = $this->getArticleID();
1579 if( $nt->exists() ) {
1580 $this->moveOverExistingRedirect( $nt, $reason );
1581 $pageCountChange = 0;
1582 } else { # Target didn't exist, do normal move.
1583 $this->moveToNewTitle( $nt, $newid, $reason );
1584 $pageCountChange = 1;
1586 $redirid = $this->getArticleID();
1588 # Fixing category links (those without piped 'alternate' names) to be sorted under the new title
1589 $dbw =& wfGetDB( DB_MASTER
);
1590 $categorylinks = $dbw->tableName( 'categorylinks' );
1591 $sql = "UPDATE $categorylinks SET cl_sortkey=" . $dbw->addQuotes( $nt->getPrefixedText() ) .
1592 " WHERE cl_from=" . $dbw->addQuotes( $pageid ) .
1593 " AND cl_sortkey=" . $dbw->addQuotes( $this->getPrefixedText() );
1594 $dbw->query( $sql, 'SpecialMovepage::doSubmit' );
1598 $oldnamespace = $this->getNamespace() & ~
1;
1599 $newnamespace = $nt->getNamespace() & ~
1;
1600 $oldtitle = $this->getDBkey();
1601 $newtitle = $nt->getDBkey();
1603 if( $oldnamespace != $newnamespace ||
$oldtitle != $newtitle ) {
1604 WatchedItem
::duplicateEntries( $this, $nt );
1607 # Update search engine
1608 $u = new SearchUpdate( $pageid, $nt->getPrefixedDBkey() );
1610 $u = new SearchUpdate( $redirid, $this->getPrefixedDBkey(), '' );
1614 if ( $this->getNamespace() == NS_MAIN
and $nt->getNamespace() != NS_MAIN
) {
1615 # Moved out of main namespace
1616 # not viewed, edited, removing
1617 $u = new SiteStatsUpdate( 0, 1, -1, $pageCountChange);
1618 } elseif ( $this->getNamespace() != NS_MAIN
and $nt->getNamespace() == NS_MAIN
) {
1619 # Moved into main namespace
1620 # not viewed, edited, adding
1621 $u = new SiteStatsUpdate( 0, 1, +
1, $pageCountChange );
1622 } elseif ( $pageCountChange ) {
1624 $u = new SiteStatsUpdate( 0, 0, 0, 1 );
1633 wfRunHooks( 'TitleMoveComplete', array( &$this, &$nt, &$wgUser, $pageid, $redirid ) );
1638 * Move page to a title which is at present a redirect to the
1641 * @param Title &$nt the page to move to, which should currently
1645 function moveOverExistingRedirect( &$nt, $reason = '' ) {
1646 global $wgUser, $wgUseSquid, $wgMwRedir;
1647 $fname = 'Title::moveOverExistingRedirect';
1648 $comment = wfMsgForContent( '1movedto2', $this->getPrefixedText(), $nt->getPrefixedText() );
1651 $comment .= ": $reason";
1654 $now = wfTimestampNow();
1656 $newid = $nt->getArticleID();
1657 $oldid = $this->getArticleID();
1658 $dbw =& wfGetDB( DB_MASTER
);
1659 $linkCache =& LinkCache
::singleton();
1661 # Delete the old redirect. We don't save it to history since
1662 # by definition if we've got here it's rather uninteresting.
1663 # We have to remove it so that the next step doesn't trigger
1664 # a conflict on the unique namespace+title index...
1665 $dbw->delete( 'page', array( 'page_id' => $newid ), $fname );
1667 # Save a null revision in the page's history notifying of the move
1668 $nullRevision = Revision
::newNullRevision( $dbw, $oldid,
1669 wfMsgForContent( '1movedto2', $this->getPrefixedText(), $nt->getPrefixedText() ),
1671 $nullRevId = $nullRevision->insertOn( $dbw );
1673 # Change the name of the target page:
1674 $dbw->update( 'page',
1676 'page_touched' => $dbw->timestamp($now),
1677 'page_namespace' => $nt->getNamespace(),
1678 'page_title' => $nt->getDBkey(),
1679 'page_latest' => $nullRevId,
1681 /* WHERE */ array( 'page_id' => $oldid ),
1684 $linkCache->clearLink( $nt->getPrefixedDBkey() );
1686 # Recreate the redirect, this time in the other direction.
1687 $redirectText = $wgMwRedir->getSynonym( 0 ) . ' [[' . $nt->getPrefixedText() . "]]\n";
1688 $redirectArticle = new Article( $this );
1689 $newid = $redirectArticle->insertOn( $dbw );
1690 $redirectRevision = new Revision( array(
1692 'comment' => $comment,
1693 'text' => $redirectText ) );
1694 $revid = $redirectRevision->insertOn( $dbw );
1695 $redirectArticle->updateRevisionOn( $dbw, $redirectRevision, 0 );
1696 $linkCache->clearLink( $this->getPrefixedDBkey() );
1699 $log = new LogPage( 'move' );
1700 $log->addEntry( 'move_redir', $this, $reason, array( 1 => $nt->getPrefixedText() ) );
1702 # Now, we record the link from the redirect to the new title.
1703 # It should have no other outgoing links...
1704 $dbw->delete( 'pagelinks', array( 'pl_from' => $newid ), $fname );
1705 $dbw->insert( 'pagelinks',
1707 'pl_from' => $newid,
1708 'pl_namespace' => $nt->getNamespace(),
1709 'pl_title' => $nt->getDbKey() ),
1713 if ( $wgUseSquid ) {
1714 $urls = array_merge( $nt->getSquidURLs(), $this->getSquidURLs() );
1715 $u = new SquidUpdate( $urls );
1721 * Move page to non-existing title.
1722 * @param Title &$nt the new Title
1723 * @param int &$newid set to be the new article ID
1726 function moveToNewTitle( &$nt, &$newid, $reason = '' ) {
1727 global $wgUser, $wgUseSquid;
1729 $fname = 'MovePageForm::moveToNewTitle';
1730 $comment = wfMsgForContent( '1movedto2', $this->getPrefixedText(), $nt->getPrefixedText() );
1732 $comment .= ": $reason";
1735 $newid = $nt->getArticleID();
1736 $oldid = $this->getArticleID();
1737 $dbw =& wfGetDB( DB_MASTER
);
1738 $now = $dbw->timestamp();
1740 $linkCache =& LinkCache
::singleton();
1742 # Save a null revision in the page's history notifying of the move
1743 $nullRevision = Revision
::newNullRevision( $dbw, $oldid,
1744 wfMsgForContent( '1movedto2', $this->getPrefixedText(), $nt->getPrefixedText() ),
1746 $nullRevId = $nullRevision->insertOn( $dbw );
1749 $dbw->update( 'page',
1751 'page_touched' => $now,
1752 'page_namespace' => $nt->getNamespace(),
1753 'page_title' => $nt->getDBkey(),
1754 'page_latest' => $nullRevId,
1756 /* WHERE */ array( 'page_id' => $oldid ),
1760 $linkCache->clearLink( $nt->getPrefixedDBkey() );
1763 $redirectText = $wgMwRedir->getSynonym( 0 ) . ' [[' . $nt->getPrefixedText() . "]]\n";
1764 $redirectArticle = new Article( $this );
1765 $newid = $redirectArticle->insertOn( $dbw );
1766 $redirectRevision = new Revision( array(
1768 'comment' => $comment,
1769 'text' => $redirectText ) );
1770 $revid = $redirectRevision->insertOn( $dbw );
1771 $redirectArticle->updateRevisionOn( $dbw, $redirectRevision, 0 );
1772 $linkCache->clearLink( $this->getPrefixedDBkey() );
1775 $log = new LogPage( 'move' );
1776 $log->addEntry( 'move', $this, $reason, array( 1 => $nt->getPrefixedText()) );
1778 # Purge caches as per article creation
1779 Article
::onArticleCreate( $nt );
1781 # Record the just-created redirect's linking to the page
1782 $dbw->insert( 'pagelinks',
1784 'pl_from' => $newid,
1785 'pl_namespace' => $nt->getNamespace(),
1786 'pl_title' => $nt->getDBkey() ),
1789 # Non-existent target may have had broken links to it; these must
1790 # now be touched to update link coloring.
1793 # Purge old title from squid
1794 # The new title, and links to the new title, are purged in Article::onArticleCreate()
1795 $titles = $nt->getLinksTo();
1796 if ( $wgUseSquid ) {
1797 $urls = $this->getSquidURLs();
1798 foreach ( $titles as $linkTitle ) {
1799 $urls[] = $linkTitle->getInternalURL();
1801 $u = new SquidUpdate( $urls );
1807 * Checks if $this can be moved to a given Title
1808 * - Selects for update, so don't call it unless you mean business
1810 * @param Title &$nt the new title to check
1813 function isValidMoveTarget( $nt ) {
1815 $fname = 'Title::isValidMoveTarget';
1816 $dbw =& wfGetDB( DB_MASTER
);
1819 $id = $nt->getArticleID();
1820 $obj = $dbw->selectRow( array( 'page', 'revision', 'text'),
1821 array( 'page_is_redirect','old_text','old_flags' ),
1822 array( 'page_id' => $id, 'page_latest=rev_id', 'rev_text_id=old_id' ),
1823 $fname, 'FOR UPDATE' );
1825 if ( !$obj ||
0 == $obj->page_is_redirect
) {
1829 $text = Revision
::getRevisionText( $obj );
1831 # Does the redirect point to the source?
1832 if ( preg_match( "/\\[\\[\\s*([^\\]\\|]*)]]/", $text, $m ) ) {
1833 $redirTitle = Title
::newFromText( $m[1] );
1834 if( !is_object( $redirTitle ) ||
1835 $redirTitle->getPrefixedDBkey() != $this->getPrefixedDBkey() ) {
1843 # Does the article have a history?
1844 $row = $dbw->selectRow( array( 'page', 'revision'),
1846 array( 'page_namespace' => $nt->getNamespace(),
1847 'page_title' => $nt->getDBkey(),
1848 'page_id=rev_page AND page_latest != rev_id'
1849 ), $fname, 'FOR UPDATE'
1852 # Return true if there was no history
1853 return $row === false;
1857 * Create a redirect; fails if the title already exists; does
1860 * @param Title $dest the destination of the redirect
1861 * @param string $comment the comment string describing the move
1862 * @return bool true on success
1865 function createRedirect( $dest, $comment ) {
1867 if ( $this->getArticleID() ) {
1871 $fname = 'Title::createRedirect';
1872 $dbw =& wfGetDB( DB_MASTER
);
1874 $article = new Article( $this );
1875 $newid = $article->insertOn( $dbw );
1876 $revision = new Revision( array(
1878 'comment' => $comment,
1879 'text' => "#REDIRECT [[" . $dest->getPrefixedText() . "]]\n",
1881 $revisionId = $revision->insertOn( $dbw );
1882 $article->updateRevisionOn( $dbw, $revision, 0 );
1885 $dbw->insert( 'pagelinks',
1887 'pl_from' => $newid,
1888 'pl_namespace' => $dest->getNamespace(),
1889 'pl_title' => $dest->getDbKey()
1893 Article
::onArticleCreate( $this );
1898 * Get categories to which this Title belongs and return an array of
1899 * categories' names.
1901 * @return array an array of parents in the form:
1902 * $parent => $currentarticle
1905 function getParentCategories() {
1906 global $wgContLang,$wgUser;
1908 $titlekey = $this->getArticleId();
1909 $dbr =& wfGetDB( DB_SLAVE
);
1910 $categorylinks = $dbr->tableName( 'categorylinks' );
1913 $sql = "SELECT * FROM $categorylinks"
1914 ." WHERE cl_from='$titlekey'"
1915 ." AND cl_from <> '0'"
1916 ." ORDER BY cl_sortkey";
1918 $res = $dbr->query ( $sql ) ;
1920 if($dbr->numRows($res) > 0) {
1921 while ( $x = $dbr->fetchObject ( $res ) )
1922 //$data[] = Title::newFromText($wgContLang->getNSText ( NS_CATEGORY ).':'.$x->cl_to);
1923 $data[$wgContLang->getNSText ( NS_CATEGORY
).':'.$x->cl_to
] = $this->getFullText();
1924 $dbr->freeResult ( $res ) ;
1932 * Get a tree of parent categories
1933 * @param array $children an array with the children in the keys, to check for circular refs
1937 function getParentCategoryTree( $children = array() ) {
1938 $parents = $this->getParentCategories();
1940 if($parents != '') {
1941 foreach($parents as $parent => $current) {
1942 if ( array_key_exists( $parent, $children ) ) {
1943 # Circular reference
1944 $stack[$parent] = array();
1946 $nt = Title
::newFromText($parent);
1947 $stack[$parent] = $nt->getParentCategoryTree( $children +
array($parent => 1) );
1958 * Get an associative array for selecting this title from
1964 function curCond() {
1965 wfDebugDieBacktrace( 'curCond called' );
1966 return array( 'cur_namespace' => $this->mNamespace
, 'cur_title' => $this->mDbkeyform
);
1970 * Get an associative array for selecting this title from the
1976 function oldCond() {
1977 wfDebugDieBacktrace( 'oldCond called' );
1978 return array( 'old_namespace' => $this->mNamespace
, 'old_title' => $this->mDbkeyform
);
1982 * Get the revision ID of the previous revision
1984 * @param integer $revision Revision ID. Get the revision that was before this one.
1985 * @return interger $oldrevision|false
1987 function getPreviousRevisionID( $revision ) {
1988 $dbr =& wfGetDB( DB_SLAVE
);
1989 return $dbr->selectField( 'revision', 'rev_id',
1990 'rev_page=' . intval( $this->getArticleId() ) .
1991 ' AND rev_id<' . intval( $revision ) . ' ORDER BY rev_id DESC' );
1995 * Get the revision ID of the next revision
1997 * @param integer $revision Revision ID. Get the revision that was after this one.
1998 * @return interger $oldrevision|false
2000 function getNextRevisionID( $revision ) {
2001 $dbr =& wfGetDB( DB_SLAVE
);
2002 return $dbr->selectField( 'revision', 'rev_id',
2003 'rev_page=' . intval( $this->getArticleId() ) .
2004 ' AND rev_id>' . intval( $revision ) . ' ORDER BY rev_id' );
2008 * Compare with another title.
2010 * @param Title $title
2013 function equals( $title ) {
2014 return $this->getInterwiki() == $title->getInterwiki()
2015 && $this->getNamespace() == $title->getNamespace()
2016 && $this->getDbkey() == $title->getDbkey();
2020 * Check if page exists
2024 return $this->getArticleId() != 0;
2028 * Should a link should be displayed as a known link, just based on its title?
2030 * Currently, a self-link with a fragment and special pages are in
2031 * this category. Special pages never exist in the database.
2033 function isAlwaysKnown() {
2034 return $this->isExternal() ||
( 0 == $this->mNamespace
&& "" == $this->mDbkeyform
)
2035 || NS_SPECIAL
== $this->mNamespace
;
2039 * Update page_touched timestamps on pages linking to this title.
2040 * In principal, this could be backgrounded and could also do squid
2043 function touchLinks() {
2044 $fname = 'Title::touchLinks';
2046 $dbw =& wfGetDB( DB_MASTER
);
2048 $res = $dbw->select( 'pagelinks',
2051 'pl_namespace' => $this->getNamespace(),
2052 'pl_title' => $this->getDbKey() ),
2055 $toucharr = array();
2056 while( $row = $dbw->fetchObject( $res ) ) {
2057 $toucharr[] = $row->pl_from
;
2059 $dbw->freeResult( $res );
2061 if( $this->getNamespace() == NS_CATEGORY
) {
2062 // Categories show up in a separate set of links as well
2063 $res = $dbw->select( 'categorylinks',
2065 array( 'cl_to' => $this->getDbKey() ),
2067 while( $row = $dbw->fetchObject( $res ) ) {
2068 $toucharr[] = $row->cl_from
;
2070 $dbw->freeResult( $res );
2073 if (!count($toucharr))
2075 $dbw->update( 'page', /* SET */ array( 'page_touched' => $dbw->timestamp() ),
2076 /* WHERE */ array( 'page_id' => $toucharr ),$fname);
2079 function trackbackURL() {
2080 global $wgTitle, $wgScriptPath, $wgServer;
2082 return "$wgServer$wgScriptPath/trackback.php?article="
2083 . htmlspecialchars(urlencode($wgTitle->getPrefixedDBkey()));
2086 function trackbackRDF() {
2087 $url = htmlspecialchars($this->getFullURL());
2088 $title = htmlspecialchars($this->getText());
2089 $tburl = $this->trackbackURL();
2092 <rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"
2093 xmlns:dc=\"http://purl.org/dc/elements/1.1/\"
2094 xmlns:trackback=\"http://madskills.com/public/xml/rss/module/trackback/\">
2097 dc:identifier=\"$url\"
2099 trackback:ping=\"$tburl\" />