Reject titles with %XX hex codes (since these have special meaning in URL links and...
[mediawiki.git] / includes / Title.php
blob3924085c5b8fb1fbbd60f44bd0a36bb8065813a9
1 <?php
2 /**
3 * $Id$
4 * See title.doc
5 *
6 * @package MediaWiki
7 */
9 /** */
10 require_once( 'normal/UtfNormal.php' );
12 $wgTitleInterwikiCache = array();
13 define ( 'GAID_FOR_UPDATE', 1 );
15 /**
16 * Title class
17 * - Represents a title, which may contain an interwiki designation or namespace
18 * - Can fetch various kinds of data from the database, albeit inefficiently.
20 * @package MediaWiki
22 class Title {
23 /**
24 * All member variables should be considered private
25 * Please use the accessor functions
28 /**#@+
29 * @access private
32 var $mTextform; # Text form (spaces not underscores) of the main part
33 var $mUrlform; # URL-encoded form of the main part
34 var $mDbkeyform; # Main part with underscores
35 var $mNamespace; # Namespace index, i.e. one of the NS_xxxx constants
36 var $mInterwiki; # Interwiki prefix (or null string)
37 var $mFragment; # Title fragment (i.e. the bit after the #)
38 var $mArticleID; # Article ID, fetched from the link cache on demand
39 var $mRestrictions; # Array of groups allowed to edit this article
40 # Only null or "sysop" are supported
41 var $mRestrictionsLoaded; # Boolean for initialisation on demand
42 var $mPrefixedText; # Text form including namespace/interwiki, initialised on demand
43 var $mDefaultNamespace; # Namespace index when there is no namespace
44 # Zero except in {{transclusion}} tags
45 /**#@-*/
48 /**
49 * Constructor
50 * @access private
52 /* private */ function Title() {
53 $this->mInterwiki = $this->mUrlform =
54 $this->mTextform = $this->mDbkeyform = '';
55 $this->mArticleID = -1;
56 $this->mNamespace = 0;
57 $this->mRestrictionsLoaded = false;
58 $this->mRestrictions = array();
59 $this->mDefaultNamespace = 0;
62 /**
63 * Create a new Title from a prefixed DB key
64 * @param string $key The database key, which has underscores
65 * instead of spaces, possibly including namespace and
66 * interwiki prefixes
67 * @return Title the new object, or NULL on an error
68 * @static
69 * @access public
71 /* static */ function newFromDBkey( $key ) {
72 $t = new Title();
73 $t->mDbkeyform = $key;
74 if( $t->secureAndSplit() )
75 return $t;
76 else
77 return NULL;
80 /**
81 * Create a new Title from text, such as what one would
82 * find in a link. Decodes any HTML entities in the text.
84 * @param string $text the link text; spaces, prefixes,
85 * and an initial ':' indicating the main namespace
86 * are accepted
87 * @param int $defaultNamespace the namespace to use if
88 * none is specified by a prefix
89 * @return Title the new object, or NULL on an error
90 * @static
91 * @access public
93 /* static */ function newFromText( $text, $defaultNamespace = 0 ) {
94 $fname = 'Title::newFromText';
95 wfProfileIn( $fname );
97 if( is_object( $text ) ) {
98 wfDebugDieBacktrace( 'Called with object instead of string.' );
100 global $wgInputEncoding;
101 $text = do_html_entity_decode( $text, ENT_COMPAT, $wgInputEncoding );
103 $text = wfMungeToUtf8( $text );
106 # What was this for? TS 2004-03-03
107 # $text = urldecode( $text );
109 $t = new Title();
110 $t->mDbkeyform = str_replace( ' ', '_', $text );
111 $t->mDefaultNamespace = $defaultNamespace;
113 wfProfileOut( $fname );
114 if ( !is_object( $t ) ) {
115 var_dump( debug_backtrace() );
117 if( $t->secureAndSplit() ) {
118 return $t;
119 } else {
120 return NULL;
125 * Create a new Title from URL-encoded text. Ensures that
126 * the given title's length does not exceed the maximum.
127 * @param string $url the title, as might be taken from a URL
128 * @return Title the new object, or NULL on an error
129 * @static
130 * @access public
132 /* static */ function newFromURL( $url ) {
133 global $wgLang, $wgServer;
134 $t = new Title();
136 # For compatibility with old buggy URLs. "+" is not valid in titles,
137 # but some URLs used it as a space replacement and they still come
138 # from some external search tools.
139 $s = str_replace( '+', ' ', $url );
141 $t->mDbkeyform = str_replace( ' ', '_', $s );
142 if( $t->secureAndSplit() ) {
143 return $t;
144 } else {
145 return NULL;
150 * Create a new Title from an article ID
151 * @todo This is inefficiently implemented, the cur row is requested
152 * but not used for anything else
153 * @param int $id the cur_id corresponding to the Title to create
154 * @return Title the new object, or NULL on an error
155 * @access public
157 /* static */ function newFromID( $id ) {
158 $fname = 'Title::newFromID';
159 $dbr =& wfGetDB( DB_SLAVE );
160 $row = $dbr->getArray( 'cur', array( 'cur_namespace', 'cur_title' ),
161 array( 'cur_id' => $id ), $fname );
162 if ( $row !== false ) {
163 $title = Title::makeTitle( $row->cur_namespace, $row->cur_title );
164 } else {
165 $title = NULL;
167 return $title;
171 * Create a new Title from a namespace index and a DB key.
172 * It's assumed that $ns and $title are *valid*, for instance when
173 * they came directly from the database or a special page name.
174 * @param int $ns the namespace of the article
175 * @param string $title the unprefixed database key form
176 * @return Title the new object
177 * @static
178 * @access public
180 /* static */ function &makeTitle( $ns, $title ) {
181 $t =& new Title();
182 $t->mInterwiki = '';
183 $t->mFragment = '';
184 $t->mNamespace = IntVal( $ns );
185 $t->mDbkeyform = $title;
186 $t->mArticleID = ( $ns >= 0 ) ? -1 : 0;
187 $t->mUrlform = wfUrlencode( $title );
188 $t->mTextform = str_replace( '_', ' ', $title );
189 return $t;
193 * Create a new Title frrom a namespace index and a DB key.
194 * The parameters will be checked for validity, which is a bit slower
195 * than makeTitle() but safer for user-provided data.
196 * @param int $ns the namespace of the article
197 * @param string $title the database key form
198 * @return Title the new object, or NULL on an error
199 * @static
200 * @access public
202 /* static */ function makeTitleSafe( $ns, $title ) {
203 $t = new Title();
204 $t->mDbkeyform = Title::makeName( $ns, $title );
205 if( $t->secureAndSplit() ) {
206 return $t;
207 } else {
208 return NULL;
213 * Create a new Title for the Main Page
214 * @static
215 * @return Title the new object
216 * @access public
218 /* static */ function newMainPage() {
219 return Title::newFromText( wfMsgForContent( 'mainpage' ) );
223 * Create a new Title for a redirect
224 * @param string $text the redirect title text
225 * @return Title the new object, or NULL if the text is not a
226 * valid redirect
227 * @static
228 * @access public
230 /* static */ function newFromRedirect( $text ) {
231 global $wgMwRedir;
232 $rt = NULL;
233 if ( $wgMwRedir->matchStart( $text ) ) {
234 if ( preg_match( '/\\[\\[([^\\]\\|]+)[\\]\\|]/', $text, $m ) ) {
235 # categories are escaped using : for example one can enter:
236 # #REDIRECT [[:Category:Music]]. Need to remove it.
237 if ( substr($m[1],0,1) == ':') {
238 # We don't want to keep the ':'
239 $m[1] = substr( $m[1], 1 );
242 $rt = Title::newFromText( $m[1] );
243 # Disallow redirects to Special:Userlogout
244 if ( !is_null($rt) && $rt->getNamespace() == NS_SPECIAL && preg_match( '/^Userlogout/i', $rt->getText() ) ) {
245 $rt = NULL;
249 return $rt;
252 #----------------------------------------------------------------------------
253 # Static functions
254 #----------------------------------------------------------------------------
257 * Get the prefixed DB key associated with an ID
258 * @param int $id the cur_id of the article
259 * @return Title an object representing the article, or NULL
260 * if no such article was found
261 * @static
262 * @access public
264 /* static */ function nameOf( $id ) {
265 $fname = 'Title::nameOf';
266 $dbr =& wfGetDB( DB_SLAVE );
268 $s = $dbr->getArray( 'cur', array( 'cur_namespace','cur_title' ), array( 'cur_id' => $id ), $fname );
269 if ( $s === false ) { return NULL; }
271 $n = Title::makeName( $s->cur_namespace, $s->cur_title );
272 return $n;
276 * Get a regex character class describing the legal characters in a link
277 * @return string the list of characters, not delimited
278 * @static
279 * @access public
281 /* static */ function legalChars() {
282 # Missing characters:
283 # * []|# Needed for link syntax
284 # * % and + are corrupted by Apache when they appear in the path
286 # % seems to work though
288 # The problem with % is that URLs are double-unescaped: once by Apache's
289 # path conversion code, and again by PHP. So %253F, for example, becomes "?".
290 # Our code does not double-escape to compensate for this, indeed double escaping
291 # would break if the double-escaped title was passed in the query string
292 # rather than the path. This is a minor security issue because articles can be
293 # created such that they are hard to view or edit. -- TS
295 # Theoretically 0x80-0x9F of ISO 8859-1 should be disallowed, but
296 # this breaks interlanguage links
298 $set = " %!\"$&'()*,\\-.\\/0-9:;=?@A-Z\\\\^_`a-z~\\x80-\\xFF";
299 return $set;
303 * Get a string representation of a title suitable for
304 * including in a search index
306 * @param int $ns a namespace index
307 * @param string $title text-form main part
308 * @return string a stripped-down title string ready for the
309 * search index
311 /* static */ function indexTitle( $ns, $title ) {
312 global $wgDBminWordLen, $wgContLang;
313 require_once( 'SearchEngine.php' );
315 $lc = SearchEngine::legalSearchChars() . '&#;';
316 $t = $wgContLang->stripForSearch( $title );
317 $t = preg_replace( "/[^{$lc}]+/", ' ', $t );
318 $t = strtolower( $t );
320 # Handle 's, s'
321 $t = preg_replace( "/([{$lc}]+)'s( |$)/", "\\1 \\1's ", $t );
322 $t = preg_replace( "/([{$lc}]+)s'( |$)/", "\\1s ", $t );
324 $t = preg_replace( "/\\s+/", ' ', $t );
326 if ( $ns == Namespace::getImage() ) {
327 $t = preg_replace( "/ (png|gif|jpg|jpeg|ogg)$/", "", $t );
329 return trim( $t );
333 * Make a prefixed DB key from a DB key and a namespace index
334 * @param int $ns numerical representation of the namespace
335 * @param string $title the DB key form the title
336 * @return string the prefixed form of the title
338 /* static */ function makeName( $ns, $title ) {
339 global $wgContLang;
341 $n = $wgContLang->getNsText( $ns );
342 if ( '' == $n ) { return $title; }
343 else { return $n.':'.$title; }
347 * Returns the URL associated with an interwiki prefix
348 * @param string $key the interwiki prefix (e.g. "MeatBall")
349 * @return the associated URL, containing "$1", which should be
350 * replaced by an article title
351 * @static (arguably)
352 * @access public
354 function getInterwikiLink( $key ) {
355 global $wgMemc, $wgDBname, $wgInterwikiExpiry, $wgTitleInterwikiCache;
356 $fname = 'Title::getInterwikiLink';
357 $k = $wgDBname.':interwiki:'.$key;
359 if( array_key_exists( $k, $wgTitleInterwikiCache ) )
360 return $wgTitleInterwikiCache[$k]->iw_url;
362 $s = $wgMemc->get( $k );
363 # Ignore old keys with no iw_local
364 if( $s && isset( $s->iw_local ) ) {
365 $wgTitleInterwikiCache[$k] = $s;
366 return $s->iw_url;
368 $dbr =& wfGetDB( DB_SLAVE );
369 $res = $dbr->select( 'interwiki', array( 'iw_url', 'iw_local' ), array( 'iw_prefix' => $key ), $fname );
370 if(!$res) return '';
372 $s = $dbr->fetchObject( $res );
373 if(!$s) {
374 # Cache non-existence: create a blank object and save it to memcached
375 $s = (object)false;
376 $s->iw_url = '';
377 $s->iw_local = 0;
379 $wgMemc->set( $k, $s, $wgInterwikiExpiry );
380 $wgTitleInterwikiCache[$k] = $s;
381 return $s->iw_url;
385 * Determine whether the object refers to a page within
386 * this project.
388 * @return bool TRUE if this is an in-project interwiki link
389 * or a wikilink, FALSE otherwise
390 * @access public
392 function isLocal() {
393 global $wgTitleInterwikiCache, $wgDBname;
395 if ( $this->mInterwiki != '' ) {
396 # Make sure key is loaded into cache
397 $this->getInterwikiLink( $this->mInterwiki );
398 $k = $wgDBname.':interwiki:' . $this->mInterwiki;
399 return (bool)($wgTitleInterwikiCache[$k]->iw_local);
400 } else {
401 return true;
406 * Update the cur_touched field for an array of title objects
407 * @todo Inefficient unless the IDs are already loaded into the
408 * link cache
409 * @param array $titles an array of Title objects to be touched
410 * @param string $timestamp the timestamp to use instead of the
411 * default current time
412 * @static
413 * @access public
415 /* static */ function touchArray( $titles, $timestamp = '' ) {
416 if ( count( $titles ) == 0 ) {
417 return;
419 $dbw =& wfGetDB( DB_MASTER );
420 if ( $timestamp == '' ) {
421 $timestamp = $dbw->timestamp();
423 $cur = $dbw->tableName( 'cur' );
424 $sql = "UPDATE $cur SET cur_touched='{$timestamp}' WHERE cur_id IN (";
425 $first = true;
427 foreach ( $titles as $title ) {
428 if ( ! $first ) {
429 $sql .= ',';
431 $first = false;
432 $sql .= $title->getArticleID();
434 $sql .= ')';
435 if ( ! $first ) {
436 $dbw->query( $sql, 'Title::touchArray' );
440 #----------------------------------------------------------------------------
441 # Other stuff
442 #----------------------------------------------------------------------------
444 /** Simple accessors */
446 * Get the text form (spaces not underscores) of the main part
447 * @return string
448 * @access public
450 function getText() { return $this->mTextform; }
452 * Get the URL-encoded form of the main part
453 * @return string
454 * @access public
456 function getPartialURL() { return $this->mUrlform; }
458 * Get the main part with underscores
459 * @return string
460 * @access public
462 function getDBkey() { return $this->mDbkeyform; }
464 * Get the namespace index, i.e. one of the NS_xxxx constants
465 * @return int
466 * @access public
468 function getNamespace() { return $this->mNamespace; }
470 * Set the namespace index
471 * @param int $n the namespace index, one of the NS_xxxx constants
472 * @access public
474 function setNamespace( $n ) { $this->mNamespace = IntVal( $n ); }
476 * Get the interwiki prefix (or null string)
477 * @return string
478 * @access public
480 function getInterwiki() { return $this->mInterwiki; }
482 * Get the Title fragment (i.e. the bit after the #)
483 * @return string
484 * @access public
486 function getFragment() { return $this->mFragment; }
488 * Get the default namespace index, for when there is no namespace
489 * @return int
490 * @access public
492 function getDefaultNamespace() { return $this->mDefaultNamespace; }
495 * Get title for search index
496 * @return string a stripped-down title string ready for the
497 * search index
499 function getIndexTitle() {
500 return Title::indexTitle( $this->mNamespace, $this->mTextform );
504 * Get the prefixed database key form
505 * @return string the prefixed title, with underscores and
506 * any interwiki and namespace prefixes
507 * @access public
509 function getPrefixedDBkey() {
510 $s = $this->prefix( $this->mDbkeyform );
511 $s = str_replace( ' ', '_', $s );
512 return $s;
516 * Get the prefixed title with spaces.
517 * This is the form usually used for display
518 * @return string the prefixed title, with spaces
519 * @access public
521 function getPrefixedText() {
522 global $wgContLang;
523 if ( empty( $this->mPrefixedText ) ) {
524 $s = $this->prefix( $this->mTextform );
525 $s = str_replace( '_', ' ', $s );
526 $this->mPrefixedText = $s;
528 return $this->mPrefixedText;
532 * Get the prefixed title with spaces, plus any fragment
533 * (part beginning with '#')
534 * @return string the prefixed title, with spaces and
535 * the fragment, including '#'
536 * @access public
538 function getFullText() {
539 global $wgContLang;
540 $text = $this->getPrefixedText();
541 if( '' != $this->mFragment ) {
542 $text .= '#' . $this->mFragment;
544 return $text;
548 * Get a URL-encoded title (not an actual URL) including interwiki
549 * @return string the URL-encoded form
550 * @access public
552 function getPrefixedURL() {
553 $s = $this->prefix( $this->mDbkeyform );
554 $s = str_replace( ' ', '_', $s );
556 $s = wfUrlencode ( $s ) ;
558 # Cleaning up URL to make it look nice -- is this safe?
559 $s = preg_replace( '/%3[Aa]/', ':', $s );
560 $s = preg_replace( '/%2[Ff]/', '/', $s );
561 $s = str_replace( '%28', '(', $s );
562 $s = str_replace( '%29', ')', $s );
564 return $s;
568 * Get a real URL referring to this title, with interwiki link and
569 * fragment
571 * @param string $query an optional query string, not used
572 * for interwiki links
573 * @return string the URL
574 * @access public
576 function getFullURL( $query = '' ) {
577 global $wgContLang, $wgArticlePath, $wgServer, $wgScript;
579 if ( '' == $this->mInterwiki ) {
580 $p = $wgArticlePath;
581 return $wgServer . $this->getLocalUrl( $query );
582 } else {
583 $baseUrl = $this->getInterwikiLink( $this->mInterwiki );
584 $namespace = $wgContLang->getNsText( $this->mNamespace );
585 if ( '' != $namespace ) {
586 # Can this actually happen? Interwikis shouldn't be parsed.
587 $namepace .= ':';
589 $url = str_replace( '$1', $namespace . $this->mUrlform, $baseUrl );
590 if ( '' != $this->mFragment ) {
591 $url .= '#' . $this->mFragment;
593 return $url;
598 * @deprecated
600 function getURL() {
601 die( 'Call to obsolete obsolete function Title::getURL()' );
605 * Get a URL with no fragment or server name
606 * @param string $query an optional query string; if not specified,
607 * $wgArticlePath will be used.
608 * @return string the URL
609 * @access public
611 function getLocalURL( $query = '' ) {
612 global $wgLang, $wgArticlePath, $wgScript;
614 if ( $this->isExternal() ) {
615 return $this->getFullURL();
618 $dbkey = wfUrlencode( $this->getPrefixedDBkey() );
619 if ( $query == '' ) {
620 $url = str_replace( '$1', $dbkey, $wgArticlePath );
621 } else {
622 if ( $query == '-' ) {
623 $query = '';
625 if ( $wgScript != '' ) {
626 $url = "{$wgScript}?title={$dbkey}&{$query}";
627 } else {
628 # Top level wiki
629 $url = "/{$dbkey}?{$query}";
632 return $url;
636 * Get an HTML-escaped version of the URL form, suitable for
637 * using in a link, without a server name or fragment
638 * @param string $query an optional query string
639 * @return string the URL
640 * @access public
642 function escapeLocalURL( $query = '' ) {
643 return htmlspecialchars( $this->getLocalURL( $query ) );
647 * Get an HTML-escaped version of the URL form, suitable for
648 * using in a link, including the server name and fragment
650 * @return string the URL
651 * @param string $query an optional query string
652 * @access public
654 function escapeFullURL( $query = '' ) {
655 return htmlspecialchars( $this->getFullURL( $query ) );
658 /**
659 * Get the URL form for an internal link.
660 * - Used in various Squid-related code, in case we have a different
661 * internal hostname for the server from the exposed one.
663 * @param string $query an optional query string
664 * @return string the URL
665 * @access public
667 function getInternalURL( $query = '' ) {
668 global $wgInternalServer;
669 return $wgInternalServer . $this->getLocalURL( $query );
673 * Get the edit URL for this Title
674 * @return string the URL, or a null string if this is an
675 * interwiki link
676 * @access public
678 function getEditURL() {
679 global $wgServer, $wgScript;
681 if ( '' != $this->mInterwiki ) { return ''; }
682 $s = $this->getLocalURL( 'action=edit' );
684 return $s;
688 * Get the HTML-escaped displayable text form.
689 * Used for the title field in <a> tags.
690 * @return string the text, including any prefixes
691 * @access public
693 function getEscapedText() {
694 return htmlspecialchars( $this->getPrefixedText() );
698 * Is this Title interwiki?
699 * @return boolean
700 * @access public
702 function isExternal() { return ( '' != $this->mInterwiki ); }
705 * Does the title correspond to a protected article?
706 * @return boolean
707 * @access public
709 function isProtected() {
710 if ( -1 == $this->mNamespace ) { return true; }
711 $a = $this->getRestrictions();
712 if ( in_array( 'sysop', $a ) ) { return true; }
713 return false;
717 * Is the page a log page, i.e. one where the history is messed up by
718 * LogPage.php? This used to be used for suppressing diff links in
719 * recent changes, but now that's done by setting a flag in the
720 * recentchanges table. Hence, this probably is no longer used.
722 * @deprecated
723 * @access public
725 function isLog() {
726 if ( $this->mNamespace != Namespace::getWikipedia() ) {
727 return false;
729 if ( ( 0 == strcmp( wfMsg( 'uploadlogpage' ), $this->mDbkeyform ) ) ||
730 ( 0 == strcmp( wfMsg( 'dellogpage' ), $this->mDbkeyform ) ) ) {
731 return true;
733 return false;
737 * Is $wgUser is watching this page?
738 * @return boolean
739 * @access public
741 function userIsWatching() {
742 global $wgUser;
744 if ( -1 == $this->mNamespace ) { return false; }
745 if ( 0 == $wgUser->getID() ) { return false; }
747 return $wgUser->isWatched( $this );
751 * Can $wgUser edit this page?
752 * @return boolean
753 * @access public
755 function userCanEdit() {
756 global $wgUser;
757 if ( -1 == $this->mNamespace ) { return false; }
758 if ( NS_MEDIAWIKI == $this->mNamespace && !$wgUser->isSysop() ) { return false; }
759 # if ( 0 == $this->getArticleID() ) { return false; }
760 if ( $this->mDbkeyform == '_' ) { return false; }
761 # protect global styles and js
762 if ( NS_MEDIAWIKI == $this->mNamespace
763 && preg_match("/\\.(css|js)$/", $this->mTextform )
764 && !$wgUser->isSysop() )
765 { return false; }
766 //if ( $this->isCssJsSubpage() and !$this->userCanEditCssJsSubpage() ) { return false; }
767 # protect css/js subpages of user pages
768 # XXX: this might be better using restrictions
769 # XXX: Find a way to work around the php bug that prevents using $this->userCanEditCssJsSubpage() from working
770 if( Namespace::getUser() == $this->mNamespace
771 and preg_match("/\\.(css|js)$/", $this->mTextform )
772 and !$wgUser->isSysop()
773 and !preg_match('/^'.preg_quote($wgUser->getName(), '/').'/', $this->mTextform) )
774 { return false; }
775 $ur = $wgUser->getRights();
776 foreach ( $this->getRestrictions() as $r ) {
777 if ( '' != $r && ( ! in_array( $r, $ur ) ) ) {
778 return false;
781 return true;
785 * Can $wgUser read this page?
786 * @return boolean
787 * @access public
789 function userCanRead() {
790 global $wgUser;
791 global $wgWhitelistRead;
793 if( 0 != $wgUser->getID() ) return true;
794 if( !is_array( $wgWhitelistRead ) ) return true;
796 $name = $this->getPrefixedText();
797 if( in_array( $name, $wgWhitelistRead ) ) return true;
799 # Compatibility with old settings
800 if( $this->getNamespace() == NS_MAIN ) {
801 if( in_array( ':' . $name, $wgWhitelistRead ) ) return true;
803 return false;
807 * Is this a .css or .js subpage of a user page?
808 * @return bool
809 * @access public
811 function isCssJsSubpage() {
812 return ( Namespace::getUser() == $this->mNamespace and preg_match("/\\.(css|js)$/", $this->mTextform ) );
815 * Is this a .css subpage of a user page?
816 * @return bool
817 * @access public
819 function isCssSubpage() {
820 return ( Namespace::getUser() == $this->mNamespace and preg_match("/\\.css$/", $this->mTextform ) );
823 * Is this a .js subpage of a user page?
824 * @return bool
825 * @access public
827 function isJsSubpage() {
828 return ( Namespace::getUser() == $this->mNamespace and preg_match("/\\.js$/", $this->mTextform ) );
831 * Protect css/js subpages of user pages: can $wgUser edit
832 * this page?
834 * @return boolean
835 * @todo XXX: this might be better using restrictions
836 * @access public
838 function userCanEditCssJsSubpage() {
839 global $wgUser;
840 return ( $wgUser->isSysop() or preg_match('/^'.preg_quote($wgUser->getName()).'/', $this->mTextform) );
844 * Accessor/initialisation for mRestrictions
845 * @return array the array of groups allowed to edit this article
846 * @access public
848 function getRestrictions() {
849 $id = $this->getArticleID();
850 if ( 0 == $id ) { return array(); }
852 if ( ! $this->mRestrictionsLoaded ) {
853 $dbr =& wfGetDB( DB_SLAVE );
854 $res = $dbr->getField( 'cur', 'cur_restrictions', 'cur_id='.$id );
855 $this->mRestrictions = explode( ',', trim( $res ) );
856 $this->mRestrictionsLoaded = true;
858 return $this->mRestrictions;
862 * Is there a version of this page in the deletion archive?
863 * @return int the number of archived revisions
864 * @access public
866 function isDeleted() {
867 $fname = 'Title::isDeleted';
868 $dbr =& wfGetDB( DB_SLAVE );
869 $n = $dbr->getField( 'archive', 'COUNT(*)', array( 'ar_namespace' => $this->getNamespace(),
870 'ar_title' => $this->getDBkey() ), $fname );
871 return (int)$n;
875 * Get the article ID for this Title from the link cache,
876 * adding it if necessary
877 * @param int $flags a bit field; may be GAID_FOR_UPDATE to select
878 * for update
879 * @return int the ID
880 * @access public
882 function getArticleID( $flags = 0 ) {
883 global $wgLinkCache;
885 if ( $flags & GAID_FOR_UPDATE ) {
886 $oldUpdate = $wgLinkCache->forUpdate( true );
887 $this->mArticleID = $wgLinkCache->addLinkObj( $this );
888 $wgLinkCache->forUpdate( $oldUpdate );
889 } else {
890 if ( -1 == $this->mArticleID ) {
891 $this->mArticleID = $wgLinkCache->addLinkObj( $this );
894 return $this->mArticleID;
898 * This clears some fields in this object, and clears any associated
899 * keys in the "bad links" section of $wgLinkCache.
901 * - This is called from Article::insertNewArticle() to allow
902 * loading of the new cur_id. It's also called from
903 * Article::doDeleteArticle()
905 * @param int $newid the new Article ID
906 * @access public
908 function resetArticleID( $newid ) {
909 global $wgLinkCache;
910 $wgLinkCache->clearBadLink( $this->getPrefixedDBkey() );
912 if ( 0 == $newid ) { $this->mArticleID = -1; }
913 else { $this->mArticleID = $newid; }
914 $this->mRestrictionsLoaded = false;
915 $this->mRestrictions = array();
919 * Updates cur_touched for this page; called from LinksUpdate.php
920 * @return bool true if the update succeded
921 * @access public
923 function invalidateCache() {
924 $now = wfTimestampNow();
925 $dbw =& wfGetDB( DB_MASTER );
926 $success = $dbw->updateArray( 'cur',
927 array( /* SET */
928 'cur_touched' => $dbw->timestamp()
929 ), array( /* WHERE */
930 'cur_namespace' => $this->getNamespace() ,
931 'cur_title' => $this->getDBkey()
932 ), 'Title::invalidateCache'
934 return $success;
938 * Prefix some arbitrary text with the namespace or interwiki prefix
939 * of this object
941 * @param string $name the text
942 * @return string the prefixed text
943 * @access private
945 /* private */ function prefix( $name ) {
946 global $wgContLang;
948 $p = '';
949 if ( '' != $this->mInterwiki ) {
950 $p = $this->mInterwiki . ':';
952 if ( 0 != $this->mNamespace ) {
953 $p .= $wgContLang->getNsText( $this->mNamespace ) . ':';
955 return $p . $name;
959 * Secure and split - main initialisation function for this object
961 * Assumes that mDbkeyform has been set, and is urldecoded
962 * and uses underscores, but not otherwise munged. This function
963 * removes illegal characters, splits off the interwiki and
964 * namespace prefixes, sets the other forms, and canonicalizes
965 * everything.
966 * @return bool true on success
967 * @access private
969 /* private */ function secureAndSplit()
971 global $wgContLang, $wgLocalInterwiki, $wgCapitalLinks;
972 $fname = 'Title::secureAndSplit';
973 wfProfileIn( $fname );
975 static $imgpre = false;
976 static $rxTc = false;
978 # Initialisation
979 if ( $imgpre === false ) {
980 $imgpre = ':' . $wgContLang->getNsText( Namespace::getImage() ) . ':';
981 # % is needed as well
982 $rxTc = '/[^' . Title::legalChars() . ']|%[0-9A-Fa-f]{2}/';
985 $this->mInterwiki = $this->mFragment = '';
986 $this->mNamespace = $this->mDefaultNamespace; # Usually NS_MAIN
988 # Clean up whitespace
990 $t = preg_replace( "/[\\s_]+/", '_', $this->mDbkeyform );
991 $t = preg_replace( '/^_*(.*?)_*$/', '$1', $t );
993 if ( '' == $t ) {
994 wfProfileOut( $fname );
995 return false;
998 global $wgUseLatin1;
999 if( !$wgUseLatin1 && false !== strpos( $t, UTF8_REPLACEMENT ) ) {
1000 # Contained illegal UTF-8 sequences or forbidden Unicode chars.
1001 wfProfileOut( $fname );
1002 return false;
1005 $this->mDbkeyform = $t;
1006 $done = false;
1008 # :Image: namespace
1009 if ( 0 == strncasecmp( $imgpre, $t, strlen( $imgpre ) ) ) {
1010 $t = substr( $t, 1 );
1013 # Initial colon indicating main namespace
1014 if ( ':' == $t{0} ) {
1015 $r = substr( $t, 1 );
1016 $this->mNamespace = NS_MAIN;
1017 } else {
1018 # Namespace or interwiki prefix
1019 if ( preg_match( "/^(.+?)_*:_*(.*)$/", $t, $m ) ) {
1020 #$p = strtolower( $m[1] );
1021 $p = $m[1];
1022 $lowerNs = strtolower( $p );
1023 if ( $ns = Namespace::getCanonicalIndex( $lowerNs ) ) {
1024 # Canonical namespace
1025 $t = $m[2];
1026 $this->mNamespace = $ns;
1027 } elseif ( $ns = $wgContLang->getNsIndex( $lowerNs )) {
1028 # Ordinary namespace
1029 $t = $m[2];
1030 $this->mNamespace = $ns;
1031 } elseif ( $this->getInterwikiLink( $p ) ) {
1032 # Interwiki link
1033 $t = $m[2];
1034 $this->mInterwiki = $p;
1036 if ( !preg_match( "/^([A-Za-z0-9_\\x80-\\xff]+):(.*)$/", $t, $m ) ) {
1037 $done = true;
1038 } elseif($this->mInterwiki != $wgLocalInterwiki) {
1039 $done = true;
1043 $r = $t;
1046 # Redundant interwiki prefix to the local wiki
1047 if ( 0 == strcmp( $this->mInterwiki, $wgLocalInterwiki ) ) {
1048 $this->mInterwiki = '';
1050 # We already know that some pages won't be in the database!
1052 if ( '' != $this->mInterwiki || -1 == $this->mNamespace ) {
1053 $this->mArticleID = 0;
1055 $f = strstr( $r, '#' );
1056 if ( false !== $f ) {
1057 $this->mFragment = substr( $f, 1 );
1058 $r = substr( $r, 0, strlen( $r ) - strlen( $f ) );
1059 # remove whitespace again: prevents "Foo_bar_#"
1060 # becoming "Foo_bar_"
1061 $r = preg_replace( '/_*$/', '', $r );
1064 # Reject illegal characters.
1066 if( preg_match( $rxTc, $r ) ) {
1067 wfProfileOut( $fname );
1068 return false;
1071 # "." and ".." conflict with the directories of those namesa
1072 if ( strpos( $r, '.' ) !== false &&
1073 ( $r === '.' || $r === '..' ||
1074 strpos( $r, './' ) === 0 ||
1075 strpos( $r, '../' ) === 0 ||
1076 strpos( $r, '/./' ) !== false ||
1077 strpos( $r, '/../' ) !== false ) )
1079 wfProfileOut( $fname );
1080 return false;
1083 # We shouldn't need to query the DB for the size.
1084 #$maxSize = $dbr->textFieldSize( 'cur', 'cur_title' );
1085 if ( strlen( $r ) > 255 ) {
1086 return false;
1089 # Initial capital letter
1090 if( $wgCapitalLinks && $this->mInterwiki == '') {
1091 $t = $wgContLang->ucfirst( $r );
1092 } else {
1093 $t = $r;
1096 # Fill fields
1097 $this->mDbkeyform = $t;
1098 $this->mUrlform = wfUrlencode( $t );
1100 $this->mTextform = str_replace( '_', ' ', $t );
1102 wfProfileOut( $fname );
1103 return true;
1107 * Get a Title object associated with the talk page of this article
1108 * @return Title the object for the talk page
1109 * @access public
1111 function getTalkPage() {
1112 return Title::makeTitle( Namespace::getTalk( $this->getNamespace() ), $this->getDBkey() );
1116 * Get a title object associated with the subject page of this
1117 * talk page
1119 * @return Title the object for the subject page
1120 * @access public
1122 function getSubjectPage() {
1123 return Title::makeTitle( Namespace::getSubject( $this->getNamespace() ), $this->getDBkey() );
1127 * Get an array of Title objects linking to this Title
1128 * - Also stores the IDs in the link cache.
1130 * @param string $options may be FOR UPDATE
1131 * @return array the Title objects linking here
1132 * @access public
1134 function getLinksTo( $options = '' ) {
1135 global $wgLinkCache;
1136 $id = $this->getArticleID();
1138 if ( $options ) {
1139 $db =& wfGetDB( DB_MASTER );
1140 } else {
1141 $db =& wfGetDB( DB_SLAVE );
1143 $cur = $db->tableName( 'cur' );
1144 $links = $db->tableName( 'links' );
1146 $sql = "SELECT cur_namespace,cur_title,cur_id FROM $cur,$links WHERE l_from=cur_id AND l_to={$id} $options";
1147 $res = $db->query( $sql, 'Title::getLinksTo' );
1148 $retVal = array();
1149 if ( $db->numRows( $res ) ) {
1150 while ( $row = $db->fetchObject( $res ) ) {
1151 if ( $titleObj = Title::makeTitle( $row->cur_namespace, $row->cur_title ) ) {
1152 $wgLinkCache->addGoodLink( $row->cur_id, $titleObj->getPrefixedDBkey() );
1153 $retVal[] = $titleObj;
1157 $db->freeResult( $res );
1158 return $retVal;
1162 * Get an array of Title objects linking to this non-existent title.
1163 * - Also stores the IDs in the link cache.
1165 * @param string $options may be FOR UPDATE
1166 * @return array the Title objects linking here
1167 * @access public
1169 function getBrokenLinksTo( $options = '' ) {
1170 global $wgLinkCache;
1172 if ( $options ) {
1173 $db =& wfGetDB( DB_MASTER );
1174 } else {
1175 $db =& wfGetDB( DB_SLAVE );
1177 $cur = $db->tableName( 'cur' );
1178 $brokenlinks = $db->tableName( 'brokenlinks' );
1179 $encTitle = $db->strencode( $this->getPrefixedDBkey() );
1181 $sql = "SELECT cur_namespace,cur_title,cur_id FROM $brokenlinks,$cur " .
1182 "WHERE bl_from=cur_id AND bl_to='$encTitle' $options";
1183 $res = $db->query( $sql, "Title::getBrokenLinksTo" );
1184 $retVal = array();
1185 if ( $db->numRows( $res ) ) {
1186 while ( $row = $db->fetchObject( $res ) ) {
1187 $titleObj = Title::makeTitle( $row->cur_namespace, $row->cur_title );
1188 $wgLinkCache->addGoodLink( $row->cur_id, $titleObj->getPrefixedDBkey() );
1189 $retVal[] = $titleObj;
1192 $db->freeResult( $res );
1193 return $retVal;
1197 * Get a list of URLs to purge from the Squid cache when this
1198 * page changes
1200 * @return array the URLs
1201 * @access public
1203 function getSquidURLs() {
1204 return array(
1205 $this->getInternalURL(),
1206 $this->getInternalURL( 'action=history' )
1211 * Move this page without authentication
1212 * @param Title &$nt the new page Title
1213 * @access public
1215 function moveNoAuth( &$nt ) {
1216 return $this->moveTo( $nt, false );
1220 * Move a title to a new location
1221 * @param Title &$nt the new title
1222 * @param bool $auth indicates whether $wgUser's permissions
1223 * should be checked
1224 * @return mixed true on success, message name on failure
1225 * @access public
1227 function moveTo( &$nt, $auth = true ) {
1228 if( !$this or !$nt ) {
1229 return 'badtitletext';
1232 $fname = 'Title::move';
1233 $oldid = $this->getArticleID();
1234 $newid = $nt->getArticleID();
1236 if ( strlen( $nt->getDBkey() ) < 1 ) {
1237 return 'articleexists';
1239 if ( ( ! Namespace::isMovable( $this->getNamespace() ) ) ||
1240 ( '' == $this->getDBkey() ) ||
1241 ( '' != $this->getInterwiki() ) ||
1242 ( !$oldid ) ||
1243 ( ! Namespace::isMovable( $nt->getNamespace() ) ) ||
1244 ( '' == $nt->getDBkey() ) ||
1245 ( '' != $nt->getInterwiki() ) ) {
1246 return 'badarticleerror';
1249 if ( $auth && ( !$this->userCanEdit() || !$nt->userCanEdit() ) ) {
1250 return 'protectedpage';
1253 # The move is allowed only if (1) the target doesn't exist, or
1254 # (2) the target is a redirect to the source, and has no history
1255 # (so we can undo bad moves right after they're done).
1257 if ( 0 != $newid ) { # Target exists; check for validity
1258 if ( ! $this->isValidMoveTarget( $nt ) ) {
1259 return 'articleexists';
1261 $this->moveOverExistingRedirect( $nt );
1262 } else { # Target didn't exist, do normal move.
1263 $this->moveToNewTitle( $nt, $newid );
1266 # Fixing category links (those without piped 'alternate' names) to be sorted under the new title
1268 $dbw =& wfGetDB( DB_MASTER );
1269 $sql = "UPDATE categorylinks SET cl_sortkey=" . $dbw->addQuotes( $nt->getPrefixedText() ) .
1270 " WHERE cl_from=" . $dbw->addQuotes( $this->getArticleID() ) .
1271 " AND cl_sortkey=" . $dbw->addQuotes( $this->getPrefixedText() );
1272 $dbw->query( $sql, 'SpecialMovepage::doSubmit' );
1274 # Update watchlists
1276 $oldnamespace = $this->getNamespace() & ~1;
1277 $newnamespace = $nt->getNamespace() & ~1;
1278 $oldtitle = $this->getDBkey();
1279 $newtitle = $nt->getDBkey();
1281 if( $oldnamespace != $newnamespace || $oldtitle != $newtitle ) {
1282 WatchedItem::duplicateEntries( $this, $nt );
1285 # Update search engine
1286 $u = new SearchUpdate( $oldid, $nt->getPrefixedDBkey() );
1287 $u->doUpdate();
1288 $u = new SearchUpdate( $newid, $this->getPrefixedDBkey(), '' );
1289 $u->doUpdate();
1291 return true;
1295 * Move page to a title which is at present a redirect to the
1296 * source page
1298 * @param Title &$nt the page to move to, which should currently
1299 * be a redirect
1300 * @access private
1302 /* private */ function moveOverExistingRedirect( &$nt ) {
1303 global $wgUser, $wgLinkCache, $wgUseSquid, $wgMwRedir;
1304 $fname = 'Title::moveOverExistingRedirect';
1305 $comment = wfMsg( '1movedto2', $this->getPrefixedText(), $nt->getPrefixedText() );
1307 $now = wfTimestampNow();
1308 $won = wfInvertTimestamp( $now );
1309 $newid = $nt->getArticleID();
1310 $oldid = $this->getArticleID();
1311 $dbw =& wfGetDB( DB_MASTER );
1312 $links = $dbw->tableName( 'links' );
1314 # Change the name of the target page:
1315 $dbw->updateArray( 'cur',
1316 /* SET */ array(
1317 'cur_touched' => $dbw->timestamp($now),
1318 'cur_namespace' => $nt->getNamespace(),
1319 'cur_title' => $nt->getDBkey()
1321 /* WHERE */ array( 'cur_id' => $oldid ),
1322 $fname
1324 $wgLinkCache->clearLink( $nt->getPrefixedDBkey() );
1326 # Repurpose the old redirect. We don't save it to history since
1327 # by definition if we've got here it's rather uninteresting.
1329 $redirectText = $wgMwRedir->getSynonym( 0 ) . ' [[' . $nt->getPrefixedText() . "]]\n";
1330 $dbw->updateArray( 'cur',
1331 /* SET */ array(
1332 'cur_touched' => $dbw->timestamp($now),
1333 'cur_timestamp' => $dbw->timestamp($now),
1334 'inverse_timestamp' => $won,
1335 'cur_namespace' => $this->getNamespace(),
1336 'cur_title' => $this->getDBkey(),
1337 'cur_text' => $wgMwRedir->getSynonym( 0 ) . ' [[' . $nt->getPrefixedText() . "]]\n",
1338 'cur_comment' => $comment,
1339 'cur_user' => $wgUser->getID(),
1340 'cur_minor_edit' => 0,
1341 'cur_counter' => 0,
1342 'cur_restrictions' => '',
1343 'cur_user_text' => $wgUser->getName(),
1344 'cur_is_redirect' => 1,
1345 'cur_is_new' => 1
1347 /* WHERE */ array( 'cur_id' => $newid ),
1348 $fname
1351 $wgLinkCache->clearLink( $this->getPrefixedDBkey() );
1353 # Fix the redundant names for the past revisions of the target page.
1354 # The redirect should have no old revisions.
1355 $dbw->updateArray(
1356 /* table */ 'old',
1357 /* SET */ array(
1358 'old_namespace' => $nt->getNamespace(),
1359 'old_title' => $nt->getDBkey(),
1361 /* WHERE */ array(
1362 'old_namespace' => $this->getNamespace(),
1363 'old_title' => $this->getDBkey(),
1365 $fname
1368 RecentChange::notifyMoveOverRedirect( $now, $this, $nt, $wgUser, $comment );
1370 # Swap links
1372 # Load titles and IDs
1373 $linksToOld = $this->getLinksTo( 'FOR UPDATE' );
1374 $linksToNew = $nt->getLinksTo( 'FOR UPDATE' );
1376 # Delete them all
1377 $sql = "DELETE FROM $links WHERE l_to=$oldid OR l_to=$newid";
1378 $dbw->query( $sql, $fname );
1380 # Reinsert
1381 if ( count( $linksToOld ) || count( $linksToNew )) {
1382 $sql = "INSERT INTO $links (l_from,l_to) VALUES ";
1383 $first = true;
1385 # Insert links to old title
1386 foreach ( $linksToOld as $linkTitle ) {
1387 if ( $first ) {
1388 $first = false;
1389 } else {
1390 $sql .= ',';
1392 $id = $linkTitle->getArticleID();
1393 $sql .= "($id,$newid)";
1396 # Insert links to new title
1397 foreach ( $linksToNew as $linkTitle ) {
1398 if ( $first ) {
1399 $first = false;
1400 } else {
1401 $sql .= ',';
1403 $id = $linkTitle->getArticleID();
1404 $sql .= "($id, $oldid)";
1407 $dbw->query( $sql, DB_MASTER, $fname );
1410 # Now, we record the link from the redirect to the new title.
1411 # It should have no other outgoing links...
1412 $dbw->delete( 'links', array( 'l_from' => $newid ) );
1413 $dbw->insertArray( 'links', array( 'l_from' => $newid, 'l_to' => $oldid ) );
1415 # Clear linkscc
1416 LinkCache::linksccClearLinksTo( $oldid );
1417 LinkCache::linksccClearLinksTo( $newid );
1419 # Purge squid
1420 if ( $wgUseSquid ) {
1421 $urls = array_merge( $nt->getSquidURLs(), $this->getSquidURLs() );
1422 $u = new SquidUpdate( $urls );
1423 $u->doUpdate();
1428 * Move page to non-existing title.
1429 * @param Title &$nt the new Title
1430 * @param int &$newid set to be the new article ID
1431 * @access private
1433 /* private */ function moveToNewTitle( &$nt, &$newid ) {
1434 global $wgUser, $wgLinkCache, $wgUseSquid;
1435 $fname = 'MovePageForm::moveToNewTitle';
1436 $comment = wfMsg( '1movedto2', $this->getPrefixedText(), $nt->getPrefixedText() );
1438 $newid = $nt->getArticleID();
1439 $oldid = $this->getArticleID();
1440 $dbw =& wfGetDB( DB_MASTER );
1441 $now = $dbw->timestamp();
1442 $won = wfInvertTimestamp( wfTimestamp(TS_MW,$now) );
1443 wfSeedRandom();
1444 $rand = wfRandom();
1446 # Rename cur entry
1447 $dbw->updateArray( 'cur',
1448 /* SET */ array(
1449 'cur_touched' => $now,
1450 'cur_namespace' => $nt->getNamespace(),
1451 'cur_title' => $nt->getDBkey()
1453 /* WHERE */ array( 'cur_id' => $oldid ),
1454 $fname
1457 $wgLinkCache->clearLink( $nt->getPrefixedDBkey() );
1459 # Insert redirect
1460 $dbw->insertArray( 'cur', array(
1461 'cur_id' => $dbw->nextSequenceValue('cur_cur_id_seq'),
1462 'cur_namespace' => $this->getNamespace(),
1463 'cur_title' => $this->getDBkey(),
1464 'cur_comment' => $comment,
1465 'cur_user' => $wgUser->getID(),
1466 'cur_user_text' => $wgUser->getName(),
1467 'cur_timestamp' => $now,
1468 'inverse_timestamp' => $won,
1469 'cur_touched' => $now,
1470 'cur_is_redirect' => 1,
1471 'cur_random' => $rand,
1472 'cur_is_new' => 1,
1473 'cur_text' => "#REDIRECT [[" . $nt->getPrefixedText() . "]]\n" ), $fname
1475 $newid = $dbw->insertId();
1476 $wgLinkCache->clearLink( $this->getPrefixedDBkey() );
1478 # Rename old entries
1479 $dbw->updateArray(
1480 /* table */ 'old',
1481 /* SET */ array(
1482 'old_namespace' => $nt->getNamespace(),
1483 'old_title' => $nt->getDBkey()
1485 /* WHERE */ array(
1486 'old_namespace' => $this->getNamespace(),
1487 'old_title' => $this->getDBkey()
1488 ), $fname
1491 # Record in RC
1492 RecentChange::notifyMoveToNew( $now, $this, $nt, $wgUser, $comment );
1494 # Purge squid and linkscc as per article creation
1495 Article::onArticleCreate( $nt );
1497 # Any text links to the old title must be reassigned to the redirect
1498 $dbw->updateArray( 'links', array( 'l_to' => $newid ), array( 'l_to' => $oldid ), $fname );
1499 LinkCache::linksccClearLinksTo( $oldid );
1501 # Record the just-created redirect's linking to the page
1502 $dbw->insertArray( 'links', array( 'l_from' => $newid, 'l_to' => $oldid ), $fname );
1504 # Non-existent target may have had broken links to it; these must
1505 # now be removed and made into good links.
1506 $update = new LinksUpdate( $oldid, $nt->getPrefixedDBkey() );
1507 $update->fixBrokenLinks();
1509 # Purge old title from squid
1510 # The new title, and links to the new title, are purged in Article::onArticleCreate()
1511 $titles = $nt->getLinksTo();
1512 if ( $wgUseSquid ) {
1513 $urls = $this->getSquidURLs();
1514 foreach ( $titles as $linkTitle ) {
1515 $urls[] = $linkTitle->getInternalURL();
1517 $u = new SquidUpdate( $urls );
1518 $u->doUpdate();
1523 * Checks if $this can be moved to a given Title
1524 * - Selects for update, so don't call it unless you mean business
1526 * @param Title &$nt the new title to check
1527 * @access public
1529 function isValidMoveTarget( $nt ) {
1530 $fname = 'Title::isValidMoveTarget';
1531 $dbw =& wfGetDB( DB_MASTER );
1533 # Is it a redirect?
1534 $id = $nt->getArticleID();
1535 $obj = $dbw->getArray( 'cur', array( 'cur_is_redirect','cur_text' ),
1536 array( 'cur_id' => $id ), $fname, 'FOR UPDATE' );
1538 if ( !$obj || 0 == $obj->cur_is_redirect ) {
1539 # Not a redirect
1540 return false;
1543 # Does the redirect point to the source?
1544 if ( preg_match( "/\\[\\[\\s*([^\\]\\|]*)]]/", $obj->cur_text, $m ) ) {
1545 $redirTitle = Title::newFromText( $m[1] );
1546 if( !is_object( $redirTitle ) ||
1547 $redirTitle->getPrefixedDBkey() != $this->getPrefixedDBkey() ) {
1548 return false;
1552 # Does the article have a history?
1553 $row = $dbw->getArray( 'old', array( 'old_id' ),
1554 array(
1555 'old_namespace' => $nt->getNamespace(),
1556 'old_title' => $nt->getDBkey()
1557 ), $fname, 'FOR UPDATE'
1560 # Return true if there was no history
1561 return $row === false;
1565 * Create a redirect; fails if the title already exists; does
1566 * not notify RC
1568 * @param Title $dest the destination of the redirect
1569 * @param string $comment the comment string describing the move
1570 * @return bool true on success
1571 * @access public
1573 function createRedirect( $dest, $comment ) {
1574 global $wgUser;
1575 if ( $this->getArticleID() ) {
1576 return false;
1579 $fname = 'Title::createRedirect';
1580 $dbw =& wfGetDB( DB_MASTER );
1581 $now = wfTimestampNow();
1582 $won = wfInvertTimestamp( $now );
1583 $seqVal = $dbw->nextSequenceValue( 'cur_cur_id_seq' );
1585 $dbw->insertArray( 'cur', array(
1586 'cur_id' => $seqVal,
1587 'cur_namespace' => $this->getNamespace(),
1588 'cur_title' => $this->getDBkey(),
1589 'cur_comment' => $comment,
1590 'cur_user' => $wgUser->getID(),
1591 'cur_user_text' => $wgUser->getName(),
1592 'cur_timestamp' => $now,
1593 'inverse_timestamp' => $won,
1594 'cur_touched' => $now,
1595 'cur_is_redirect' => 1,
1596 'cur_is_new' => 1,
1597 'cur_text' => "#REDIRECT [[" . $dest->getPrefixedText() . "]]\n"
1598 ), $fname );
1599 $newid = $dbw->insertId();
1600 $this->resetArticleID( $newid );
1602 # Link table
1603 if ( $dest->getArticleID() ) {
1604 $dbw->insertArray( 'links',
1605 array(
1606 'l_to' => $dest->getArticleID(),
1607 'l_from' => $newid
1608 ), $fname
1610 } else {
1611 $dbw->insertArray( 'brokenlinks',
1612 array(
1613 'bl_to' => $dest->getPrefixedDBkey(),
1614 'bl_from' => $newid
1615 ), $fname
1619 Article::onArticleCreate( $this );
1620 return true;
1624 * Get categories to which this Title belongs and return an array of
1625 * categories' names.
1627 * @return array an array of parents in the form:
1628 * $parent => $currentarticle
1629 * @access public
1631 function getParentCategories() {
1632 global $wgContLang,$wgUser;
1634 $titlekey = $this->getArticleId();
1635 $sk =& $wgUser->getSkin();
1636 $parents = array();
1637 $dbr =& wfGetDB( DB_SLAVE );
1638 $cur = $dbr->tableName( 'cur' );
1639 $categorylinks = $dbr->tableName( 'categorylinks' );
1641 # NEW SQL
1642 $sql = "SELECT * FROM categorylinks"
1643 ." WHERE cl_from='$titlekey'"
1644 ." AND cl_from <> '0'"
1645 ." ORDER BY cl_sortkey";
1647 $res = $dbr->query ( $sql ) ;
1649 if($dbr->numRows($res) > 0) {
1650 while ( $x = $dbr->fetchObject ( $res ) )
1651 //$data[] = Title::newFromText($wgContLang->getNSText ( NS_CATEGORY ).':'.$x->cl_to);
1652 $data[$wgContLang->getNSText ( NS_CATEGORY ).':'.$x->cl_to] = $this->getFullText();
1653 $dbr->freeResult ( $res ) ;
1654 } else {
1655 $data = '';
1657 return $data;
1661 * Go through all parent categories of this Title
1662 * @return array
1663 * @access public
1665 function getCategorieBrowser() {
1666 $parents = $this->getParentCategories();
1668 if($parents != '') {
1669 foreach($parents as $parent => $current)
1671 $nt = Title::newFromText($parent);
1672 $stack[$parent] = $nt->getCategorieBrowser();
1674 return $stack;
1675 } else {
1676 return array();
1682 * Get an associative array for selecting this title from
1683 * the "cur" table
1685 * @return array
1686 * @access public
1688 function curCond() {
1689 return array( 'cur_namespace' => $this->mNamespace, 'cur_title' => $this->mDbkeyform );
1693 * Get an associative array for selecting this title from the
1694 * "old" table
1696 * @return array
1697 * @access public
1699 function oldCond() {
1700 return array( 'old_namespace' => $this->mNamespace, 'old_title' => $this->mDbkeyform );
1704 * Get the revision ID of the previous revision
1706 * @param integer $revision Revision ID. Get the revision that was before this one.
1707 * @return interger $oldrevision|false
1709 function getPreviousRevisionID( $revision ) {
1710 $dbr =& wfGetDB( DB_SLAVE );
1711 return $dbr->selectField( 'old', 'old_id',
1712 'old_title=' . $dbr->addQuotes( $this->getDBkey() ) .
1713 ' AND old_namespace=' . IntVal( $this->getNamespace() ) .
1714 ' AND old_id<' . IntVal( $revision ) . ' ORDER BY old_id DESC' );
1718 * Get the revision ID of the next revision
1720 * @param integer $revision Revision ID. Get the revision that was after this one.
1721 * @return interger $oldrevision|false
1723 function getNextRevisionID( $revision ) {
1724 $dbr =& wfGetDB( DB_SLAVE );
1725 return $dbr->selectField( 'old', 'old_id',
1726 'old_title=' . $dbr->addQuotes( $this->getDBkey() ) .
1727 ' AND old_namespace=' . IntVal( $this->getNamespace() ) .
1728 ' AND old_id>' . IntVal( $revision ) . ' ORDER BY old_id' );