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 $mRestrictions; # Array of groups allowed to edit this article
46 # Only null or "sysop" are supported
47 var $mRestrictionsLoaded; # Boolean for initialisation on demand
48 var $mPrefixedText; # Text form including namespace/interwiki, initialised on demand
49 var $mDefaultNamespace; # Namespace index when there is no namespace
50 # Zero except in {{transclusion}} tags
58 /* private */ function Title() {
59 $this->mInterwiki
= $this->mUrlform
=
60 $this->mTextform
= $this->mDbkeyform
= '';
61 $this->mArticleID
= -1;
62 $this->mNamespace
= NS_MAIN
;
63 $this->mRestrictionsLoaded
= false;
64 $this->mRestrictions
= array();
65 # Dont change the following, NS_MAIN is hardcoded in several place
67 $this->mDefaultNamespace
= NS_MAIN
;
71 * Create a new Title from a prefixed DB key
72 * @param string $key The database key, which has underscores
73 * instead of spaces, possibly including namespace and
75 * @return Title the new object, or NULL on an error
79 /* static */ function newFromDBkey( $key ) {
81 $t->mDbkeyform
= $key;
82 if( $t->secureAndSplit() )
89 * Create a new Title from text, such as what one would
90 * find in a link. Decodes any HTML entities in the text.
92 * @param string $text the link text; spaces, prefixes,
93 * and an initial ':' indicating the main namespace
95 * @param int $defaultNamespace the namespace to use if
96 * none is specified by a prefix
97 * @return Title the new object, or NULL on an error
101 function &newFromText( $text, $defaultNamespace = NS_MAIN
) {
102 $fname = 'Title::newFromText';
103 wfProfileIn( $fname );
105 if( is_object( $text ) ) {
106 wfDebugDieBacktrace( 'Title::newFromText given an object' );
110 * Wiki pages often contain multiple links to the same page.
111 * Title normalization and parsing can become expensive on
112 * pages with many links, so we can save a little time by
115 * In theory these are value objects and won't get changed...
117 static $titleCache = array();
118 if( $defaultNamespace == NS_MAIN
&& isset( $titleCache[$text] ) ) {
119 wfProfileOut( $fname );
120 return $titleCache[$text];
124 * Convert things like é into real text...
126 global $wgInputEncoding;
127 $filteredText = do_html_entity_decode( $text, ENT_COMPAT
, $wgInputEncoding );
130 * Convert things like ā or 〗 into real text...
131 * WARNING: Not friendly to internal links on a latin-1 wiki.
133 $filteredText = wfMungeToUtf8( $filteredText );
135 # What was this for? TS 2004-03-03
136 # $text = urldecode( $text );
139 $t->mDbkeyform
= str_replace( ' ', '_', $filteredText );
140 $t->mDefaultNamespace
= $defaultNamespace;
142 if( $t->secureAndSplit() ) {
143 if( $defaultNamespace == NS_MAIN
) {
144 if( count( $titleCache ) >= MW_TITLECACHE_MAX
) {
145 # Avoid memory leaks on mass operations...
146 $titleCache = array();
148 $titleCache[$text] =& $t;
150 wfProfileOut( $fname );
153 wfProfileOut( $fname );
159 * Create a new Title from URL-encoded text. Ensures that
160 * the given title's length does not exceed the maximum.
161 * @param string $url the title, as might be taken from a URL
162 * @return Title the new object, or NULL on an error
166 /* static */ function newFromURL( $url ) {
167 global $wgLang, $wgServer;
170 # For compatibility with old buggy URLs. "+" is not valid in titles,
171 # but some URLs used it as a space replacement and they still come
172 # from some external search tools.
173 $s = str_replace( '+', ' ', $url );
175 $t->mDbkeyform
= str_replace( ' ', '_', $s );
176 if( $t->secureAndSplit() ) {
184 * Create a new Title from an article ID
185 * @todo This is inefficiently implemented, the page row is requested
186 * but not used for anything else
187 * @param int $id the page_id corresponding to the Title to create
188 * @return Title the new object, or NULL on an error
191 /* static */ function newFromID( $id ) {
192 $fname = 'Title::newFromID';
193 $dbr =& wfGetDB( DB_SLAVE
);
194 $row = $dbr->selectRow( 'page', array( 'page_namespace', 'page_title' ),
195 array( 'page_id' => $id ), $fname );
196 if ( $row !== false ) {
197 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
205 * Create a new Title from a namespace index and a DB key.
206 * It's assumed that $ns and $title are *valid*, for instance when
207 * they came directly from the database or a special page name.
208 * For convenience, spaces are converted to underscores so that
209 * eg user_text fields can be used directly.
211 * @param int $ns the namespace of the article
212 * @param string $title the unprefixed database key form
213 * @return Title the new object
217 /* static */ function &makeTitle( $ns, $title ) {
221 $t->mNamespace
= IntVal( $ns );
222 $t->mDbkeyform
= str_replace( ' ', '_', $title );
223 $t->mArticleID
= ( $ns >= 0 ) ?
-1 : 0;
224 $t->mUrlform
= wfUrlencode( $t->mDbkeyform
);
225 $t->mTextform
= str_replace( '_', ' ', $title );
230 * Create a new Title frrom a namespace index and a DB key.
231 * The parameters will be checked for validity, which is a bit slower
232 * 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 /* static */ function makeTitleSafe( $ns, $title ) {
241 $t->mDbkeyform
= Title
::makeName( $ns, $title );
242 if( $t->secureAndSplit() ) {
250 * Create a new Title for the Main Page
252 * @return Title the new object
255 /* static */ 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 /* static */ function newFromRedirect( $text ) {
270 if ( $wgMwRedir->matchStart( $text ) ) {
271 if ( preg_match( '/\\[\\[([^\\]\\|]+)[\\]\\|]/', $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 /* static */ 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 /* static */ function legalChars() {
319 # Missing characters:
320 # * []|# Needed for link syntax
321 # * % and + are corrupted by Apache when they appear in the path
323 # % seems to work though
325 # The problem with % is that URLs are double-unescaped: once by Apache's
326 # path conversion code, and again by PHP. So %253F, for example, becomes "?".
327 # Our code does not double-escape to compensate for this, indeed double escaping
328 # would break if the double-escaped title was passed in the query string
329 # rather than the path. This is a minor security issue because articles can be
330 # created such that they are hard to view or edit. -- TS
332 # Theoretically 0x80-0x9F of ISO 8859-1 should be disallowed, but
333 # this breaks interlanguage links
335 $set = " %!\"$&'()*,\\-.\\/0-9:;=?@A-Z\\\\^_`a-z~\\x80-\\xFF";
340 * Get a string representation of a title suitable for
341 * including in a search index
343 * @param int $ns a namespace index
344 * @param string $title text-form main part
345 * @return string a stripped-down title string ready for the
348 /* static */ function indexTitle( $ns, $title ) {
349 global $wgDBminWordLen, $wgContLang;
350 require_once( 'SearchEngine.php' );
352 $lc = SearchEngine
::legalSearchChars() . '&#;';
353 $t = $wgContLang->stripForSearch( $title );
354 $t = preg_replace( "/[^{$lc}]+/", ' ', $t );
355 $t = strtolower( $t );
358 $t = preg_replace( "/([{$lc}]+)'s( |$)/", "\\1 \\1's ", $t );
359 $t = preg_replace( "/([{$lc}]+)s'( |$)/", "\\1s ", $t );
361 $t = preg_replace( "/\\s+/", ' ', $t );
363 if ( $ns == NS_IMAGE
) {
364 $t = preg_replace( "/ (png|gif|jpg|jpeg|ogg)$/", "", $t );
370 * Make a prefixed DB key from a DB key and a namespace index
371 * @param int $ns numerical representation of the namespace
372 * @param string $title the DB key form the title
373 * @return string the prefixed form of the title
375 /* static */ function makeName( $ns, $title ) {
378 $n = $wgContLang->getNsText( $ns );
379 if ( '' == $n ) { return $title; }
380 else { return $n.':'.$title; }
384 * Returns the URL associated with an interwiki prefix
385 * @param string $key the interwiki prefix (e.g. "MeatBall")
386 * @return the associated URL, containing "$1", which should be
387 * replaced by an article title
391 function getInterwikiLink( $key ) {
392 global $wgMemc, $wgDBname, $wgInterwikiExpiry, $wgTitleInterwikiCache;
393 $fname = 'Title::getInterwikiLink';
395 wfProfileIn( $fname );
397 $k = $wgDBname.':interwiki:'.$key;
398 if( array_key_exists( $k, $wgTitleInterwikiCache ) ) {
399 wfProfileOut( $fname );
400 return $wgTitleInterwikiCache[$k]->iw_url
;
403 $s = $wgMemc->get( $k );
404 # Ignore old keys with no iw_local
405 if( $s && isset( $s->iw_local
) ) {
406 $wgTitleInterwikiCache[$k] = $s;
407 wfProfileOut( $fname );
411 $dbr =& wfGetDB( DB_SLAVE
);
412 $res = $dbr->select( 'interwiki',
413 array( 'iw_url', 'iw_local' ),
414 array( 'iw_prefix' => $key ), $fname );
416 wfProfileOut( $fname );
420 $s = $dbr->fetchObject( $res );
422 # Cache non-existence: create a blank object and save it to memcached
427 $wgMemc->set( $k, $s, $wgInterwikiExpiry );
428 $wgTitleInterwikiCache[$k] = $s;
430 wfProfileOut( $fname );
435 * Determine whether the object refers to a page within
438 * @return bool TRUE if this is an in-project interwiki link
439 * or a wikilink, FALSE otherwise
443 global $wgTitleInterwikiCache, $wgDBname;
445 if ( $this->mInterwiki
!= '' ) {
446 # Make sure key is loaded into cache
447 $this->getInterwikiLink( $this->mInterwiki
);
448 $k = $wgDBname.':interwiki:' . $this->mInterwiki
;
449 return (bool)($wgTitleInterwikiCache[$k]->iw_local
);
456 * Update the page_touched field for an array of title objects
457 * @todo Inefficient unless the IDs are already loaded into the
459 * @param array $titles an array of Title objects to be touched
460 * @param string $timestamp the timestamp to use instead of the
461 * default current time
465 /* static */ function touchArray( $titles, $timestamp = '' ) {
466 if ( count( $titles ) == 0 ) {
469 $dbw =& wfGetDB( DB_MASTER
);
470 if ( $timestamp == '' ) {
471 $timestamp = $dbw->timestamp();
473 $page = $dbw->tableName( 'page' );
474 $sql = "UPDATE $page SET page_touched='{$timestamp}' WHERE page_id IN (";
477 foreach ( $titles as $title ) {
482 $sql .= $title->getArticleID();
486 $dbw->query( $sql, 'Title::touchArray' );
490 #----------------------------------------------------------------------------
492 #----------------------------------------------------------------------------
494 /** Simple accessors */
496 * Get the text form (spaces not underscores) of the main part
500 function getText() { return $this->mTextform
; }
502 * Get the URL-encoded form of the main part
506 function getPartialURL() { return $this->mUrlform
; }
508 * Get the main part with underscores
512 function getDBkey() { return $this->mDbkeyform
; }
514 * Get the namespace index, i.e. one of the NS_xxxx constants
518 function getNamespace() { return $this->mNamespace
; }
520 * Get the interwiki prefix (or null string)
524 function getInterwiki() { return $this->mInterwiki
; }
526 * Get the Title fragment (i.e. the bit after the #)
530 function getFragment() { return $this->mFragment
; }
532 * Get the default namespace index, for when there is no namespace
536 function getDefaultNamespace() { return $this->mDefaultNamespace
; }
539 * Get title for search index
540 * @return string a stripped-down title string ready for the
543 function getIndexTitle() {
544 return Title
::indexTitle( $this->mNamespace
, $this->mTextform
);
548 * Get the prefixed database key form
549 * @return string the prefixed title, with underscores and
550 * any interwiki and namespace prefixes
553 function getPrefixedDBkey() {
554 $s = $this->prefix( $this->mDbkeyform
);
555 $s = str_replace( ' ', '_', $s );
560 * Get the prefixed title with spaces.
561 * This is the form usually used for display
562 * @return string the prefixed title, with spaces
565 function getPrefixedText() {
567 if ( empty( $this->mPrefixedText
) ) {
568 $s = $this->prefix( $this->mTextform
);
569 $s = str_replace( '_', ' ', $s );
570 $this->mPrefixedText
= $s;
572 return $this->mPrefixedText
;
576 * Get the prefixed title with spaces, plus any fragment
577 * (part beginning with '#')
578 * @return string the prefixed title, with spaces and
579 * the fragment, including '#'
582 function getFullText() {
584 $text = $this->getPrefixedText();
585 if( '' != $this->mFragment
) {
586 $text .= '#' . $this->mFragment
;
592 * Get a URL-encoded title (not an actual URL) including interwiki
593 * @return string the URL-encoded form
596 function getPrefixedURL() {
597 $s = $this->prefix( $this->mDbkeyform
);
598 $s = str_replace( ' ', '_', $s );
600 $s = wfUrlencode ( $s ) ;
602 # Cleaning up URL to make it look nice -- is this safe?
603 $s = str_replace( '%28', '(', $s );
604 $s = str_replace( '%29', ')', $s );
610 * Get a real URL referring to this title, with interwiki link and
613 * @param string $query an optional query string, not used
614 * for interwiki links
615 * @return string the URL
618 function getFullURL( $query = '' ) {
619 global $wgContLang, $wgServer, $wgScript;
621 if ( '' == $this->mInterwiki
) {
622 return $wgServer . $this->getLocalUrl( $query );
624 $baseUrl = $this->getInterwikiLink( $this->mInterwiki
);
625 $namespace = $wgContLang->getNsText( $this->mNamespace
);
626 if ( '' != $namespace ) {
627 # Can this actually happen? Interwikis shouldn't be parsed.
630 $url = str_replace( '$1', $namespace . $this->mUrlform
, $baseUrl );
632 if( false === strpos( $url, '?' ) ) {
639 if ( '' != $this->mFragment
) {
640 $url .= '#' . $this->mFragment
;
647 * Get a relative directory for putting an HTML version of this article into
649 function getHashedDirectory() {
650 $dbkey = $this->getPrefixedDBkey();
651 if ( strlen( $dbkey ) < 2 ) {
652 $dbkey = sprintf( "%2s", $dbkey );
655 for ( $i=0; $i<=1; $i++
) {
659 if ( ord( $dbkey{$i} ) < 128 && ord( $dbkey{$i} ) > 32 ) {
660 $dir .= strtolower( $dbkey{$i} );
662 $dir .= sprintf( "%02X", ord( $dbkey{$i} ) );
668 function getHashedFilename() {
669 $dbkey = $this->getPrefixedDBkey();
670 $dir = $this->getHashedDirectory();
671 $friendlyName = strtr( $dbkey, '/\\:*?"<>|', '_________' );
672 return "$dir/$friendlyName.html";
676 * Get a URL with no fragment or server name
677 * @param string $query an optional query string; if not specified,
678 * $wgArticlePath will be used.
679 * @return string the URL
682 function getLocalURL( $query = '' ) {
683 global $wgLang, $wgArticlePath, $wgScript, $wgMakeDumpLinks;
685 if ( $this->isExternal() ) {
686 return $this->getFullURL();
689 $dbkey = wfUrlencode( $this->getPrefixedDBkey() );
690 if ( $wgMakeDumpLinks ) {
691 $url = str_replace( '$1', wfUrlencode( $this->getHashedFilename() ), $wgArticlePath );
692 } elseif ( $query == '' ) {
693 $url = str_replace( '$1', $dbkey, $wgArticlePath );
695 if( preg_match( '/^(.*&|)action=([^&]*)(&(.*)|)$/', $query, $matches ) ) {
696 global $wgActionPaths;
697 $action = urldecode( $matches[2] );
698 if( isset( $wgActionPaths[$action] ) ) {
699 $query = $matches[1];
700 if( isset( $matches[4] ) ) $query .= $matches[4];
701 $url = str_replace( '$1', $dbkey, $wgActionPaths[$action] );
702 if( $query != '' ) $url .= '?' . $query;
706 if ( $query == '-' ) {
709 $url = "{$wgScript}?title={$dbkey}&{$query}";
715 * Get an HTML-escaped version of the URL form, suitable for
716 * using in a link, without a server name or fragment
717 * @param string $query an optional query string
718 * @return string the URL
721 function escapeLocalURL( $query = '' ) {
722 return htmlspecialchars( $this->getLocalURL( $query ) );
726 * Get an HTML-escaped version of the URL form, suitable for
727 * using in a link, including the server name and fragment
729 * @return string the URL
730 * @param string $query an optional query string
733 function escapeFullURL( $query = '' ) {
734 return htmlspecialchars( $this->getFullURL( $query ) );
738 * Get the URL form for an internal link.
739 * - Used in various Squid-related code, in case we have a different
740 * internal hostname for the server from the exposed one.
742 * @param string $query an optional query string
743 * @return string the URL
746 function getInternalURL( $query = '' ) {
747 global $wgInternalServer;
748 return $wgInternalServer . $this->getLocalURL( $query );
752 * Get the edit URL for this Title
753 * @return string the URL, or a null string if this is an
757 function getEditURL() {
758 global $wgServer, $wgScript;
760 if ( '' != $this->mInterwiki
) { return ''; }
761 $s = $this->getLocalURL( 'action=edit' );
767 * Get the HTML-escaped displayable text form.
768 * Used for the title field in <a> tags.
769 * @return string the text, including any prefixes
772 function getEscapedText() {
773 return htmlspecialchars( $this->getPrefixedText() );
777 * Is this Title interwiki?
781 function isExternal() { return ( '' != $this->mInterwiki
); }
784 * Does the title correspond to a protected article?
785 * @param string $what the action the page is protected from,
786 * by default checks move and edit
790 function isProtected($action = '') {
791 if ( -1 == $this->mNamespace
) { return true; }
792 if($action == 'edit' ||
$action == '') {
793 $a = $this->getRestrictions("edit");
794 if ( in_array( 'sysop', $a ) ) { return true; }
796 if($action == 'move' ||
$action == '') {
797 $a = $this->getRestrictions("move");
798 if ( in_array( 'sysop', $a ) ) { return true; }
804 * Is $wgUser is watching this page?
808 function userIsWatching() {
811 if ( -1 == $this->mNamespace
) { return false; }
812 if ( 0 == $wgUser->getID() ) { return false; }
814 return $wgUser->isWatched( $this );
818 * Is $wgUser perform $action this page?
819 * @param string $action action that permission needs to be checked for
823 function userCan($action) {
824 $fname = 'Title::userCanEdit';
825 wfProfileIn( $fname );
828 if( NS_SPECIAL
== $this->mNamespace
) {
829 wfProfileOut( $fname );
832 if( NS_MEDIAWIKI
== $this->mNamespace
&&
833 !$wgUser->isAllowed('editinterface') ) {
834 wfProfileOut( $fname );
837 if( $this->mDbkeyform
== '_' ) {
838 # FIXME: Is this necessary? Shouldn't be allowed anyway...
839 wfProfileOut( $fname );
843 # protect global styles and js
844 if ( NS_MEDIAWIKI
== $this->mNamespace
845 && preg_match("/\\.(css|js)$/", $this->mTextform
)
846 && !$wgUser->isAllowed('editinterface') ) {
847 wfProfileOut( $fname );
851 # protect css/js subpages of user pages
852 # XXX: this might be better using restrictions
853 # XXX: Find a way to work around the php bug that prevents using $this->userCanEditCssJsSubpage() from working
854 if( NS_USER
== $this->mNamespace
855 && preg_match("/\\.(css|js)$/", $this->mTextform
)
856 && !$wgUser->isAllowed('editinterface')
857 && !preg_match('/^'.preg_quote($wgUser->getName(), '/').'\//', $this->mTextform
) ) {
858 wfProfileOut( $fname );
862 foreach( $this->getRestrictions($action) as $right ) {
863 if( '' != $right && !$wgUser->isAllowed( $right ) ) {
864 wfProfileOut( $fname );
868 wfProfileOut( $fname );
873 * Can $wgUser edit this page?
877 function userCanEdit() {
878 return $this->userCan('edit');
882 * Can $wgUser move this page?
886 function userCanMove() {
887 return $this->userCan('move');
891 * Can $wgUser read this page?
895 function userCanRead() {
898 if( $wgUser->isAllowed('read') ) {
901 global $wgWhitelistRead;
903 /** If anon users can create an account,
904 they need to reach the login page first! */
905 if( $wgUser->isAllowed( 'createaccount' )
906 && $this->mId
== NS_SPECIAL
907 && $this->getText() == 'Userlogin' ) {
911 /** some pages are explicitly allowed */
912 $name = $this->getPrefixedText();
913 if( in_array( $name, $wgWhitelistRead ) ) {
917 # Compatibility with old settings
918 if( $this->getNamespace() == NS_MAIN
) {
919 if( in_array( ':' . $name, $wgWhitelistRead ) ) {
928 * Is this a talk page of some sort?
932 function isTalkPage() {
933 return Namespace::isTalk( $this->getNamespace() );
937 * Is this a .css or .js subpage of a user page?
941 function isCssJsSubpage() {
942 return ( NS_USER
== $this->mNamespace
and preg_match("/\\.(css|js)$/", $this->mTextform
) );
945 * Is this a .css subpage of a user page?
949 function isCssSubpage() {
950 return ( NS_USER
== $this->mNamespace
and preg_match("/\\.css$/", $this->mTextform
) );
953 * Is this a .js subpage of a user page?
957 function isJsSubpage() {
958 return ( NS_USER
== $this->mNamespace
and preg_match("/\\.js$/", $this->mTextform
) );
961 * Protect css/js subpages of user pages: can $wgUser edit
965 * @todo XXX: this might be better using restrictions
968 function userCanEditCssJsSubpage() {
970 return ( $wgUser->isAllowed('editinterface') or preg_match('/^'.preg_quote($wgUser->getName(), '/').'\//', $this->mTextform
) );
974 * Loads a string into mRestrictions array
975 * @param string $res restrictions in string format
978 function loadRestrictions( $res ) {
979 foreach( explode( ':', trim( $res ) ) as $restrict ) {
980 $temp = explode( '=', trim( $restrict ) );
981 if(count($temp) == 1) {
982 // old format should be treated as edit/move restriction
983 $this->mRestrictions
["edit"] = explode( ',', trim( $temp[0] ) );
984 $this->mRestrictions
["move"] = explode( ',', trim( $temp[0] ) );
986 $this->mRestrictions
[$temp[0]] = explode( ',', trim( $temp[1] ) );
989 $this->mRestrictionsLoaded
= true;
993 * Accessor/initialisation for mRestrictions
994 * @param string $action action that permission needs to be checked for
995 * @return array the array of groups allowed to edit this article
998 function getRestrictions($action) {
999 $id = $this->getArticleID();
1000 if ( 0 == $id ) { return array(); }
1002 if ( ! $this->mRestrictionsLoaded
) {
1003 $dbr =& wfGetDB( DB_SLAVE
);
1004 $res = $dbr->selectField( 'page', 'page_restrictions', 'page_id='.$id );
1005 $this->loadRestrictions( $res );
1007 if( isset( $this->mRestrictions
[$action] ) ) {
1008 return $this->mRestrictions
[$action];
1014 * Is there a version of this page in the deletion archive?
1015 * @return int the number of archived revisions
1018 function isDeleted() {
1019 $fname = 'Title::isDeleted';
1020 $dbr =& wfGetDB( DB_SLAVE
);
1021 $n = $dbr->selectField( 'archive', 'COUNT(*)', array( 'ar_namespace' => $this->getNamespace(),
1022 'ar_title' => $this->getDBkey() ), $fname );
1027 * Get the article ID for this Title from the link cache,
1028 * adding it if necessary
1029 * @param int $flags a bit field; may be GAID_FOR_UPDATE to select
1031 * @return int the ID
1034 function getArticleID( $flags = 0 ) {
1035 global $wgLinkCache;
1037 if ( $flags & GAID_FOR_UPDATE
) {
1038 $oldUpdate = $wgLinkCache->forUpdate( true );
1039 $this->mArticleID
= $wgLinkCache->addLinkObj( $this );
1040 $wgLinkCache->forUpdate( $oldUpdate );
1042 if ( -1 == $this->mArticleID
) {
1043 $this->mArticleID
= $wgLinkCache->addLinkObj( $this );
1046 return $this->mArticleID
;
1050 * This clears some fields in this object, and clears any associated
1051 * keys in the "bad links" section of $wgLinkCache.
1053 * - This is called from Article::insertNewArticle() to allow
1054 * loading of the new page_id. It's also called from
1055 * Article::doDeleteArticle()
1057 * @param int $newid the new Article ID
1060 function resetArticleID( $newid ) {
1061 global $wgLinkCache;
1062 $wgLinkCache->clearBadLink( $this->getPrefixedDBkey() );
1064 if ( 0 == $newid ) { $this->mArticleID
= -1; }
1065 else { $this->mArticleID
= $newid; }
1066 $this->mRestrictionsLoaded
= false;
1067 $this->mRestrictions
= array();
1071 * Updates page_touched for this page; called from LinksUpdate.php
1072 * @return bool true if the update succeded
1075 function invalidateCache() {
1076 $now = wfTimestampNow();
1077 $dbw =& wfGetDB( DB_MASTER
);
1078 $success = $dbw->update( 'page',
1080 'page_touched' => $dbw->timestamp()
1081 ), array( /* WHERE */
1082 'page_namespace' => $this->getNamespace() ,
1083 'page_title' => $this->getDBkey()
1084 ), 'Title::invalidateCache'
1090 * Prefix some arbitrary text with the namespace or interwiki prefix
1093 * @param string $name the text
1094 * @return string the prefixed text
1097 /* private */ function prefix( $name ) {
1101 if ( '' != $this->mInterwiki
) {
1102 $p = $this->mInterwiki
. ':';
1104 if ( 0 != $this->mNamespace
) {
1105 $p .= $wgContLang->getNsText( $this->mNamespace
) . ':';
1111 * Secure and split - main initialisation function for this object
1113 * Assumes that mDbkeyform has been set, and is urldecoded
1114 * and uses underscores, but not otherwise munged. This function
1115 * removes illegal characters, splits off the interwiki and
1116 * namespace prefixes, sets the other forms, and canonicalizes
1118 * @return bool true on success
1121 /* private */ function secureAndSplit() {
1122 global $wgContLang, $wgLocalInterwiki, $wgCapitalLinks;
1123 $fname = 'Title::secureAndSplit';
1124 wfProfileIn( $fname );
1127 static $rxTc = false;
1129 # % is needed as well
1130 $rxTc = '/[^' . Title
::legalChars() . ']|%[0-9A-Fa-f]{2}/S';
1133 $this->mInterwiki
= $this->mFragment
= '';
1134 $this->mNamespace
= $this->mDefaultNamespace
; # Usually NS_MAIN
1136 # Clean up whitespace
1138 $t = preg_replace( '/[ _]+/', '_', $this->mDbkeyform
);
1139 $t = trim( $t, '_' );
1142 wfProfileOut( $fname );
1146 if( false !== strpos( $t, UTF8_REPLACEMENT
) ) {
1147 # Contained illegal UTF-8 sequences or forbidden Unicode chars.
1148 wfProfileOut( $fname );
1152 $this->mDbkeyform
= $t;
1154 # Initial colon indicating main namespace
1155 if ( ':' == $t{0} ) {
1156 $r = substr( $t, 1 );
1157 $this->mNamespace
= NS_MAIN
;
1159 # Namespace or interwiki prefix
1162 if ( preg_match( "/^(.+?)_*:_*(.*)$/S", $t, $m ) ) {
1164 $lowerNs = strtolower( $p );
1165 if ( $ns = Namespace::getCanonicalIndex( $lowerNs ) ) {
1166 # Canonical namespace
1168 $this->mNamespace
= $ns;
1169 } elseif ( $ns = $wgContLang->getNsIndex( $lowerNs )) {
1170 # Ordinary namespace
1172 $this->mNamespace
= $ns;
1173 } elseif( $this->getInterwikiLink( $p ) ) {
1175 # Can't make a local interwiki link to an interwiki link.
1176 # That's just crazy!
1177 wfProfileOut( $fname );
1183 $this->mInterwiki
= $p;
1185 # Redundant interwiki prefix to the local wiki
1186 if ( 0 == strcasecmp( $this->mInterwiki
, $wgLocalInterwiki ) ) {
1188 # Can't have an empty self-link
1189 wfProfileOut( $fname );
1192 $this->mInterwiki
= '';
1194 # Do another namespace split...
1198 # If there's no recognized interwiki or namespace,
1199 # then let the colon expression be part of the title.
1206 # We already know that some pages won't be in the database!
1208 if ( '' != $this->mInterwiki ||
-1 == $this->mNamespace
) {
1209 $this->mArticleID
= 0;
1211 $f = strstr( $r, '#' );
1212 if ( false !== $f ) {
1213 $this->mFragment
= substr( $f, 1 );
1214 $r = substr( $r, 0, strlen( $r ) - strlen( $f ) );
1215 # remove whitespace again: prevents "Foo_bar_#"
1216 # becoming "Foo_bar_"
1217 $r = preg_replace( '/_*$/', '', $r );
1220 # Reject illegal characters.
1222 if( preg_match( $rxTc, $r ) ) {
1223 wfProfileOut( $fname );
1228 * Pages with "/./" or "/../" appearing in the URLs will
1229 * often be unreachable due to the way web browsers deal
1230 * with 'relative' URLs. Forbid them explicitly.
1232 if ( strpos( $r, '.' ) !== false &&
1233 ( $r === '.' ||
$r === '..' ||
1234 strpos( $r, './' ) === 0 ||
1235 strpos( $r, '../' ) === 0 ||
1236 strpos( $r, '/./' ) !== false ||
1237 strpos( $r, '/../' ) !== false ) )
1239 wfProfileOut( $fname );
1243 # We shouldn't need to query the DB for the size.
1244 #$maxSize = $dbr->textFieldSize( 'page', 'page_title' );
1245 if ( strlen( $r ) > 255 ) {
1246 wfProfileOut( $fname );
1251 * Normally, all wiki links are forced to have
1252 * an initial capital letter so [[foo]] and [[Foo]]
1253 * point to the same place.
1255 * Don't force it for interwikis, since the other
1256 * site might be case-sensitive.
1258 if( $wgCapitalLinks && $this->mInterwiki
== '') {
1259 $t = $wgContLang->ucfirst( $r );
1265 * Can't make a link to a namespace alone...
1266 * "empty" local links can only be self-links
1267 * with a fragment identifier.
1270 $this->mInterwiki
== '' &&
1271 $this->mNamespace
!= NS_MAIN
) {
1272 wfProfileOut( $fname );
1277 $this->mDbkeyform
= $t;
1278 $this->mUrlform
= wfUrlencode( $t );
1280 $this->mTextform
= str_replace( '_', ' ', $t );
1282 wfProfileOut( $fname );
1287 * Get a Title object associated with the talk page of this article
1288 * @return Title the object for the talk page
1291 function getTalkPage() {
1292 return Title
::makeTitle( Namespace::getTalk( $this->getNamespace() ), $this->getDBkey() );
1296 * Get a title object associated with the subject page of this
1299 * @return Title the object for the subject page
1302 function getSubjectPage() {
1303 return Title
::makeTitle( Namespace::getSubject( $this->getNamespace() ), $this->getDBkey() );
1307 * Get an array of Title objects linking to this Title
1308 * - Also stores the IDs in the link cache.
1310 * @param string $options may be FOR UPDATE
1311 * @return array the Title objects linking here
1314 function getLinksTo( $options = '' ) {
1315 global $wgLinkCache;
1316 $id = $this->getArticleID();
1319 $db =& wfGetDB( DB_MASTER
);
1321 $db =& wfGetDB( DB_SLAVE
);
1323 $page = $db->tableName( 'page' );
1324 $links = $db->tableName( 'links' );
1326 $sql = "SELECT page_namespace,page_title,page_id FROM $page,$links WHERE l_from=page_id AND l_to={$id} $options";
1327 $res = $db->query( $sql, 'Title::getLinksTo' );
1329 if ( $db->numRows( $res ) ) {
1330 while ( $row = $db->fetchObject( $res ) ) {
1331 if ( $titleObj = Title
::makeTitle( $row->page_namespace
, $row->page_title
) ) {
1332 $wgLinkCache->addGoodLink( $row->page_id
, $titleObj->getPrefixedDBkey() );
1333 $retVal[] = $titleObj;
1337 $db->freeResult( $res );
1342 * Get an array of Title objects linking to this non-existent title.
1343 * - Also stores the IDs in the link cache.
1345 * @param string $options may be FOR UPDATE
1346 * @return array the Title objects linking here
1349 function getBrokenLinksTo( $options = '' ) {
1350 global $wgLinkCache;
1353 $db =& wfGetDB( DB_MASTER
);
1355 $db =& wfGetDB( DB_SLAVE
);
1357 $page = $db->tableName( 'page' );
1358 $brokenlinks = $db->tableName( 'brokenlinks' );
1359 $encTitle = $db->strencode( $this->getPrefixedDBkey() );
1361 $sql = "SELECT page_namespace,page_title,page_id FROM $brokenlinks,$page " .
1362 "WHERE bl_from=page_id AND bl_to='$encTitle' $options";
1363 $res = $db->query( $sql, "Title::getBrokenLinksTo" );
1365 if ( $db->numRows( $res ) ) {
1366 while ( $row = $db->fetchObject( $res ) ) {
1367 $titleObj = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
1368 $wgLinkCache->addGoodLink( $row->page_id
, $titleObj->getPrefixedDBkey() );
1369 $retVal[] = $titleObj;
1372 $db->freeResult( $res );
1377 * Get a list of URLs to purge from the Squid cache when this
1380 * @return array the URLs
1383 function getSquidURLs() {
1385 $this->getInternalURL(),
1386 $this->getInternalURL( 'action=history' )
1391 * Move this page without authentication
1392 * @param Title &$nt the new page Title
1395 function moveNoAuth( &$nt ) {
1396 return $this->moveTo( $nt, false );
1400 * Move a title to a new location
1401 * @param Title &$nt the new title
1402 * @param bool $auth indicates whether $wgUser's permissions
1404 * @return mixed true on success, message name on failure
1407 function moveTo( &$nt, $auth = true, $reason = '' ) {
1409 if( !$this or !$nt ) {
1410 return 'badtitletext';
1413 $fname = 'Title::move';
1414 $oldid = $this->getArticleID();
1415 $newid = $nt->getArticleID();
1417 if ( strlen( $nt->getDBkey() ) < 1 ) {
1418 return 'articleexists';
1420 if ( ( ! Namespace::isMovable( $this->getNamespace() ) ) ||
1421 ( '' == $this->getDBkey() ) ||
1422 ( '' != $this->getInterwiki() ) ||
1424 ( ! Namespace::isMovable( $nt->getNamespace() ) ) ||
1425 ( '' == $nt->getDBkey() ) ||
1426 ( '' != $nt->getInterwiki() ) ) {
1427 return 'badarticleerror';
1431 !$this->userCanEdit() ||
!$nt->userCanEdit() ||
1432 !$this->userCanMove() ||
!$nt->userCanMove() ) ) {
1433 return 'protectedpage';
1436 # The move is allowed only if (1) the target doesn't exist, or
1437 # (2) the target is a redirect to the source, and has no history
1438 # (so we can undo bad moves right after they're done).
1440 if ( 0 != $newid ) { # Target exists; check for validity
1441 if ( ! $this->isValidMoveTarget( $nt ) ) {
1442 return 'articleexists';
1444 $this->moveOverExistingRedirect( $nt, $reason );
1445 } else { # Target didn't exist, do normal move.
1446 $this->moveToNewTitle( $nt, $newid, $reason );
1449 # Fixing category links (those without piped 'alternate' names) to be sorted under the new title
1451 $dbw =& wfGetDB( DB_MASTER
);
1452 $categorylinks = $dbw->tableName( 'categorylinks' );
1453 $sql = "UPDATE $categorylinks SET cl_sortkey=" . $dbw->addQuotes( $nt->getPrefixedText() ) .
1454 " WHERE cl_from=" . $dbw->addQuotes( $this->getArticleID() ) .
1455 " AND cl_sortkey=" . $dbw->addQuotes( $this->getPrefixedText() );
1456 $dbw->query( $sql, 'SpecialMovepage::doSubmit' );
1460 $oldnamespace = $this->getNamespace() & ~
1;
1461 $newnamespace = $nt->getNamespace() & ~
1;
1462 $oldtitle = $this->getDBkey();
1463 $newtitle = $nt->getDBkey();
1465 if( $oldnamespace != $newnamespace ||
$oldtitle != $newtitle ) {
1466 WatchedItem
::duplicateEntries( $this, $nt );
1469 # Update search engine
1470 $u = new SearchUpdate( $oldid, $nt->getPrefixedDBkey() );
1472 $u = new SearchUpdate( $newid, $this->getPrefixedDBkey(), '' );
1475 wfRunHooks( 'TitleMoveComplete', array(&$this, &$nt, &$wgUser, $oldid, $newid) );
1480 * Move page to a title which is at present a redirect to the
1483 * @param Title &$nt the page to move to, which should currently
1487 /* private */ function moveOverExistingRedirect( &$nt, $reason = '' ) {
1488 global $wgUser, $wgLinkCache, $wgUseSquid, $wgMwRedir;
1489 $fname = 'Title::moveOverExistingRedirect';
1490 $comment = wfMsgForContent( '1movedto2', $this->getPrefixedText(), $nt->getPrefixedText() );
1493 $comment .= ": $reason";
1496 $now = wfTimestampNow();
1498 $newid = $nt->getArticleID();
1499 $oldid = $this->getArticleID();
1500 $dbw =& wfGetDB( DB_MASTER
);
1501 $links = $dbw->tableName( 'links' );
1503 # Delete the old redirect. We don't save it to history since
1504 # by definition if we've got here it's rather uninteresting.
1505 # We have to remove it so that the next step doesn't trigger
1506 # a conflict on the unique namespace+title index...
1507 $dbw->delete( 'page', array( 'page_id' => $newid ), $fname );
1509 # Save a null revision in the page's history notifying of the move
1510 $nullRevision = Revision
::newNullRevision( $dbw, $oldid,
1511 wfMsg( '1movedto2', $this->getPrefixedText(), $nt->getPrefixedText() ),
1513 $nullRevId = $nullRevision->insertOn( $dbw );
1515 # Change the name of the target page:
1516 $dbw->update( 'page',
1518 'page_touched' => $dbw->timestamp($now),
1519 'page_namespace' => $nt->getNamespace(),
1520 'page_title' => $nt->getDBkey(),
1521 'page_latest' => $nullRevId,
1523 /* WHERE */ array( 'page_id' => $oldid ),
1526 $wgLinkCache->clearLink( $nt->getPrefixedDBkey() );
1528 # Recreate the redirect, this time in the other direction.
1529 $redirectText = $wgMwRedir->getSynonym( 0 ) . ' [[' . $nt->getPrefixedText() . "]]\n";
1530 $redirectArticle = new Article( $this );
1531 $newid = $redirectArticle->insertOn( $dbw );
1532 $redirectRevision = new Revision( array(
1534 'comment' => $comment,
1535 'text' => $redirectText ) );
1536 $revid = $redirectRevision->insertOn( $dbw );
1537 $redirectArticle->updateRevisionOn( $dbw, $redirectRevision, 0 );
1538 $wgLinkCache->clearLink( $this->getPrefixedDBkey() );
1541 $log = new LogPage( 'move' );
1542 $log->addEntry( 'move_redir', $this, $reason, array( 1 => $nt->getPrefixedText() ) );
1546 # Load titles and IDs
1547 $linksToOld = $this->getLinksTo( 'FOR UPDATE' );
1548 $linksToNew = $nt->getLinksTo( 'FOR UPDATE' );
1551 $sql = "DELETE FROM $links WHERE l_to=$oldid OR l_to=$newid";
1552 $dbw->query( $sql, $fname );
1555 if ( count( $linksToOld ) ||
count( $linksToNew )) {
1556 $sql = "INSERT INTO $links (l_from,l_to) VALUES ";
1559 # Insert links to old title
1560 foreach ( $linksToOld as $linkTitle ) {
1566 $id = $linkTitle->getArticleID();
1567 $sql .= "($id,$newid)";
1570 # Insert links to new title
1571 foreach ( $linksToNew as $linkTitle ) {
1577 $id = $linkTitle->getArticleID();
1578 $sql .= "($id, $oldid)";
1581 $dbw->query( $sql, $fname );
1584 # Now, we record the link from the redirect to the new title.
1585 # It should have no other outgoing links...
1586 $dbw->delete( 'links', array( 'l_from' => $newid ) );
1587 $dbw->insert( 'links', array( 'l_from' => $newid, 'l_to' => $oldid ) );
1590 LinkCache
::linksccClearLinksTo( $oldid );
1591 LinkCache
::linksccClearLinksTo( $newid );
1594 if ( $wgUseSquid ) {
1595 $urls = array_merge( $nt->getSquidURLs(), $this->getSquidURLs() );
1596 $u = new SquidUpdate( $urls );
1602 * Move page to non-existing title.
1603 * @param Title &$nt the new Title
1604 * @param int &$newid set to be the new article ID
1607 /* private */ function moveToNewTitle( &$nt, &$newid, $reason = '' ) {
1608 global $wgUser, $wgLinkCache, $wgUseSquid;
1610 $fname = 'MovePageForm::moveToNewTitle';
1611 $comment = wfMsgForContent( '1movedto2', $this->getPrefixedText(), $nt->getPrefixedText() );
1613 $comment .= ": $reason";
1616 $newid = $nt->getArticleID();
1617 $oldid = $this->getArticleID();
1618 $dbw =& wfGetDB( DB_MASTER
);
1619 $now = $dbw->timestamp();
1623 # Save a null revision in the page's history notifying of the move
1624 $nullRevision = Revision
::newNullRevision( $dbw, $oldid,
1625 wfMsg( '1movedto2', $this->getPrefixedText(), $nt->getPrefixedText() ),
1627 $nullRevId = $nullRevision->insertOn( $dbw );
1630 $dbw->update( 'page',
1632 'page_touched' => $now,
1633 'page_namespace' => $nt->getNamespace(),
1634 'page_title' => $nt->getDBkey(),
1635 'page_latest' => $nullRevId,
1637 /* WHERE */ array( 'page_id' => $oldid ),
1641 $wgLinkCache->clearLink( $nt->getPrefixedDBkey() );
1644 $redirectText = $wgMwRedir->getSynonym( 0 ) . ' [[' . $nt->getPrefixedText() . "]]\n";
1645 $redirectArticle = new Article( $this );
1646 $newid = $redirectArticle->insertOn( $dbw );
1647 $redirectRevision = new Revision( array(
1649 'comment' => $comment,
1650 'text' => $redirectText ) );
1651 $revid = $redirectRevision->insertOn( $dbw );
1652 $redirectArticle->updateRevisionOn( $dbw, $redirectRevision, 0 );
1653 $wgLinkCache->clearLink( $this->getPrefixedDBkey() );
1656 $log = new LogPage( 'move' );
1657 $log->addEntry( 'move', $this, $reason, array( 1 => $nt->getPrefixedText()) );
1659 # Purge squid and linkscc as per article creation
1660 Article
::onArticleCreate( $nt );
1662 # Any text links to the old title must be reassigned to the redirect
1663 $dbw->update( 'links', array( 'l_to' => $newid ), array( 'l_to' => $oldid ), $fname );
1664 LinkCache
::linksccClearLinksTo( $oldid );
1666 # Record the just-created redirect's linking to the page
1667 $dbw->insert( 'links', array( 'l_from' => $newid, 'l_to' => $oldid ), $fname );
1669 # Non-existent target may have had broken links to it; these must
1670 # now be removed and made into good links.
1671 $update = new LinksUpdate( $oldid, $nt->getPrefixedDBkey() );
1672 $update->fixBrokenLinks();
1674 # Purge old title from squid
1675 # The new title, and links to the new title, are purged in Article::onArticleCreate()
1676 $titles = $nt->getLinksTo();
1677 if ( $wgUseSquid ) {
1678 $urls = $this->getSquidURLs();
1679 foreach ( $titles as $linkTitle ) {
1680 $urls[] = $linkTitle->getInternalURL();
1682 $u = new SquidUpdate( $urls );
1688 * Checks if $this can be moved to a given Title
1689 * - Selects for update, so don't call it unless you mean business
1691 * @param Title &$nt the new title to check
1694 function isValidMoveTarget( $nt ) {
1696 $fname = 'Title::isValidMoveTarget';
1697 $dbw =& wfGetDB( DB_MASTER
);
1700 $id = $nt->getArticleID();
1701 $obj = $dbw->selectRow( array( 'page', 'revision', 'text'),
1702 array( 'page_is_redirect','old_text' ),
1703 array( 'page_id' => $id, 'page_latest=rev_id', 'rev_text_id=old_id' ),
1704 $fname, 'FOR UPDATE' );
1706 if ( !$obj ||
0 == $obj->page_is_redirect
) {
1711 # Does the redirect point to the source?
1712 if ( preg_match( "/\\[\\[\\s*([^\\]\\|]*)]]/", $obj->old_text
, $m ) ) {
1713 $redirTitle = Title
::newFromText( $m[1] );
1714 if( !is_object( $redirTitle ) ||
1715 $redirTitle->getPrefixedDBkey() != $this->getPrefixedDBkey() ) {
1720 # Does the article have a history?
1721 $row = $dbw->selectRow( array( 'page', 'revision'),
1723 array( 'page_namespace' => $nt->getNamespace(),
1724 'page_title' => $nt->getDBkey(),
1725 'page_id=rev_page AND page_latest != rev_id'
1726 ), $fname, 'FOR UPDATE'
1729 # Return true if there was no history
1730 return $row === false;
1734 * Create a redirect; fails if the title already exists; does
1737 * @param Title $dest the destination of the redirect
1738 * @param string $comment the comment string describing the move
1739 * @return bool true on success
1742 function createRedirect( $dest, $comment ) {
1744 if ( $this->getArticleID() ) {
1748 $fname = 'Title::createRedirect';
1749 $dbw =& wfGetDB( DB_MASTER
);
1751 $article = new Article( $this );
1752 $newid = $article->insertOn( $dbw );
1753 $revision = new Revision( array(
1755 'comment' => $comment,
1756 'text' => "#REDIRECT [[" . $dest->getPrefixedText() . "]]\n",
1758 $revisionId = $revision->insertOn( $dbw );
1759 $article->updateRevisionOn( $dbw, $revision, 0 );
1762 if ( $dest->getArticleID() ) {
1763 $dbw->insert( 'links',
1765 'l_to' => $dest->getArticleID(),
1770 $dbw->insert( 'brokenlinks',
1772 'bl_to' => $dest->getPrefixedDBkey(),
1778 Article
::onArticleCreate( $this );
1783 * Get categories to which this Title belongs and return an array of
1784 * categories' names.
1786 * @return array an array of parents in the form:
1787 * $parent => $currentarticle
1790 function getParentCategories() {
1791 global $wgContLang,$wgUser;
1793 $titlekey = $this->getArticleId();
1794 $sk =& $wgUser->getSkin();
1796 $dbr =& wfGetDB( DB_SLAVE
);
1797 $categorylinks = $dbr->tableName( 'categorylinks' );
1800 $sql = "SELECT * FROM $categorylinks"
1801 ." WHERE cl_from='$titlekey'"
1802 ." AND cl_from <> '0'"
1803 ." ORDER BY cl_sortkey";
1805 $res = $dbr->query ( $sql ) ;
1807 if($dbr->numRows($res) > 0) {
1808 while ( $x = $dbr->fetchObject ( $res ) )
1809 //$data[] = Title::newFromText($wgContLang->getNSText ( NS_CATEGORY ).':'.$x->cl_to);
1810 $data[$wgContLang->getNSText ( NS_CATEGORY
).':'.$x->cl_to
] = $this->getFullText();
1811 $dbr->freeResult ( $res ) ;
1819 * Get a tree of parent categories
1820 * @param array $children an array with the children in the keys, to check for circular refs
1824 function getParentCategoryTree( $children = array() ) {
1825 $parents = $this->getParentCategories();
1827 if($parents != '') {
1828 foreach($parents as $parent => $current)
1830 if ( array_key_exists( $parent, $children ) ) {
1831 # Circular reference
1832 $stack[$parent] = array();
1834 $nt = Title
::newFromText($parent);
1835 $stack[$parent] = $nt->getParentCategoryTree( $children +
array($parent => 1) );
1846 * Get an associative array for selecting this title from
1852 function curCond() {
1853 wfDebugDieBacktrace( 'curCond called' );
1854 return array( 'cur_namespace' => $this->mNamespace
, 'cur_title' => $this->mDbkeyform
);
1858 * Get an associative array for selecting this title from the
1864 function oldCond() {
1865 wfDebugDieBacktrace( 'oldCond called' );
1866 return array( 'old_namespace' => $this->mNamespace
, 'old_title' => $this->mDbkeyform
);
1870 * Get the revision ID of the previous revision
1872 * @param integer $revision Revision ID. Get the revision that was before this one.
1873 * @return interger $oldrevision|false
1875 function getPreviousRevisionID( $revision ) {
1876 $dbr =& wfGetDB( DB_SLAVE
);
1877 return $dbr->selectField( 'revision', 'rev_id',
1878 'rev_page=' . IntVal( $this->getArticleId() ) .
1879 ' AND rev_id<' . IntVal( $revision ) . ' ORDER BY rev_id DESC' );
1883 * Get the revision ID of the next revision
1885 * @param integer $revision Revision ID. Get the revision that was after this one.
1886 * @return interger $oldrevision|false
1888 function getNextRevisionID( $revision ) {
1889 $dbr =& wfGetDB( DB_SLAVE
);
1890 return $dbr->selectField( 'revision', 'rev_id',
1891 'rev_page=' . IntVal( $this->getArticleId() ) .
1892 ' AND rev_id>' . IntVal( $revision ) . ' ORDER BY rev_id' );
1896 * Compare with another title.
1898 * @param Title $title
1901 function equals( &$title ) {
1902 return $this->getInterwiki() == $title->getInterwiki()
1903 && $this->getNamespace() == $title->getNamespace()
1904 && $this->getDbkey() == $title->getDbkey();