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 ) {
166 # For compatibility with old buggy URLs. "+" is not valid in titles,
167 # but some URLs used it as a space replacement and they still come
168 # from some external search tools.
169 $s = str_replace( '+', ' ', $url );
171 $t->mDbkeyform
= str_replace( ' ', '_', $s );
172 if( $t->secureAndSplit() ) {
180 * Create a new Title from an article ID
182 * @todo This is inefficiently implemented, the page row is requested
183 * but not used for anything else
185 * @param int $id the page_id corresponding to the Title to create
186 * @return Title the new object, or NULL on an error
190 function newFromID( $id ) {
191 $fname = 'Title::newFromID';
192 $dbr =& wfGetDB( DB_SLAVE
);
193 $row = $dbr->selectRow( 'page', array( 'page_namespace', 'page_title' ),
194 array( 'page_id' => $id ), $fname );
195 if ( $row !== false ) {
196 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
204 * Create a new Title from a namespace index and a DB key.
205 * It's assumed that $ns and $title are *valid*, for instance when
206 * they came directly from the database or a special page name.
207 * For convenience, spaces are converted to underscores so that
208 * eg user_text fields can be used directly.
210 * @param int $ns the namespace of the article
211 * @param string $title the unprefixed database key form
212 * @return Title the new object
216 function &makeTitle( $ns, $title ) {
220 $t->mNamespace
= intval( $ns );
221 $t->mDbkeyform
= str_replace( ' ', '_', $title );
222 $t->mArticleID
= ( $ns >= 0 ) ?
-1 : 0;
223 $t->mUrlform
= wfUrlencode( $t->mDbkeyform
);
224 $t->mTextform
= str_replace( '_', ' ', $title );
229 * Create a new Title frrom a namespace index and a DB key.
230 * The parameters will be checked for validity, which is a bit slower
231 * than makeTitle() but safer for user-provided data.
233 * @param int $ns the namespace of the article
234 * @param string $title the database key form
235 * @return Title the new object, or NULL on an error
239 function makeTitleSafe( $ns, $title ) {
241 $t->mDbkeyform
= Title
::makeName( $ns, $title );
242 if( $t->secureAndSplit() ) {
250 * Create a new Title for the Main Page
253 * @return Title the new object
256 function newMainPage() {
257 return Title
::newFromText( wfMsgForContent( 'mainpage' ) );
261 * Create a new Title for a redirect
262 * @param string $text the redirect title text
263 * @return Title the new object, or NULL if the text is not a
268 function newFromRedirect( $text ) {
271 if ( $wgMwRedir->matchStart( $text ) ) {
272 if ( preg_match( '/\[{2}(.*?)(?:\||\]{2})/', $text, $m ) ) {
273 # categories are escaped using : for example one can enter:
274 # #REDIRECT [[:Category:Music]]. Need to remove it.
275 if ( substr($m[1],0,1) == ':') {
276 # We don't want to keep the ':'
277 $m[1] = substr( $m[1], 1 );
280 $rt = Title
::newFromText( $m[1] );
281 # Disallow redirects to Special:Userlogout
282 if ( !is_null($rt) && $rt->getNamespace() == NS_SPECIAL
&& preg_match( '/^Userlogout/i', $rt->getText() ) ) {
290 #----------------------------------------------------------------------------
292 #----------------------------------------------------------------------------
295 * Get the prefixed DB key associated with an ID
296 * @param int $id the page_id of the article
297 * @return Title an object representing the article, or NULL
298 * if no such article was found
302 function nameOf( $id ) {
303 $fname = 'Title::nameOf';
304 $dbr =& wfGetDB( DB_SLAVE
);
306 $s = $dbr->selectRow( 'page', array( 'page_namespace','page_title' ), array( 'page_id' => $id ), $fname );
307 if ( $s === false ) { return NULL; }
309 $n = Title
::makeName( $s->page_namespace
, $s->page_title
);
314 * Get a regex character class describing the legal characters in a link
315 * @return string the list of characters, not delimited
319 function legalChars() {
320 global $wgLegalTitleChars;
321 return $wgLegalTitleChars;
325 * Get a string representation of a title suitable for
326 * including in a search index
328 * @param int $ns a namespace index
329 * @param string $title text-form main part
330 * @return string a stripped-down title string ready for the
333 /* static */ function indexTitle( $ns, $title ) {
335 require_once( 'SearchEngine.php' );
337 $lc = SearchEngine
::legalSearchChars() . '&#;';
338 $t = $wgContLang->stripForSearch( $title );
339 $t = preg_replace( "/[^{$lc}]+/", ' ', $t );
340 $t = strtolower( $t );
343 $t = preg_replace( "/([{$lc}]+)'s( |$)/", "\\1 \\1's ", $t );
344 $t = preg_replace( "/([{$lc}]+)s'( |$)/", "\\1s ", $t );
346 $t = preg_replace( "/\\s+/", ' ', $t );
348 if ( $ns == NS_IMAGE
) {
349 $t = preg_replace( "/ (png|gif|jpg|jpeg|ogg)$/", "", $t );
355 * Make a prefixed DB key from a DB key and a namespace index
356 * @param int $ns numerical representation of the namespace
357 * @param string $title the DB key form the title
358 * @return string the prefixed form of the title
360 /* static */ function makeName( $ns, $title ) {
363 $n = $wgContLang->getNsText( $ns );
364 return $n == '' ?
$title : "$n:$title";
368 * Returns the URL associated with an interwiki prefix
369 * @param string $key the interwiki prefix (e.g. "MeatBall")
370 * @return the associated URL, containing "$1", which should be
371 * replaced by an article title
375 function getInterwikiLink( $key ) {
376 global $wgMemc, $wgDBname, $wgInterwikiExpiry, $wgTitleInterwikiCache;
377 $fname = 'Title::getInterwikiLink';
379 wfProfileIn( $fname );
381 $key = strtolower( $key );
383 $k = $wgDBname.':interwiki:'.$key;
384 if( array_key_exists( $k, $wgTitleInterwikiCache ) ) {
385 wfProfileOut( $fname );
386 return $wgTitleInterwikiCache[$k]->iw_url
;
389 $s = $wgMemc->get( $k );
390 # Ignore old keys with no iw_local
391 if( $s && isset( $s->iw_local
) && isset($s->iw_trans
)) {
392 $wgTitleInterwikiCache[$k] = $s;
393 wfProfileOut( $fname );
397 $dbr =& wfGetDB( DB_SLAVE
);
398 $res = $dbr->select( 'interwiki',
399 array( 'iw_url', 'iw_local', 'iw_trans' ),
400 array( 'iw_prefix' => $key ), $fname );
402 wfProfileOut( $fname );
406 $s = $dbr->fetchObject( $res );
408 # Cache non-existence: create a blank object and save it to memcached
414 $wgMemc->set( $k, $s, $wgInterwikiExpiry );
415 $wgTitleInterwikiCache[$k] = $s;
417 wfProfileOut( $fname );
422 * Determine whether the object refers to a page within
425 * @return bool TRUE if this is an in-project interwiki link
426 * or a wikilink, FALSE otherwise
430 global $wgTitleInterwikiCache, $wgDBname;
432 if ( $this->mInterwiki
!= '' ) {
433 # Make sure key is loaded into cache
434 $this->getInterwikiLink( $this->mInterwiki
);
435 $k = $wgDBname.':interwiki:' . $this->mInterwiki
;
436 return (bool)($wgTitleInterwikiCache[$k]->iw_local
);
443 * Determine whether the object refers to a page within
444 * this project and is transcludable.
446 * @return bool TRUE if this is transcludable
450 global $wgTitleInterwikiCache, $wgDBname;
452 if ($this->mInterwiki
== '' ||
!$this->isLocal())
454 # Make sure key is loaded into cache
455 $this->getInterwikiLink( $this->mInterwiki
);
456 $k = $wgDBname.':interwiki:' . $this->mInterwiki
;
457 return (bool)($wgTitleInterwikiCache[$k]->iw_trans
);
461 * Update the page_touched field for an array of title objects
462 * @todo Inefficient unless the IDs are already loaded into the
464 * @param array $titles an array of Title objects to be touched
465 * @param string $timestamp the timestamp to use instead of the
466 * default current time
470 function touchArray( $titles, $timestamp = '' ) {
472 if ( count( $titles ) == 0 ) {
475 $dbw =& wfGetDB( DB_MASTER
);
476 if ( $timestamp == '' ) {
477 $timestamp = $dbw->timestamp();
480 $page = $dbw->tableName( 'page' );
481 $sql = "UPDATE $page SET page_touched='{$timestamp}' WHERE page_id IN (";
484 foreach ( $titles as $title ) {
485 if ( $wgUseFileCache ) {
486 $cm = new CacheManager($title);
487 @unlink($cm->fileCacheName());
494 $sql .= $title->getArticleID();
498 $dbw->query( $sql, 'Title::touchArray' );
501 // hack hack hack -- brion 2005-07-11. this was unfriendly to db.
502 // do them in small chunks:
503 $fname = 'Title::touchArray';
504 foreach( $titles as $title ) {
505 $dbw->update( 'page',
506 array( 'page_touched' => $timestamp ),
508 'page_namespace' => $title->getNamespace(),
509 'page_title' => $title->getDBkey() ),
514 #----------------------------------------------------------------------------
516 #----------------------------------------------------------------------------
518 /** Simple accessors */
520 * Get the text form (spaces not underscores) of the main part
524 function getText() { return $this->mTextform
; }
526 * Get the URL-encoded form of the main part
530 function getPartialURL() { return $this->mUrlform
; }
532 * Get the main part with underscores
536 function getDBkey() { return $this->mDbkeyform
; }
538 * Get the namespace index, i.e. one of the NS_xxxx constants
542 function getNamespace() { return $this->mNamespace
; }
544 * Get the namespace text
548 function getNsText() {
550 return $wgContLang->getNsText( $this->mNamespace
);
553 * Get the namespace text of the subject (rather than talk) page
557 function getSubjectNsText() {
559 return $wgContLang->getNsText( Namespace::getSubject( $this->mNamespace
) );
563 * Get the interwiki prefix (or null string)
567 function getInterwiki() { return $this->mInterwiki
; }
569 * Get the Title fragment (i.e. the bit after the #)
573 function getFragment() { return $this->mFragment
; }
575 * Get the default namespace index, for when there is no namespace
579 function getDefaultNamespace() { return $this->mDefaultNamespace
; }
582 * Get title for search index
583 * @return string a stripped-down title string ready for the
586 function getIndexTitle() {
587 return Title
::indexTitle( $this->mNamespace
, $this->mTextform
);
591 * Get the prefixed database key form
592 * @return string the prefixed title, with underscores and
593 * any interwiki and namespace prefixes
596 function getPrefixedDBkey() {
597 $s = $this->prefix( $this->mDbkeyform
);
598 $s = str_replace( ' ', '_', $s );
603 * Get the prefixed title with spaces.
604 * This is the form usually used for display
605 * @return string the prefixed title, with spaces
608 function getPrefixedText() {
610 if ( empty( $this->mPrefixedText
) ) { // FIXME: bad usage of empty() ?
611 $s = $this->prefix( $this->mTextform
);
612 $s = str_replace( '_', ' ', $s );
613 $this->mPrefixedText
= $s;
615 return $this->mPrefixedText
;
619 * Get the prefixed title with spaces, plus any fragment
620 * (part beginning with '#')
621 * @return string the prefixed title, with spaces and
622 * the fragment, including '#'
625 function getFullText() {
627 $text = $this->getPrefixedText();
628 if( '' != $this->mFragment
) {
629 $text .= '#' . $this->mFragment
;
635 * Get a URL-encoded title (not an actual URL) including interwiki
636 * @return string the URL-encoded form
639 function getPrefixedURL() {
640 $s = $this->prefix( $this->mDbkeyform
);
641 $s = str_replace( ' ', '_', $s );
643 $s = wfUrlencode ( $s ) ;
645 # Cleaning up URL to make it look nice -- is this safe?
646 $s = str_replace( '%28', '(', $s );
647 $s = str_replace( '%29', ')', $s );
653 * Get a real URL referring to this title, with interwiki link and
656 * @param string $query an optional query string, not used
657 * for interwiki links
658 * @return string the URL
661 function getFullURL( $query = '' ) {
662 global $wgContLang, $wgServer;
664 if ( '' == $this->mInterwiki
) {
665 $url = $wgServer . $this->getLocalUrl( $query );
667 $baseUrl = $this->getInterwikiLink( $this->mInterwiki
);
669 $namespace = $wgContLang->getNsText( $this->mNamespace
);
670 if ( '' != $namespace ) {
671 # Can this actually happen? Interwikis shouldn't be parsed.
674 $url = str_replace( '$1', $namespace . $this->mUrlform
, $baseUrl );
676 if( false === strpos( $url, '?' ) ) {
683 if ( '' != $this->mFragment
) {
684 $url .= '#' . $this->mFragment
;
687 wfRunHooks( 'GetFullURL', array( &$this, &$url, $query ) );
692 * Get a URL with no fragment or server name. If this page is generated
693 * with action=render, $wgServer is prepended.
694 * @param string $query an optional query string; if not specified,
695 * $wgArticlePath will be used.
696 * @return string the URL
699 function getLocalURL( $query = '' ) {
700 global $wgArticlePath, $wgScript, $wgServer, $wgRequest;
702 if ( $this->isExternal() ) {
703 $url = $this->getFullURL();
705 $dbkey = wfUrlencode( $this->getPrefixedDBkey() );
706 if ( $query == '' ) {
707 $url = str_replace( '$1', $dbkey, $wgArticlePath );
709 global $wgActionPaths;
711 if( !empty( $wgActionPaths ) &&
712 preg_match( '/^(.*&|)action=([^&]*)(&(.*)|)$/', $query, $matches ) )
714 $action = urldecode( $matches[2] );
715 if( isset( $wgActionPaths[$action] ) ) {
716 $query = $matches[1];
717 if( isset( $matches[4] ) ) $query .= $matches[4];
718 $url = str_replace( '$1', $dbkey, $wgActionPaths[$action] );
719 if( $query != '' ) $url .= '?' . $query;
722 if ( $url === false ) {
723 if ( $query == '-' ) {
726 $url = "{$wgScript}?title={$dbkey}&{$query}";
730 if ($wgRequest->getText('action') == 'render') {
731 $url = $wgServer . $url;
734 wfRunHooks( 'GetLocalURL', array( &$this, &$url, $query ) );
739 * Get an HTML-escaped version of the URL form, suitable for
740 * using in a link, without a server name or fragment
741 * @param string $query an optional query string
742 * @return string the URL
745 function escapeLocalURL( $query = '' ) {
746 return htmlspecialchars( $this->getLocalURL( $query ) );
750 * Get an HTML-escaped version of the URL form, suitable for
751 * using in a link, including the server name and fragment
753 * @return string the URL
754 * @param string $query an optional query string
757 function escapeFullURL( $query = '' ) {
758 return htmlspecialchars( $this->getFullURL( $query ) );
762 * Get the URL form for an internal link.
763 * - Used in various Squid-related code, in case we have a different
764 * internal hostname for the server from the exposed one.
766 * @param string $query an optional query string
767 * @return string the URL
770 function getInternalURL( $query = '' ) {
771 global $wgInternalServer;
772 return $wgInternalServer . $this->getLocalURL( $query );
776 * Get the edit URL for this Title
777 * @return string the URL, or a null string if this is an
781 function getEditURL() {
782 global $wgServer, $wgScript;
784 if ( '' != $this->mInterwiki
) { return ''; }
785 $s = $this->getLocalURL( 'action=edit' );
791 * Get the HTML-escaped displayable text form.
792 * Used for the title field in <a> tags.
793 * @return string the text, including any prefixes
796 function getEscapedText() {
797 return htmlspecialchars( $this->getPrefixedText() );
801 * Is this Title interwiki?
805 function isExternal() { return ( '' != $this->mInterwiki
); }
808 * Does the title correspond to a protected article?
809 * @param string $what the action the page is protected from,
810 * by default checks move and edit
814 function isProtected($action = '') {
815 if ( -1 == $this->mNamespace
) { return true; }
816 if($action == 'edit' ||
$action == '') {
817 $a = $this->getRestrictions("edit");
818 if ( in_array( 'sysop', $a ) ) { return true; }
820 if($action == 'move' ||
$action == '') {
821 $a = $this->getRestrictions("move");
822 if ( in_array( 'sysop', $a ) ) { return true; }
828 * Is $wgUser is watching this page?
832 function userIsWatching() {
835 if ( is_null( $this->mWatched
) ) {
836 if ( -1 == $this->mNamespace ||
0 == $wgUser->getID()) {
837 $this->mWatched
= false;
839 $this->mWatched
= $wgUser->isWatched( $this );
842 return $this->mWatched
;
846 * Can $wgUser perform $action this page?
847 * @param string $action action that permission needs to be checked for
851 function userCan($action) {
852 $fname = 'Title::userCan';
853 wfProfileIn( $fname );
856 if( NS_SPECIAL
== $this->mNamespace
) {
857 wfProfileOut( $fname );
860 // XXX: This is the code that prevents unprotecting a page in NS_MEDIAWIKI
861 // from taking effect -ævar
862 if( NS_MEDIAWIKI
== $this->mNamespace
&&
863 !$wgUser->isAllowed('editinterface') ) {
864 wfProfileOut( $fname );
868 if( $this->mDbkeyform
== '_' ) {
869 # FIXME: Is this necessary? Shouldn't be allowed anyway...
870 wfProfileOut( $fname );
874 # protect global styles and js
875 if ( NS_MEDIAWIKI
== $this->mNamespace
876 && preg_match("/\\.(css|js)$/", $this->mTextform
)
877 && !$wgUser->isAllowed('editinterface') ) {
878 wfProfileOut( $fname );
882 # protect css/js subpages of user pages
883 # XXX: this might be better using restrictions
884 # XXX: Find a way to work around the php bug that prevents using $this->userCanEditCssJsSubpage() from working
885 if( NS_USER
== $this->mNamespace
886 && preg_match("/\\.(css|js)$/", $this->mTextform
)
887 && !$wgUser->isAllowed('editinterface')
888 && !preg_match('/^'.preg_quote($wgUser->getName(), '/').'\//', $this->mTextform
) ) {
889 wfProfileOut( $fname );
893 foreach( $this->getRestrictions($action) as $right ) {
894 // Backwards compatibility, rewrite sysop -> protect
895 if ( $right == 'sysop' ) {
898 if( '' != $right && !$wgUser->isAllowed( $right ) ) {
899 wfProfileOut( $fname );
904 if( $action == 'move' &&
905 !( $this->isMovable() && $wgUser->isAllowed( 'move' ) ) ) {
906 wfProfileOut( $fname );
910 if( $action == 'create' ) {
911 if( ( $this->isTalkPage() && !$wgUser->isAllowed( 'createtalk' ) ) ||
912 ( !$this->isTalkPage() && !$wgUser->isAllowed( 'createpage' ) ) ) {
917 wfProfileOut( $fname );
922 * Can $wgUser edit this page?
926 function userCanEdit() {
927 return $this->userCan('edit');
931 * Can $wgUser move this page?
935 function userCanMove() {
936 return $this->userCan('move');
940 * Would anybody with sufficient privileges be able to move this page?
941 * Some pages just aren't movable.
946 function isMovable() {
947 return Namespace::isMovable( $this->getNamespace() )
948 && $this->getInterwiki() == '';
952 * Can $wgUser read this page?
956 function userCanRead() {
959 if( $wgUser->isAllowed('read') ) {
962 global $wgWhitelistRead;
964 /** If anon users can create an account,
965 they need to reach the login page first! */
966 if( $wgUser->isAllowed( 'createaccount' )
967 && $this->getNamespace() == NS_SPECIAL
968 && $this->getText() == 'Userlogin' ) {
972 /** some pages are explicitly allowed */
973 $name = $this->getPrefixedText();
974 if( $wgWhitelistRead && in_array( $name, $wgWhitelistRead ) ) {
978 # Compatibility with old settings
979 if( $wgWhitelistRead && $this->getNamespace() == NS_MAIN
) {
980 if( in_array( ':' . $name, $wgWhitelistRead ) ) {
989 * Is this a talk page of some sort?
993 function isTalkPage() {
994 return Namespace::isTalk( $this->getNamespace() );
998 * Is this a .css or .js subpage of a user page?
1002 function isCssJsSubpage() {
1003 return ( NS_USER
== $this->mNamespace
and preg_match("/\\.(css|js)$/", $this->mTextform
) );
1006 * Is this a .css subpage of a user page?
1010 function isCssSubpage() {
1011 return ( NS_USER
== $this->mNamespace
and preg_match("/\\.css$/", $this->mTextform
) );
1014 * Is this a .js subpage of a user page?
1018 function isJsSubpage() {
1019 return ( NS_USER
== $this->mNamespace
and preg_match("/\\.js$/", $this->mTextform
) );
1022 * Protect css/js subpages of user pages: can $wgUser edit
1026 * @todo XXX: this might be better using restrictions
1029 function userCanEditCssJsSubpage() {
1031 return ( $wgUser->isAllowed('editinterface') or preg_match('/^'.preg_quote($wgUser->getName(), '/').'\//', $this->mTextform
) );
1035 * Loads a string into mRestrictions array
1036 * @param string $res restrictions in string format
1039 function loadRestrictions( $res ) {
1040 foreach( explode( ':', trim( $res ) ) as $restrict ) {
1041 $temp = explode( '=', trim( $restrict ) );
1042 if(count($temp) == 1) {
1043 // old format should be treated as edit/move restriction
1044 $this->mRestrictions
["edit"] = explode( ',', trim( $temp[0] ) );
1045 $this->mRestrictions
["move"] = explode( ',', trim( $temp[0] ) );
1047 $this->mRestrictions
[$temp[0]] = explode( ',', trim( $temp[1] ) );
1050 $this->mRestrictionsLoaded
= true;
1054 * Accessor/initialisation for mRestrictions
1055 * @param string $action action that permission needs to be checked for
1056 * @return array the array of groups allowed to edit this article
1059 function getRestrictions($action) {
1060 $id = $this->getArticleID();
1061 if ( 0 == $id ) { return array(); }
1063 if ( ! $this->mRestrictionsLoaded
) {
1064 $dbr =& wfGetDB( DB_SLAVE
);
1065 $res = $dbr->selectField( 'page', 'page_restrictions', 'page_id='.$id );
1066 $this->loadRestrictions( $res );
1068 if( isset( $this->mRestrictions
[$action] ) ) {
1069 return $this->mRestrictions
[$action];
1075 * Is there a version of this page in the deletion archive?
1076 * @return int the number of archived revisions
1079 function isDeleted() {
1080 $fname = 'Title::isDeleted';
1081 if ( $this->getNamespace() < 0 ) {
1084 $dbr =& wfGetDB( DB_SLAVE
);
1085 $n = $dbr->selectField( 'archive', 'COUNT(*)', array( 'ar_namespace' => $this->getNamespace(),
1086 'ar_title' => $this->getDBkey() ), $fname );
1092 * Get the article ID for this Title from the link cache,
1093 * adding it if necessary
1094 * @param int $flags a bit field; may be GAID_FOR_UPDATE to select
1096 * @return int the ID
1099 function getArticleID( $flags = 0 ) {
1100 global $wgLinkCache;
1101 if ( $flags & GAID_FOR_UPDATE
) {
1102 $oldUpdate = $wgLinkCache->forUpdate( true );
1103 $this->mArticleID
= $wgLinkCache->addLinkObj( $this );
1104 $wgLinkCache->forUpdate( $oldUpdate );
1106 if ( -1 == $this->mArticleID
) {
1107 $this->mArticleID
= $wgLinkCache->addLinkObj( $this );
1110 return $this->mArticleID
;
1113 function getLatestRevID() {
1114 if ($this->mLatestID
!== false)
1115 return $this->mLatestID
;
1117 $db =& wfGetDB(DB_SLAVE
);
1118 return $this->mLatestID
= $db->selectField( 'revision',
1120 array('rev_page' => $this->getArticleID()),
1121 'Title::getLatestRevID' );
1125 * This clears some fields in this object, and clears any associated
1126 * keys in the "bad links" section of $wgLinkCache.
1128 * - This is called from Article::insertNewArticle() to allow
1129 * loading of the new page_id. It's also called from
1130 * Article::doDeleteArticle()
1132 * @param int $newid the new Article ID
1135 function resetArticleID( $newid ) {
1136 global $wgLinkCache;
1137 $wgLinkCache->clearBadLink( $this->getPrefixedDBkey() );
1139 if ( 0 == $newid ) { $this->mArticleID
= -1; }
1140 else { $this->mArticleID
= $newid; }
1141 $this->mRestrictionsLoaded
= false;
1142 $this->mRestrictions
= array();
1146 * Updates page_touched for this page; called from LinksUpdate.php
1147 * @return bool true if the update succeded
1150 function invalidateCache() {
1151 global $wgUseFileCache;
1153 if ( wfReadOnly() ) {
1157 $dbw =& wfGetDB( DB_MASTER
);
1158 $success = $dbw->update( 'page',
1160 'page_touched' => $dbw->timestamp()
1161 ), array( /* WHERE */
1162 'page_namespace' => $this->getNamespace() ,
1163 'page_title' => $this->getDBkey()
1164 ), 'Title::invalidateCache'
1167 if ($wgUseFileCache) {
1168 $cache = new CacheManager($this);
1169 @unlink
($cache->fileCacheName());
1176 * Prefix some arbitrary text with the namespace or interwiki prefix
1179 * @param string $name the text
1180 * @return string the prefixed text
1183 /* private */ function prefix( $name ) {
1187 if ( '' != $this->mInterwiki
) {
1188 $p = $this->mInterwiki
. ':';
1190 if ( 0 != $this->mNamespace
) {
1191 $p .= $wgContLang->getNsText( $this->mNamespace
) . ':';
1197 * Secure and split - main initialisation function for this object
1199 * Assumes that mDbkeyform has been set, and is urldecoded
1200 * and uses underscores, but not otherwise munged. This function
1201 * removes illegal characters, splits off the interwiki and
1202 * namespace prefixes, sets the other forms, and canonicalizes
1204 * @return bool true on success
1207 /* private */ function secureAndSplit() {
1208 global $wgContLang, $wgLocalInterwiki, $wgCapitalLinks;
1209 $fname = 'Title::secureAndSplit';
1210 wfProfileIn( $fname );
1213 static $rxTc = false;
1215 # % is needed as well
1216 $rxTc = '/[^' . Title
::legalChars() . ']|%[0-9A-Fa-f]{2}/S';
1219 $this->mInterwiki
= $this->mFragment
= '';
1220 $this->mNamespace
= $this->mDefaultNamespace
; # Usually NS_MAIN
1222 # Clean up whitespace
1224 $t = preg_replace( '/[ _]+/', '_', $this->mDbkeyform
);
1225 $t = trim( $t, '_' );
1228 wfProfileOut( $fname );
1232 if( false !== strpos( $t, UTF8_REPLACEMENT
) ) {
1233 # Contained illegal UTF-8 sequences or forbidden Unicode chars.
1234 wfProfileOut( $fname );
1238 $this->mDbkeyform
= $t;
1240 # Initial colon indicates main namespace rather than specified default
1241 # but should not create invalid {ns,title} pairs such as {0,Project:Foo}
1242 if ( ':' == $t{0} ) {
1243 $this->mNamespace
= NS_MAIN
;
1244 $t = substr( $t, 1 ); # remove the colon but continue processing
1247 # Namespace or interwiki prefix
1250 if ( preg_match( "/^(.+?)_*:_*(.*)$/S", $t, $m ) ) {
1252 $lowerNs = strtolower( $p );
1253 if ( $ns = Namespace::getCanonicalIndex( $lowerNs ) ) {
1254 # Canonical namespace
1256 $this->mNamespace
= $ns;
1257 } elseif ( $ns = $wgContLang->getNsIndex( $lowerNs )) {
1258 # Ordinary namespace
1260 $this->mNamespace
= $ns;
1261 } elseif( $this->getInterwikiLink( $p ) ) {
1263 # Can't make a local interwiki link to an interwiki link.
1264 # That's just crazy!
1265 wfProfileOut( $fname );
1271 $this->mInterwiki
= strtolower( $p );
1273 # Redundant interwiki prefix to the local wiki
1274 if ( 0 == strcasecmp( $this->mInterwiki
, $wgLocalInterwiki ) ) {
1276 # Can't have an empty self-link
1277 wfProfileOut( $fname );
1280 $this->mInterwiki
= '';
1282 # Do another namespace split...
1286 # If there's no recognized interwiki or namespace,
1287 # then let the colon expression be part of the title.
1293 # We already know that some pages won't be in the database!
1295 if ( '' != $this->mInterwiki ||
-1 == $this->mNamespace
) {
1296 $this->mArticleID
= 0;
1298 $f = strstr( $r, '#' );
1299 if ( false !== $f ) {
1300 $this->mFragment
= substr( $f, 1 );
1301 $r = substr( $r, 0, strlen( $r ) - strlen( $f ) );
1302 # remove whitespace again: prevents "Foo_bar_#"
1303 # becoming "Foo_bar_"
1304 $r = preg_replace( '/_*$/', '', $r );
1307 # Reject illegal characters.
1309 if( preg_match( $rxTc, $r ) ) {
1310 wfProfileOut( $fname );
1315 * Pages with "/./" or "/../" appearing in the URLs will
1316 * often be unreachable due to the way web browsers deal
1317 * with 'relative' URLs. Forbid them explicitly.
1319 if ( strpos( $r, '.' ) !== false &&
1320 ( $r === '.' ||
$r === '..' ||
1321 strpos( $r, './' ) === 0 ||
1322 strpos( $r, '../' ) === 0 ||
1323 strpos( $r, '/./' ) !== false ||
1324 strpos( $r, '/../' ) !== false ) )
1326 wfProfileOut( $fname );
1330 # We shouldn't need to query the DB for the size.
1331 #$maxSize = $dbr->textFieldSize( 'page', 'page_title' );
1332 if ( strlen( $r ) > 255 ) {
1333 wfProfileOut( $fname );
1338 * Normally, all wiki links are forced to have
1339 * an initial capital letter so [[foo]] and [[Foo]]
1340 * point to the same place.
1342 * Don't force it for interwikis, since the other
1343 * site might be case-sensitive.
1345 if( $wgCapitalLinks && $this->mInterwiki
== '') {
1346 $t = $wgContLang->ucfirst( $r );
1352 * Can't make a link to a namespace alone...
1353 * "empty" local links can only be self-links
1354 * with a fragment identifier.
1357 $this->mInterwiki
== '' &&
1358 $this->mNamespace
!= NS_MAIN
) {
1359 wfProfileOut( $fname );
1364 $this->mDbkeyform
= $t;
1365 $this->mUrlform
= wfUrlencode( $t );
1367 $this->mTextform
= str_replace( '_', ' ', $t );
1369 wfProfileOut( $fname );
1374 * Get a Title object associated with the talk page of this article
1375 * @return Title the object for the talk page
1378 function getTalkPage() {
1379 return Title
::makeTitle( Namespace::getTalk( $this->getNamespace() ), $this->getDBkey() );
1383 * Get a title object associated with the subject page of this
1386 * @return Title the object for the subject page
1389 function getSubjectPage() {
1390 return Title
::makeTitle( Namespace::getSubject( $this->getNamespace() ), $this->getDBkey() );
1394 * Get an array of Title objects linking to this Title
1395 * Also stores the IDs in the link cache.
1397 * @param string $options may be FOR UPDATE
1398 * @return array the Title objects linking here
1401 function getLinksTo( $options = '' ) {
1402 global $wgLinkCache;
1403 $id = $this->getArticleID();
1406 $db =& wfGetDB( DB_MASTER
);
1408 $db =& wfGetDB( DB_SLAVE
);
1411 $res = $db->select( array( 'page', 'pagelinks' ),
1412 array( 'page_namespace', 'page_title', 'page_id' ),
1415 'pl_namespace' => $this->getNamespace(),
1416 'pl_title' => $this->getDbKey() ),
1417 'Title::getLinksTo',
1421 if ( $db->numRows( $res ) ) {
1422 while ( $row = $db->fetchObject( $res ) ) {
1423 if ( $titleObj = Title
::makeTitle( $row->page_namespace
, $row->page_title
) ) {
1424 $wgLinkCache->addGoodLinkObj( $row->page_id
, $titleObj );
1425 $retVal[] = $titleObj;
1429 $db->freeResult( $res );
1434 * Get an array of Title objects referring to non-existent articles linked from this page
1436 * @param string $options may be FOR UPDATE
1437 * @return array the Title objects
1440 function getBrokenLinksFrom( $options = '' ) {
1441 global $wgLinkCache;
1444 $db =& wfGetDB( DB_MASTER
);
1446 $db =& wfGetDB( DB_SLAVE
);
1449 $res = $db->safeQuery(
1450 "SELECT pl_namespace, pl_title
1453 ON pl_namespace=page_namespace
1454 AND pl_title=page_title
1456 AND page_namespace IS NULL
1458 $db->tableName( 'pagelinks' ),
1459 $db->tableName( 'page' ),
1460 $this->getArticleId(),
1464 if ( $db->numRows( $res ) ) {
1465 while ( $row = $db->fetchObject( $res ) ) {
1466 $retVal[] = Title
::makeTitle( $row->pl_namespace
, $row->pl_title
);
1469 $db->freeResult( $res );
1475 * Get a list of URLs to purge from the Squid cache when this
1478 * @return array the URLs
1481 function getSquidURLs() {
1483 $this->getInternalURL(),
1484 $this->getInternalURL( 'action=history' )
1489 * Move this page without authentication
1490 * @param Title &$nt the new page Title
1493 function moveNoAuth( &$nt ) {
1494 return $this->moveTo( $nt, false );
1498 * Check whether a given move operation would be valid.
1499 * Returns true if ok, or a message key string for an error message
1500 * if invalid. (Scarrrrry ugly interface this.)
1501 * @param Title &$nt the new title
1502 * @param bool $auth indicates whether $wgUser's permissions
1504 * @return mixed true on success, message name on failure
1507 function isValidMoveOperation( &$nt, $auth = true ) {
1509 if( !$this or !$nt ) {
1510 return 'badtitletext';
1512 if( $this->equals( $nt ) ) {
1515 if( !$this->isMovable() ||
!$nt->isMovable() ) {
1516 return 'immobile_namespace';
1519 $oldid = $this->getArticleID();
1520 $newid = $nt->getArticleID();
1522 if ( strlen( $nt->getDBkey() ) < 1 ) {
1523 return 'articleexists';
1525 if ( ( '' == $this->getDBkey() ) ||
1527 ( '' == $nt->getDBkey() ) ) {
1528 return 'badarticleerror';
1532 !$this->userCanEdit() ||
!$nt->userCanEdit() ||
1533 !$this->userCanMove() ||
!$nt->userCanMove() ) ) {
1534 return 'protectedpage';
1537 # The move is allowed only if (1) the target doesn't exist, or
1538 # (2) the target is a redirect to the source, and has no history
1539 # (so we can undo bad moves right after they're done).
1541 if ( 0 != $newid ) { # Target exists; check for validity
1542 if ( ! $this->isValidMoveTarget( $nt ) ) {
1543 return 'articleexists';
1550 * Move a title to a new location
1551 * @param Title &$nt the new title
1552 * @param bool $auth indicates whether $wgUser's permissions
1554 * @return mixed true on success, message name on failure
1557 function moveTo( &$nt, $auth = true, $reason = '' ) {
1558 $err = $this->isValidMoveOperation( $nt, $auth );
1559 if( is_string( $err ) ) {
1563 $pageid = $this->getArticleID();
1564 if( $nt->exists() ) {
1565 $this->moveOverExistingRedirect( $nt, $reason );
1566 $pageCountChange = 0;
1567 } else { # Target didn't exist, do normal move.
1568 $this->moveToNewTitle( $nt, $newid, $reason );
1569 $pageCountChange = 1;
1571 $redirid = $this->getArticleID();
1573 # Fixing category links (those without piped 'alternate' names) to be sorted under the new title
1574 $dbw =& wfGetDB( DB_MASTER
);
1575 $categorylinks = $dbw->tableName( 'categorylinks' );
1576 $sql = "UPDATE $categorylinks SET cl_sortkey=" . $dbw->addQuotes( $nt->getPrefixedText() ) .
1577 " WHERE cl_from=" . $dbw->addQuotes( $pageid ) .
1578 " AND cl_sortkey=" . $dbw->addQuotes( $this->getPrefixedText() );
1579 $dbw->query( $sql, 'SpecialMovepage::doSubmit' );
1583 $oldnamespace = $this->getNamespace() & ~
1;
1584 $newnamespace = $nt->getNamespace() & ~
1;
1585 $oldtitle = $this->getDBkey();
1586 $newtitle = $nt->getDBkey();
1588 if( $oldnamespace != $newnamespace ||
$oldtitle != $newtitle ) {
1589 WatchedItem
::duplicateEntries( $this, $nt );
1592 # Update search engine
1593 $u = new SearchUpdate( $pageid, $nt->getPrefixedDBkey() );
1595 $u = new SearchUpdate( $redirid, $this->getPrefixedDBkey(), '' );
1599 if ( $this->getNamespace() == NS_MAIN
and $nt->getNamespace() != NS_MAIN
) {
1600 # Moved out of main namespace
1601 # not viewed, edited, removing
1602 $u = new SiteStatsUpdate( 0, 1, -1, $pageCountChange);
1603 } elseif ( $this->getNamespace() != NS_MAIN
and $nt->getNamespace() == NS_MAIN
) {
1604 # Moved into main namespace
1605 # not viewed, edited, adding
1606 $u = new SiteStatsUpdate( 0, 1, +
1, $pageCountChange );
1607 } elseif ( $pageCountChange ) {
1609 $u = new SiteStatsUpdate( 0, 0, 0, 1 );
1618 wfRunHooks( 'TitleMoveComplete', array( &$this, &$nt, &$wgUser, $pageid, $redirid ) );
1623 * Move page to a title which is at present a redirect to the
1626 * @param Title &$nt the page to move to, which should currently
1630 function moveOverExistingRedirect( &$nt, $reason = '' ) {
1631 global $wgUser, $wgLinkCache, $wgUseSquid, $wgMwRedir;
1632 $fname = 'Title::moveOverExistingRedirect';
1633 $comment = wfMsgForContent( '1movedto2', $this->getPrefixedText(), $nt->getPrefixedText() );
1636 $comment .= ": $reason";
1639 $now = wfTimestampNow();
1641 $newid = $nt->getArticleID();
1642 $oldid = $this->getArticleID();
1643 $dbw =& wfGetDB( DB_MASTER
);
1645 # Delete the old redirect. We don't save it to history since
1646 # by definition if we've got here it's rather uninteresting.
1647 # We have to remove it so that the next step doesn't trigger
1648 # a conflict on the unique namespace+title index...
1649 $dbw->delete( 'page', array( 'page_id' => $newid ), $fname );
1651 # Save a null revision in the page's history notifying of the move
1652 $nullRevision = Revision
::newNullRevision( $dbw, $oldid,
1653 wfMsgForContent( '1movedto2', $this->getPrefixedText(), $nt->getPrefixedText() ),
1655 $nullRevId = $nullRevision->insertOn( $dbw );
1657 # Change the name of the target page:
1658 $dbw->update( 'page',
1660 'page_touched' => $dbw->timestamp($now),
1661 'page_namespace' => $nt->getNamespace(),
1662 'page_title' => $nt->getDBkey(),
1663 'page_latest' => $nullRevId,
1665 /* WHERE */ array( 'page_id' => $oldid ),
1668 $wgLinkCache->clearLink( $nt->getPrefixedDBkey() );
1670 # Recreate the redirect, this time in the other direction.
1671 $redirectText = $wgMwRedir->getSynonym( 0 ) . ' [[' . $nt->getPrefixedText() . "]]\n";
1672 $redirectArticle = new Article( $this );
1673 $newid = $redirectArticle->insertOn( $dbw );
1674 $redirectRevision = new Revision( array(
1676 'comment' => $comment,
1677 'text' => $redirectText ) );
1678 $revid = $redirectRevision->insertOn( $dbw );
1679 $redirectArticle->updateRevisionOn( $dbw, $redirectRevision, 0 );
1680 $wgLinkCache->clearLink( $this->getPrefixedDBkey() );
1683 $log = new LogPage( 'move' );
1684 $log->addEntry( 'move_redir', $this, $reason, array( 1 => $nt->getPrefixedText() ) );
1686 # Now, we record the link from the redirect to the new title.
1687 # It should have no other outgoing links...
1688 $dbw->delete( 'pagelinks', array( 'pl_from' => $newid ), $fname );
1689 $dbw->insert( 'pagelinks',
1691 'pl_from' => $newid,
1692 'pl_namespace' => $nt->getNamespace(),
1693 'pl_title' => $nt->getDbKey() ),
1697 if ( $wgUseSquid ) {
1698 $urls = array_merge( $nt->getSquidURLs(), $this->getSquidURLs() );
1699 $u = new SquidUpdate( $urls );
1705 * Move page to non-existing title.
1706 * @param Title &$nt the new Title
1707 * @param int &$newid set to be the new article ID
1710 function moveToNewTitle( &$nt, &$newid, $reason = '' ) {
1711 global $wgUser, $wgLinkCache, $wgUseSquid;
1713 $fname = 'MovePageForm::moveToNewTitle';
1714 $comment = wfMsgForContent( '1movedto2', $this->getPrefixedText(), $nt->getPrefixedText() );
1716 $comment .= ": $reason";
1719 $newid = $nt->getArticleID();
1720 $oldid = $this->getArticleID();
1721 $dbw =& wfGetDB( DB_MASTER
);
1722 $now = $dbw->timestamp();
1726 # Save a null revision in the page's history notifying of the move
1727 $nullRevision = Revision
::newNullRevision( $dbw, $oldid,
1728 wfMsgForContent( '1movedto2', $this->getPrefixedText(), $nt->getPrefixedText() ),
1730 $nullRevId = $nullRevision->insertOn( $dbw );
1733 $dbw->update( 'page',
1735 'page_touched' => $now,
1736 'page_namespace' => $nt->getNamespace(),
1737 'page_title' => $nt->getDBkey(),
1738 'page_latest' => $nullRevId,
1740 /* WHERE */ array( 'page_id' => $oldid ),
1744 $wgLinkCache->clearLink( $nt->getPrefixedDBkey() );
1747 $redirectText = $wgMwRedir->getSynonym( 0 ) . ' [[' . $nt->getPrefixedText() . "]]\n";
1748 $redirectArticle = new Article( $this );
1749 $newid = $redirectArticle->insertOn( $dbw );
1750 $redirectRevision = new Revision( array(
1752 'comment' => $comment,
1753 'text' => $redirectText ) );
1754 $revid = $redirectRevision->insertOn( $dbw );
1755 $redirectArticle->updateRevisionOn( $dbw, $redirectRevision, 0 );
1756 $wgLinkCache->clearLink( $this->getPrefixedDBkey() );
1759 $log = new LogPage( 'move' );
1760 $log->addEntry( 'move', $this, $reason, array( 1 => $nt->getPrefixedText()) );
1762 # Purge caches as per article creation
1763 Article
::onArticleCreate( $nt );
1765 # Record the just-created redirect's linking to the page
1766 $dbw->insert( 'pagelinks',
1768 'pl_from' => $newid,
1769 'pl_namespace' => $nt->getNamespace(),
1770 'pl_title' => $nt->getDBkey() ),
1773 # Non-existent target may have had broken links to it; these must
1774 # now be touched to update link coloring.
1777 # Purge old title from squid
1778 # The new title, and links to the new title, are purged in Article::onArticleCreate()
1779 $titles = $nt->getLinksTo();
1780 if ( $wgUseSquid ) {
1781 $urls = $this->getSquidURLs();
1782 foreach ( $titles as $linkTitle ) {
1783 $urls[] = $linkTitle->getInternalURL();
1785 $u = new SquidUpdate( $urls );
1791 * Checks if $this can be moved to a given Title
1792 * - Selects for update, so don't call it unless you mean business
1794 * @param Title &$nt the new title to check
1797 function isValidMoveTarget( $nt ) {
1799 $fname = 'Title::isValidMoveTarget';
1800 $dbw =& wfGetDB( DB_MASTER
);
1803 $id = $nt->getArticleID();
1804 $obj = $dbw->selectRow( array( 'page', 'revision', 'text'),
1805 array( 'page_is_redirect','old_text','old_flags' ),
1806 array( 'page_id' => $id, 'page_latest=rev_id', 'rev_text_id=old_id' ),
1807 $fname, 'FOR UPDATE' );
1809 if ( !$obj ||
0 == $obj->page_is_redirect
) {
1813 $text = Revision
::getRevisionText( $obj );
1815 # Does the redirect point to the source?
1816 if ( preg_match( "/\\[\\[\\s*([^\\]\\|]*)]]/", $text, $m ) ) {
1817 $redirTitle = Title
::newFromText( $m[1] );
1818 if( !is_object( $redirTitle ) ||
1819 $redirTitle->getPrefixedDBkey() != $this->getPrefixedDBkey() ) {
1827 # Does the article have a history?
1828 $row = $dbw->selectRow( array( 'page', 'revision'),
1830 array( 'page_namespace' => $nt->getNamespace(),
1831 'page_title' => $nt->getDBkey(),
1832 'page_id=rev_page AND page_latest != rev_id'
1833 ), $fname, 'FOR UPDATE'
1836 # Return true if there was no history
1837 return $row === false;
1841 * Create a redirect; fails if the title already exists; does
1844 * @param Title $dest the destination of the redirect
1845 * @param string $comment the comment string describing the move
1846 * @return bool true on success
1849 function createRedirect( $dest, $comment ) {
1851 if ( $this->getArticleID() ) {
1855 $fname = 'Title::createRedirect';
1856 $dbw =& wfGetDB( DB_MASTER
);
1858 $article = new Article( $this );
1859 $newid = $article->insertOn( $dbw );
1860 $revision = new Revision( array(
1862 'comment' => $comment,
1863 'text' => "#REDIRECT [[" . $dest->getPrefixedText() . "]]\n",
1865 $revisionId = $revision->insertOn( $dbw );
1866 $article->updateRevisionOn( $dbw, $revision, 0 );
1869 $dbw->insert( 'pagelinks',
1871 'pl_from' => $newid,
1872 'pl_namespace' => $dest->getNamespace(),
1873 'pl_title' => $dest->getDbKey()
1877 Article
::onArticleCreate( $this );
1882 * Get categories to which this Title belongs and return an array of
1883 * categories' names.
1885 * @return array an array of parents in the form:
1886 * $parent => $currentarticle
1889 function getParentCategories() {
1890 global $wgContLang,$wgUser;
1892 $titlekey = $this->getArticleId();
1893 $dbr =& wfGetDB( DB_SLAVE
);
1894 $categorylinks = $dbr->tableName( 'categorylinks' );
1897 $sql = "SELECT * FROM $categorylinks"
1898 ." WHERE cl_from='$titlekey'"
1899 ." AND cl_from <> '0'"
1900 ." ORDER BY cl_sortkey";
1902 $res = $dbr->query ( $sql ) ;
1904 if($dbr->numRows($res) > 0) {
1905 while ( $x = $dbr->fetchObject ( $res ) )
1906 //$data[] = Title::newFromText($wgContLang->getNSText ( NS_CATEGORY ).':'.$x->cl_to);
1907 $data[$wgContLang->getNSText ( NS_CATEGORY
).':'.$x->cl_to
] = $this->getFullText();
1908 $dbr->freeResult ( $res ) ;
1916 * Get a tree of parent categories
1917 * @param array $children an array with the children in the keys, to check for circular refs
1921 function getParentCategoryTree( $children = array() ) {
1922 $parents = $this->getParentCategories();
1924 if($parents != '') {
1925 foreach($parents as $parent => $current) {
1926 if ( array_key_exists( $parent, $children ) ) {
1927 # Circular reference
1928 $stack[$parent] = array();
1930 $nt = Title
::newFromText($parent);
1931 $stack[$parent] = $nt->getParentCategoryTree( $children +
array($parent => 1) );
1942 * Get an associative array for selecting this title from
1948 function curCond() {
1949 wfDebugDieBacktrace( 'curCond called' );
1950 return array( 'cur_namespace' => $this->mNamespace
, 'cur_title' => $this->mDbkeyform
);
1954 * Get an associative array for selecting this title from the
1960 function oldCond() {
1961 wfDebugDieBacktrace( 'oldCond called' );
1962 return array( 'old_namespace' => $this->mNamespace
, 'old_title' => $this->mDbkeyform
);
1966 * Get the revision ID of the previous revision
1968 * @param integer $revision Revision ID. Get the revision that was before this one.
1969 * @return interger $oldrevision|false
1971 function getPreviousRevisionID( $revision ) {
1972 $dbr =& wfGetDB( DB_SLAVE
);
1973 return $dbr->selectField( 'revision', 'rev_id',
1974 'rev_page=' . intval( $this->getArticleId() ) .
1975 ' AND rev_id<' . intval( $revision ) . ' ORDER BY rev_id DESC' );
1979 * Get the revision ID of the next revision
1981 * @param integer $revision Revision ID. Get the revision that was after this one.
1982 * @return interger $oldrevision|false
1984 function getNextRevisionID( $revision ) {
1985 $dbr =& wfGetDB( DB_SLAVE
);
1986 return $dbr->selectField( 'revision', 'rev_id',
1987 'rev_page=' . intval( $this->getArticleId() ) .
1988 ' AND rev_id>' . intval( $revision ) . ' ORDER BY rev_id' );
1992 * Compare with another title.
1994 * @param Title $title
1997 function equals( $title ) {
1998 return $this->getInterwiki() == $title->getInterwiki()
1999 && $this->getNamespace() == $title->getNamespace()
2000 && $this->getDbkey() == $title->getDbkey();
2004 * Check if page exists
2008 return $this->getArticleId() != 0;
2012 * Should a link should be displayed as a known link, just based on its title?
2014 * Currently, a self-link with a fragment and special pages are in
2015 * this category. Special pages never exist in the database.
2017 function isAlwaysKnown() {
2018 return $this->isExternal() ||
( 0 == $this->mNamespace
&& "" == $this->mDbkeyform
)
2019 || NS_SPECIAL
== $this->mNamespace
;
2023 * Update page_touched timestamps on pages linking to this title.
2024 * In principal, this could be backgrounded and could also do squid
2027 function touchLinks() {
2028 $fname = 'Title::touchLinks';
2030 $dbw =& wfGetDB( DB_MASTER
);
2032 $res = $dbw->select( 'pagelinks',
2035 'pl_namespace' => $this->getNamespace(),
2036 'pl_title' => $this->getDbKey() ),
2038 if ( 0 == $dbw->numRows( $res ) ) {
2042 $toucharr = array();
2043 while( $row = $dbw->fetchObject( $res ) ) {
2044 $toucharr[] = $row->pl_from
;
2046 if (!count($toucharr))
2048 $dbw->update( 'page', /* SET */ array( 'page_touched' => $dbw->timestamp() ),
2049 /* WHERE */ array( 'page_id' => $toucharr ),$fname);
2052 function trackbackURL() {
2053 global $wgTitle, $wgScriptPath, $wgServer;
2055 return "$wgServer$wgScriptPath/trackback.php?article="
2056 . htmlspecialchars(urlencode($wgTitle->getPrefixedDBkey()));
2059 function trackbackRDF() {
2060 $url = htmlspecialchars($this->getFullURL());
2061 $title = htmlspecialchars($this->getText());
2062 $tburl = $this->trackbackURL();
2065 <rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"
2066 xmlns:dc=\"http://purl.org/dc/elements/1.1/\"
2067 xmlns:trackback=\"http://madskills.com/public/xml/rss/module/trackback/\">
2070 dc:identifier=\"$url\"
2072 trackback:ping=\"$tburl\" />