Remove insecure mode (causes infinite loops due to protection against Internet Explor...
[lhc/web/wiklou.git] / includes / Title.php
1 <?php
2 /**
3 * $Id$
4 * See title.doc
5 *
6 * @package MediaWiki
7 */
8
9 /** */
10 require_once( 'normal/UtfNormal.php' );
11
12 $wgTitleInterwikiCache = array();
13 define ( 'GAID_FOR_UPDATE', 1 );
14
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.
19 *
20 * @package MediaWiki
21 */
22 class Title {
23 /**
24 * All member variables should be considered private
25 * Please use the accessor functions
26 */
27
28 /**#@+
29 * @access private
30 */
31
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 /**#@-*/
46
47
48 /**
49 * Constructor
50 * @access private
51 */
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;
60 }
61
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
70 */
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;
78 }
79
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.
83 *
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
92 */
93 /* static */ function newFromText( $text, $defaultNamespace = 0 ) {
94 $fname = 'Title::newFromText';
95 wfProfileIn( $fname );
96
97 if( is_object( $text ) ) {
98 wfDebugDieBacktrace( 'Called with object instead of string.' );
99 }
100 global $wgInputEncoding;
101 $text = do_html_entity_decode( $text, ENT_COMPAT, $wgInputEncoding );
102
103 $text = wfMungeToUtf8( $text );
104
105
106 # What was this for? TS 2004-03-03
107 # $text = urldecode( $text );
108
109 $t = new Title();
110 $t->mDbkeyform = str_replace( ' ', '_', $text );
111 $t->mDefaultNamespace = $defaultNamespace;
112
113 wfProfileOut( $fname );
114 if ( !is_object( $t ) ) {
115 var_dump( debug_backtrace() );
116 }
117 if( $t->secureAndSplit() ) {
118 return $t;
119 } else {
120 return NULL;
121 }
122 }
123
124 /**
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
131 */
132 /* static */ function newFromURL( $url ) {
133 global $wgLang, $wgServer;
134 $t = new Title();
135
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 );
140
141 $t->mDbkeyform = str_replace( ' ', '_', $s );
142 if( $t->secureAndSplit() ) {
143 return $t;
144 } else {
145 return NULL;
146 }
147 }
148
149 /**
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
156 */
157 /* static */ function newFromID( $id ) {
158 $fname = 'Title::newFromID';
159 $dbr =& wfGetDB( DB_SLAVE );
160 $row = $dbr->selectRow( '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;
166 }
167 return $title;
168 }
169
170 /**
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
179 */
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;
190 }
191
192 /**
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
201 */
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;
209 }
210 }
211
212 /**
213 * Create a new Title for the Main Page
214 * @static
215 * @return Title the new object
216 * @access public
217 */
218 /* static */ function newMainPage() {
219 return Title::newFromText( wfMsgForContent( 'mainpage' ) );
220 }
221
222 /**
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
229 */
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 );
240 }
241
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;
246 }
247 }
248 }
249 return $rt;
250 }
251
252 #----------------------------------------------------------------------------
253 # Static functions
254 #----------------------------------------------------------------------------
255
256 /**
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
263 */
264 /* static */ function nameOf( $id ) {
265 $fname = 'Title::nameOf';
266 $dbr =& wfGetDB( DB_SLAVE );
267
268 $s = $dbr->selectRow( 'cur', array( 'cur_namespace','cur_title' ), array( 'cur_id' => $id ), $fname );
269 if ( $s === false ) { return NULL; }
270
271 $n = Title::makeName( $s->cur_namespace, $s->cur_title );
272 return $n;
273 }
274
275 /**
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
280 */
281 /* static */ function legalChars() {
282 # Missing characters:
283 # * []|# Needed for link syntax
284 # * % and + are corrupted by Apache when they appear in the path
285 #
286 # % seems to work though
287 #
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
294 #
295 # Theoretically 0x80-0x9F of ISO 8859-1 should be disallowed, but
296 # this breaks interlanguage links
297
298 $set = " %!\"$&'()*,\\-.\\/0-9:;=?@A-Z\\\\^_`a-z~\\x80-\\xFF";
299 return $set;
300 }
301
302 /**
303 * Get a string representation of a title suitable for
304 * including in a search index
305 *
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
310 */
311 /* static */ function indexTitle( $ns, $title ) {
312 global $wgDBminWordLen, $wgContLang;
313 require_once( 'SearchEngine.php' );
314
315 $lc = SearchEngine::legalSearchChars() . '&#;';
316 $t = $wgContLang->stripForSearch( $title );
317 $t = preg_replace( "/[^{$lc}]+/", ' ', $t );
318 $t = strtolower( $t );
319
320 # Handle 's, s'
321 $t = preg_replace( "/([{$lc}]+)'s( |$)/", "\\1 \\1's ", $t );
322 $t = preg_replace( "/([{$lc}]+)s'( |$)/", "\\1s ", $t );
323
324 $t = preg_replace( "/\\s+/", ' ', $t );
325
326 if ( $ns == Namespace::getImage() ) {
327 $t = preg_replace( "/ (png|gif|jpg|jpeg|ogg)$/", "", $t );
328 }
329 return trim( $t );
330 }
331
332 /*
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
337 */
338 /* static */ function makeName( $ns, $title ) {
339 global $wgContLang;
340
341 $n = $wgContLang->getNsText( $ns );
342 if ( '' == $n ) { return $title; }
343 else { return $n.':'.$title; }
344 }
345
346 /**
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
353 */
354 function getInterwikiLink( $key ) {
355 global $wgMemc, $wgDBname, $wgInterwikiExpiry, $wgTitleInterwikiCache;
356 $fname = 'Title::getInterwikiLink';
357 $k = $wgDBname.':interwiki:'.$key;
358
359 if( array_key_exists( $k, $wgTitleInterwikiCache ) )
360 return $wgTitleInterwikiCache[$k]->iw_url;
361
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;
367 }
368 $dbr =& wfGetDB( DB_SLAVE );
369 $res = $dbr->select( 'interwiki', array( 'iw_url', 'iw_local' ), array( 'iw_prefix' => $key ), $fname );
370 if(!$res) return '';
371
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;
378 }
379 $wgMemc->set( $k, $s, $wgInterwikiExpiry );
380 $wgTitleInterwikiCache[$k] = $s;
381 return $s->iw_url;
382 }
383
384 /**
385 * Determine whether the object refers to a page within
386 * this project.
387 *
388 * @return bool TRUE if this is an in-project interwiki link
389 * or a wikilink, FALSE otherwise
390 * @access public
391 */
392 function isLocal() {
393 global $wgTitleInterwikiCache, $wgDBname;
394
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;
402 }
403 }
404
405 /**
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
414 */
415 /* static */ function touchArray( $titles, $timestamp = '' ) {
416 if ( count( $titles ) == 0 ) {
417 return;
418 }
419 $dbw =& wfGetDB( DB_MASTER );
420 if ( $timestamp == '' ) {
421 $timestamp = $dbw->timestamp();
422 }
423 $cur = $dbw->tableName( 'cur' );
424 $sql = "UPDATE $cur SET cur_touched='{$timestamp}' WHERE cur_id IN (";
425 $first = true;
426
427 foreach ( $titles as $title ) {
428 if ( ! $first ) {
429 $sql .= ',';
430 }
431 $first = false;
432 $sql .= $title->getArticleID();
433 }
434 $sql .= ')';
435 if ( ! $first ) {
436 $dbw->query( $sql, 'Title::touchArray' );
437 }
438 }
439
440 #----------------------------------------------------------------------------
441 # Other stuff
442 #----------------------------------------------------------------------------
443
444 /** Simple accessors */
445 /**
446 * Get the text form (spaces not underscores) of the main part
447 * @return string
448 * @access public
449 */
450 function getText() { return $this->mTextform; }
451 /**
452 * Get the URL-encoded form of the main part
453 * @return string
454 * @access public
455 */
456 function getPartialURL() { return $this->mUrlform; }
457 /**
458 * Get the main part with underscores
459 * @return string
460 * @access public
461 */
462 function getDBkey() { return $this->mDbkeyform; }
463 /**
464 * Get the namespace index, i.e. one of the NS_xxxx constants
465 * @return int
466 * @access public
467 */
468 function getNamespace() { return $this->mNamespace; }
469 /**
470 * Set the namespace index
471 * @param int $n the namespace index, one of the NS_xxxx constants
472 * @access public
473 */
474 function setNamespace( $n ) { $this->mNamespace = IntVal( $n ); }
475 /**
476 * Get the interwiki prefix (or null string)
477 * @return string
478 * @access public
479 */
480 function getInterwiki() { return $this->mInterwiki; }
481 /**
482 * Get the Title fragment (i.e. the bit after the #)
483 * @return string
484 * @access public
485 */
486 function getFragment() { return $this->mFragment; }
487 /**
488 * Get the default namespace index, for when there is no namespace
489 * @return int
490 * @access public
491 */
492 function getDefaultNamespace() { return $this->mDefaultNamespace; }
493
494 /**
495 * Get title for search index
496 * @return string a stripped-down title string ready for the
497 * search index
498 */
499 function getIndexTitle() {
500 return Title::indexTitle( $this->mNamespace, $this->mTextform );
501 }
502
503 /**
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
508 */
509 function getPrefixedDBkey() {
510 $s = $this->prefix( $this->mDbkeyform );
511 $s = str_replace( ' ', '_', $s );
512 return $s;
513 }
514
515 /**
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
520 */
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;
527 }
528 return $this->mPrefixedText;
529 }
530
531 /**
532 * Get the prefixed title with spaces, plus any fragment
533 * (part beginning with '#')
534 * @return string the prefixed title, with spaces and
535 * the fragment, including '#'
536 * @access public
537 */
538 function getFullText() {
539 global $wgContLang;
540 $text = $this->getPrefixedText();
541 if( '' != $this->mFragment ) {
542 $text .= '#' . $this->mFragment;
543 }
544 return $text;
545 }
546
547 /**
548 * Get a URL-encoded title (not an actual URL) including interwiki
549 * @return string the URL-encoded form
550 * @access public
551 */
552 function getPrefixedURL() {
553 $s = $this->prefix( $this->mDbkeyform );
554 $s = str_replace( ' ', '_', $s );
555
556 $s = wfUrlencode ( $s ) ;
557
558 # Cleaning up URL to make it look nice -- is this safe?
559 $s = preg_replace( '/%3[Aa]/', ':', $s );
560 $s = preg_replace( '/%2[Ff]/', '/', $s );
561 $s = str_replace( '%28', '(', $s );
562 $s = str_replace( '%29', ')', $s );
563
564 return $s;
565 }
566
567 /**
568 * Get a real URL referring to this title, with interwiki link and
569 * fragment
570 *
571 * @param string $query an optional query string, not used
572 * for interwiki links
573 * @return string the URL
574 * @access public
575 */
576 function getFullURL( $query = '' ) {
577 global $wgContLang, $wgArticlePath, $wgServer, $wgScript;
578
579 if ( '' == $this->mInterwiki ) {
580 $p = $wgArticlePath;
581 return $wgServer . $this->getLocalUrl( $query );
582 } else {
583 $baseUrl = $this->getInterwikiLink( $this->mInterwiki );
584 $namespace = $wgContLang->getNsText( $this->mNamespace );
585 if ( '' != $namespace ) {
586 # Can this actually happen? Interwikis shouldn't be parsed.
587 $namepace .= ':';
588 }
589 $url = str_replace( '$1', $namespace . $this->mUrlform, $baseUrl );
590 if ( '' != $this->mFragment ) {
591 $url .= '#' . $this->mFragment;
592 }
593 return $url;
594 }
595 }
596
597 /**
598 * @deprecated
599 */
600 function getURL() {
601 die( 'Call to obsolete obsolete function Title::getURL()' );
602 }
603
604 /**
605 * Get a URL with no fragment or server name
606 * @param string $query an optional query string; if not specified,
607 * $wgArticlePath will be used.
608 * @return string the URL
609 * @access public
610 */
611 function getLocalURL( $query = '' ) {
612 global $wgLang, $wgArticlePath, $wgScript;
613
614 if ( $this->isExternal() ) {
615 return $this->getFullURL();
616 }
617
618 $dbkey = wfUrlencode( $this->getPrefixedDBkey() );
619 if ( $query == '' ) {
620 $url = str_replace( '$1', $dbkey, $wgArticlePath );
621 } else {
622 if ( $query == '-' ) {
623 $query = '';
624 }
625 $url = "{$wgScript}?title={$dbkey}&{$query}";
626 }
627 return $url;
628 }
629
630 /**
631 * Get an HTML-escaped version of the URL form, suitable for
632 * using in a link, without a server name or fragment
633 * @param string $query an optional query string
634 * @return string the URL
635 * @access public
636 */
637 function escapeLocalURL( $query = '' ) {
638 return htmlspecialchars( $this->getLocalURL( $query ) );
639 }
640
641 /**
642 * Get an HTML-escaped version of the URL form, suitable for
643 * using in a link, including the server name and fragment
644 *
645 * @return string the URL
646 * @param string $query an optional query string
647 * @access public
648 */
649 function escapeFullURL( $query = '' ) {
650 return htmlspecialchars( $this->getFullURL( $query ) );
651 }
652
653 /**
654 * Get the URL form for an internal link.
655 * - Used in various Squid-related code, in case we have a different
656 * internal hostname for the server from the exposed one.
657 *
658 * @param string $query an optional query string
659 * @return string the URL
660 * @access public
661 */
662 function getInternalURL( $query = '' ) {
663 global $wgInternalServer;
664 return $wgInternalServer . $this->getLocalURL( $query );
665 }
666
667 /**
668 * Get the edit URL for this Title
669 * @return string the URL, or a null string if this is an
670 * interwiki link
671 * @access public
672 */
673 function getEditURL() {
674 global $wgServer, $wgScript;
675
676 if ( '' != $this->mInterwiki ) { return ''; }
677 $s = $this->getLocalURL( 'action=edit' );
678
679 return $s;
680 }
681
682 /**
683 * Get the HTML-escaped displayable text form.
684 * Used for the title field in <a> tags.
685 * @return string the text, including any prefixes
686 * @access public
687 */
688 function getEscapedText() {
689 return htmlspecialchars( $this->getPrefixedText() );
690 }
691
692 /**
693 * Is this Title interwiki?
694 * @return boolean
695 * @access public
696 */
697 function isExternal() { return ( '' != $this->mInterwiki ); }
698
699 /**
700 * Does the title correspond to a protected article?
701 * @return boolean
702 * @access public
703 */
704 function isProtected() {
705 if ( -1 == $this->mNamespace ) { return true; }
706 $a = $this->getRestrictions();
707 if ( in_array( 'sysop', $a ) ) { return true; }
708 return false;
709 }
710
711 /**
712 * Is the page a log page, i.e. one where the history is messed up by
713 * LogPage.php? This used to be used for suppressing diff links in
714 * recent changes, but now that's done by setting a flag in the
715 * recentchanges table. Hence, this probably is no longer used.
716 *
717 * @deprecated
718 * @access public
719 */
720 function isLog() {
721 if ( $this->mNamespace != Namespace::getWikipedia() ) {
722 return false;
723 }
724 if ( ( 0 == strcmp( wfMsg( 'uploadlogpage' ), $this->mDbkeyform ) ) ||
725 ( 0 == strcmp( wfMsg( 'dellogpage' ), $this->mDbkeyform ) ) ) {
726 return true;
727 }
728 return false;
729 }
730
731 /**
732 * Is $wgUser is watching this page?
733 * @return boolean
734 * @access public
735 */
736 function userIsWatching() {
737 global $wgUser;
738
739 if ( -1 == $this->mNamespace ) { return false; }
740 if ( 0 == $wgUser->getID() ) { return false; }
741
742 return $wgUser->isWatched( $this );
743 }
744
745 /**
746 * Can $wgUser edit this page?
747 * @return boolean
748 * @access public
749 */
750 function userCanEdit() {
751 global $wgUser;
752 if( NS_SPECIAL == $this->mNamespace ) {
753 return false;
754 }
755 if( NS_MEDIAWIKI == $this->mNamespace &&
756 !$wgUser->isAllowed('editinterface') ) {
757 return false;
758 }
759 if( $this->mDbkeyform == '_' ) {
760 # FIXME: Is this necessary? Shouldn't be allowed anyway...
761 return false;
762 }
763
764 # protect global styles and js
765 if ( NS_MEDIAWIKI == $this->mNamespace
766 && preg_match("/\\.(css|js)$/", $this->mTextform )
767 && !$wgUser->isAllowed('editinterface') ) {
768 return false;
769 }
770
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( NS_USER == $this->mNamespace
775 && preg_match("/\\.(css|js)$/", $this->mTextform )
776 && !$wgUser->isAllowed('editinterface')
777 && !preg_match('/^'.preg_quote($wgUser->getName(), '/').'\//', $this->mTextform) ) {
778 return false;
779 }
780
781 foreach( $this->getRestrictions() as $right ) {
782 if( '' != $right && !$wgUser->isAllowed( $right ) ) {
783 return false;
784 }
785 }
786 return true;
787 }
788
789 /**
790 * Can $wgUser read this page?
791 * @return boolean
792 * @access public
793 */
794 function userCanRead() {
795 global $wgUser;
796
797 if( $wgUser->isAllowed('read') ) {
798 return true;
799 } else {
800 global $wgWhitelistRead;
801
802 /** If anon users can create an account,
803 they need to reach the login page first! */
804 if( $wgUser->isAllowed( 'createaccount' )
805 && $this->mId == NS_SPECIAL
806 && $this->getText() == 'Userlogin' ) {
807 return true;
808 }
809
810 /** some pages are explicitly allowed */
811 $name = $this->getPrefixedText();
812 if( in_array( $name, $wgWhitelistRead ) ) {
813 return true;
814 }
815
816 # Compatibility with old settings
817 if( $this->getNamespace() == NS_MAIN ) {
818 if( in_array( ':' . $name, $wgWhitelistRead ) ) {
819 return true;
820 }
821 }
822 }
823 return false;
824 }
825
826 /**
827 * Is this a .css or .js subpage of a user page?
828 * @return bool
829 * @access public
830 */
831 function isCssJsSubpage() {
832 return ( Namespace::getUser() == $this->mNamespace and preg_match("/\\.(css|js)$/", $this->mTextform ) );
833 }
834 /**
835 * Is this a .css subpage of a user page?
836 * @return bool
837 * @access public
838 */
839 function isCssSubpage() {
840 return ( Namespace::getUser() == $this->mNamespace and preg_match("/\\.css$/", $this->mTextform ) );
841 }
842 /**
843 * Is this a .js subpage of a user page?
844 * @return bool
845 * @access public
846 */
847 function isJsSubpage() {
848 return ( Namespace::getUser() == $this->mNamespace and preg_match("/\\.js$/", $this->mTextform ) );
849 }
850 /**
851 * Protect css/js subpages of user pages: can $wgUser edit
852 * this page?
853 *
854 * @return boolean
855 * @todo XXX: this might be better using restrictions
856 * @access public
857 */
858 function userCanEditCssJsSubpage() {
859 global $wgUser;
860 return ( $wgUser->isAllowed('editinterface') or preg_match('/^'.preg_quote($wgUser->getName(), '/').'\//', $this->mTextform) );
861 }
862
863 /**
864 * Accessor/initialisation for mRestrictions
865 * @return array the array of groups allowed to edit this article
866 * @access public
867 */
868 function getRestrictions() {
869 $id = $this->getArticleID();
870 if ( 0 == $id ) { return array(); }
871
872 if ( ! $this->mRestrictionsLoaded ) {
873 $dbr =& wfGetDB( DB_SLAVE );
874 $res = $dbr->selectField( 'cur', 'cur_restrictions', 'cur_id='.$id );
875 $this->mRestrictions = explode( ',', trim( $res ) );
876 $this->mRestrictionsLoaded = true;
877 }
878 return $this->mRestrictions;
879 }
880
881 /**
882 * Is there a version of this page in the deletion archive?
883 * @return int the number of archived revisions
884 * @access public
885 */
886 function isDeleted() {
887 $fname = 'Title::isDeleted';
888 $dbr =& wfGetDB( DB_SLAVE );
889 $n = $dbr->selectField( 'archive', 'COUNT(*)', array( 'ar_namespace' => $this->getNamespace(),
890 'ar_title' => $this->getDBkey() ), $fname );
891 return (int)$n;
892 }
893
894 /**
895 * Get the article ID for this Title from the link cache,
896 * adding it if necessary
897 * @param int $flags a bit field; may be GAID_FOR_UPDATE to select
898 * for update
899 * @return int the ID
900 * @access public
901 */
902 function getArticleID( $flags = 0 ) {
903 global $wgLinkCache;
904
905 if ( $flags & GAID_FOR_UPDATE ) {
906 $oldUpdate = $wgLinkCache->forUpdate( true );
907 $this->mArticleID = $wgLinkCache->addLinkObj( $this );
908 $wgLinkCache->forUpdate( $oldUpdate );
909 } else {
910 if ( -1 == $this->mArticleID ) {
911 $this->mArticleID = $wgLinkCache->addLinkObj( $this );
912 }
913 }
914 return $this->mArticleID;
915 }
916
917 /**
918 * This clears some fields in this object, and clears any associated
919 * keys in the "bad links" section of $wgLinkCache.
920 *
921 * - This is called from Article::insertNewArticle() to allow
922 * loading of the new cur_id. It's also called from
923 * Article::doDeleteArticle()
924 *
925 * @param int $newid the new Article ID
926 * @access public
927 */
928 function resetArticleID( $newid ) {
929 global $wgLinkCache;
930 $wgLinkCache->clearBadLink( $this->getPrefixedDBkey() );
931
932 if ( 0 == $newid ) { $this->mArticleID = -1; }
933 else { $this->mArticleID = $newid; }
934 $this->mRestrictionsLoaded = false;
935 $this->mRestrictions = array();
936 }
937
938 /**
939 * Updates cur_touched for this page; called from LinksUpdate.php
940 * @return bool true if the update succeded
941 * @access public
942 */
943 function invalidateCache() {
944 $now = wfTimestampNow();
945 $dbw =& wfGetDB( DB_MASTER );
946 $success = $dbw->update( 'cur',
947 array( /* SET */
948 'cur_touched' => $dbw->timestamp()
949 ), array( /* WHERE */
950 'cur_namespace' => $this->getNamespace() ,
951 'cur_title' => $this->getDBkey()
952 ), 'Title::invalidateCache'
953 );
954 return $success;
955 }
956
957 /**
958 * Prefix some arbitrary text with the namespace or interwiki prefix
959 * of this object
960 *
961 * @param string $name the text
962 * @return string the prefixed text
963 * @access private
964 */
965 /* private */ function prefix( $name ) {
966 global $wgContLang;
967
968 $p = '';
969 if ( '' != $this->mInterwiki ) {
970 $p = $this->mInterwiki . ':';
971 }
972 if ( 0 != $this->mNamespace ) {
973 $p .= $wgContLang->getNsText( $this->mNamespace ) . ':';
974 }
975 return $p . $name;
976 }
977
978 /**
979 * Secure and split - main initialisation function for this object
980 *
981 * Assumes that mDbkeyform has been set, and is urldecoded
982 * and uses underscores, but not otherwise munged. This function
983 * removes illegal characters, splits off the interwiki and
984 * namespace prefixes, sets the other forms, and canonicalizes
985 * everything.
986 * @return bool true on success
987 * @access private
988 */
989 /* private */ function secureAndSplit()
990 {
991 global $wgContLang, $wgLocalInterwiki, $wgCapitalLinks;
992 $fname = 'Title::secureAndSplit';
993 wfProfileIn( $fname );
994
995 static $imgpre = false;
996 static $rxTc = false;
997
998 # Initialisation
999 if ( $imgpre === false ) {
1000 $imgpre = ':' . $wgContLang->getNsText( Namespace::getImage() ) . ':';
1001 # % is needed as well
1002 $rxTc = '/[^' . Title::legalChars() . ']|%[0-9A-Fa-f]{2}/';
1003 }
1004
1005 $this->mInterwiki = $this->mFragment = '';
1006 $this->mNamespace = $this->mDefaultNamespace; # Usually NS_MAIN
1007
1008 # Clean up whitespace
1009 #
1010 $t = preg_replace( "/[\\s_]+/", '_', $this->mDbkeyform );
1011 $t = preg_replace( '/^_*(.*?)_*$/', '$1', $t );
1012
1013 if ( '' == $t ) {
1014 wfProfileOut( $fname );
1015 return false;
1016 }
1017
1018 global $wgUseLatin1;
1019 if( !$wgUseLatin1 && false !== strpos( $t, UTF8_REPLACEMENT ) ) {
1020 # Contained illegal UTF-8 sequences or forbidden Unicode chars.
1021 wfProfileOut( $fname );
1022 return false;
1023 }
1024
1025 $this->mDbkeyform = $t;
1026 $done = false;
1027
1028 # :Image: namespace
1029 if ( 0 == strncasecmp( $imgpre, $t, strlen( $imgpre ) ) ) {
1030 $t = substr( $t, 1 );
1031 }
1032
1033 # Initial colon indicating main namespace
1034 if ( ':' == $t{0} ) {
1035 $r = substr( $t, 1 );
1036 $this->mNamespace = NS_MAIN;
1037 } else {
1038 # Namespace or interwiki prefix
1039 if ( preg_match( "/^(.+?)_*:_*(.*)$/", $t, $m ) ) {
1040 #$p = strtolower( $m[1] );
1041 $p = $m[1];
1042 $lowerNs = strtolower( $p );
1043 if ( $ns = Namespace::getCanonicalIndex( $lowerNs ) ) {
1044 # Canonical namespace
1045 $t = $m[2];
1046 $this->mNamespace = $ns;
1047 } elseif ( $ns = $wgContLang->getNsIndex( $lowerNs )) {
1048 # Ordinary namespace
1049 $t = $m[2];
1050 $this->mNamespace = $ns;
1051 } elseif ( $this->getInterwikiLink( $p ) ) {
1052 # Interwiki link
1053 $t = $m[2];
1054 $this->mInterwiki = $p;
1055
1056 if ( !preg_match( "/^([A-Za-z0-9_\\x80-\\xff]+):(.*)$/", $t, $m ) ) {
1057 $done = true;
1058 } elseif($this->mInterwiki != $wgLocalInterwiki) {
1059 $done = true;
1060 }
1061 }
1062 }
1063 $r = $t;
1064 }
1065
1066 # Redundant interwiki prefix to the local wiki
1067 if ( 0 == strcmp( $this->mInterwiki, $wgLocalInterwiki ) ) {
1068 $this->mInterwiki = '';
1069 }
1070 # We already know that some pages won't be in the database!
1071 #
1072 if ( '' != $this->mInterwiki || -1 == $this->mNamespace ) {
1073 $this->mArticleID = 0;
1074 }
1075 $f = strstr( $r, '#' );
1076 if ( false !== $f ) {
1077 $this->mFragment = substr( $f, 1 );
1078 $r = substr( $r, 0, strlen( $r ) - strlen( $f ) );
1079 # remove whitespace again: prevents "Foo_bar_#"
1080 # becoming "Foo_bar_"
1081 $r = preg_replace( '/_*$/', '', $r );
1082 }
1083
1084 # Reject illegal characters.
1085 #
1086 if( preg_match( $rxTc, $r ) ) {
1087 wfProfileOut( $fname );
1088 return false;
1089 }
1090
1091 # "." and ".." conflict with the directories of those namesa
1092 if ( strpos( $r, '.' ) !== false &&
1093 ( $r === '.' || $r === '..' ||
1094 strpos( $r, './' ) === 0 ||
1095 strpos( $r, '../' ) === 0 ||
1096 strpos( $r, '/./' ) !== false ||
1097 strpos( $r, '/../' ) !== false ) )
1098 {
1099 wfProfileOut( $fname );
1100 return false;
1101 }
1102
1103 # We shouldn't need to query the DB for the size.
1104 #$maxSize = $dbr->textFieldSize( 'cur', 'cur_title' );
1105 if ( strlen( $r ) > 255 ) {
1106 return false;
1107 }
1108
1109 # Initial capital letter
1110 if( $wgCapitalLinks && $this->mInterwiki == '') {
1111 $t = $wgContLang->ucfirst( $r );
1112 } else {
1113 $t = $r;
1114 }
1115
1116 # Fill fields
1117 $this->mDbkeyform = $t;
1118 $this->mUrlform = wfUrlencode( $t );
1119
1120 $this->mTextform = str_replace( '_', ' ', $t );
1121
1122 wfProfileOut( $fname );
1123 return true;
1124 }
1125
1126 /**
1127 * Get a Title object associated with the talk page of this article
1128 * @return Title the object for the talk page
1129 * @access public
1130 */
1131 function getTalkPage() {
1132 return Title::makeTitle( Namespace::getTalk( $this->getNamespace() ), $this->getDBkey() );
1133 }
1134
1135 /**
1136 * Get a title object associated with the subject page of this
1137 * talk page
1138 *
1139 * @return Title the object for the subject page
1140 * @access public
1141 */
1142 function getSubjectPage() {
1143 return Title::makeTitle( Namespace::getSubject( $this->getNamespace() ), $this->getDBkey() );
1144 }
1145
1146 /**
1147 * Get an array of Title objects linking to this Title
1148 * - Also stores the IDs in the link cache.
1149 *
1150 * @param string $options may be FOR UPDATE
1151 * @return array the Title objects linking here
1152 * @access public
1153 */
1154 function getLinksTo( $options = '' ) {
1155 global $wgLinkCache;
1156 $id = $this->getArticleID();
1157
1158 if ( $options ) {
1159 $db =& wfGetDB( DB_MASTER );
1160 } else {
1161 $db =& wfGetDB( DB_SLAVE );
1162 }
1163 $cur = $db->tableName( 'cur' );
1164 $links = $db->tableName( 'links' );
1165
1166 $sql = "SELECT cur_namespace,cur_title,cur_id FROM $cur,$links WHERE l_from=cur_id AND l_to={$id} $options";
1167 $res = $db->query( $sql, 'Title::getLinksTo' );
1168 $retVal = array();
1169 if ( $db->numRows( $res ) ) {
1170 while ( $row = $db->fetchObject( $res ) ) {
1171 if ( $titleObj = Title::makeTitle( $row->cur_namespace, $row->cur_title ) ) {
1172 $wgLinkCache->addGoodLink( $row->cur_id, $titleObj->getPrefixedDBkey() );
1173 $retVal[] = $titleObj;
1174 }
1175 }
1176 }
1177 $db->freeResult( $res );
1178 return $retVal;
1179 }
1180
1181 /**
1182 * Get an array of Title objects linking to this non-existent title.
1183 * - Also stores the IDs in the link cache.
1184 *
1185 * @param string $options may be FOR UPDATE
1186 * @return array the Title objects linking here
1187 * @access public
1188 */
1189 function getBrokenLinksTo( $options = '' ) {
1190 global $wgLinkCache;
1191
1192 if ( $options ) {
1193 $db =& wfGetDB( DB_MASTER );
1194 } else {
1195 $db =& wfGetDB( DB_SLAVE );
1196 }
1197 $cur = $db->tableName( 'cur' );
1198 $brokenlinks = $db->tableName( 'brokenlinks' );
1199 $encTitle = $db->strencode( $this->getPrefixedDBkey() );
1200
1201 $sql = "SELECT cur_namespace,cur_title,cur_id FROM $brokenlinks,$cur " .
1202 "WHERE bl_from=cur_id AND bl_to='$encTitle' $options";
1203 $res = $db->query( $sql, "Title::getBrokenLinksTo" );
1204 $retVal = array();
1205 if ( $db->numRows( $res ) ) {
1206 while ( $row = $db->fetchObject( $res ) ) {
1207 $titleObj = Title::makeTitle( $row->cur_namespace, $row->cur_title );
1208 $wgLinkCache->addGoodLink( $row->cur_id, $titleObj->getPrefixedDBkey() );
1209 $retVal[] = $titleObj;
1210 }
1211 }
1212 $db->freeResult( $res );
1213 return $retVal;
1214 }
1215
1216 /**
1217 * Get a list of URLs to purge from the Squid cache when this
1218 * page changes
1219 *
1220 * @return array the URLs
1221 * @access public
1222 */
1223 function getSquidURLs() {
1224 return array(
1225 $this->getInternalURL(),
1226 $this->getInternalURL( 'action=history' )
1227 );
1228 }
1229
1230 /**
1231 * Move this page without authentication
1232 * @param Title &$nt the new page Title
1233 * @access public
1234 */
1235 function moveNoAuth( &$nt ) {
1236 return $this->moveTo( $nt, false );
1237 }
1238
1239 /**
1240 * Move a title to a new location
1241 * @param Title &$nt the new title
1242 * @param bool $auth indicates whether $wgUser's permissions
1243 * should be checked
1244 * @return mixed true on success, message name on failure
1245 * @access public
1246 */
1247 function moveTo( &$nt, $auth = true ) {
1248 if( !$this or !$nt ) {
1249 return 'badtitletext';
1250 }
1251
1252 $fname = 'Title::move';
1253 $oldid = $this->getArticleID();
1254 $newid = $nt->getArticleID();
1255
1256 if ( strlen( $nt->getDBkey() ) < 1 ) {
1257 return 'articleexists';
1258 }
1259 if ( ( ! Namespace::isMovable( $this->getNamespace() ) ) ||
1260 ( '' == $this->getDBkey() ) ||
1261 ( '' != $this->getInterwiki() ) ||
1262 ( !$oldid ) ||
1263 ( ! Namespace::isMovable( $nt->getNamespace() ) ) ||
1264 ( '' == $nt->getDBkey() ) ||
1265 ( '' != $nt->getInterwiki() ) ) {
1266 return 'badarticleerror';
1267 }
1268
1269 if ( $auth && ( !$this->userCanEdit() || !$nt->userCanEdit() ) ) {
1270 return 'protectedpage';
1271 }
1272
1273 # The move is allowed only if (1) the target doesn't exist, or
1274 # (2) the target is a redirect to the source, and has no history
1275 # (so we can undo bad moves right after they're done).
1276
1277 if ( 0 != $newid ) { # Target exists; check for validity
1278 if ( ! $this->isValidMoveTarget( $nt ) ) {
1279 return 'articleexists';
1280 }
1281 $this->moveOverExistingRedirect( $nt );
1282 } else { # Target didn't exist, do normal move.
1283 $this->moveToNewTitle( $nt, $newid );
1284 }
1285
1286 # Fixing category links (those without piped 'alternate' names) to be sorted under the new title
1287
1288 $dbw =& wfGetDB( DB_MASTER );
1289 $sql = "UPDATE categorylinks SET cl_sortkey=" . $dbw->addQuotes( $nt->getPrefixedText() ) .
1290 " WHERE cl_from=" . $dbw->addQuotes( $this->getArticleID() ) .
1291 " AND cl_sortkey=" . $dbw->addQuotes( $this->getPrefixedText() );
1292 $dbw->query( $sql, 'SpecialMovepage::doSubmit' );
1293
1294 # Update watchlists
1295
1296 $oldnamespace = $this->getNamespace() & ~1;
1297 $newnamespace = $nt->getNamespace() & ~1;
1298 $oldtitle = $this->getDBkey();
1299 $newtitle = $nt->getDBkey();
1300
1301 if( $oldnamespace != $newnamespace || $oldtitle != $newtitle ) {
1302 WatchedItem::duplicateEntries( $this, $nt );
1303 }
1304
1305 # Update search engine
1306 $u = new SearchUpdate( $oldid, $nt->getPrefixedDBkey() );
1307 $u->doUpdate();
1308 $u = new SearchUpdate( $newid, $this->getPrefixedDBkey(), '' );
1309 $u->doUpdate();
1310
1311 return true;
1312 }
1313
1314 /**
1315 * Move page to a title which is at present a redirect to the
1316 * source page
1317 *
1318 * @param Title &$nt the page to move to, which should currently
1319 * be a redirect
1320 * @access private
1321 */
1322 /* private */ function moveOverExistingRedirect( &$nt ) {
1323 global $wgUser, $wgLinkCache, $wgUseSquid, $wgMwRedir;
1324 $fname = 'Title::moveOverExistingRedirect';
1325 $comment = wfMsg( '1movedto2', $this->getPrefixedText(), $nt->getPrefixedText() );
1326
1327 $now = wfTimestampNow();
1328 $won = wfInvertTimestamp( $now );
1329 $newid = $nt->getArticleID();
1330 $oldid = $this->getArticleID();
1331 $dbw =& wfGetDB( DB_MASTER );
1332 $links = $dbw->tableName( 'links' );
1333
1334 # Change the name of the target page:
1335 $dbw->update( 'cur',
1336 /* SET */ array(
1337 'cur_touched' => $dbw->timestamp($now),
1338 'cur_namespace' => $nt->getNamespace(),
1339 'cur_title' => $nt->getDBkey()
1340 ),
1341 /* WHERE */ array( 'cur_id' => $oldid ),
1342 $fname
1343 );
1344 $wgLinkCache->clearLink( $nt->getPrefixedDBkey() );
1345
1346 # Repurpose the old redirect. We don't save it to history since
1347 # by definition if we've got here it's rather uninteresting.
1348
1349 $redirectText = $wgMwRedir->getSynonym( 0 ) . ' [[' . $nt->getPrefixedText() . "]]\n";
1350 $dbw->update( 'cur',
1351 /* SET */ array(
1352 'cur_touched' => $dbw->timestamp($now),
1353 'cur_timestamp' => $dbw->timestamp($now),
1354 'inverse_timestamp' => $won,
1355 'cur_namespace' => $this->getNamespace(),
1356 'cur_title' => $this->getDBkey(),
1357 'cur_text' => $wgMwRedir->getSynonym( 0 ) . ' [[' . $nt->getPrefixedText() . "]]\n",
1358 'cur_comment' => $comment,
1359 'cur_user' => $wgUser->getID(),
1360 'cur_minor_edit' => 0,
1361 'cur_counter' => 0,
1362 'cur_restrictions' => '',
1363 'cur_user_text' => $wgUser->getName(),
1364 'cur_is_redirect' => 1,
1365 'cur_is_new' => 1
1366 ),
1367 /* WHERE */ array( 'cur_id' => $newid ),
1368 $fname
1369 );
1370
1371 $wgLinkCache->clearLink( $this->getPrefixedDBkey() );
1372
1373 # Fix the redundant names for the past revisions of the target page.
1374 # The redirect should have no old revisions.
1375 $dbw->update(
1376 /* table */ 'old',
1377 /* SET */ array(
1378 'old_namespace' => $nt->getNamespace(),
1379 'old_title' => $nt->getDBkey(),
1380 ),
1381 /* WHERE */ array(
1382 'old_namespace' => $this->getNamespace(),
1383 'old_title' => $this->getDBkey(),
1384 ),
1385 $fname
1386 );
1387
1388 RecentChange::notifyMoveOverRedirect( $now, $this, $nt, $wgUser, $comment );
1389
1390 # Swap links
1391
1392 # Load titles and IDs
1393 $linksToOld = $this->getLinksTo( 'FOR UPDATE' );
1394 $linksToNew = $nt->getLinksTo( 'FOR UPDATE' );
1395
1396 # Delete them all
1397 $sql = "DELETE FROM $links WHERE l_to=$oldid OR l_to=$newid";
1398 $dbw->query( $sql, $fname );
1399
1400 # Reinsert
1401 if ( count( $linksToOld ) || count( $linksToNew )) {
1402 $sql = "INSERT INTO $links (l_from,l_to) VALUES ";
1403 $first = true;
1404
1405 # Insert links to old title
1406 foreach ( $linksToOld as $linkTitle ) {
1407 if ( $first ) {
1408 $first = false;
1409 } else {
1410 $sql .= ',';
1411 }
1412 $id = $linkTitle->getArticleID();
1413 $sql .= "($id,$newid)";
1414 }
1415
1416 # Insert links to new title
1417 foreach ( $linksToNew as $linkTitle ) {
1418 if ( $first ) {
1419 $first = false;
1420 } else {
1421 $sql .= ',';
1422 }
1423 $id = $linkTitle->getArticleID();
1424 $sql .= "($id, $oldid)";
1425 }
1426
1427 $dbw->query( $sql, DB_MASTER, $fname );
1428 }
1429
1430 # Now, we record the link from the redirect to the new title.
1431 # It should have no other outgoing links...
1432 $dbw->delete( 'links', array( 'l_from' => $newid ) );
1433 $dbw->insert( 'links', array( 'l_from' => $newid, 'l_to' => $oldid ) );
1434
1435 # Clear linkscc
1436 LinkCache::linksccClearLinksTo( $oldid );
1437 LinkCache::linksccClearLinksTo( $newid );
1438
1439 # Purge squid
1440 if ( $wgUseSquid ) {
1441 $urls = array_merge( $nt->getSquidURLs(), $this->getSquidURLs() );
1442 $u = new SquidUpdate( $urls );
1443 $u->doUpdate();
1444 }
1445 }
1446
1447 /**
1448 * Move page to non-existing title.
1449 * @param Title &$nt the new Title
1450 * @param int &$newid set to be the new article ID
1451 * @access private
1452 */
1453 /* private */ function moveToNewTitle( &$nt, &$newid ) {
1454 global $wgUser, $wgLinkCache, $wgUseSquid;
1455 $fname = 'MovePageForm::moveToNewTitle';
1456 $comment = wfMsg( '1movedto2', $this->getPrefixedText(), $nt->getPrefixedText() );
1457
1458 $newid = $nt->getArticleID();
1459 $oldid = $this->getArticleID();
1460 $dbw =& wfGetDB( DB_MASTER );
1461 $now = $dbw->timestamp();
1462 $won = wfInvertTimestamp( wfTimestamp(TS_MW,$now) );
1463 wfSeedRandom();
1464 $rand = wfRandom();
1465
1466 # Rename cur entry
1467 $dbw->update( 'cur',
1468 /* SET */ array(
1469 'cur_touched' => $now,
1470 'cur_namespace' => $nt->getNamespace(),
1471 'cur_title' => $nt->getDBkey()
1472 ),
1473 /* WHERE */ array( 'cur_id' => $oldid ),
1474 $fname
1475 );
1476
1477 $wgLinkCache->clearLink( $nt->getPrefixedDBkey() );
1478
1479 # Insert redirect
1480 $dbw->insert( 'cur', array(
1481 'cur_id' => $dbw->nextSequenceValue('cur_cur_id_seq'),
1482 'cur_namespace' => $this->getNamespace(),
1483 'cur_title' => $this->getDBkey(),
1484 'cur_comment' => $comment,
1485 'cur_user' => $wgUser->getID(),
1486 'cur_user_text' => $wgUser->getName(),
1487 'cur_timestamp' => $now,
1488 'inverse_timestamp' => $won,
1489 'cur_touched' => $now,
1490 'cur_is_redirect' => 1,
1491 'cur_random' => $rand,
1492 'cur_is_new' => 1,
1493 'cur_text' => "#REDIRECT [[" . $nt->getPrefixedText() . "]]\n" ), $fname
1494 );
1495 $newid = $dbw->insertId();
1496 $wgLinkCache->clearLink( $this->getPrefixedDBkey() );
1497
1498 # Rename old entries
1499 $dbw->update(
1500 /* table */ 'old',
1501 /* SET */ array(
1502 'old_namespace' => $nt->getNamespace(),
1503 'old_title' => $nt->getDBkey()
1504 ),
1505 /* WHERE */ array(
1506 'old_namespace' => $this->getNamespace(),
1507 'old_title' => $this->getDBkey()
1508 ), $fname
1509 );
1510
1511 # Record in RC
1512 RecentChange::notifyMoveToNew( $now, $this, $nt, $wgUser, $comment );
1513
1514 # Purge squid and linkscc as per article creation
1515 Article::onArticleCreate( $nt );
1516
1517 # Any text links to the old title must be reassigned to the redirect
1518 $dbw->update( 'links', array( 'l_to' => $newid ), array( 'l_to' => $oldid ), $fname );
1519 LinkCache::linksccClearLinksTo( $oldid );
1520
1521 # Record the just-created redirect's linking to the page
1522 $dbw->insert( 'links', array( 'l_from' => $newid, 'l_to' => $oldid ), $fname );
1523
1524 # Non-existent target may have had broken links to it; these must
1525 # now be removed and made into good links.
1526 $update = new LinksUpdate( $oldid, $nt->getPrefixedDBkey() );
1527 $update->fixBrokenLinks();
1528
1529 # Purge old title from squid
1530 # The new title, and links to the new title, are purged in Article::onArticleCreate()
1531 $titles = $nt->getLinksTo();
1532 if ( $wgUseSquid ) {
1533 $urls = $this->getSquidURLs();
1534 foreach ( $titles as $linkTitle ) {
1535 $urls[] = $linkTitle->getInternalURL();
1536 }
1537 $u = new SquidUpdate( $urls );
1538 $u->doUpdate();
1539 }
1540 }
1541
1542 /**
1543 * Checks if $this can be moved to a given Title
1544 * - Selects for update, so don't call it unless you mean business
1545 *
1546 * @param Title &$nt the new title to check
1547 * @access public
1548 */
1549 function isValidMoveTarget( $nt ) {
1550 $fname = 'Title::isValidMoveTarget';
1551 $dbw =& wfGetDB( DB_MASTER );
1552
1553 # Is it a redirect?
1554 $id = $nt->getArticleID();
1555 $obj = $dbw->selectRow( 'cur', array( 'cur_is_redirect','cur_text' ),
1556 array( 'cur_id' => $id ), $fname, 'FOR UPDATE' );
1557
1558 if ( !$obj || 0 == $obj->cur_is_redirect ) {
1559 # Not a redirect
1560 return false;
1561 }
1562
1563 # Does the redirect point to the source?
1564 if ( preg_match( "/\\[\\[\\s*([^\\]\\|]*)]]/", $obj->cur_text, $m ) ) {
1565 $redirTitle = Title::newFromText( $m[1] );
1566 if( !is_object( $redirTitle ) ||
1567 $redirTitle->getPrefixedDBkey() != $this->getPrefixedDBkey() ) {
1568 return false;
1569 }
1570 }
1571
1572 # Does the article have a history?
1573 $row = $dbw->selectRow( 'old', array( 'old_id' ),
1574 array(
1575 'old_namespace' => $nt->getNamespace(),
1576 'old_title' => $nt->getDBkey()
1577 ), $fname, 'FOR UPDATE'
1578 );
1579
1580 # Return true if there was no history
1581 return $row === false;
1582 }
1583
1584 /**
1585 * Create a redirect; fails if the title already exists; does
1586 * not notify RC
1587 *
1588 * @param Title $dest the destination of the redirect
1589 * @param string $comment the comment string describing the move
1590 * @return bool true on success
1591 * @access public
1592 */
1593 function createRedirect( $dest, $comment ) {
1594 global $wgUser;
1595 if ( $this->getArticleID() ) {
1596 return false;
1597 }
1598
1599 $fname = 'Title::createRedirect';
1600 $dbw =& wfGetDB( DB_MASTER );
1601 $now = wfTimestampNow();
1602 $won = wfInvertTimestamp( $now );
1603 $seqVal = $dbw->nextSequenceValue( 'cur_cur_id_seq' );
1604
1605 $dbw->insert( 'cur', array(
1606 'cur_id' => $seqVal,
1607 'cur_namespace' => $this->getNamespace(),
1608 'cur_title' => $this->getDBkey(),
1609 'cur_comment' => $comment,
1610 'cur_user' => $wgUser->getID(),
1611 'cur_user_text' => $wgUser->getName(),
1612 'cur_timestamp' => $now,
1613 'inverse_timestamp' => $won,
1614 'cur_touched' => $now,
1615 'cur_is_redirect' => 1,
1616 'cur_is_new' => 1,
1617 'cur_text' => "#REDIRECT [[" . $dest->getPrefixedText() . "]]\n"
1618 ), $fname );
1619 $newid = $dbw->insertId();
1620 $this->resetArticleID( $newid );
1621
1622 # Link table
1623 if ( $dest->getArticleID() ) {
1624 $dbw->insert( 'links',
1625 array(
1626 'l_to' => $dest->getArticleID(),
1627 'l_from' => $newid
1628 ), $fname
1629 );
1630 } else {
1631 $dbw->insert( 'brokenlinks',
1632 array(
1633 'bl_to' => $dest->getPrefixedDBkey(),
1634 'bl_from' => $newid
1635 ), $fname
1636 );
1637 }
1638
1639 Article::onArticleCreate( $this );
1640 return true;
1641 }
1642
1643 /**
1644 * Get categories to which this Title belongs and return an array of
1645 * categories' names.
1646 *
1647 * @return array an array of parents in the form:
1648 * $parent => $currentarticle
1649 * @access public
1650 */
1651 function getParentCategories() {
1652 global $wgContLang,$wgUser;
1653
1654 $titlekey = $this->getArticleId();
1655 $sk =& $wgUser->getSkin();
1656 $parents = array();
1657 $dbr =& wfGetDB( DB_SLAVE );
1658 $cur = $dbr->tableName( 'cur' );
1659 $categorylinks = $dbr->tableName( 'categorylinks' );
1660
1661 # NEW SQL
1662 $sql = "SELECT * FROM categorylinks"
1663 ." WHERE cl_from='$titlekey'"
1664 ." AND cl_from <> '0'"
1665 ." ORDER BY cl_sortkey";
1666
1667 $res = $dbr->query ( $sql ) ;
1668
1669 if($dbr->numRows($res) > 0) {
1670 while ( $x = $dbr->fetchObject ( $res ) )
1671 //$data[] = Title::newFromText($wgContLang->getNSText ( NS_CATEGORY ).':'.$x->cl_to);
1672 $data[$wgContLang->getNSText ( NS_CATEGORY ).':'.$x->cl_to] = $this->getFullText();
1673 $dbr->freeResult ( $res ) ;
1674 } else {
1675 $data = '';
1676 }
1677 return $data;
1678 }
1679
1680 /**
1681 * Go through all parent categories of this Title
1682 * @return array
1683 * @access public
1684 */
1685 function getCategorieBrowser() {
1686 $parents = $this->getParentCategories();
1687
1688 if($parents != '') {
1689 foreach($parents as $parent => $current)
1690 {
1691 $nt = Title::newFromText($parent);
1692 $stack[$parent] = $nt->getCategorieBrowser();
1693 }
1694 return $stack;
1695 } else {
1696 return array();
1697 }
1698 }
1699
1700
1701 /**
1702 * Get an associative array for selecting this title from
1703 * the "cur" table
1704 *
1705 * @return array
1706 * @access public
1707 */
1708 function curCond() {
1709 return array( 'cur_namespace' => $this->mNamespace, 'cur_title' => $this->mDbkeyform );
1710 }
1711
1712 /**
1713 * Get an associative array for selecting this title from the
1714 * "old" table
1715 *
1716 * @return array
1717 * @access public
1718 */
1719 function oldCond() {
1720 return array( 'old_namespace' => $this->mNamespace, 'old_title' => $this->mDbkeyform );
1721 }
1722
1723 /**
1724 * Get the revision ID of the previous revision
1725 *
1726 * @param integer $revision Revision ID. Get the revision that was before this one.
1727 * @return interger $oldrevision|false
1728 */
1729 function getPreviousRevisionID( $revision ) {
1730 $dbr =& wfGetDB( DB_SLAVE );
1731 return $dbr->selectField( 'old', 'old_id',
1732 'old_title=' . $dbr->addQuotes( $this->getDBkey() ) .
1733 ' AND old_namespace=' . IntVal( $this->getNamespace() ) .
1734 ' AND old_id<' . IntVal( $revision ) . ' ORDER BY old_id DESC' );
1735 }
1736
1737 /**
1738 * Get the revision ID of the next revision
1739 *
1740 * @param integer $revision Revision ID. Get the revision that was after this one.
1741 * @return interger $oldrevision|false
1742 */
1743 function getNextRevisionID( $revision ) {
1744 $dbr =& wfGetDB( DB_SLAVE );
1745 return $dbr->selectField( 'old', 'old_id',
1746 'old_title=' . $dbr->addQuotes( $this->getDBkey() ) .
1747 ' AND old_namespace=' . IntVal( $this->getNamespace() ) .
1748 ' AND old_id>' . IntVal( $revision ) . ' ORDER BY old_id' );
1749 }
1750
1751 }
1752 ?>