On Category: pages, show images as thumbnail gallery, and not
[mediawiki.git] / includes / Title.php
blob94da807b4189b911d84d4bf93ec816aefe76e8e8
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 # check that length of title is < cur_title size
144 $dbr =& wfGetDB( DB_SLAVE );
145 $maxSize = $dbr->textFieldSize( 'cur', 'cur_title' );
146 if ( $maxSize != -1 && strlen( $t->mDbkeyform ) > $maxSize ) {
147 return NULL;
150 return $t;
151 } else {
152 return NULL;
157 * Create a new Title from an article ID
158 * @todo This is inefficiently implemented, the cur row is requested
159 * but not used for anything else
160 * @param int $id the cur_id corresponding to the Title to create
161 * @return Title the new object, or NULL on an error
162 * @access public
164 /* static */ function newFromID( $id ) {
165 $fname = 'Title::newFromID';
166 $dbr =& wfGetDB( DB_SLAVE );
167 $row = $dbr->getArray( 'cur', array( 'cur_namespace', 'cur_title' ),
168 array( 'cur_id' => $id ), $fname );
169 if ( $row !== false ) {
170 $title = Title::makeTitle( $row->cur_namespace, $row->cur_title );
171 } else {
172 $title = NULL;
174 return $title;
178 * Create a new Title from a namespace index and a DB key.
179 * It's assumed that $ns and $title are *valid*, for instance when
180 * they came directly from the database or a special page name.
181 * @param int $ns the namespace of the article
182 * @param string $title the unprefixed database key form
183 * @return Title the new object
184 * @static
185 * @access public
187 /* static */ function &makeTitle( $ns, $title ) {
188 $t =& new Title();
189 $t->mInterwiki = '';
190 $t->mFragment = '';
191 $t->mNamespace = $ns;
192 $t->mDbkeyform = $title;
193 $t->mArticleID = ( $ns >= 0 ) ? -1 : 0;
194 $t->mUrlform = wfUrlencode( $title );
195 $t->mTextform = str_replace( '_', ' ', $title );
196 return $t;
200 * Create a new Title frrom a namespace index and a DB key.
201 * The parameters will be checked for validity, which is a bit slower
202 * than makeTitle() but safer for user-provided data.
203 * @param int $ns the namespace of the article
204 * @param string $title the database key form
205 * @return Title the new object, or NULL on an error
206 * @static
207 * @access public
209 /* static */ function makeTitleSafe( $ns, $title ) {
210 $t = new Title();
211 $t->mDbkeyform = Title::makeName( $ns, $title );
212 if( $t->secureAndSplit() ) {
213 return $t;
214 } else {
215 return NULL;
220 * Create a new Title for the Main Page
221 * @static
222 * @return Title the new object
223 * @access public
225 /* static */ function newMainPage() {
226 return Title::newFromText( wfMsgForContent( 'mainpage' ) );
230 * Create a new Title for a redirect
231 * @param string $text the redirect title text
232 * @return Title the new object, or NULL if the text is not a
233 * valid redirect
234 * @static
235 * @access public
237 /* static */ function newFromRedirect( $text ) {
238 global $wgMwRedir;
239 $rt = NULL;
240 if ( $wgMwRedir->matchStart( $text ) ) {
241 if ( preg_match( '/\\[\\[([^\\]\\|]+)[\\]\\|]/', $text, $m ) ) {
242 # categories are escaped using : for example one can enter:
243 # #REDIRECT [[:Category:Music]]. Need to remove it.
244 if ( substr($m[1],0,1) == ':') {
245 # We don't want to keep the ':'
246 $m[1] = substr( $m[1], 1 );
249 $rt = Title::newFromText( $m[1] );
250 # Disallow redirects to Special:Userlogout
251 if ( !is_null($rt) && $rt->getNamespace() == NS_SPECIAL && preg_match( '/^Userlogout/i', $rt->getText() ) ) {
252 $rt = NULL;
256 return $rt;
259 #----------------------------------------------------------------------------
260 # Static functions
261 #----------------------------------------------------------------------------
264 * Get the prefixed DB key associated with an ID
265 * @param int $id the cur_id of the article
266 * @return Title an object representing the article, or NULL
267 * if no such article was found
268 * @static
269 * @access public
271 /* static */ function nameOf( $id ) {
272 $fname = 'Title::nameOf';
273 $dbr =& wfGetDB( DB_SLAVE );
275 $s = $dbr->getArray( 'cur', array( 'cur_namespace','cur_title' ), array( 'cur_id' => $id ), $fname );
276 if ( $s === false ) { return NULL; }
278 $n = Title::makeName( $s->cur_namespace, $s->cur_title );
279 return $n;
283 * Get a regex character class describing the legal characters in a link
284 * @return string the list of characters, not delimited
285 * @static
286 * @access public
288 /* static */ function legalChars() {
289 # Missing characters:
290 # * []|# Needed for link syntax
291 # * % and + are corrupted by Apache when they appear in the path
293 # % seems to work though
295 # The problem with % is that URLs are double-unescaped: once by Apache's
296 # path conversion code, and again by PHP. So %253F, for example, becomes "?".
297 # Our code does not double-escape to compensate for this, indeed double escaping
298 # would break if the double-escaped title was passed in the query string
299 # rather than the path. This is a minor security issue because articles can be
300 # created such that they are hard to view or edit. -- TS
302 # Theoretically 0x80-0x9F of ISO 8859-1 should be disallowed, but
303 # this breaks interlanguage links
305 $set = " %!\"$&'()*,\\-.\\/0-9:;=?@A-Z\\\\^_`a-z~\\x80-\\xFF";
306 return $set;
310 * Get a string representation of a title suitable for
311 * including in a search index
313 * @param int $ns a namespace index
314 * @param string $title text-form main part
315 * @return string a stripped-down title string ready for the
316 * search index
318 /* static */ function indexTitle( $ns, $title ) {
319 global $wgDBminWordLen, $wgContLang;
320 require_once( 'SearchEngine.php' );
322 $lc = SearchEngine::legalSearchChars() . '&#;';
323 $t = $wgContLang->stripForSearch( $title );
324 $t = preg_replace( "/[^{$lc}]+/", ' ', $t );
325 $t = strtolower( $t );
327 # Handle 's, s'
328 $t = preg_replace( "/([{$lc}]+)'s( |$)/", "\\1 \\1's ", $t );
329 $t = preg_replace( "/([{$lc}]+)s'( |$)/", "\\1s ", $t );
331 $t = preg_replace( "/\\s+/", ' ', $t );
333 if ( $ns == Namespace::getImage() ) {
334 $t = preg_replace( "/ (png|gif|jpg|jpeg|ogg)$/", "", $t );
336 return trim( $t );
340 * Make a prefixed DB key from a DB key and a namespace index
341 * @param int $ns numerical representation of the namespace
342 * @param string $title the DB key form the title
343 * @return string the prefixed form of the title
345 /* static */ function makeName( $ns, $title ) {
346 global $wgContLang;
348 $n = $wgContLang->getNsText( $ns );
349 if ( '' == $n ) { return $title; }
350 else { return $n.':'.$title; }
354 * Returns the URL associated with an interwiki prefix
355 * @param string $key the interwiki prefix (e.g. "MeatBall")
356 * @return the associated URL, containing "$1", which should be
357 * replaced by an article title
358 * @static (arguably)
359 * @access public
361 function getInterwikiLink( $key ) {
362 global $wgMemc, $wgDBname, $wgInterwikiExpiry, $wgTitleInterwikiCache;
363 $fname = 'Title::getInterwikiLink';
364 $k = $wgDBname.':interwiki:'.$key;
366 if( array_key_exists( $k, $wgTitleInterwikiCache ) )
367 return $wgTitleInterwikiCache[$k]->iw_url;
369 $s = $wgMemc->get( $k );
370 # Ignore old keys with no iw_local
371 if( $s && isset( $s->iw_local ) ) {
372 $wgTitleInterwikiCache[$k] = $s;
373 return $s->iw_url;
375 $dbr =& wfGetDB( DB_SLAVE );
376 $res = $dbr->select( 'interwiki', array( 'iw_url', 'iw_local' ), array( 'iw_prefix' => $key ), $fname );
377 if(!$res) return '';
379 $s = $dbr->fetchObject( $res );
380 if(!$s) {
381 # Cache non-existence: create a blank object and save it to memcached
382 $s = (object)false;
383 $s->iw_url = '';
384 $s->iw_local = 0;
386 $wgMemc->set( $k, $s, $wgInterwikiExpiry );
387 $wgTitleInterwikiCache[$k] = $s;
388 return $s->iw_url;
392 * Determine whether the object refers to a page within
393 * this project.
395 * @return bool TRUE if this is an in-project interwiki link
396 * or a wikilink, FALSE otherwise
397 * @access public
399 function isLocal() {
400 global $wgTitleInterwikiCache, $wgDBname;
402 if ( $this->mInterwiki != '' ) {
403 # Make sure key is loaded into cache
404 $this->getInterwikiLink( $this->mInterwiki );
405 $k = $wgDBname.':interwiki:' . $this->mInterwiki;
406 return (bool)($wgTitleInterwikiCache[$k]->iw_local);
407 } else {
408 return true;
413 * Update the cur_touched field for an array of title objects
414 * @todo Inefficient unless the IDs are already loaded into the
415 * link cache
416 * @param array $titles an array of Title objects to be touched
417 * @param string $timestamp the timestamp to use instead of the
418 * default current time
419 * @static
420 * @access public
422 /* static */ function touchArray( $titles, $timestamp = '' ) {
423 if ( count( $titles ) == 0 ) {
424 return;
426 $dbw =& wfGetDB( DB_MASTER );
427 if ( $timestamp == '' ) {
428 $timestamp = $dbw->timestamp();
430 $cur = $dbw->tableName( 'cur' );
431 $sql = "UPDATE $cur SET cur_touched='{$timestamp}' WHERE cur_id IN (";
432 $first = true;
434 foreach ( $titles as $title ) {
435 if ( ! $first ) {
436 $sql .= ',';
438 $first = false;
439 $sql .= $title->getArticleID();
441 $sql .= ')';
442 if ( ! $first ) {
443 $dbw->query( $sql, 'Title::touchArray' );
447 #----------------------------------------------------------------------------
448 # Other stuff
449 #----------------------------------------------------------------------------
451 /** Simple accessors */
453 * Get the text form (spaces not underscores) of the main part
454 * @return string
455 * @access public
457 function getText() { return $this->mTextform; }
459 * Get the URL-encoded form of the main part
460 * @return string
461 * @access public
463 function getPartialURL() { return $this->mUrlform; }
465 * Get the main part with underscores
466 * @return string
467 * @access public
469 function getDBkey() { return $this->mDbkeyform; }
471 * Get the namespace index, i.e. one of the NS_xxxx constants
472 * @return int
473 * @access public
475 function getNamespace() { return $this->mNamespace; }
477 * Set the namespace index
478 * @param int $n the namespace index, one of the NS_xxxx constants
479 * @access public
481 function setNamespace( $n ) { $this->mNamespace = $n; }
483 * Get the interwiki prefix (or null string)
484 * @return string
485 * @access public
487 function getInterwiki() { return $this->mInterwiki; }
489 * Get the Title fragment (i.e. the bit after the #)
490 * @return string
491 * @access public
493 function getFragment() { return $this->mFragment; }
495 * Get the default namespace index, for when there is no namespace
496 * @return int
497 * @access public
499 function getDefaultNamespace() { return $this->mDefaultNamespace; }
502 * Get title for search index
503 * @return string a stripped-down title string ready for the
504 * search index
506 function getIndexTitle() {
507 return Title::indexTitle( $this->mNamespace, $this->mTextform );
511 * Get the prefixed database key form
512 * @return string the prefixed title, with underscores and
513 * any interwiki and namespace prefixes
514 * @access public
516 function getPrefixedDBkey() {
517 $s = $this->prefix( $this->mDbkeyform );
518 $s = str_replace( ' ', '_', $s );
519 return $s;
523 * Get the prefixed title with spaces.
524 * This is the form usually used for display
525 * @return string the prefixed title, with spaces
526 * @access public
528 function getPrefixedText() {
529 if ( empty( $this->mPrefixedText ) ) {
530 $s = $this->prefix( $this->mTextform );
531 $s = str_replace( '_', ' ', $s );
532 $this->mPrefixedText = $s;
534 return $this->mPrefixedText;
538 * Get the prefixed title with spaces, plus any fragment
539 * (part beginning with '#')
540 * @return string the prefixed title, with spaces and
541 * the fragment, including '#'
542 * @access public
544 function getFullText() {
545 $text = $this->getPrefixedText();
546 if( '' != $this->mFragment ) {
547 $text .= '#' . $this->mFragment;
549 return $text;
553 * Get a URL-encoded title (not an actual URL) including interwiki
554 * @return string the URL-encoded form
555 * @access public
557 function getPrefixedURL() {
558 $s = $this->prefix( $this->mDbkeyform );
559 $s = str_replace( ' ', '_', $s );
561 $s = wfUrlencode ( $s ) ;
563 # Cleaning up URL to make it look nice -- is this safe?
564 $s = preg_replace( '/%3[Aa]/', ':', $s );
565 $s = preg_replace( '/%2[Ff]/', '/', $s );
566 $s = str_replace( '%28', '(', $s );
567 $s = str_replace( '%29', ')', $s );
569 return $s;
573 * Get a real URL referring to this title, with interwiki link and
574 * fragment
576 * @param string $query an optional query string, not used
577 * for interwiki links
578 * @return string the URL
579 * @access public
581 function getFullURL( $query = '' ) {
582 global $wgContLang, $wgArticlePath, $wgServer, $wgScript;
584 if ( '' == $this->mInterwiki ) {
585 $p = $wgArticlePath;
586 return $wgServer . $this->getLocalUrl( $query );
587 } else {
588 $baseUrl = $this->getInterwikiLink( $this->mInterwiki );
589 $namespace = $wgContLang->getNsText( $this->mNamespace );
590 if ( '' != $namespace ) {
591 # Can this actually happen? Interwikis shouldn't be parsed.
592 $namepace .= ':';
594 $url = str_replace( '$1', $namespace . $this->mUrlform, $baseUrl );
595 if ( '' != $this->mFragment ) {
596 $url .= '#' . $this->mFragment;
598 return $url;
603 * @deprecated
605 function getURL() {
606 die( 'Call to obsolete obsolete function Title::getURL()' );
610 * Get a URL with no fragment or server name
611 * @param string $query an optional query string; if not specified,
612 * $wgArticlePath will be used.
613 * @return string the URL
614 * @access public
616 function getLocalURL( $query = '' ) {
617 global $wgLang, $wgArticlePath, $wgScript;
619 if ( $this->isExternal() ) {
620 return $this->getFullURL();
623 $dbkey = wfUrlencode( $this->getPrefixedDBkey() );
624 if ( $query == '' ) {
625 $url = str_replace( '$1', $dbkey, $wgArticlePath );
626 } else {
627 if ( $query == '-' ) {
628 $query = '';
630 if ( $wgScript != '' ) {
631 $url = "{$wgScript}?title={$dbkey}&{$query}";
632 } else {
633 # Top level wiki
634 $url = "/{$dbkey}?{$query}";
637 return $url;
641 * Get an HTML-escaped version of the URL form, suitable for
642 * using in a link, without a server name or fragment
643 * @param string $query an optional query string
644 * @return string the URL
645 * @access public
647 function escapeLocalURL( $query = '' ) {
648 return htmlspecialchars( $this->getLocalURL( $query ) );
652 * Get an HTML-escaped version of the URL form, suitable for
653 * using in a link, including the server name and fragment
655 * @return string the URL
656 * @param string $query an optional query string
657 * @access public
659 function escapeFullURL( $query = '' ) {
660 return htmlspecialchars( $this->getFullURL( $query ) );
663 /**
664 * Get the URL form for an internal link.
665 * - Used in various Squid-related code, in case we have a different
666 * internal hostname for the server from the exposed one.
668 * @param string $query an optional query string
669 * @return string the URL
670 * @access public
672 function getInternalURL( $query = '' ) {
673 global $wgInternalServer;
674 return $wgInternalServer . $this->getLocalURL( $query );
678 * Get the edit URL for this Title
679 * @return string the URL, or a null string if this is an
680 * interwiki link
681 * @access public
683 function getEditURL() {
684 global $wgServer, $wgScript;
686 if ( '' != $this->mInterwiki ) { return ''; }
687 $s = $this->getLocalURL( 'action=edit' );
689 return $s;
693 * Get the HTML-escaped displayable text form.
694 * Used for the title field in <a> tags.
695 * @return string the text, including any prefixes
696 * @access public
698 function getEscapedText() {
699 return htmlspecialchars( $this->getPrefixedText() );
703 * Is this Title interwiki?
704 * @return boolean
705 * @access public
707 function isExternal() { return ( '' != $this->mInterwiki ); }
710 * Does the title correspond to a protected article?
711 * @return boolean
712 * @access public
714 function isProtected() {
715 if ( -1 == $this->mNamespace ) { return true; }
716 $a = $this->getRestrictions();
717 if ( in_array( 'sysop', $a ) ) { return true; }
718 return false;
722 * Is the page a log page, i.e. one where the history is messed up by
723 * LogPage.php? This used to be used for suppressing diff links in
724 * recent changes, but now that's done by setting a flag in the
725 * recentchanges table. Hence, this probably is no longer used.
727 * @deprecated
728 * @access public
730 function isLog() {
731 if ( $this->mNamespace != Namespace::getWikipedia() ) {
732 return false;
734 if ( ( 0 == strcmp( wfMsg( 'uploadlogpage' ), $this->mDbkeyform ) ) ||
735 ( 0 == strcmp( wfMsg( 'dellogpage' ), $this->mDbkeyform ) ) ) {
736 return true;
738 return false;
742 * Is $wgUser is watching this page?
743 * @return boolean
744 * @access public
746 function userIsWatching() {
747 global $wgUser;
749 if ( -1 == $this->mNamespace ) { return false; }
750 if ( 0 == $wgUser->getID() ) { return false; }
752 return $wgUser->isWatched( $this );
756 * Can $wgUser edit this page?
757 * @return boolean
758 * @access public
760 function userCanEdit() {
761 global $wgUser;
762 if ( -1 == $this->mNamespace ) { return false; }
763 if ( NS_MEDIAWIKI == $this->mNamespace && !$wgUser->isSysop() ) { return false; }
764 # if ( 0 == $this->getArticleID() ) { return false; }
765 if ( $this->mDbkeyform == '_' ) { return false; }
766 # protect global styles and js
767 if ( NS_MEDIAWIKI == $this->mNamespace
768 && preg_match("/\\.(css|js)$/", $this->mTextform )
769 && !$wgUser->isSysop() )
770 { return false; }
771 //if ( $this->isCssJsSubpage() and !$this->userCanEditCssJsSubpage() ) { return false; }
772 # protect css/js subpages of user pages
773 # XXX: this might be better using restrictions
774 # XXX: Find a way to work around the php bug that prevents using $this->userCanEditCssJsSubpage() from working
775 if( Namespace::getUser() == $this->mNamespace
776 and preg_match("/\\.(css|js)$/", $this->mTextform )
777 and !$wgUser->isSysop()
778 and !preg_match('/^'.preg_quote($wgUser->getName(), '/').'/', $this->mTextform) )
779 { return false; }
780 $ur = $wgUser->getRights();
781 foreach ( $this->getRestrictions() as $r ) {
782 if ( '' != $r && ( ! in_array( $r, $ur ) ) ) {
783 return false;
786 return true;
790 * Can $wgUser read this page?
791 * @return boolean
792 * @access public
794 function userCanRead() {
795 global $wgUser;
796 global $wgWhitelistRead;
798 if( 0 != $wgUser->getID() ) return true;
799 if( !is_array( $wgWhitelistRead ) ) return true;
801 $name = $this->getPrefixedText();
802 if( in_array( $name, $wgWhitelistRead ) ) return true;
804 # Compatibility with old settings
805 if( $this->getNamespace() == NS_MAIN ) {
806 if( in_array( ':' . $name, $wgWhitelistRead ) ) return true;
808 return false;
812 * Is this a .css or .js subpage of a user page?
813 * @return bool
814 * @access public
816 function isCssJsSubpage() {
817 return ( Namespace::getUser() == $this->mNamespace and preg_match("/\\.(css|js)$/", $this->mTextform ) );
820 * Is this a .css subpage of a user page?
821 * @return bool
822 * @access public
824 function isCssSubpage() {
825 return ( Namespace::getUser() == $this->mNamespace and preg_match("/\\.css$/", $this->mTextform ) );
828 * Is this a .js subpage of a user page?
829 * @return bool
830 * @access public
832 function isJsSubpage() {
833 return ( Namespace::getUser() == $this->mNamespace and preg_match("/\\.js$/", $this->mTextform ) );
836 * Protect css/js subpages of user pages: can $wgUser edit
837 * this page?
839 * @return boolean
840 * @todo XXX: this might be better using restrictions
841 * @access public
843 function userCanEditCssJsSubpage() {
844 global $wgUser;
845 return ( $wgUser->isSysop() or preg_match('/^'.preg_quote($wgUser->getName()).'/', $this->mTextform) );
849 * Accessor/initialisation for mRestrictions
850 * @return array the array of groups allowed to edit this article
851 * @access public
853 function getRestrictions() {
854 $id = $this->getArticleID();
855 if ( 0 == $id ) { return array(); }
857 if ( ! $this->mRestrictionsLoaded ) {
858 $dbr =& wfGetDB( DB_SLAVE );
859 $res = $dbr->getField( 'cur', 'cur_restrictions', 'cur_id='.$id );
860 $this->mRestrictions = explode( ',', trim( $res ) );
861 $this->mRestrictionsLoaded = true;
863 return $this->mRestrictions;
867 * Is there a version of this page in the deletion archive?
868 * @return int the number of archived revisions
869 * @access public
871 function isDeleted() {
872 $fname = 'Title::isDeleted';
873 $dbr =& wfGetDB( DB_SLAVE );
874 $n = $dbr->getField( 'archive', 'COUNT(*)', array( 'ar_namespace' => $this->getNamespace(),
875 'ar_title' => $this->getDBkey() ), $fname );
876 return (int)$n;
880 * Get the article ID for this Title from the link cache,
881 * adding it if necessary
882 * @param int $flags a bit field; may be GAID_FOR_UPDATE to select
883 * for update
884 * @return int the ID
885 * @access public
887 function getArticleID( $flags = 0 ) {
888 global $wgLinkCache;
890 if ( $flags & GAID_FOR_UPDATE ) {
891 $oldUpdate = $wgLinkCache->forUpdate( true );
892 $this->mArticleID = $wgLinkCache->addLinkObj( $this );
893 $wgLinkCache->forUpdate( $oldUpdate );
894 } else {
895 if ( -1 == $this->mArticleID ) {
896 $this->mArticleID = $wgLinkCache->addLinkObj( $this );
899 return $this->mArticleID;
903 * This clears some fields in this object, and clears any associated
904 * keys in the "bad links" section of $wgLinkCache.
906 * - This is called from Article::insertNewArticle() to allow
907 * loading of the new cur_id. It's also called from
908 * Article::doDeleteArticle()
910 * @param int $newid the new Article ID
911 * @access public
913 function resetArticleID( $newid ) {
914 global $wgLinkCache;
915 $wgLinkCache->clearBadLink( $this->getPrefixedDBkey() );
917 if ( 0 == $newid ) { $this->mArticleID = -1; }
918 else { $this->mArticleID = $newid; }
919 $this->mRestrictionsLoaded = false;
920 $this->mRestrictions = array();
924 * Updates cur_touched for this page; called from LinksUpdate.php
925 * @return bool true if the update succeded
926 * @access public
928 function invalidateCache() {
929 $now = wfTimestampNow();
930 $dbw =& wfGetDB( DB_MASTER );
931 $success = $dbw->updateArray( 'cur',
932 array( /* SET */
933 'cur_touched' => $dbw->timestamp()
934 ), array( /* WHERE */
935 'cur_namespace' => $this->getNamespace() ,
936 'cur_title' => $this->getDBkey()
937 ), 'Title::invalidateCache'
939 return $success;
943 * Prefix some arbitrary text with the namespace or interwiki prefix
944 * of this object
946 * @param string $name the text
947 * @return string the prefixed text
948 * @access private
950 /* private */ function prefix( $name ) {
951 global $wgContLang;
953 $p = '';
954 if ( '' != $this->mInterwiki ) {
955 $p = $this->mInterwiki . ':';
957 if ( 0 != $this->mNamespace ) {
958 $p .= $wgContLang->getNsText( $this->mNamespace ) . ':';
960 return $p . $name;
964 * Secure and split - main initialisation function for this object
966 * Assumes that mDbkeyform has been set, and is urldecoded
967 * and uses underscores, but not otherwise munged. This function
968 * removes illegal characters, splits off the interwiki and
969 * namespace prefixes, sets the other forms, and canonicalizes
970 * everything.
971 * @return bool true on success
972 * @access private
974 /* private */ function secureAndSplit()
976 global $wgContLang, $wgLocalInterwiki, $wgCapitalLinks;
977 $fname = 'Title::secureAndSplit';
978 wfProfileIn( $fname );
980 static $imgpre = false;
981 static $rxTc = false;
983 # Initialisation
984 if ( $imgpre === false ) {
985 $imgpre = ':' . $wgContLang->getNsText( Namespace::getImage() ) . ':';
986 # % is needed as well
987 $rxTc = '/[^' . Title::legalChars() . ']/';
990 $this->mInterwiki = $this->mFragment = '';
991 $this->mNamespace = $this->mDefaultNamespace; # Usually NS_MAIN
993 # Clean up whitespace
995 $t = preg_replace( "/[\\s_]+/", '_', $this->mDbkeyform );
996 $t = preg_replace( '/^_*(.*?)_*$/', '$1', $t );
998 if ( '' == $t ) {
999 wfProfileOut( $fname );
1000 return false;
1003 global $wgUseLatin1;
1004 if( !$wgUseLatin1 && false !== strpos( $t, UTF8_REPLACEMENT ) ) {
1005 # Contained illegal UTF-8 sequences or forbidden Unicode chars.
1006 wfProfileOut( $fname );
1007 return false;
1010 $this->mDbkeyform = $t;
1011 $done = false;
1013 # :Image: namespace
1014 if ( 0 == strncasecmp( $imgpre, $t, strlen( $imgpre ) ) ) {
1015 $t = substr( $t, 1 );
1018 # Initial colon indicating main namespace
1019 if ( ':' == $t{0} ) {
1020 $r = substr( $t, 1 );
1021 $this->mNamespace = NS_MAIN;
1022 } else {
1023 # Namespace or interwiki prefix
1024 if ( preg_match( "/^(.+?)_*:_*(.*)$/", $t, $m ) ) {
1025 #$p = strtolower( $m[1] );
1026 $p = $m[1];
1027 $lowerNs = strtolower( $p );
1028 if ( $ns = Namespace::getCanonicalIndex( $lowerNs ) ) {
1029 # Canonical namespace
1030 $t = $m[2];
1031 $this->mNamespace = $ns;
1032 } elseif ( $ns = $wgContLang->getNsIndex( $lowerNs )) {
1033 # Ordinary namespace
1034 $t = $m[2];
1035 $this->mNamespace = $ns;
1036 } elseif ( $this->getInterwikiLink( $p ) ) {
1037 # Interwiki link
1038 $t = $m[2];
1039 $this->mInterwiki = $p;
1041 if ( !preg_match( "/^([A-Za-z0-9_\\x80-\\xff]+):(.*)$/", $t, $m ) ) {
1042 $done = true;
1043 } elseif($this->mInterwiki != $wgLocalInterwiki) {
1044 $done = true;
1048 $r = $t;
1051 # Redundant interwiki prefix to the local wiki
1052 if ( 0 == strcmp( $this->mInterwiki, $wgLocalInterwiki ) ) {
1053 $this->mInterwiki = '';
1055 # We already know that some pages won't be in the database!
1057 if ( '' != $this->mInterwiki || -1 == $this->mNamespace ) {
1058 $this->mArticleID = 0;
1060 $f = strstr( $r, '#' );
1061 if ( false !== $f ) {
1062 $this->mFragment = substr( $f, 1 );
1063 $r = substr( $r, 0, strlen( $r ) - strlen( $f ) );
1064 # remove whitespace again: prevents "Foo_bar_#"
1065 # becoming "Foo_bar_"
1066 $r = preg_replace( '/_*$/', '', $r );
1069 # Reject illegal characters.
1071 if( preg_match( $rxTc, $r ) ) {
1072 wfProfileOut( $fname );
1073 return false;
1076 # "." and ".." conflict with the directories of those namesa
1077 if ( strpos( $r, '.' ) !== false &&
1078 ( $r === '.' || $r === '..' ||
1079 strpos( $r, './' ) === 0 ||
1080 strpos( $r, '../' ) === 0 ||
1081 strpos( $r, '/./' ) !== false ||
1082 strpos( $r, '/../' ) !== false ) )
1084 wfProfileOut( $fname );
1085 return false;
1088 # Initial capital letter
1089 if( $wgCapitalLinks && $this->mInterwiki == '') {
1090 $t = $wgContLang->ucfirst( $r );
1091 } else {
1092 $t = $r;
1095 # Fill fields
1096 $this->mDbkeyform = $t;
1097 $this->mUrlform = wfUrlencode( $t );
1099 $this->mTextform = str_replace( '_', ' ', $t );
1101 wfProfileOut( $fname );
1102 return true;
1106 * Get a Title object associated with the talk page of this article
1107 * @return Title the object for the talk page
1108 * @access public
1110 function getTalkPage() {
1111 return Title::makeTitle( Namespace::getTalk( $this->getNamespace() ), $this->getDBkey() );
1115 * Get a title object associated with the subject page of this
1116 * talk page
1118 * @return Title the object for the subject page
1119 * @access public
1121 function getSubjectPage() {
1122 return Title::makeTitle( Namespace::getSubject( $this->getNamespace() ), $this->getDBkey() );
1126 * Get an array of Title objects linking to this Title
1127 * - Also stores the IDs in the link cache.
1129 * @param string $options may be FOR UPDATE
1130 * @return array the Title objects linking here
1131 * @access public
1133 function getLinksTo( $options = '' ) {
1134 global $wgLinkCache;
1135 $id = $this->getArticleID();
1137 if ( $options ) {
1138 $db =& wfGetDB( DB_MASTER );
1139 } else {
1140 $db =& wfGetDB( DB_SLAVE );
1142 $cur = $db->tableName( 'cur' );
1143 $links = $db->tableName( 'links' );
1145 $sql = "SELECT cur_namespace,cur_title,cur_id FROM $cur,$links WHERE l_from=cur_id AND l_to={$id} $options";
1146 $res = $db->query( $sql, 'Title::getLinksTo' );
1147 $retVal = array();
1148 if ( $db->numRows( $res ) ) {
1149 while ( $row = $db->fetchObject( $res ) ) {
1150 if ( $titleObj = Title::makeTitle( $row->cur_namespace, $row->cur_title ) ) {
1151 $wgLinkCache->addGoodLink( $row->cur_id, $titleObj->getPrefixedDBkey() );
1152 $retVal[] = $titleObj;
1156 $db->freeResult( $res );
1157 return $retVal;
1161 * Get an array of Title objects linking to this non-existent title.
1162 * - Also stores the IDs in the link cache.
1164 * @param string $options may be FOR UPDATE
1165 * @return array the Title objects linking here
1166 * @access public
1168 function getBrokenLinksTo( $options = '' ) {
1169 global $wgLinkCache;
1171 if ( $options ) {
1172 $db =& wfGetDB( DB_MASTER );
1173 } else {
1174 $db =& wfGetDB( DB_SLAVE );
1176 $cur = $db->tableName( 'cur' );
1177 $brokenlinks = $db->tableName( 'brokenlinks' );
1178 $encTitle = $db->strencode( $this->getPrefixedDBkey() );
1180 $sql = "SELECT cur_namespace,cur_title,cur_id FROM $brokenlinks,$cur " .
1181 "WHERE bl_from=cur_id AND bl_to='$encTitle' $options";
1182 $res = $db->query( $sql, "Title::getBrokenLinksTo" );
1183 $retVal = array();
1184 if ( $db->numRows( $res ) ) {
1185 while ( $row = $db->fetchObject( $res ) ) {
1186 $titleObj = Title::makeTitle( $row->cur_namespace, $row->cur_title );
1187 $wgLinkCache->addGoodLink( $row->cur_id, $titleObj->getPrefixedDBkey() );
1188 $retVal[] = $titleObj;
1191 $db->freeResult( $res );
1192 return $retVal;
1196 * Get a list of URLs to purge from the Squid cache when this
1197 * page changes
1199 * @return array the URLs
1200 * @access public
1202 function getSquidURLs() {
1203 return array(
1204 $this->getInternalURL(),
1205 $this->getInternalURL( 'action=history' )
1210 * Move this page without authentication
1211 * @param Title &$nt the new page Title
1212 * @access public
1214 function moveNoAuth( &$nt ) {
1215 return $this->moveTo( $nt, false );
1219 * Move a title to a new location
1220 * @param Title &$nt the new title
1221 * @param bool $auth indicates whether $wgUser's permissions
1222 * should be checked
1223 * @return mixed true on success, message name on failure
1224 * @access public
1226 function moveTo( &$nt, $auth = true ) {
1227 if( !$this or !$nt ) {
1228 return 'badtitletext';
1231 $fname = 'Title::move';
1232 $oldid = $this->getArticleID();
1233 $newid = $nt->getArticleID();
1235 if ( strlen( $nt->getDBkey() ) < 1 ) {
1236 return 'articleexists';
1238 if ( ( ! Namespace::isMovable( $this->getNamespace() ) ) ||
1239 ( '' == $this->getDBkey() ) ||
1240 ( '' != $this->getInterwiki() ) ||
1241 ( !$oldid ) ||
1242 ( ! Namespace::isMovable( $nt->getNamespace() ) ) ||
1243 ( '' == $nt->getDBkey() ) ||
1244 ( '' != $nt->getInterwiki() ) ) {
1245 return 'badarticleerror';
1248 if ( $auth && ( !$this->userCanEdit() || !$nt->userCanEdit() ) ) {
1249 return 'protectedpage';
1252 # The move is allowed only if (1) the target doesn't exist, or
1253 # (2) the target is a redirect to the source, and has no history
1254 # (so we can undo bad moves right after they're done).
1256 if ( 0 != $newid ) { # Target exists; check for validity
1257 if ( ! $this->isValidMoveTarget( $nt ) ) {
1258 return 'articleexists';
1260 $this->moveOverExistingRedirect( $nt );
1261 } else { # Target didn't exist, do normal move.
1262 $this->moveToNewTitle( $nt, $newid );
1265 # Fixing category links (those without piped 'alternate' names) to be sorted under the new title
1267 $dbw =& wfGetDB( DB_MASTER );
1268 $sql = "UPDATE categorylinks SET cl_sortkey=" . $dbw->addQuotes( $nt->getPrefixedText() ) .
1269 " WHERE cl_from=" . $dbw->addQuotes( $this->getArticleID() ) .
1270 " AND cl_sortkey=" . $dbw->addQuotes( $this->getPrefixedText() );
1271 $dbw->query( $sql, 'SpecialMovepage::doSubmit' );
1273 # Update watchlists
1275 $oldnamespace = $this->getNamespace() & ~1;
1276 $newnamespace = $nt->getNamespace() & ~1;
1277 $oldtitle = $this->getDBkey();
1278 $newtitle = $nt->getDBkey();
1280 if( $oldnamespace != $newnamespace || $oldtitle != $newtitle ) {
1281 WatchedItem::duplicateEntries( $this, $nt );
1284 # Update search engine
1285 $u = new SearchUpdate( $oldid, $nt->getPrefixedDBkey() );
1286 $u->doUpdate();
1287 $u = new SearchUpdate( $newid, $this->getPrefixedDBkey(), '' );
1288 $u->doUpdate();
1290 return true;
1294 * Move page to a title which is at present a redirect to the
1295 * source page
1297 * @param Title &$nt the page to move to, which should currently
1298 * be a redirect
1299 * @access private
1301 /* private */ function moveOverExistingRedirect( &$nt ) {
1302 global $wgUser, $wgLinkCache, $wgUseSquid, $wgMwRedir;
1303 $fname = 'Title::moveOverExistingRedirect';
1304 $comment = wfMsg( '1movedto2', $this->getPrefixedText(), $nt->getPrefixedText() );
1306 $now = wfTimestampNow();
1307 $won = wfInvertTimestamp( $now );
1308 $newid = $nt->getArticleID();
1309 $oldid = $this->getArticleID();
1310 $dbw =& wfGetDB( DB_MASTER );
1311 $links = $dbw->tableName( 'links' );
1313 # Change the name of the target page:
1314 $dbw->updateArray( 'cur',
1315 /* SET */ array(
1316 'cur_touched' => $dbw->timestamp($now),
1317 'cur_namespace' => $nt->getNamespace(),
1318 'cur_title' => $nt->getDBkey()
1320 /* WHERE */ array( 'cur_id' => $oldid ),
1321 $fname
1323 $wgLinkCache->clearLink( $nt->getPrefixedDBkey() );
1325 # Repurpose the old redirect. We don't save it to history since
1326 # by definition if we've got here it's rather uninteresting.
1328 $redirectText = $wgMwRedir->getSynonym( 0 ) . ' [[' . $nt->getPrefixedText() . "]]\n";
1329 $dbw->updateArray( 'cur',
1330 /* SET */ array(
1331 'cur_touched' => $dbw->timestamp($now),
1332 'cur_timestamp' => $dbw->timestamp($now),
1333 'inverse_timestamp' => $won,
1334 'cur_namespace' => $this->getNamespace(),
1335 'cur_title' => $this->getDBkey(),
1336 'cur_text' => $wgMwRedir->getSynonym( 0 ) . ' [[' . $nt->getPrefixedText() . "]]\n",
1337 'cur_comment' => $comment,
1338 'cur_user' => $wgUser->getID(),
1339 'cur_minor_edit' => 0,
1340 'cur_counter' => 0,
1341 'cur_restrictions' => '',
1342 'cur_user_text' => $wgUser->getName(),
1343 'cur_is_redirect' => 1,
1344 'cur_is_new' => 1
1346 /* WHERE */ array( 'cur_id' => $newid ),
1347 $fname
1350 $wgLinkCache->clearLink( $this->getPrefixedDBkey() );
1352 # Fix the redundant names for the past revisions of the target page.
1353 # The redirect should have no old revisions.
1354 $dbw->updateArray(
1355 /* table */ 'old',
1356 /* SET */ array(
1357 'old_namespace' => $nt->getNamespace(),
1358 'old_title' => $nt->getDBkey(),
1360 /* WHERE */ array(
1361 'old_namespace' => $this->getNamespace(),
1362 'old_title' => $this->getDBkey(),
1364 $fname
1367 RecentChange::notifyMoveOverRedirect( $now, $this, $nt, $wgUser, $comment );
1369 # Swap links
1371 # Load titles and IDs
1372 $linksToOld = $this->getLinksTo( 'FOR UPDATE' );
1373 $linksToNew = $nt->getLinksTo( 'FOR UPDATE' );
1375 # Delete them all
1376 $sql = "DELETE FROM $links WHERE l_to=$oldid OR l_to=$newid";
1377 $dbw->query( $sql, $fname );
1379 # Reinsert
1380 if ( count( $linksToOld ) || count( $linksToNew )) {
1381 $sql = "INSERT INTO $links (l_from,l_to) VALUES ";
1382 $first = true;
1384 # Insert links to old title
1385 foreach ( $linksToOld as $linkTitle ) {
1386 if ( $first ) {
1387 $first = false;
1388 } else {
1389 $sql .= ',';
1391 $id = $linkTitle->getArticleID();
1392 $sql .= "($id,$newid)";
1395 # Insert links to new title
1396 foreach ( $linksToNew as $linkTitle ) {
1397 if ( $first ) {
1398 $first = false;
1399 } else {
1400 $sql .= ',';
1402 $id = $linkTitle->getArticleID();
1403 $sql .= "($id, $oldid)";
1406 $dbw->query( $sql, DB_MASTER, $fname );
1409 # Now, we record the link from the redirect to the new title.
1410 # It should have no other outgoing links...
1411 $dbw->delete( 'links', array( 'l_from' => $newid ) );
1412 $dbw->insertArray( 'links', array( 'l_from' => $newid, 'l_to' => $oldid ) );
1414 # Clear linkscc
1415 LinkCache::linksccClearLinksTo( $oldid );
1416 LinkCache::linksccClearLinksTo( $newid );
1418 # Purge squid
1419 if ( $wgUseSquid ) {
1420 $urls = array_merge( $nt->getSquidURLs(), $this->getSquidURLs() );
1421 $u = new SquidUpdate( $urls );
1422 $u->doUpdate();
1427 * Move page to non-existing title.
1428 * @param Title &$nt the new Title
1429 * @param int &$newid set to be the new article ID
1430 * @access private
1432 /* private */ function moveToNewTitle( &$nt, &$newid ) {
1433 global $wgUser, $wgLinkCache, $wgUseSquid;
1434 $fname = 'MovePageForm::moveToNewTitle';
1435 $comment = wfMsg( '1movedto2', $this->getPrefixedText(), $nt->getPrefixedText() );
1437 $newid = $nt->getArticleID();
1438 $oldid = $this->getArticleID();
1439 $dbw =& wfGetDB( DB_MASTER );
1440 $now = $dbw->timestamp();
1441 $won = wfInvertTimestamp( wfTimestamp(TS_MW,$now) );
1442 wfSeedRandom();
1443 $rand = number_format( mt_rand() / mt_getrandmax(), 12, '.', '' );
1445 # Rename cur entry
1446 $dbw->updateArray( 'cur',
1447 /* SET */ array(
1448 'cur_touched' => $now,
1449 'cur_namespace' => $nt->getNamespace(),
1450 'cur_title' => $nt->getDBkey()
1452 /* WHERE */ array( 'cur_id' => $oldid ),
1453 $fname
1456 $wgLinkCache->clearLink( $nt->getPrefixedDBkey() );
1458 # Insert redirect
1459 $dbw->insertArray( 'cur', array(
1460 'cur_id' => $dbw->nextSequenceValue('cur_cur_id_seq'),
1461 'cur_namespace' => $this->getNamespace(),
1462 'cur_title' => $this->getDBkey(),
1463 'cur_comment' => $comment,
1464 'cur_user' => $wgUser->getID(),
1465 'cur_user_text' => $wgUser->getName(),
1466 'cur_timestamp' => $now,
1467 'inverse_timestamp' => $won,
1468 'cur_touched' => $now,
1469 'cur_is_redirect' => 1,
1470 'cur_random' => $rand,
1471 'cur_is_new' => 1,
1472 'cur_text' => "#REDIRECT [[" . $nt->getPrefixedText() . "]]\n" ), $fname
1474 $newid = $dbw->insertId();
1475 $wgLinkCache->clearLink( $this->getPrefixedDBkey() );
1477 # Rename old entries
1478 $dbw->updateArray(
1479 /* table */ 'old',
1480 /* SET */ array(
1481 'old_namespace' => $nt->getNamespace(),
1482 'old_title' => $nt->getDBkey()
1484 /* WHERE */ array(
1485 'old_namespace' => $this->getNamespace(),
1486 'old_title' => $this->getDBkey()
1487 ), $fname
1490 # Record in RC
1491 RecentChange::notifyMoveToNew( $now, $this, $nt, $wgUser, $comment );
1493 # Purge squid and linkscc as per article creation
1494 Article::onArticleCreate( $nt );
1496 # Any text links to the old title must be reassigned to the redirect
1497 $dbw->updateArray( 'links', array( 'l_to' => $newid ), array( 'l_to' => $oldid ), $fname );
1498 LinkCache::linksccClearLinksTo( $oldid );
1500 # Record the just-created redirect's linking to the page
1501 $dbw->insertArray( 'links', array( 'l_from' => $newid, 'l_to' => $oldid ), $fname );
1503 # Non-existent target may have had broken links to it; these must
1504 # now be removed and made into good links.
1505 $update = new LinksUpdate( $oldid, $nt->getPrefixedDBkey() );
1506 $update->fixBrokenLinks();
1508 # Purge old title from squid
1509 # The new title, and links to the new title, are purged in Article::onArticleCreate()
1510 $titles = $nt->getLinksTo();
1511 if ( $wgUseSquid ) {
1512 $urls = $this->getSquidURLs();
1513 foreach ( $titles as $linkTitle ) {
1514 $urls[] = $linkTitle->getInternalURL();
1516 $u = new SquidUpdate( $urls );
1517 $u->doUpdate();
1522 * Checks if $this can be moved to a given Title
1523 * - Selects for update, so don't call it unless you mean business
1525 * @param Title &$nt the new title to check
1526 * @access public
1528 function isValidMoveTarget( $nt ) {
1529 $fname = 'Title::isValidMoveTarget';
1530 $dbw =& wfGetDB( DB_MASTER );
1532 # Is it a redirect?
1533 $id = $nt->getArticleID();
1534 $obj = $dbw->getArray( 'cur', array( 'cur_is_redirect','cur_text' ),
1535 array( 'cur_id' => $id ), $fname, 'FOR UPDATE' );
1537 if ( !$obj || 0 == $obj->cur_is_redirect ) {
1538 # Not a redirect
1539 return false;
1542 # Does the redirect point to the source?
1543 if ( preg_match( "/\\[\\[\\s*([^\\]\\|]*)]]/", $obj->cur_text, $m ) ) {
1544 $redirTitle = Title::newFromText( $m[1] );
1545 if( !is_object( $redirTitle ) ||
1546 $redirTitle->getPrefixedDBkey() != $this->getPrefixedDBkey() ) {
1547 return false;
1551 # Does the article have a history?
1552 $row = $dbw->getArray( 'old', array( 'old_id' ),
1553 array(
1554 'old_namespace' => $nt->getNamespace(),
1555 'old_title' => $nt->getDBkey()
1556 ), $fname, 'FOR UPDATE'
1559 # Return true if there was no history
1560 return $row === false;
1564 * Create a redirect; fails if the title already exists; does
1565 * not notify RC
1567 * @param Title $dest the destination of the redirect
1568 * @param string $comment the comment string describing the move
1569 * @return bool true on success
1570 * @access public
1572 function createRedirect( $dest, $comment ) {
1573 global $wgUser;
1574 if ( $this->getArticleID() ) {
1575 return false;
1578 $fname = 'Title::createRedirect';
1579 $dbw =& wfGetDB( DB_MASTER );
1580 $now = wfTimestampNow();
1581 $won = wfInvertTimestamp( $now );
1582 $seqVal = $dbw->nextSequenceValue( 'cur_cur_id_seq' );
1584 $dbw->insertArray( 'cur', array(
1585 'cur_id' => $seqVal,
1586 'cur_namespace' => $this->getNamespace(),
1587 'cur_title' => $this->getDBkey(),
1588 'cur_comment' => $comment,
1589 'cur_user' => $wgUser->getID(),
1590 'cur_user_text' => $wgUser->getName(),
1591 'cur_timestamp' => $now,
1592 'inverse_timestamp' => $won,
1593 'cur_touched' => $now,
1594 'cur_is_redirect' => 1,
1595 'cur_is_new' => 1,
1596 'cur_text' => "#REDIRECT [[" . $dest->getPrefixedText() . "]]\n"
1597 ), $fname );
1598 $newid = $dbw->insertId();
1599 $this->resetArticleID( $newid );
1601 # Link table
1602 if ( $dest->getArticleID() ) {
1603 $dbw->insertArray( 'links',
1604 array(
1605 'l_to' => $dest->getArticleID(),
1606 'l_from' => $newid
1607 ), $fname
1609 } else {
1610 $dbw->insertArray( 'brokenlinks',
1611 array(
1612 'bl_to' => $dest->getPrefixedDBkey(),
1613 'bl_from' => $newid
1614 ), $fname
1618 Article::onArticleCreate( $this );
1619 return true;
1623 * Get categories to which this Title belongs and return an array of
1624 * categories' names.
1626 * @return array an array of parents in the form:
1627 * $parent => $currentarticle
1628 * @access public
1630 function getParentCategories() {
1631 global $wgContLang,$wgUser;
1633 $titlekey = $this->getArticleId();
1634 $sk =& $wgUser->getSkin();
1635 $parents = array();
1636 $dbr =& wfGetDB( DB_SLAVE );
1637 $cur = $dbr->tableName( 'cur' );
1638 $categorylinks = $dbr->tableName( 'categorylinks' );
1640 # NEW SQL
1641 $sql = "SELECT * FROM categorylinks"
1642 ." WHERE cl_from='$titlekey'"
1643 ." AND cl_from <> '0'"
1644 ." ORDER BY cl_sortkey";
1646 $res = $dbr->query ( $sql ) ;
1648 if($dbr->numRows($res) > 0) {
1649 while ( $x = $dbr->fetchObject ( $res ) )
1650 //$data[] = Title::newFromText($wgContLang->getNSText ( NS_CATEGORY ).':'.$x->cl_to);
1651 $data[$wgContLang->getNSText ( NS_CATEGORY ).':'.$x->cl_to] = $this->getFullText();
1652 $dbr->freeResult ( $res ) ;
1653 } else {
1654 $data = '';
1656 return $data;
1660 * Go through all parent categories of this Title
1661 * @return array
1662 * @access public
1664 function getCategorieBrowser() {
1665 $parents = $this->getParentCategories();
1667 if($parents != '') {
1668 foreach($parents as $parent => $current)
1670 $nt = Title::newFromText($parent);
1671 $stack[$parent] = $nt->getCategorieBrowser();
1673 return $stack;
1674 } else {
1675 return array();
1681 * Get an associative array for selecting this title from
1682 * the "cur" table
1684 * @return array
1685 * @access public
1687 function curCond() {
1688 return array( 'cur_namespace' => $this->mNamespace, 'cur_title' => $this->mDbkeyform );
1692 * Get an associative array for selecting this title from the
1693 * "old" table
1695 * @return array
1696 * @access public
1698 function oldCond() {
1699 return array( 'old_namespace' => $this->mNamespace, 'old_title' => $this->mDbkeyform );