9 require_once( 'normal/UtfNormal.php' );
11 $wgTitleInterwikiCache = array();
12 define ( 'GAID_FOR_UPDATE', 1 );
14 # Title::newFromTitle maintains a cache to avoid
15 # expensive re-normalization of commonly used titles.
16 # On a batch operation this can become a memory leak
17 # if not bounded. After hitting this many titles,
19 define( 'MW_TITLECACHE_MAX', 1000 );
23 * - Represents a title, which may contain an interwiki designation or namespace
24 * - Can fetch various kinds of data from the database, albeit inefficiently.
30 * All member variables should be considered private
31 * Please use the accessor functions
38 var $mTextform; # Text form (spaces not underscores) of the main part
39 var $mUrlform; # URL-encoded form of the main part
40 var $mDbkeyform; # Main part with underscores
41 var $mNamespace; # Namespace index, i.e. one of the NS_xxxx constants
42 var $mInterwiki; # Interwiki prefix (or null string)
43 var $mFragment; # Title fragment (i.e. the bit after the #)
44 var $mArticleID; # Article ID, fetched from the link cache on demand
45 var $mLatestID; # ID of most recent revision
46 var $mRestrictions; # Array of groups allowed to edit this article
47 # Only null or "sysop" are supported
48 var $mRestrictionsLoaded; # Boolean for initialisation on demand
49 var $mPrefixedText; # Text form including namespace/interwiki, initialised on demand
50 var $mDefaultNamespace; # Namespace index when there is no namespace
51 # Zero except in {{transclusion}} tags
52 var $mWatched; # Is $wgUser watching this page? NULL if unfilled, accessed through userIsWatching()
60 /* private */ function Title() {
61 $this->mInterwiki
= $this->mUrlform
=
62 $this->mTextform
= $this->mDbkeyform
= '';
63 $this->mArticleID
= -1;
64 $this->mNamespace
= NS_MAIN
;
65 $this->mRestrictionsLoaded
= false;
66 $this->mRestrictions
= array();
67 # Dont change the following, NS_MAIN is hardcoded in several place
69 $this->mDefaultNamespace
= NS_MAIN
;
70 $this->mWatched
= NULL;
71 $this->mLatestID
= false;
75 * Create a new Title from a prefixed DB key
76 * @param string $key The database key, which has underscores
77 * instead of spaces, possibly including namespace and
79 * @return Title the new object, or NULL on an error
83 /* static */ function newFromDBkey( $key ) {
85 $t->mDbkeyform
= $key;
86 if( $t->secureAndSplit() )
93 * Create a new Title from text, such as what one would
94 * find in a link. Decodes any HTML entities in the text.
96 * @param string $text the link text; spaces, prefixes,
97 * and an initial ':' indicating the main namespace
99 * @param int $defaultNamespace the namespace to use if
100 * none is specified by a prefix
101 * @return Title the new object, or NULL on an error
105 function newFromText( $text, $defaultNamespace = NS_MAIN
) {
106 $fname = 'Title::newFromText';
107 wfProfileIn( $fname );
109 if( is_object( $text ) ) {
110 wfDebugDieBacktrace( 'Title::newFromText given an object' );
114 * Wiki pages often contain multiple links to the same page.
115 * Title normalization and parsing can become expensive on
116 * pages with many links, so we can save a little time by
119 * In theory these are value objects and won't get changed...
121 static $titleCache = array();
122 if( $defaultNamespace == NS_MAIN
&& isset( $titleCache[$text] ) ) {
123 wfProfileOut( $fname );
124 return $titleCache[$text];
128 * Convert things like é ā or 〗 into real text...
130 $filteredText = Sanitizer
::decodeCharReferences( $text );
133 $t->mDbkeyform
= str_replace( ' ', '_', $filteredText );
134 $t->mDefaultNamespace
= $defaultNamespace;
136 if( $t->secureAndSplit() ) {
137 if( $defaultNamespace == NS_MAIN
) {
138 if( count( $titleCache ) >= MW_TITLECACHE_MAX
) {
139 # Avoid memory leaks on mass operations...
140 $titleCache = array();
142 $titleCache[$text] =& $t;
144 wfProfileOut( $fname );
147 wfProfileOut( $fname );
154 * Create a new Title from URL-encoded text. Ensures that
155 * the given title's length does not exceed the maximum.
156 * @param string $url the title, as might be taken from a URL
157 * @return Title the new object, or NULL on an error
161 function newFromURL( $url ) {
162 global $wgLang, $wgServer;
165 # For compatibility with old buggy URLs. "+" is not valid in titles,
166 # but some URLs used it as a space replacement and they still come
167 # from some external search tools.
168 $s = str_replace( '+', ' ', $url );
170 $t->mDbkeyform
= str_replace( ' ', '_', $s );
171 if( $t->secureAndSplit() ) {
179 * Create a new Title from an article ID
181 * @todo This is inefficiently implemented, the page row is requested
182 * but not used for anything else
184 * @param int $id the page_id corresponding to the Title to create
185 * @return Title the new object, or NULL on an error
189 function newFromID( $id ) {
190 $fname = 'Title::newFromID';
191 $dbr =& wfGetDB( DB_SLAVE
);
192 $row = $dbr->selectRow( 'page', array( 'page_namespace', 'page_title' ),
193 array( 'page_id' => $id ), $fname );
194 if ( $row !== false ) {
195 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
203 * Create a new Title from a namespace index and a DB key.
204 * It's assumed that $ns and $title are *valid*, for instance when
205 * they came directly from the database or a special page name.
206 * For convenience, spaces are converted to underscores so that
207 * eg user_text fields can be used directly.
209 * @param int $ns the namespace of the article
210 * @param string $title the unprefixed database key form
211 * @return Title the new object
215 function &makeTitle( $ns, $title ) {
219 $t->mNamespace
= intval( $ns );
220 $t->mDbkeyform
= str_replace( ' ', '_', $title );
221 $t->mArticleID
= ( $ns >= 0 ) ?
-1 : 0;
222 $t->mUrlform
= wfUrlencode( $t->mDbkeyform
);
223 $t->mTextform
= str_replace( '_', ' ', $title );
228 * Create a new Title frrom a namespace index and a DB key.
229 * The parameters will be checked for validity, which is a bit slower
230 * than makeTitle() but safer for user-provided data.
232 * @param int $ns the namespace of the article
233 * @param string $title the database key form
234 * @return Title the new object, or NULL on an error
238 function makeTitleSafe( $ns, $title ) {
240 $t->mDbkeyform
= Title
::makeName( $ns, $title );
241 if( $t->secureAndSplit() ) {
249 * Create a new Title for the Main Page
252 * @return Title the new object
255 function newMainPage() {
256 return Title
::newFromText( wfMsgForContent( 'mainpage' ) );
260 * Create a new Title for a redirect
261 * @param string $text the redirect title text
262 * @return Title the new object, or NULL if the text is not a
267 function newFromRedirect( $text ) {
270 if ( $wgMwRedir->matchStart( $text ) ) {
271 if ( preg_match( '/\[{2}(.*?)(?:\||\]{2})/', $text, $m ) ) {
272 # categories are escaped using : for example one can enter:
273 # #REDIRECT [[:Category:Music]]. Need to remove it.
274 if ( substr($m[1],0,1) == ':') {
275 # We don't want to keep the ':'
276 $m[1] = substr( $m[1], 1 );
279 $rt = Title
::newFromText( $m[1] );
280 # Disallow redirects to Special:Userlogout
281 if ( !is_null($rt) && $rt->getNamespace() == NS_SPECIAL
&& preg_match( '/^Userlogout/i', $rt->getText() ) ) {
289 #----------------------------------------------------------------------------
291 #----------------------------------------------------------------------------
294 * Get the prefixed DB key associated with an ID
295 * @param int $id the page_id of the article
296 * @return Title an object representing the article, or NULL
297 * if no such article was found
301 function nameOf( $id ) {
302 $fname = 'Title::nameOf';
303 $dbr =& wfGetDB( DB_SLAVE
);
305 $s = $dbr->selectRow( 'page', array( 'page_namespace','page_title' ), array( 'page_id' => $id ), $fname );
306 if ( $s === false ) { return NULL; }
308 $n = Title
::makeName( $s->page_namespace
, $s->page_title
);
313 * Get a regex character class describing the legal characters in a link
314 * @return string the list of characters, not delimited
318 function legalChars() {
319 global $wgLegalTitleChars;
320 return $wgLegalTitleChars;
324 * Get a string representation of a title suitable for
325 * including in a search index
327 * @param int $ns a namespace index
328 * @param string $title text-form main part
329 * @return string a stripped-down title string ready for the
332 /* static */ function indexTitle( $ns, $title ) {
333 global $wgDBminWordLen, $wgContLang;
334 require_once( 'SearchEngine.php' );
336 $lc = SearchEngine
::legalSearchChars() . '&#;';
337 $t = $wgContLang->stripForSearch( $title );
338 $t = preg_replace( "/[^{$lc}]+/", ' ', $t );
339 $t = strtolower( $t );
342 $t = preg_replace( "/([{$lc}]+)'s( |$)/", "\\1 \\1's ", $t );
343 $t = preg_replace( "/([{$lc}]+)s'( |$)/", "\\1s ", $t );
345 $t = preg_replace( "/\\s+/", ' ', $t );
347 if ( $ns == NS_IMAGE
) {
348 $t = preg_replace( "/ (png|gif|jpg|jpeg|ogg)$/", "", $t );
354 * Make a prefixed DB key from a DB key and a namespace index
355 * @param int $ns numerical representation of the namespace
356 * @param string $title the DB key form the title
357 * @return string the prefixed form of the title
359 /* static */ function makeName( $ns, $title ) {
362 $n = $wgContLang->getNsText( $ns );
363 return $n == '' ?
$title : "$n:$title";
367 * Returns the URL associated with an interwiki prefix
368 * @param string $key the interwiki prefix (e.g. "MeatBall")
369 * @return the associated URL, containing "$1", which should be
370 * replaced by an article title
374 function getInterwikiLink( $key, $transludeonly = false ) {
375 global $wgMemc, $wgDBname, $wgInterwikiExpiry, $wgTitleInterwikiCache;
376 $fname = 'Title::getInterwikiLink';
378 wfProfileIn( $fname );
380 $key = strtolower( $key );
382 $k = $wgDBname.':interwiki:'.$key;
383 if( array_key_exists( $k, $wgTitleInterwikiCache ) ) {
384 wfProfileOut( $fname );
385 return $wgTitleInterwikiCache[$k]->iw_url
;
388 $s = $wgMemc->get( $k );
389 # Ignore old keys with no iw_local
390 if( $s && isset( $s->iw_local
) && isset($s->iw_trans
)) {
391 $wgTitleInterwikiCache[$k] = $s;
392 wfProfileOut( $fname );
396 $dbr =& wfGetDB( DB_SLAVE
);
397 $res = $dbr->select( 'interwiki',
398 array( 'iw_url', 'iw_local', 'iw_trans' ),
399 array( 'iw_prefix' => $key ), $fname );
401 wfProfileOut( $fname );
405 $s = $dbr->fetchObject( $res );
407 # Cache non-existence: create a blank object and save it to memcached
413 $wgMemc->set( $k, $s, $wgInterwikiExpiry );
414 $wgTitleInterwikiCache[$k] = $s;
416 wfProfileOut( $fname );
421 * Determine whether the object refers to a page within
424 * @return bool TRUE if this is an in-project interwiki link
425 * or a wikilink, FALSE otherwise
429 global $wgTitleInterwikiCache, $wgDBname;
431 if ( $this->mInterwiki
!= '' ) {
432 # Make sure key is loaded into cache
433 $this->getInterwikiLink( $this->mInterwiki
);
434 $k = $wgDBname.':interwiki:' . $this->mInterwiki
;
435 return (bool)($wgTitleInterwikiCache[$k]->iw_local
);
442 * Determine whether the object refers to a page within
443 * this project and is transcludable.
445 * @return bool TRUE if this is transcludable
449 global $wgTitleInterwikiCache, $wgDBname;
451 if ($this->mInterwiki
== '' ||
!$this->isLocal())
453 # Make sure key is loaded into cache
454 $this->getInterwikiLink( $this->mInterwiki
);
455 $k = $wgDBname.':interwiki:' . $this->mInterwiki
;
456 return (bool)($wgTitleInterwikiCache[$k]->iw_trans
);
460 * Update the page_touched field for an array of title objects
461 * @todo Inefficient unless the IDs are already loaded into the
463 * @param array $titles an array of Title objects to be touched
464 * @param string $timestamp the timestamp to use instead of the
465 * default current time
469 function touchArray( $titles, $timestamp = '' ) {
470 global $wgUseFileCache;
472 if ( count( $titles ) == 0 ) {
475 $dbw =& wfGetDB( DB_MASTER
);
476 if ( $timestamp == '' ) {
477 $timestamp = $dbw->timestamp();
479 $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 wfProfileOut( $fname );
915 * Can $wgUser edit this page?
919 function userCanEdit() {
920 return $this->userCan('edit');
924 * Can $wgUser move this page?
928 function userCanMove() {
929 return $this->userCan('move');
933 * Would anybody with sufficient privileges be able to move this page?
934 * Some pages just aren't movable.
939 function isMovable() {
940 return Namespace::isMovable( $this->getNamespace() )
941 && $this->getInterwiki() == '';
945 * Can $wgUser read this page?
949 function userCanRead() {
952 if( $wgUser->isAllowed('read') ) {
955 global $wgWhitelistRead;
957 /** If anon users can create an account,
958 they need to reach the login page first! */
959 if( $wgUser->isAllowed( 'createaccount' )
960 && $this->getNamespace() == NS_SPECIAL
961 && $this->getText() == 'Userlogin' ) {
965 /** some pages are explicitly allowed */
966 $name = $this->getPrefixedText();
967 if( $wgWhitelistRead && in_array( $name, $wgWhitelistRead ) ) {
971 # Compatibility with old settings
972 if( $wgWhitelistRead && $this->getNamespace() == NS_MAIN
) {
973 if( in_array( ':' . $name, $wgWhitelistRead ) ) {
982 * Is this a talk page of some sort?
986 function isTalkPage() {
987 return Namespace::isTalk( $this->getNamespace() );
991 * Is this a .css or .js subpage of a user page?
995 function isCssJsSubpage() {
996 return ( NS_USER
== $this->mNamespace
and preg_match("/\\.(css|js)$/", $this->mTextform
) );
999 * Is this a .css subpage of a user page?
1003 function isCssSubpage() {
1004 return ( NS_USER
== $this->mNamespace
and preg_match("/\\.css$/", $this->mTextform
) );
1007 * Is this a .js subpage of a user page?
1011 function isJsSubpage() {
1012 return ( NS_USER
== $this->mNamespace
and preg_match("/\\.js$/", $this->mTextform
) );
1015 * Protect css/js subpages of user pages: can $wgUser edit
1019 * @todo XXX: this might be better using restrictions
1022 function userCanEditCssJsSubpage() {
1024 return ( $wgUser->isAllowed('editinterface') or preg_match('/^'.preg_quote($wgUser->getName(), '/').'\//', $this->mTextform
) );
1028 * Loads a string into mRestrictions array
1029 * @param string $res restrictions in string format
1032 function loadRestrictions( $res ) {
1033 foreach( explode( ':', trim( $res ) ) as $restrict ) {
1034 $temp = explode( '=', trim( $restrict ) );
1035 if(count($temp) == 1) {
1036 // old format should be treated as edit/move restriction
1037 $this->mRestrictions
["edit"] = explode( ',', trim( $temp[0] ) );
1038 $this->mRestrictions
["move"] = explode( ',', trim( $temp[0] ) );
1040 $this->mRestrictions
[$temp[0]] = explode( ',', trim( $temp[1] ) );
1043 $this->mRestrictionsLoaded
= true;
1047 * Accessor/initialisation for mRestrictions
1048 * @param string $action action that permission needs to be checked for
1049 * @return array the array of groups allowed to edit this article
1052 function getRestrictions($action) {
1053 $id = $this->getArticleID();
1054 if ( 0 == $id ) { return array(); }
1056 if ( ! $this->mRestrictionsLoaded
) {
1057 $dbr =& wfGetDB( DB_SLAVE
);
1058 $res = $dbr->selectField( 'page', 'page_restrictions', 'page_id='.$id );
1059 $this->loadRestrictions( $res );
1061 if( isset( $this->mRestrictions
[$action] ) ) {
1062 return $this->mRestrictions
[$action];
1068 * Is there a version of this page in the deletion archive?
1069 * @return int the number of archived revisions
1072 function isDeleted() {
1073 $fname = 'Title::isDeleted';
1074 if ( $this->getNamespace() < 0 ) {
1077 $dbr =& wfGetDB( DB_SLAVE
);
1078 $n = $dbr->selectField( 'archive', 'COUNT(*)', array( 'ar_namespace' => $this->getNamespace(),
1079 'ar_title' => $this->getDBkey() ), $fname );
1085 * Get the article ID for this Title from the link cache,
1086 * adding it if necessary
1087 * @param int $flags a bit field; may be GAID_FOR_UPDATE to select
1089 * @return int the ID
1092 function getArticleID( $flags = 0 ) {
1093 global $wgLinkCache;
1094 if ( $flags & GAID_FOR_UPDATE
) {
1095 $oldUpdate = $wgLinkCache->forUpdate( true );
1096 $this->mArticleID
= $wgLinkCache->addLinkObj( $this );
1097 $wgLinkCache->forUpdate( $oldUpdate );
1099 if ( -1 == $this->mArticleID
) {
1100 $this->mArticleID
= $wgLinkCache->addLinkObj( $this );
1103 return $this->mArticleID
;
1106 function getLatestRevID() {
1107 if ($this->mLatestID
!== false)
1108 return $this->mLatestID
;
1110 $db =& wfGetDB(DB_SLAVE
);
1111 return $this->mLatestID
= $db->selectField( 'revision',
1113 array('rev_page' => $this->getArticleID()),
1114 'Title::getLatestRevID' );
1118 * This clears some fields in this object, and clears any associated
1119 * keys in the "bad links" section of $wgLinkCache.
1121 * - This is called from Article::insertNewArticle() to allow
1122 * loading of the new page_id. It's also called from
1123 * Article::doDeleteArticle()
1125 * @param int $newid the new Article ID
1128 function resetArticleID( $newid ) {
1129 global $wgLinkCache;
1130 $wgLinkCache->clearBadLink( $this->getPrefixedDBkey() );
1132 if ( 0 == $newid ) { $this->mArticleID
= -1; }
1133 else { $this->mArticleID
= $newid; }
1134 $this->mRestrictionsLoaded
= false;
1135 $this->mRestrictions
= array();
1139 * Updates page_touched for this page; called from LinksUpdate.php
1140 * @return bool true if the update succeded
1143 function invalidateCache() {
1144 global $wgUseFileCache;
1146 if ( wfReadOnly() ) {
1150 $now = wfTimestampNow();
1151 $dbw =& wfGetDB( DB_MASTER
);
1152 $success = $dbw->update( 'page',
1154 'page_touched' => $dbw->timestamp()
1155 ), array( /* WHERE */
1156 'page_namespace' => $this->getNamespace() ,
1157 'page_title' => $this->getDBkey()
1158 ), 'Title::invalidateCache'
1161 if ($wgUseFileCache) {
1162 $cache = new CacheManager($this);
1163 @unlink
($cache->fileCacheName());
1170 * Prefix some arbitrary text with the namespace or interwiki prefix
1173 * @param string $name the text
1174 * @return string the prefixed text
1177 /* private */ function prefix( $name ) {
1181 if ( '' != $this->mInterwiki
) {
1182 $p = $this->mInterwiki
. ':';
1184 if ( 0 != $this->mNamespace
) {
1185 $p .= $wgContLang->getNsText( $this->mNamespace
) . ':';
1191 * Secure and split - main initialisation function for this object
1193 * Assumes that mDbkeyform has been set, and is urldecoded
1194 * and uses underscores, but not otherwise munged. This function
1195 * removes illegal characters, splits off the interwiki and
1196 * namespace prefixes, sets the other forms, and canonicalizes
1198 * @return bool true on success
1201 /* private */ function secureAndSplit() {
1202 global $wgContLang, $wgLocalInterwiki, $wgCapitalLinks;
1203 $fname = 'Title::secureAndSplit';
1204 wfProfileIn( $fname );
1207 static $rxTc = false;
1209 # % is needed as well
1210 $rxTc = '/[^' . Title
::legalChars() . ']|%[0-9A-Fa-f]{2}/S';
1213 $this->mInterwiki
= $this->mFragment
= '';
1214 $this->mNamespace
= $this->mDefaultNamespace
; # Usually NS_MAIN
1216 # Clean up whitespace
1218 $t = preg_replace( '/[ _]+/', '_', $this->mDbkeyform
);
1219 $t = trim( $t, '_' );
1222 wfProfileOut( $fname );
1226 if( false !== strpos( $t, UTF8_REPLACEMENT
) ) {
1227 # Contained illegal UTF-8 sequences or forbidden Unicode chars.
1228 wfProfileOut( $fname );
1232 $this->mDbkeyform
= $t;
1234 # Initial colon indicates main namespace rather than specified default
1235 # but should not create invalid {ns,title} pairs such as {0,Project:Foo}
1236 if ( ':' == $t{0} ) {
1237 $this->mNamespace
= NS_MAIN
;
1238 $t = substr( $t, 1 ); # remove the colon but continue processing
1241 # Namespace or interwiki prefix
1244 if ( preg_match( "/^(.+?)_*:_*(.*)$/S", $t, $m ) ) {
1246 $lowerNs = strtolower( $p );
1247 if ( $ns = Namespace::getCanonicalIndex( $lowerNs ) ) {
1248 # Canonical namespace
1250 $this->mNamespace
= $ns;
1251 } elseif ( $ns = $wgContLang->getNsIndex( $lowerNs )) {
1252 # Ordinary namespace
1254 $this->mNamespace
= $ns;
1255 } elseif( $this->getInterwikiLink( $p ) ) {
1257 # Can't make a local interwiki link to an interwiki link.
1258 # That's just crazy!
1259 wfProfileOut( $fname );
1265 $this->mInterwiki
= strtolower( $p );
1267 # Redundant interwiki prefix to the local wiki
1268 if ( 0 == strcasecmp( $this->mInterwiki
, $wgLocalInterwiki ) ) {
1270 # Can't have an empty self-link
1271 wfProfileOut( $fname );
1274 $this->mInterwiki
= '';
1276 # Do another namespace split...
1280 # If there's no recognized interwiki or namespace,
1281 # then let the colon expression be part of the title.
1287 # We already know that some pages won't be in the database!
1289 if ( '' != $this->mInterwiki ||
-1 == $this->mNamespace
) {
1290 $this->mArticleID
= 0;
1292 $f = strstr( $r, '#' );
1293 if ( false !== $f ) {
1294 $this->mFragment
= substr( $f, 1 );
1295 $r = substr( $r, 0, strlen( $r ) - strlen( $f ) );
1296 # remove whitespace again: prevents "Foo_bar_#"
1297 # becoming "Foo_bar_"
1298 $r = preg_replace( '/_*$/', '', $r );
1301 # Reject illegal characters.
1303 if( preg_match( $rxTc, $r ) ) {
1304 wfProfileOut( $fname );
1309 * Pages with "/./" or "/../" appearing in the URLs will
1310 * often be unreachable due to the way web browsers deal
1311 * with 'relative' URLs. Forbid them explicitly.
1313 if ( strpos( $r, '.' ) !== false &&
1314 ( $r === '.' ||
$r === '..' ||
1315 strpos( $r, './' ) === 0 ||
1316 strpos( $r, '../' ) === 0 ||
1317 strpos( $r, '/./' ) !== false ||
1318 strpos( $r, '/../' ) !== false ) )
1320 wfProfileOut( $fname );
1324 # We shouldn't need to query the DB for the size.
1325 #$maxSize = $dbr->textFieldSize( 'page', 'page_title' );
1326 if ( strlen( $r ) > 255 ) {
1327 wfProfileOut( $fname );
1332 * Normally, all wiki links are forced to have
1333 * an initial capital letter so [[foo]] and [[Foo]]
1334 * point to the same place.
1336 * Don't force it for interwikis, since the other
1337 * site might be case-sensitive.
1339 if( $wgCapitalLinks && $this->mInterwiki
== '') {
1340 $t = $wgContLang->ucfirst( $r );
1346 * Can't make a link to a namespace alone...
1347 * "empty" local links can only be self-links
1348 * with a fragment identifier.
1351 $this->mInterwiki
== '' &&
1352 $this->mNamespace
!= NS_MAIN
) {
1353 wfProfileOut( $fname );
1358 $this->mDbkeyform
= $t;
1359 $this->mUrlform
= wfUrlencode( $t );
1361 $this->mTextform
= str_replace( '_', ' ', $t );
1363 wfProfileOut( $fname );
1368 * Get a Title object associated with the talk page of this article
1369 * @return Title the object for the talk page
1372 function getTalkPage() {
1373 return Title
::makeTitle( Namespace::getTalk( $this->getNamespace() ), $this->getDBkey() );
1377 * Get a title object associated with the subject page of this
1380 * @return Title the object for the subject page
1383 function getSubjectPage() {
1384 return Title
::makeTitle( Namespace::getSubject( $this->getNamespace() ), $this->getDBkey() );
1388 * Get an array of Title objects linking to this Title
1389 * Also stores the IDs in the link cache.
1391 * @param string $options may be FOR UPDATE
1392 * @return array the Title objects linking here
1395 function getLinksTo( $options = '' ) {
1396 global $wgLinkCache;
1397 $id = $this->getArticleID();
1400 $db =& wfGetDB( DB_MASTER
);
1402 $db =& wfGetDB( DB_SLAVE
);
1405 $res = $db->select( array( 'page', 'pagelinks' ),
1406 array( 'page_namespace', 'page_title', 'page_id' ),
1409 'pl_namespace' => $this->getNamespace(),
1410 'pl_title' => $this->getDbKey() ),
1411 'Title::getLinksTo',
1415 if ( $db->numRows( $res ) ) {
1416 while ( $row = $db->fetchObject( $res ) ) {
1417 if ( $titleObj = Title
::makeTitle( $row->page_namespace
, $row->page_title
) ) {
1418 $wgLinkCache->addGoodLinkObj( $row->page_id
, $titleObj );
1419 $retVal[] = $titleObj;
1423 $db->freeResult( $res );
1428 * Get an array of Title objects referring to non-existent articles linked from this page
1430 * @param string $options may be FOR UPDATE
1431 * @return array the Title objects
1434 function getBrokenLinksFrom( $options = '' ) {
1435 global $wgLinkCache;
1438 $db =& wfGetDB( DB_MASTER
);
1440 $db =& wfGetDB( DB_SLAVE
);
1443 $res = $db->safeQuery(
1444 "SELECT pl_namespace, pl_title
1447 ON pl_namespace=page_namespace
1448 AND pl_title=page_title
1450 AND page_namespace IS NULL
1452 $db->tableName( 'pagelinks' ),
1453 $db->tableName( 'page' ),
1454 $this->getArticleId(),
1458 if ( $db->numRows( $res ) ) {
1459 while ( $row = $db->fetchObject( $res ) ) {
1460 $retVal[] = Title
::makeTitle( $row->pl_namespace
, $row->pl_title
);
1463 $db->freeResult( $res );
1469 * Get a list of URLs to purge from the Squid cache when this
1472 * @return array the URLs
1475 function getSquidURLs() {
1477 $this->getInternalURL(),
1478 $this->getInternalURL( 'action=history' )
1483 * Move this page without authentication
1484 * @param Title &$nt the new page Title
1487 function moveNoAuth( &$nt ) {
1488 return $this->moveTo( $nt, false );
1492 * Check whether a given move operation would be valid.
1493 * Returns true if ok, or a message key string for an error message
1494 * if invalid. (Scarrrrry ugly interface this.)
1495 * @param Title &$nt the new title
1496 * @param bool $auth indicates whether $wgUser's permissions
1498 * @return mixed true on success, message name on failure
1501 function isValidMoveOperation( &$nt, $auth = true, $reason = '' ) {
1503 if( !$this or !$nt ) {
1504 return 'badtitletext';
1506 if( $this->equals( $nt ) ) {
1509 if( !$this->isMovable() ||
!$nt->isMovable() ) {
1510 return 'immobile_namespace';
1513 $fname = 'Title::move';
1514 $oldid = $this->getArticleID();
1515 $newid = $nt->getArticleID();
1517 if ( strlen( $nt->getDBkey() ) < 1 ) {
1518 return 'articleexists';
1520 if ( ( '' == $this->getDBkey() ) ||
1522 ( '' == $nt->getDBkey() ) ) {
1523 return 'badarticleerror';
1527 !$this->userCanEdit() ||
!$nt->userCanEdit() ||
1528 !$this->userCanMove() ||
!$nt->userCanMove() ) ) {
1529 return 'protectedpage';
1532 # The move is allowed only if (1) the target doesn't exist, or
1533 # (2) the target is a redirect to the source, and has no history
1534 # (so we can undo bad moves right after they're done).
1536 if ( 0 != $newid ) { # Target exists; check for validity
1537 if ( ! $this->isValidMoveTarget( $nt ) ) {
1538 return 'articleexists';
1545 * Move a title to a new location
1546 * @param Title &$nt the new title
1547 * @param bool $auth indicates whether $wgUser's permissions
1549 * @return mixed true on success, message name on failure
1552 function moveTo( &$nt, $auth = true, $reason = '' ) {
1553 $err = $this->isValidMoveOperation( $nt, $auth, $reason );
1554 if( is_string( $err ) ) {
1558 $pageid = $this->getArticleID();
1559 if( $nt->exists() ) {
1560 $this->moveOverExistingRedirect( $nt, $reason );
1561 $pageCountChange = 0;
1562 } else { # Target didn't exist, do normal move.
1563 $this->moveToNewTitle( $nt, $newid, $reason );
1564 $pageCountChange = 1;
1566 $redirid = $this->getArticleID();
1568 # Fixing category links (those without piped 'alternate' names) to be sorted under the new title
1569 $dbw =& wfGetDB( DB_MASTER
);
1570 $categorylinks = $dbw->tableName( 'categorylinks' );
1571 $sql = "UPDATE $categorylinks SET cl_sortkey=" . $dbw->addQuotes( $nt->getPrefixedText() ) .
1572 " WHERE cl_from=" . $dbw->addQuotes( $pageid ) .
1573 " AND cl_sortkey=" . $dbw->addQuotes( $this->getPrefixedText() );
1574 $dbw->query( $sql, 'SpecialMovepage::doSubmit' );
1578 $oldnamespace = $this->getNamespace() & ~
1;
1579 $newnamespace = $nt->getNamespace() & ~
1;
1580 $oldtitle = $this->getDBkey();
1581 $newtitle = $nt->getDBkey();
1583 if( $oldnamespace != $newnamespace ||
$oldtitle != $newtitle ) {
1584 WatchedItem
::duplicateEntries( $this, $nt );
1587 # Update search engine
1588 $u = new SearchUpdate( $pageid, $nt->getPrefixedDBkey() );
1590 $u = new SearchUpdate( $redirid, $this->getPrefixedDBkey(), '' );
1594 if ( $this->getNamespace() == NS_MAIN
and $nt->getNamespace() != NS_MAIN
) {
1595 # Moved out of main namespace
1596 # not viewed, edited, removing
1597 $u = new SiteStatsUpdate( 0, 1, -1, $pageCountChange);
1598 } elseif ( $this->getNamespace() != NS_MAIN
and $nt->getNamespace() == NS_MAIN
) {
1599 # Moved into main namespace
1600 # not viewed, edited, adding
1601 $u = new SiteStatsUpdate( 0, 1, +
1, $pageCountChange );
1602 } elseif ( $pageCountChange ) {
1604 $u = new SiteStatsUpdate( 0, 0, 0, 1 );
1613 wfRunHooks( 'TitleMoveComplete', array( &$this, &$nt, &$wgUser, $pageid, $redirid ) );
1618 * Move page to a title which is at present a redirect to the
1621 * @param Title &$nt the page to move to, which should currently
1625 function moveOverExistingRedirect( &$nt, $reason = '' ) {
1626 global $wgUser, $wgLinkCache, $wgUseSquid, $wgMwRedir;
1627 $fname = 'Title::moveOverExistingRedirect';
1628 $comment = wfMsgForContent( '1movedto2', $this->getPrefixedText(), $nt->getPrefixedText() );
1631 $comment .= ": $reason";
1634 $now = wfTimestampNow();
1636 $newid = $nt->getArticleID();
1637 $oldid = $this->getArticleID();
1638 $dbw =& wfGetDB( DB_MASTER
);
1639 $links = $dbw->tableName( 'links' );
1641 # Delete the old redirect. We don't save it to history since
1642 # by definition if we've got here it's rather uninteresting.
1643 # We have to remove it so that the next step doesn't trigger
1644 # a conflict on the unique namespace+title index...
1645 $dbw->delete( 'page', array( 'page_id' => $newid ), $fname );
1647 # Save a null revision in the page's history notifying of the move
1648 $nullRevision = Revision
::newNullRevision( $dbw, $oldid,
1649 wfMsgForContent( '1movedto2', $this->getPrefixedText(), $nt->getPrefixedText() ),
1651 $nullRevId = $nullRevision->insertOn( $dbw );
1653 # Change the name of the target page:
1654 $dbw->update( 'page',
1656 'page_touched' => $dbw->timestamp($now),
1657 'page_namespace' => $nt->getNamespace(),
1658 'page_title' => $nt->getDBkey(),
1659 'page_latest' => $nullRevId,
1661 /* WHERE */ array( 'page_id' => $oldid ),
1664 $wgLinkCache->clearLink( $nt->getPrefixedDBkey() );
1666 # Recreate the redirect, this time in the other direction.
1667 $redirectText = $wgMwRedir->getSynonym( 0 ) . ' [[' . $nt->getPrefixedText() . "]]\n";
1668 $redirectArticle = new Article( $this );
1669 $newid = $redirectArticle->insertOn( $dbw );
1670 $redirectRevision = new Revision( array(
1672 'comment' => $comment,
1673 'text' => $redirectText ) );
1674 $revid = $redirectRevision->insertOn( $dbw );
1675 $redirectArticle->updateRevisionOn( $dbw, $redirectRevision, 0 );
1676 $wgLinkCache->clearLink( $this->getPrefixedDBkey() );
1679 $log = new LogPage( 'move' );
1680 $log->addEntry( 'move_redir', $this, $reason, array( 1 => $nt->getPrefixedText() ) );
1682 # Now, we record the link from the redirect to the new title.
1683 # It should have no other outgoing links...
1684 $dbw->delete( 'pagelinks', array( 'pl_from' => $newid ), $fname );
1685 $dbw->insert( 'pagelinks',
1687 'pl_from' => $newid,
1688 'pl_namespace' => $nt->getNamespace(),
1689 'pl_title' => $nt->getDbKey() ),
1693 if ( $wgUseSquid ) {
1694 $urls = array_merge( $nt->getSquidURLs(), $this->getSquidURLs() );
1695 $u = new SquidUpdate( $urls );
1701 * Move page to non-existing title.
1702 * @param Title &$nt the new Title
1703 * @param int &$newid set to be the new article ID
1706 function moveToNewTitle( &$nt, &$newid, $reason = '' ) {
1707 global $wgUser, $wgLinkCache, $wgUseSquid;
1709 $fname = 'MovePageForm::moveToNewTitle';
1710 $comment = wfMsgForContent( '1movedto2', $this->getPrefixedText(), $nt->getPrefixedText() );
1712 $comment .= ": $reason";
1715 $newid = $nt->getArticleID();
1716 $oldid = $this->getArticleID();
1717 $dbw =& wfGetDB( DB_MASTER
);
1718 $now = $dbw->timestamp();
1722 # Save a null revision in the page's history notifying of the move
1723 $nullRevision = Revision
::newNullRevision( $dbw, $oldid,
1724 wfMsgForContent( '1movedto2', $this->getPrefixedText(), $nt->getPrefixedText() ),
1726 $nullRevId = $nullRevision->insertOn( $dbw );
1729 $dbw->update( 'page',
1731 'page_touched' => $now,
1732 'page_namespace' => $nt->getNamespace(),
1733 'page_title' => $nt->getDBkey(),
1734 'page_latest' => $nullRevId,
1736 /* WHERE */ array( 'page_id' => $oldid ),
1740 $wgLinkCache->clearLink( $nt->getPrefixedDBkey() );
1743 $redirectText = $wgMwRedir->getSynonym( 0 ) . ' [[' . $nt->getPrefixedText() . "]]\n";
1744 $redirectArticle = new Article( $this );
1745 $newid = $redirectArticle->insertOn( $dbw );
1746 $redirectRevision = new Revision( array(
1748 'comment' => $comment,
1749 'text' => $redirectText ) );
1750 $revid = $redirectRevision->insertOn( $dbw );
1751 $redirectArticle->updateRevisionOn( $dbw, $redirectRevision, 0 );
1752 $wgLinkCache->clearLink( $this->getPrefixedDBkey() );
1755 $log = new LogPage( 'move' );
1756 $log->addEntry( 'move', $this, $reason, array( 1 => $nt->getPrefixedText()) );
1758 # Purge caches as per article creation
1759 Article
::onArticleCreate( $nt );
1761 # Record the just-created redirect's linking to the page
1762 $dbw->insert( 'pagelinks',
1764 'pl_from' => $newid,
1765 'pl_namespace' => $nt->getNamespace(),
1766 'pl_title' => $nt->getDBkey() ),
1769 # Non-existent target may have had broken links to it; these must
1770 # now be touched to update link coloring.
1773 # Purge old title from squid
1774 # The new title, and links to the new title, are purged in Article::onArticleCreate()
1775 $titles = $nt->getLinksTo();
1776 if ( $wgUseSquid ) {
1777 $urls = $this->getSquidURLs();
1778 foreach ( $titles as $linkTitle ) {
1779 $urls[] = $linkTitle->getInternalURL();
1781 $u = new SquidUpdate( $urls );
1787 * Checks if $this can be moved to a given Title
1788 * - Selects for update, so don't call it unless you mean business
1790 * @param Title &$nt the new title to check
1793 function isValidMoveTarget( $nt ) {
1795 $fname = 'Title::isValidMoveTarget';
1796 $dbw =& wfGetDB( DB_MASTER
);
1799 $id = $nt->getArticleID();
1800 $obj = $dbw->selectRow( array( 'page', 'revision', 'text'),
1801 array( 'page_is_redirect','old_text','old_flags' ),
1802 array( 'page_id' => $id, 'page_latest=rev_id', 'rev_text_id=old_id' ),
1803 $fname, 'FOR UPDATE' );
1805 if ( !$obj ||
0 == $obj->page_is_redirect
) {
1809 $text = Revision
::getRevisionText( $obj );
1811 # Does the redirect point to the source?
1812 if ( preg_match( "/\\[\\[\\s*([^\\]\\|]*)]]/", $text, $m ) ) {
1813 $redirTitle = Title
::newFromText( $m[1] );
1814 if( !is_object( $redirTitle ) ||
1815 $redirTitle->getPrefixedDBkey() != $this->getPrefixedDBkey() ) {
1823 # Does the article have a history?
1824 $row = $dbw->selectRow( array( 'page', 'revision'),
1826 array( 'page_namespace' => $nt->getNamespace(),
1827 'page_title' => $nt->getDBkey(),
1828 'page_id=rev_page AND page_latest != rev_id'
1829 ), $fname, 'FOR UPDATE'
1832 # Return true if there was no history
1833 return $row === false;
1837 * Create a redirect; fails if the title already exists; does
1840 * @param Title $dest the destination of the redirect
1841 * @param string $comment the comment string describing the move
1842 * @return bool true on success
1845 function createRedirect( $dest, $comment ) {
1847 if ( $this->getArticleID() ) {
1851 $fname = 'Title::createRedirect';
1852 $dbw =& wfGetDB( DB_MASTER
);
1854 $article = new Article( $this );
1855 $newid = $article->insertOn( $dbw );
1856 $revision = new Revision( array(
1858 'comment' => $comment,
1859 'text' => "#REDIRECT [[" . $dest->getPrefixedText() . "]]\n",
1861 $revisionId = $revision->insertOn( $dbw );
1862 $article->updateRevisionOn( $dbw, $revision, 0 );
1865 $dbw->insert( 'pagelinks',
1867 'pl_from' => $newid,
1868 'pl_namespace' => $dest->getNamespace(),
1869 'pl_title' => $dest->getDbKey()
1873 Article
::onArticleCreate( $this );
1878 * Get categories to which this Title belongs and return an array of
1879 * categories' names.
1881 * @return array an array of parents in the form:
1882 * $parent => $currentarticle
1885 function getParentCategories() {
1886 global $wgContLang,$wgUser;
1888 $titlekey = $this->getArticleId();
1889 $sk =& $wgUser->getSkin();
1891 $dbr =& wfGetDB( DB_SLAVE
);
1892 $categorylinks = $dbr->tableName( 'categorylinks' );
1895 $sql = "SELECT * FROM $categorylinks"
1896 ." WHERE cl_from='$titlekey'"
1897 ." AND cl_from <> '0'"
1898 ." ORDER BY cl_sortkey";
1900 $res = $dbr->query ( $sql ) ;
1902 if($dbr->numRows($res) > 0) {
1903 while ( $x = $dbr->fetchObject ( $res ) )
1904 //$data[] = Title::newFromText($wgContLang->getNSText ( NS_CATEGORY ).':'.$x->cl_to);
1905 $data[$wgContLang->getNSText ( NS_CATEGORY
).':'.$x->cl_to
] = $this->getFullText();
1906 $dbr->freeResult ( $res ) ;
1914 * Get a tree of parent categories
1915 * @param array $children an array with the children in the keys, to check for circular refs
1919 function getParentCategoryTree( $children = array() ) {
1920 $parents = $this->getParentCategories();
1922 if($parents != '') {
1923 foreach($parents as $parent => $current)
1925 if ( array_key_exists( $parent, $children ) ) {
1926 # Circular reference
1927 $stack[$parent] = array();
1929 $nt = Title
::newFromText($parent);
1930 $stack[$parent] = $nt->getParentCategoryTree( $children +
array($parent => 1) );
1941 * Get an associative array for selecting this title from
1947 function curCond() {
1948 wfDebugDieBacktrace( 'curCond called' );
1949 return array( 'cur_namespace' => $this->mNamespace
, 'cur_title' => $this->mDbkeyform
);
1953 * Get an associative array for selecting this title from the
1959 function oldCond() {
1960 wfDebugDieBacktrace( 'oldCond called' );
1961 return array( 'old_namespace' => $this->mNamespace
, 'old_title' => $this->mDbkeyform
);
1965 * Get the revision ID of the previous revision
1967 * @param integer $revision Revision ID. Get the revision that was before this one.
1968 * @return interger $oldrevision|false
1970 function getPreviousRevisionID( $revision ) {
1971 $dbr =& wfGetDB( DB_SLAVE
);
1972 return $dbr->selectField( 'revision', 'rev_id',
1973 'rev_page=' . intval( $this->getArticleId() ) .
1974 ' AND rev_id<' . intval( $revision ) . ' ORDER BY rev_id DESC' );
1978 * Get the revision ID of the next revision
1980 * @param integer $revision Revision ID. Get the revision that was after this one.
1981 * @return interger $oldrevision|false
1983 function getNextRevisionID( $revision ) {
1984 $dbr =& wfGetDB( DB_SLAVE
);
1985 return $dbr->selectField( 'revision', 'rev_id',
1986 'rev_page=' . intval( $this->getArticleId() ) .
1987 ' AND rev_id>' . intval( $revision ) . ' ORDER BY rev_id' );
1991 * Compare with another title.
1993 * @param Title $title
1996 function equals( &$title ) {
1997 return $this->getInterwiki() == $title->getInterwiki()
1998 && $this->getNamespace() == $title->getNamespace()
1999 && $this->getDbkey() == $title->getDbkey();
2003 * Check if page exists
2007 return $this->getArticleId() != 0;
2011 * Should a link should be displayed as a known link, just based on its title?
2013 * Currently, a self-link with a fragment, special pages and image pages are in
2014 * this category. Special pages never exist in the database. Some images do not
2015 * have description pages in the database, but the description page contains
2016 * useful history information that the user may want to link to.
2018 function isAlwaysKnown() {
2019 return $this->isExternal() ||
( 0 == $this->mNamespace
&& "" == $this->mDbkeyform
)
2020 || NS_SPECIAL
== $this->mNamespace || NS_IMAGE
== $this->mNamespace
;
2024 * Update page_touched timestamps on pages linking to this title.
2025 * In principal, this could be backgrounded and could also do squid
2028 function touchLinks() {
2029 $fname = 'Title::touchLinks';
2031 $dbw =& wfGetDB( DB_MASTER
);
2033 $res = $dbw->select( 'pagelinks',
2036 'pl_namespace' => $this->getNamespace(),
2037 'pl_title' => $this->getDbKey() ),
2039 if ( 0 == $dbw->numRows( $res ) ) {
2044 $toucharr = array();
2045 while( $row = $dbw->fetchObject( $res ) ) {
2046 $toucharr[] = $row->pl_from
;
2048 if (!count($toucharr))
2050 $dbw->update( 'page', /* SET */ array( 'page_touched' => $dbw->timestamp() ),
2051 /* WHERE */ array( 'page_id' => $toucharr ),$fname);
2054 function trackbackURL() {
2055 global $wgTitle, $wgScriptPath, $wgServer;
2057 return "$wgServer$wgScriptPath/trackback.php?article="
2058 . htmlspecialchars(urlencode($wgTitle->getPrefixedDBkey()));
2061 function trackbackRDF() {
2062 $url = htmlspecialchars($this->getFullURL());
2063 $title = htmlspecialchars($this->getText());
2064 $tburl = $this->trackbackURL();
2067 <rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"
2068 xmlns:dc=\"http://purl.org/dc/elements/1.1/\"
2069 xmlns:trackback=\"http://madskills.com/public/xml/rss/module/trackback/\">
2072 dc:identifier=\"$url\"
2074 trackback:ping=\"$tburl\" />