Remove stylesheets & templates subdirectories, whose contents have been migrated...
[mediawiki.git] / includes / Title.php
bloba34bde86b82f9551590b2861812803c85223428b
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 //convert to the desired language variant
529 $t = $wgContLang->convert($this->mPrefixedText);
530 return $t;
534 * Get the prefixed title with spaces, plus any fragment
535 * (part beginning with '#')
536 * @return string the prefixed title, with spaces and
537 * the fragment, including '#'
538 * @access public
540 function getFullText() {
541 global $wgContLang;
542 $text = $this->getPrefixedText();
543 if( '' != $this->mFragment ) {
544 $text .= '#' . $this->mFragment;
546 //convert to desired language variant
547 $t = $wgContLang->convert($text);
548 return $t;
552 * Get a URL-encoded title (not an actual URL) including interwiki
553 * @return string the URL-encoded form
554 * @access public
556 function getPrefixedURL() {
557 $s = $this->prefix( $this->mDbkeyform );
558 $s = str_replace( ' ', '_', $s );
560 $s = wfUrlencode ( $s ) ;
562 # Cleaning up URL to make it look nice -- is this safe?
563 $s = preg_replace( '/%3[Aa]/', ':', $s );
564 $s = preg_replace( '/%2[Ff]/', '/', $s );
565 $s = str_replace( '%28', '(', $s );
566 $s = str_replace( '%29', ')', $s );
568 return $s;
572 * Get a real URL referring to this title, with interwiki link and
573 * fragment
575 * @param string $query an optional query string, not used
576 * for interwiki links
577 * @return string the URL
578 * @access public
580 function getFullURL( $query = '' ) {
581 global $wgContLang, $wgArticlePath, $wgServer, $wgScript;
583 if ( '' == $this->mInterwiki ) {
584 $p = $wgArticlePath;
585 return $wgServer . $this->getLocalUrl( $query );
586 } else {
587 $baseUrl = $this->getInterwikiLink( $this->mInterwiki );
588 $namespace = $wgContLang->getNsText( $this->mNamespace );
589 if ( '' != $namespace ) {
590 # Can this actually happen? Interwikis shouldn't be parsed.
591 $namepace .= ':';
593 $url = str_replace( '$1', $namespace . $this->mUrlform, $baseUrl );
594 if ( '' != $this->mFragment ) {
595 $url .= '#' . $this->mFragment;
597 return $url;
602 * @deprecated
604 function getURL() {
605 die( 'Call to obsolete obsolete function Title::getURL()' );
609 * Get a URL with no fragment or server name
610 * @param string $query an optional query string; if not specified,
611 * $wgArticlePath will be used.
612 * @return string the URL
613 * @access public
615 function getLocalURL( $query = '' ) {
616 global $wgLang, $wgArticlePath, $wgScript;
618 if ( $this->isExternal() ) {
619 return $this->getFullURL();
622 $dbkey = wfUrlencode( $this->getPrefixedDBkey() );
623 if ( $query == '' ) {
624 $url = str_replace( '$1', $dbkey, $wgArticlePath );
625 } else {
626 if ( $query == '-' ) {
627 $query = '';
629 if ( $wgScript != '' ) {
630 $url = "{$wgScript}?title={$dbkey}&{$query}";
631 } else {
632 # Top level wiki
633 $url = "/{$dbkey}?{$query}";
636 return $url;
640 * Get an HTML-escaped version of the URL form, suitable for
641 * using in a link, without a server name or fragment
642 * @param string $query an optional query string
643 * @return string the URL
644 * @access public
646 function escapeLocalURL( $query = '' ) {
647 return htmlspecialchars( $this->getLocalURL( $query ) );
651 * Get an HTML-escaped version of the URL form, suitable for
652 * using in a link, including the server name and fragment
654 * @return string the URL
655 * @param string $query an optional query string
656 * @access public
658 function escapeFullURL( $query = '' ) {
659 return htmlspecialchars( $this->getFullURL( $query ) );
662 /**
663 * Get the URL form for an internal link.
664 * - Used in various Squid-related code, in case we have a different
665 * internal hostname for the server from the exposed one.
667 * @param string $query an optional query string
668 * @return string the URL
669 * @access public
671 function getInternalURL( $query = '' ) {
672 global $wgInternalServer;
673 return $wgInternalServer . $this->getLocalURL( $query );
677 * Get the edit URL for this Title
678 * @return string the URL, or a null string if this is an
679 * interwiki link
680 * @access public
682 function getEditURL() {
683 global $wgServer, $wgScript;
685 if ( '' != $this->mInterwiki ) { return ''; }
686 $s = $this->getLocalURL( 'action=edit' );
688 return $s;
692 * Get the HTML-escaped displayable text form.
693 * Used for the title field in <a> tags.
694 * @return string the text, including any prefixes
695 * @access public
697 function getEscapedText() {
698 return htmlspecialchars( $this->getPrefixedText() );
702 * Is this Title interwiki?
703 * @return boolean
704 * @access public
706 function isExternal() { return ( '' != $this->mInterwiki ); }
709 * Does the title correspond to a protected article?
710 * @return boolean
711 * @access public
713 function isProtected() {
714 if ( -1 == $this->mNamespace ) { return true; }
715 $a = $this->getRestrictions();
716 if ( in_array( 'sysop', $a ) ) { return true; }
717 return false;
721 * Is the page a log page, i.e. one where the history is messed up by
722 * LogPage.php? This used to be used for suppressing diff links in
723 * recent changes, but now that's done by setting a flag in the
724 * recentchanges table. Hence, this probably is no longer used.
726 * @deprecated
727 * @access public
729 function isLog() {
730 if ( $this->mNamespace != Namespace::getWikipedia() ) {
731 return false;
733 if ( ( 0 == strcmp( wfMsg( 'uploadlogpage' ), $this->mDbkeyform ) ) ||
734 ( 0 == strcmp( wfMsg( 'dellogpage' ), $this->mDbkeyform ) ) ) {
735 return true;
737 return false;
741 * Is $wgUser is watching this page?
742 * @return boolean
743 * @access public
745 function userIsWatching() {
746 global $wgUser;
748 if ( -1 == $this->mNamespace ) { return false; }
749 if ( 0 == $wgUser->getID() ) { return false; }
751 return $wgUser->isWatched( $this );
755 * Can $wgUser edit this page?
756 * @return boolean
757 * @access public
759 function userCanEdit() {
760 global $wgUser;
761 if ( -1 == $this->mNamespace ) { return false; }
762 if ( NS_MEDIAWIKI == $this->mNamespace && !$wgUser->isSysop() ) { return false; }
763 # if ( 0 == $this->getArticleID() ) { return false; }
764 if ( $this->mDbkeyform == '_' ) { return false; }
765 # protect global styles and js
766 if ( NS_MEDIAWIKI == $this->mNamespace
767 && preg_match("/\\.(css|js)$/", $this->mTextform )
768 && !$wgUser->isSysop() )
769 { return false; }
770 //if ( $this->isCssJsSubpage() and !$this->userCanEditCssJsSubpage() ) { return false; }
771 # protect css/js subpages of user pages
772 # XXX: this might be better using restrictions
773 # XXX: Find a way to work around the php bug that prevents using $this->userCanEditCssJsSubpage() from working
774 if( Namespace::getUser() == $this->mNamespace
775 and preg_match("/\\.(css|js)$/", $this->mTextform )
776 and !$wgUser->isSysop()
777 and !preg_match('/^'.preg_quote($wgUser->getName(), '/').'/', $this->mTextform) )
778 { return false; }
779 $ur = $wgUser->getRights();
780 foreach ( $this->getRestrictions() as $r ) {
781 if ( '' != $r && ( ! in_array( $r, $ur ) ) ) {
782 return false;
785 return true;
789 * Can $wgUser read this page?
790 * @return boolean
791 * @access public
793 function userCanRead() {
794 global $wgUser;
795 global $wgWhitelistRead;
797 if( 0 != $wgUser->getID() ) return true;
798 if( !is_array( $wgWhitelistRead ) ) return true;
800 $name = $this->getPrefixedText();
801 if( in_array( $name, $wgWhitelistRead ) ) return true;
803 # Compatibility with old settings
804 if( $this->getNamespace() == NS_MAIN ) {
805 if( in_array( ':' . $name, $wgWhitelistRead ) ) return true;
807 return false;
811 * Is this a .css or .js subpage of a user page?
812 * @return bool
813 * @access public
815 function isCssJsSubpage() {
816 return ( Namespace::getUser() == $this->mNamespace and preg_match("/\\.(css|js)$/", $this->mTextform ) );
819 * Is this a .css subpage of a user page?
820 * @return bool
821 * @access public
823 function isCssSubpage() {
824 return ( Namespace::getUser() == $this->mNamespace and preg_match("/\\.css$/", $this->mTextform ) );
827 * Is this a .js subpage of a user page?
828 * @return bool
829 * @access public
831 function isJsSubpage() {
832 return ( Namespace::getUser() == $this->mNamespace and preg_match("/\\.js$/", $this->mTextform ) );
835 * Protect css/js subpages of user pages: can $wgUser edit
836 * this page?
838 * @return boolean
839 * @todo XXX: this might be better using restrictions
840 * @access public
842 function userCanEditCssJsSubpage() {
843 global $wgUser;
844 return ( $wgUser->isSysop() or preg_match('/^'.preg_quote($wgUser->getName()).'/', $this->mTextform) );
848 * Accessor/initialisation for mRestrictions
849 * @return array the array of groups allowed to edit this article
850 * @access public
852 function getRestrictions() {
853 $id = $this->getArticleID();
854 if ( 0 == $id ) { return array(); }
856 if ( ! $this->mRestrictionsLoaded ) {
857 $dbr =& wfGetDB( DB_SLAVE );
858 $res = $dbr->getField( 'cur', 'cur_restrictions', 'cur_id='.$id );
859 $this->mRestrictions = explode( ',', trim( $res ) );
860 $this->mRestrictionsLoaded = true;
862 return $this->mRestrictions;
866 * Is there a version of this page in the deletion archive?
867 * @return int the number of archived revisions
868 * @access public
870 function isDeleted() {
871 $fname = 'Title::isDeleted';
872 $dbr =& wfGetDB( DB_SLAVE );
873 $n = $dbr->getField( 'archive', 'COUNT(*)', array( 'ar_namespace' => $this->getNamespace(),
874 'ar_title' => $this->getDBkey() ), $fname );
875 return (int)$n;
879 * Get the article ID for this Title from the link cache,
880 * adding it if necessary
881 * @param int $flags a bit field; may be GAID_FOR_UPDATE to select
882 * for update
883 * @return int the ID
884 * @access public
886 function getArticleID( $flags = 0 ) {
887 global $wgLinkCache;
889 if ( $flags & GAID_FOR_UPDATE ) {
890 $oldUpdate = $wgLinkCache->forUpdate( true );
891 $this->mArticleID = $wgLinkCache->addLinkObj( $this );
892 $wgLinkCache->forUpdate( $oldUpdate );
893 } else {
894 if ( -1 == $this->mArticleID ) {
895 $this->mArticleID = $wgLinkCache->addLinkObj( $this );
898 return $this->mArticleID;
902 * This clears some fields in this object, and clears any associated
903 * keys in the "bad links" section of $wgLinkCache.
905 * - This is called from Article::insertNewArticle() to allow
906 * loading of the new cur_id. It's also called from
907 * Article::doDeleteArticle()
909 * @param int $newid the new Article ID
910 * @access public
912 function resetArticleID( $newid ) {
913 global $wgLinkCache;
914 $wgLinkCache->clearBadLink( $this->getPrefixedDBkey() );
916 if ( 0 == $newid ) { $this->mArticleID = -1; }
917 else { $this->mArticleID = $newid; }
918 $this->mRestrictionsLoaded = false;
919 $this->mRestrictions = array();
923 * Updates cur_touched for this page; called from LinksUpdate.php
924 * @return bool true if the update succeded
925 * @access public
927 function invalidateCache() {
928 $now = wfTimestampNow();
929 $dbw =& wfGetDB( DB_MASTER );
930 $success = $dbw->updateArray( 'cur',
931 array( /* SET */
932 'cur_touched' => $dbw->timestamp()
933 ), array( /* WHERE */
934 'cur_namespace' => $this->getNamespace() ,
935 'cur_title' => $this->getDBkey()
936 ), 'Title::invalidateCache'
938 return $success;
942 * Prefix some arbitrary text with the namespace or interwiki prefix
943 * of this object
945 * @param string $name the text
946 * @return string the prefixed text
947 * @access private
949 /* private */ function prefix( $name ) {
950 global $wgContLang;
952 $p = '';
953 if ( '' != $this->mInterwiki ) {
954 $p = $this->mInterwiki . ':';
956 if ( 0 != $this->mNamespace ) {
957 $p .= $wgContLang->getNsText( $this->mNamespace ) . ':';
959 return $p . $name;
963 * Secure and split - main initialisation function for this object
965 * Assumes that mDbkeyform has been set, and is urldecoded
966 * and uses underscores, but not otherwise munged. This function
967 * removes illegal characters, splits off the interwiki and
968 * namespace prefixes, sets the other forms, and canonicalizes
969 * everything.
970 * @return bool true on success
971 * @access private
973 /* private */ function secureAndSplit()
975 global $wgContLang, $wgLocalInterwiki, $wgCapitalLinks;
976 $fname = 'Title::secureAndSplit';
977 wfProfileIn( $fname );
979 static $imgpre = false;
980 static $rxTc = false;
982 # Initialisation
983 if ( $imgpre === false ) {
984 $imgpre = ':' . $wgContLang->getNsText( Namespace::getImage() ) . ':';
985 # % is needed as well
986 $rxTc = '/[^' . Title::legalChars() . ']/';
989 $this->mInterwiki = $this->mFragment = '';
990 $this->mNamespace = $this->mDefaultNamespace; # Usually NS_MAIN
992 # Clean up whitespace
994 $t = preg_replace( "/[\\s_]+/", '_', $this->mDbkeyform );
995 $t = preg_replace( '/^_*(.*?)_*$/', '$1', $t );
997 if ( '' == $t ) {
998 wfProfileOut( $fname );
999 return false;
1002 global $wgUseLatin1;
1003 if( !$wgUseLatin1 && false !== strpos( $t, UTF8_REPLACEMENT ) ) {
1004 # Contained illegal UTF-8 sequences or forbidden Unicode chars.
1005 wfProfileOut( $fname );
1006 return false;
1009 $this->mDbkeyform = $t;
1010 $done = false;
1012 # :Image: namespace
1013 if ( 0 == strncasecmp( $imgpre, $t, strlen( $imgpre ) ) ) {
1014 $t = substr( $t, 1 );
1017 # Initial colon indicating main namespace
1018 if ( ':' == $t{0} ) {
1019 $r = substr( $t, 1 );
1020 $this->mNamespace = NS_MAIN;
1021 } else {
1022 # Namespace or interwiki prefix
1023 if ( preg_match( "/^(.+?)_*:_*(.*)$/", $t, $m ) ) {
1024 #$p = strtolower( $m[1] );
1025 $p = $m[1];
1026 $lowerNs = strtolower( $p );
1027 if ( $ns = Namespace::getCanonicalIndex( $lowerNs ) ) {
1028 # Canonical namespace
1029 $t = $m[2];
1030 $this->mNamespace = $ns;
1031 } elseif ( $ns = $wgContLang->getNsIndex( $lowerNs )) {
1032 # Ordinary namespace
1033 $t = $m[2];
1034 $this->mNamespace = $ns;
1035 } elseif ( $this->getInterwikiLink( $p ) ) {
1036 # Interwiki link
1037 $t = $m[2];
1038 $this->mInterwiki = $p;
1040 if ( !preg_match( "/^([A-Za-z0-9_\\x80-\\xff]+):(.*)$/", $t, $m ) ) {
1041 $done = true;
1042 } elseif($this->mInterwiki != $wgLocalInterwiki) {
1043 $done = true;
1047 $r = $t;
1050 # Redundant interwiki prefix to the local wiki
1051 if ( 0 == strcmp( $this->mInterwiki, $wgLocalInterwiki ) ) {
1052 $this->mInterwiki = '';
1054 # We already know that some pages won't be in the database!
1056 if ( '' != $this->mInterwiki || -1 == $this->mNamespace ) {
1057 $this->mArticleID = 0;
1059 $f = strstr( $r, '#' );
1060 if ( false !== $f ) {
1061 $this->mFragment = substr( $f, 1 );
1062 $r = substr( $r, 0, strlen( $r ) - strlen( $f ) );
1063 # remove whitespace again: prevents "Foo_bar_#"
1064 # becoming "Foo_bar_"
1065 $r = preg_replace( '/_*$/', '', $r );
1068 # Reject illegal characters.
1070 if( preg_match( $rxTc, $r ) ) {
1071 wfProfileOut( $fname );
1072 return false;
1075 # "." and ".." conflict with the directories of those namesa
1076 if ( strpos( $r, '.' ) !== false &&
1077 ( $r === '.' || $r === '..' ||
1078 strpos( $r, './' ) === 0 ||
1079 strpos( $r, '../' ) === 0 ||
1080 strpos( $r, '/./' ) !== false ||
1081 strpos( $r, '/../' ) !== false ) )
1083 wfProfileOut( $fname );
1084 return false;
1087 # We shouldn't need to query the DB for the size.
1088 #$maxSize = $dbr->textFieldSize( 'cur', 'cur_title' );
1089 if ( strlen( $t ) > 255 ) {
1090 return false;
1093 # Initial capital letter
1094 if( $wgCapitalLinks && $this->mInterwiki == '') {
1095 $t = $wgContLang->ucfirst( $r );
1096 } else {
1097 $t = $r;
1100 # Fill fields
1101 $this->mDbkeyform = $t;
1102 $this->mUrlform = wfUrlencode( $t );
1104 $this->mTextform = str_replace( '_', ' ', $t );
1106 wfProfileOut( $fname );
1107 return true;
1111 * Get a Title object associated with the talk page of this article
1112 * @return Title the object for the talk page
1113 * @access public
1115 function getTalkPage() {
1116 return Title::makeTitle( Namespace::getTalk( $this->getNamespace() ), $this->getDBkey() );
1120 * Get a title object associated with the subject page of this
1121 * talk page
1123 * @return Title the object for the subject page
1124 * @access public
1126 function getSubjectPage() {
1127 return Title::makeTitle( Namespace::getSubject( $this->getNamespace() ), $this->getDBkey() );
1131 * Get an array of Title objects linking to this Title
1132 * - Also stores the IDs in the link cache.
1134 * @param string $options may be FOR UPDATE
1135 * @return array the Title objects linking here
1136 * @access public
1138 function getLinksTo( $options = '' ) {
1139 global $wgLinkCache;
1140 $id = $this->getArticleID();
1142 if ( $options ) {
1143 $db =& wfGetDB( DB_MASTER );
1144 } else {
1145 $db =& wfGetDB( DB_SLAVE );
1147 $cur = $db->tableName( 'cur' );
1148 $links = $db->tableName( 'links' );
1150 $sql = "SELECT cur_namespace,cur_title,cur_id FROM $cur,$links WHERE l_from=cur_id AND l_to={$id} $options";
1151 $res = $db->query( $sql, 'Title::getLinksTo' );
1152 $retVal = array();
1153 if ( $db->numRows( $res ) ) {
1154 while ( $row = $db->fetchObject( $res ) ) {
1155 if ( $titleObj = Title::makeTitle( $row->cur_namespace, $row->cur_title ) ) {
1156 $wgLinkCache->addGoodLink( $row->cur_id, $titleObj->getPrefixedDBkey() );
1157 $retVal[] = $titleObj;
1161 $db->freeResult( $res );
1162 return $retVal;
1166 * Get an array of Title objects linking to this non-existent title.
1167 * - Also stores the IDs in the link cache.
1169 * @param string $options may be FOR UPDATE
1170 * @return array the Title objects linking here
1171 * @access public
1173 function getBrokenLinksTo( $options = '' ) {
1174 global $wgLinkCache;
1176 if ( $options ) {
1177 $db =& wfGetDB( DB_MASTER );
1178 } else {
1179 $db =& wfGetDB( DB_SLAVE );
1181 $cur = $db->tableName( 'cur' );
1182 $brokenlinks = $db->tableName( 'brokenlinks' );
1183 $encTitle = $db->strencode( $this->getPrefixedDBkey() );
1185 $sql = "SELECT cur_namespace,cur_title,cur_id FROM $brokenlinks,$cur " .
1186 "WHERE bl_from=cur_id AND bl_to='$encTitle' $options";
1187 $res = $db->query( $sql, "Title::getBrokenLinksTo" );
1188 $retVal = array();
1189 if ( $db->numRows( $res ) ) {
1190 while ( $row = $db->fetchObject( $res ) ) {
1191 $titleObj = Title::makeTitle( $row->cur_namespace, $row->cur_title );
1192 $wgLinkCache->addGoodLink( $row->cur_id, $titleObj->getPrefixedDBkey() );
1193 $retVal[] = $titleObj;
1196 $db->freeResult( $res );
1197 return $retVal;
1201 * Get a list of URLs to purge from the Squid cache when this
1202 * page changes
1204 * @return array the URLs
1205 * @access public
1207 function getSquidURLs() {
1208 return array(
1209 $this->getInternalURL(),
1210 $this->getInternalURL( 'action=history' )
1215 * Move this page without authentication
1216 * @param Title &$nt the new page Title
1217 * @access public
1219 function moveNoAuth( &$nt ) {
1220 return $this->moveTo( $nt, false );
1224 * Move a title to a new location
1225 * @param Title &$nt the new title
1226 * @param bool $auth indicates whether $wgUser's permissions
1227 * should be checked
1228 * @return mixed true on success, message name on failure
1229 * @access public
1231 function moveTo( &$nt, $auth = true ) {
1232 if( !$this or !$nt ) {
1233 return 'badtitletext';
1236 $fname = 'Title::move';
1237 $oldid = $this->getArticleID();
1238 $newid = $nt->getArticleID();
1240 if ( strlen( $nt->getDBkey() ) < 1 ) {
1241 return 'articleexists';
1243 if ( ( ! Namespace::isMovable( $this->getNamespace() ) ) ||
1244 ( '' == $this->getDBkey() ) ||
1245 ( '' != $this->getInterwiki() ) ||
1246 ( !$oldid ) ||
1247 ( ! Namespace::isMovable( $nt->getNamespace() ) ) ||
1248 ( '' == $nt->getDBkey() ) ||
1249 ( '' != $nt->getInterwiki() ) ) {
1250 return 'badarticleerror';
1253 if ( $auth && ( !$this->userCanEdit() || !$nt->userCanEdit() ) ) {
1254 return 'protectedpage';
1257 # The move is allowed only if (1) the target doesn't exist, or
1258 # (2) the target is a redirect to the source, and has no history
1259 # (so we can undo bad moves right after they're done).
1261 if ( 0 != $newid ) { # Target exists; check for validity
1262 if ( ! $this->isValidMoveTarget( $nt ) ) {
1263 return 'articleexists';
1265 $this->moveOverExistingRedirect( $nt );
1266 } else { # Target didn't exist, do normal move.
1267 $this->moveToNewTitle( $nt, $newid );
1270 # Fixing category links (those without piped 'alternate' names) to be sorted under the new title
1272 $dbw =& wfGetDB( DB_MASTER );
1273 $sql = "UPDATE categorylinks SET cl_sortkey=" . $dbw->addQuotes( $nt->getPrefixedText() ) .
1274 " WHERE cl_from=" . $dbw->addQuotes( $this->getArticleID() ) .
1275 " AND cl_sortkey=" . $dbw->addQuotes( $this->getPrefixedText() );
1276 $dbw->query( $sql, 'SpecialMovepage::doSubmit' );
1278 # Update watchlists
1280 $oldnamespace = $this->getNamespace() & ~1;
1281 $newnamespace = $nt->getNamespace() & ~1;
1282 $oldtitle = $this->getDBkey();
1283 $newtitle = $nt->getDBkey();
1285 if( $oldnamespace != $newnamespace || $oldtitle != $newtitle ) {
1286 WatchedItem::duplicateEntries( $this, $nt );
1289 # Update search engine
1290 $u = new SearchUpdate( $oldid, $nt->getPrefixedDBkey() );
1291 $u->doUpdate();
1292 $u = new SearchUpdate( $newid, $this->getPrefixedDBkey(), '' );
1293 $u->doUpdate();
1295 return true;
1299 * Move page to a title which is at present a redirect to the
1300 * source page
1302 * @param Title &$nt the page to move to, which should currently
1303 * be a redirect
1304 * @access private
1306 /* private */ function moveOverExistingRedirect( &$nt ) {
1307 global $wgUser, $wgLinkCache, $wgUseSquid, $wgMwRedir;
1308 $fname = 'Title::moveOverExistingRedirect';
1309 $comment = wfMsg( '1movedto2', $this->getPrefixedText(), $nt->getPrefixedText() );
1311 $now = wfTimestampNow();
1312 $won = wfInvertTimestamp( $now );
1313 $newid = $nt->getArticleID();
1314 $oldid = $this->getArticleID();
1315 $dbw =& wfGetDB( DB_MASTER );
1316 $links = $dbw->tableName( 'links' );
1318 # Change the name of the target page:
1319 $dbw->updateArray( 'cur',
1320 /* SET */ array(
1321 'cur_touched' => $dbw->timestamp($now),
1322 'cur_namespace' => $nt->getNamespace(),
1323 'cur_title' => $nt->getDBkey()
1325 /* WHERE */ array( 'cur_id' => $oldid ),
1326 $fname
1328 $wgLinkCache->clearLink( $nt->getPrefixedDBkey() );
1330 # Repurpose the old redirect. We don't save it to history since
1331 # by definition if we've got here it's rather uninteresting.
1333 $redirectText = $wgMwRedir->getSynonym( 0 ) . ' [[' . $nt->getPrefixedText() . "]]\n";
1334 $dbw->updateArray( 'cur',
1335 /* SET */ array(
1336 'cur_touched' => $dbw->timestamp($now),
1337 'cur_timestamp' => $dbw->timestamp($now),
1338 'inverse_timestamp' => $won,
1339 'cur_namespace' => $this->getNamespace(),
1340 'cur_title' => $this->getDBkey(),
1341 'cur_text' => $wgMwRedir->getSynonym( 0 ) . ' [[' . $nt->getPrefixedText() . "]]\n",
1342 'cur_comment' => $comment,
1343 'cur_user' => $wgUser->getID(),
1344 'cur_minor_edit' => 0,
1345 'cur_counter' => 0,
1346 'cur_restrictions' => '',
1347 'cur_user_text' => $wgUser->getName(),
1348 'cur_is_redirect' => 1,
1349 'cur_is_new' => 1
1351 /* WHERE */ array( 'cur_id' => $newid ),
1352 $fname
1355 $wgLinkCache->clearLink( $this->getPrefixedDBkey() );
1357 # Fix the redundant names for the past revisions of the target page.
1358 # The redirect should have no old revisions.
1359 $dbw->updateArray(
1360 /* table */ 'old',
1361 /* SET */ array(
1362 'old_namespace' => $nt->getNamespace(),
1363 'old_title' => $nt->getDBkey(),
1365 /* WHERE */ array(
1366 'old_namespace' => $this->getNamespace(),
1367 'old_title' => $this->getDBkey(),
1369 $fname
1372 RecentChange::notifyMoveOverRedirect( $now, $this, $nt, $wgUser, $comment );
1374 # Swap links
1376 # Load titles and IDs
1377 $linksToOld = $this->getLinksTo( 'FOR UPDATE' );
1378 $linksToNew = $nt->getLinksTo( 'FOR UPDATE' );
1380 # Delete them all
1381 $sql = "DELETE FROM $links WHERE l_to=$oldid OR l_to=$newid";
1382 $dbw->query( $sql, $fname );
1384 # Reinsert
1385 if ( count( $linksToOld ) || count( $linksToNew )) {
1386 $sql = "INSERT INTO $links (l_from,l_to) VALUES ";
1387 $first = true;
1389 # Insert links to old title
1390 foreach ( $linksToOld as $linkTitle ) {
1391 if ( $first ) {
1392 $first = false;
1393 } else {
1394 $sql .= ',';
1396 $id = $linkTitle->getArticleID();
1397 $sql .= "($id,$newid)";
1400 # Insert links to new title
1401 foreach ( $linksToNew as $linkTitle ) {
1402 if ( $first ) {
1403 $first = false;
1404 } else {
1405 $sql .= ',';
1407 $id = $linkTitle->getArticleID();
1408 $sql .= "($id, $oldid)";
1411 $dbw->query( $sql, DB_MASTER, $fname );
1414 # Now, we record the link from the redirect to the new title.
1415 # It should have no other outgoing links...
1416 $dbw->delete( 'links', array( 'l_from' => $newid ) );
1417 $dbw->insertArray( 'links', array( 'l_from' => $newid, 'l_to' => $oldid ) );
1419 # Clear linkscc
1420 LinkCache::linksccClearLinksTo( $oldid );
1421 LinkCache::linksccClearLinksTo( $newid );
1423 # Purge squid
1424 if ( $wgUseSquid ) {
1425 $urls = array_merge( $nt->getSquidURLs(), $this->getSquidURLs() );
1426 $u = new SquidUpdate( $urls );
1427 $u->doUpdate();
1432 * Move page to non-existing title.
1433 * @param Title &$nt the new Title
1434 * @param int &$newid set to be the new article ID
1435 * @access private
1437 /* private */ function moveToNewTitle( &$nt, &$newid ) {
1438 global $wgUser, $wgLinkCache, $wgUseSquid;
1439 $fname = 'MovePageForm::moveToNewTitle';
1440 $comment = wfMsg( '1movedto2', $this->getPrefixedText(), $nt->getPrefixedText() );
1442 $newid = $nt->getArticleID();
1443 $oldid = $this->getArticleID();
1444 $dbw =& wfGetDB( DB_MASTER );
1445 $now = $dbw->timestamp();
1446 $won = wfInvertTimestamp( wfTimestamp(TS_MW,$now) );
1447 wfSeedRandom();
1448 $rand = number_format( mt_rand() / mt_getrandmax(), 12, '.', '' );
1450 # Rename cur entry
1451 $dbw->updateArray( 'cur',
1452 /* SET */ array(
1453 'cur_touched' => $now,
1454 'cur_namespace' => $nt->getNamespace(),
1455 'cur_title' => $nt->getDBkey()
1457 /* WHERE */ array( 'cur_id' => $oldid ),
1458 $fname
1461 $wgLinkCache->clearLink( $nt->getPrefixedDBkey() );
1463 # Insert redirect
1464 $dbw->insertArray( 'cur', array(
1465 'cur_id' => $dbw->nextSequenceValue('cur_cur_id_seq'),
1466 'cur_namespace' => $this->getNamespace(),
1467 'cur_title' => $this->getDBkey(),
1468 'cur_comment' => $comment,
1469 'cur_user' => $wgUser->getID(),
1470 'cur_user_text' => $wgUser->getName(),
1471 'cur_timestamp' => $now,
1472 'inverse_timestamp' => $won,
1473 'cur_touched' => $now,
1474 'cur_is_redirect' => 1,
1475 'cur_random' => $rand,
1476 'cur_is_new' => 1,
1477 'cur_text' => "#REDIRECT [[" . $nt->getPrefixedText() . "]]\n" ), $fname
1479 $newid = $dbw->insertId();
1480 $wgLinkCache->clearLink( $this->getPrefixedDBkey() );
1482 # Rename old entries
1483 $dbw->updateArray(
1484 /* table */ 'old',
1485 /* SET */ array(
1486 'old_namespace' => $nt->getNamespace(),
1487 'old_title' => $nt->getDBkey()
1489 /* WHERE */ array(
1490 'old_namespace' => $this->getNamespace(),
1491 'old_title' => $this->getDBkey()
1492 ), $fname
1495 # Record in RC
1496 RecentChange::notifyMoveToNew( $now, $this, $nt, $wgUser, $comment );
1498 # Purge squid and linkscc as per article creation
1499 Article::onArticleCreate( $nt );
1501 # Any text links to the old title must be reassigned to the redirect
1502 $dbw->updateArray( 'links', array( 'l_to' => $newid ), array( 'l_to' => $oldid ), $fname );
1503 LinkCache::linksccClearLinksTo( $oldid );
1505 # Record the just-created redirect's linking to the page
1506 $dbw->insertArray( 'links', array( 'l_from' => $newid, 'l_to' => $oldid ), $fname );
1508 # Non-existent target may have had broken links to it; these must
1509 # now be removed and made into good links.
1510 $update = new LinksUpdate( $oldid, $nt->getPrefixedDBkey() );
1511 $update->fixBrokenLinks();
1513 # Purge old title from squid
1514 # The new title, and links to the new title, are purged in Article::onArticleCreate()
1515 $titles = $nt->getLinksTo();
1516 if ( $wgUseSquid ) {
1517 $urls = $this->getSquidURLs();
1518 foreach ( $titles as $linkTitle ) {
1519 $urls[] = $linkTitle->getInternalURL();
1521 $u = new SquidUpdate( $urls );
1522 $u->doUpdate();
1527 * Checks if $this can be moved to a given Title
1528 * - Selects for update, so don't call it unless you mean business
1530 * @param Title &$nt the new title to check
1531 * @access public
1533 function isValidMoveTarget( $nt ) {
1534 $fname = 'Title::isValidMoveTarget';
1535 $dbw =& wfGetDB( DB_MASTER );
1537 # Is it a redirect?
1538 $id = $nt->getArticleID();
1539 $obj = $dbw->getArray( 'cur', array( 'cur_is_redirect','cur_text' ),
1540 array( 'cur_id' => $id ), $fname, 'FOR UPDATE' );
1542 if ( !$obj || 0 == $obj->cur_is_redirect ) {
1543 # Not a redirect
1544 return false;
1547 # Does the redirect point to the source?
1548 if ( preg_match( "/\\[\\[\\s*([^\\]\\|]*)]]/", $obj->cur_text, $m ) ) {
1549 $redirTitle = Title::newFromText( $m[1] );
1550 if( !is_object( $redirTitle ) ||
1551 $redirTitle->getPrefixedDBkey() != $this->getPrefixedDBkey() ) {
1552 return false;
1556 # Does the article have a history?
1557 $row = $dbw->getArray( 'old', array( 'old_id' ),
1558 array(
1559 'old_namespace' => $nt->getNamespace(),
1560 'old_title' => $nt->getDBkey()
1561 ), $fname, 'FOR UPDATE'
1564 # Return true if there was no history
1565 return $row === false;
1569 * Create a redirect; fails if the title already exists; does
1570 * not notify RC
1572 * @param Title $dest the destination of the redirect
1573 * @param string $comment the comment string describing the move
1574 * @return bool true on success
1575 * @access public
1577 function createRedirect( $dest, $comment ) {
1578 global $wgUser;
1579 if ( $this->getArticleID() ) {
1580 return false;
1583 $fname = 'Title::createRedirect';
1584 $dbw =& wfGetDB( DB_MASTER );
1585 $now = wfTimestampNow();
1586 $won = wfInvertTimestamp( $now );
1587 $seqVal = $dbw->nextSequenceValue( 'cur_cur_id_seq' );
1589 $dbw->insertArray( 'cur', array(
1590 'cur_id' => $seqVal,
1591 'cur_namespace' => $this->getNamespace(),
1592 'cur_title' => $this->getDBkey(),
1593 'cur_comment' => $comment,
1594 'cur_user' => $wgUser->getID(),
1595 'cur_user_text' => $wgUser->getName(),
1596 'cur_timestamp' => $now,
1597 'inverse_timestamp' => $won,
1598 'cur_touched' => $now,
1599 'cur_is_redirect' => 1,
1600 'cur_is_new' => 1,
1601 'cur_text' => "#REDIRECT [[" . $dest->getPrefixedText() . "]]\n"
1602 ), $fname );
1603 $newid = $dbw->insertId();
1604 $this->resetArticleID( $newid );
1606 # Link table
1607 if ( $dest->getArticleID() ) {
1608 $dbw->insertArray( 'links',
1609 array(
1610 'l_to' => $dest->getArticleID(),
1611 'l_from' => $newid
1612 ), $fname
1614 } else {
1615 $dbw->insertArray( 'brokenlinks',
1616 array(
1617 'bl_to' => $dest->getPrefixedDBkey(),
1618 'bl_from' => $newid
1619 ), $fname
1623 Article::onArticleCreate( $this );
1624 return true;
1628 * Get categories to which this Title belongs and return an array of
1629 * categories' names.
1631 * @return array an array of parents in the form:
1632 * $parent => $currentarticle
1633 * @access public
1635 function getParentCategories() {
1636 global $wgContLang,$wgUser;
1638 $titlekey = $this->getArticleId();
1639 $sk =& $wgUser->getSkin();
1640 $parents = array();
1641 $dbr =& wfGetDB( DB_SLAVE );
1642 $cur = $dbr->tableName( 'cur' );
1643 $categorylinks = $dbr->tableName( 'categorylinks' );
1645 # NEW SQL
1646 $sql = "SELECT * FROM categorylinks"
1647 ." WHERE cl_from='$titlekey'"
1648 ." AND cl_from <> '0'"
1649 ." ORDER BY cl_sortkey";
1651 $res = $dbr->query ( $sql ) ;
1653 if($dbr->numRows($res) > 0) {
1654 while ( $x = $dbr->fetchObject ( $res ) )
1655 //$data[] = Title::newFromText($wgContLang->getNSText ( NS_CATEGORY ).':'.$x->cl_to);
1656 $data[$wgContLang->getNSText ( NS_CATEGORY ).':'.$x->cl_to] = $this->getFullText();
1657 $dbr->freeResult ( $res ) ;
1658 } else {
1659 $data = '';
1661 return $data;
1665 * Go through all parent categories of this Title
1666 * @return array
1667 * @access public
1669 function getCategorieBrowser() {
1670 $parents = $this->getParentCategories();
1672 if($parents != '') {
1673 foreach($parents as $parent => $current)
1675 $nt = Title::newFromText($parent);
1676 $stack[$parent] = $nt->getCategorieBrowser();
1678 return $stack;
1679 } else {
1680 return array();
1686 * Get an associative array for selecting this title from
1687 * the "cur" table
1689 * @return array
1690 * @access public
1692 function curCond() {
1693 return array( 'cur_namespace' => $this->mNamespace, 'cur_title' => $this->mDbkeyform );
1697 * Get an associative array for selecting this title from the
1698 * "old" table
1700 * @return array
1701 * @access public
1703 function oldCond() {
1704 return array( 'old_namespace' => $this->mNamespace, 'old_title' => $this->mDbkeyform );
1708 * Get the revision ID of the previous revision
1710 * @param integer $revision Revision ID. Get the revision that was before this one.
1711 * @return interger $oldrevision|false
1713 function getPreviousRevisionID( $revision ) {
1714 $dbr =& wfGetDB( DB_SLAVE );
1715 return $dbr->selectField( 'old', 'old_id',
1716 'old_title=' . $dbr->addQuotes( $this->getDBkey() ) .
1717 ' AND old_namespace=' . IntVal( $this->getNamespace() ) .
1718 ' AND old_id<' . IntVal( $revision ) . ' ORDER BY old_id DESC' );
1722 * Get the revision ID of the next revision
1724 * @param integer $revision Revision ID. Get the revision that was after this one.
1725 * @return interger $oldrevision|false
1727 function getNextRevisionID( $revision ) {
1728 $dbr =& wfGetDB( DB_SLAVE );
1729 return $dbr->selectField( 'old', 'old_id',
1730 'old_title=' . $dbr->addQuotes( $this->getDBkey() ) .
1731 ' AND old_namespace=' . IntVal( $this->getNamespace() ) .
1732 ' AND old_id>' . IntVal( $revision ) . ' ORDER BY old_id' );