Implementing user levels management. This is only a very basic interface and several...
[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 if ( $wgScript != '' ) {
626 $url = "{$wgScript}?title={$dbkey}&{$query}";
627 } else {
628 # Top level wiki
629 $url = "/{$dbkey}?{$query}";
630 }
631 }
632 return $url;
633 }
634
635 /**
636 * Get an HTML-escaped version of the URL form, suitable for
637 * using in a link, without a server name or fragment
638 * @param string $query an optional query string
639 * @return string the URL
640 * @access public
641 */
642 function escapeLocalURL( $query = '' ) {
643 return htmlspecialchars( $this->getLocalURL( $query ) );
644 }
645
646 /**
647 * Get an HTML-escaped version of the URL form, suitable for
648 * using in a link, including the server name and fragment
649 *
650 * @return string the URL
651 * @param string $query an optional query string
652 * @access public
653 */
654 function escapeFullURL( $query = '' ) {
655 return htmlspecialchars( $this->getFullURL( $query ) );
656 }
657
658 /**
659 * Get the URL form for an internal link.
660 * - Used in various Squid-related code, in case we have a different
661 * internal hostname for the server from the exposed one.
662 *
663 * @param string $query an optional query string
664 * @return string the URL
665 * @access public
666 */
667 function getInternalURL( $query = '' ) {
668 global $wgInternalServer;
669 return $wgInternalServer . $this->getLocalURL( $query );
670 }
671
672 /**
673 * Get the edit URL for this Title
674 * @return string the URL, or a null string if this is an
675 * interwiki link
676 * @access public
677 */
678 function getEditURL() {
679 global $wgServer, $wgScript;
680
681 if ( '' != $this->mInterwiki ) { return ''; }
682 $s = $this->getLocalURL( 'action=edit' );
683
684 return $s;
685 }
686
687 /**
688 * Get the HTML-escaped displayable text form.
689 * Used for the title field in <a> tags.
690 * @return string the text, including any prefixes
691 * @access public
692 */
693 function getEscapedText() {
694 return htmlspecialchars( $this->getPrefixedText() );
695 }
696
697 /**
698 * Is this Title interwiki?
699 * @return boolean
700 * @access public
701 */
702 function isExternal() { return ( '' != $this->mInterwiki ); }
703
704 /**
705 * Does the title correspond to a protected article?
706 * @return boolean
707 * @access public
708 */
709 function isProtected() {
710 if ( -1 == $this->mNamespace ) { return true; }
711 $a = $this->getRestrictions();
712 if ( in_array( 'sysop', $a ) ) { return true; }
713 return false;
714 }
715
716 /**
717 * Is the page a log page, i.e. one where the history is messed up by
718 * LogPage.php? This used to be used for suppressing diff links in
719 * recent changes, but now that's done by setting a flag in the
720 * recentchanges table. Hence, this probably is no longer used.
721 *
722 * @deprecated
723 * @access public
724 */
725 function isLog() {
726 if ( $this->mNamespace != Namespace::getWikipedia() ) {
727 return false;
728 }
729 if ( ( 0 == strcmp( wfMsg( 'uploadlogpage' ), $this->mDbkeyform ) ) ||
730 ( 0 == strcmp( wfMsg( 'dellogpage' ), $this->mDbkeyform ) ) ) {
731 return true;
732 }
733 return false;
734 }
735
736 /**
737 * Is $wgUser is watching this page?
738 * @return boolean
739 * @access public
740 */
741 function userIsWatching() {
742 global $wgUser;
743
744 if ( -1 == $this->mNamespace ) { return false; }
745 if ( 0 == $wgUser->getID() ) { return false; }
746
747 return $wgUser->isWatched( $this );
748 }
749
750 /**
751 * Can $wgUser edit this page?
752 * @return boolean
753 * @access public
754 */
755 function userCanEdit() {
756 global $wgUser;
757 if ( -1 == $this->mNamespace ) { return false; }
758 if ( NS_MEDIAWIKI == $this->mNamespace && !$wgUser->isAllowed('editinterface') ) { return false; }
759 # if ( 0 == $this->getArticleID() ) { return false; }
760 if ( $this->mDbkeyform == '_' ) { return false; }
761 # protect global styles and js
762 if ( NS_MEDIAWIKI == $this->mNamespace
763 && preg_match("/\\.(css|js)$/", $this->mTextform )
764 && !$wgUser->isAllowed('editinterface') )
765 { return false; }
766 //if ( $this->isCssJsSubpage() and !$this->userCanEditCssJsSubpage() ) { return false; }
767 # protect css/js subpages of user pages
768 # XXX: this might be better using restrictions
769 # XXX: Find a way to work around the php bug that prevents using $this->userCanEditCssJsSubpage() from working
770 if( Namespace::getUser() == $this->mNamespace
771 and preg_match("/\\.(css|js)$/", $this->mTextform )
772 and !$wgUser->isAllowed('editinterface')
773 and !preg_match('/^'.preg_quote($wgUser->getName(), '/').'\//', $this->mTextform) )
774 { return false; }
775 $ur = $wgUser->getRights();
776
777 foreach ( $this->getRestrictions() as $r ) {
778 if ( '' != $r && ( ! in_array( $r, $ur ) ) ) {
779 return false;
780 }
781 }
782 return true;
783 }
784
785 /**
786 * Can $wgUser read this page?
787 * @return boolean
788 * @access public
789 */
790 function userCanRead() {
791 global $wgUser;
792 global $wgWhitelistRead;
793
794 if($wgUser->isAllowed('read')) {
795 return true;
796 } else {
797 $name = $this->getPrefixedText();
798
799 /** user can create an account */
800 if($wgUser->isAllowed('createaccount')
801 && $name == 'Special:Userlogin') { return true; }
802 else {
803 echo $name;
804 print_r($wgUser->getRights());
805 }
806
807 /** some pages are explicitly allowed */
808 if( in_array( $name, $wgWhitelistRead ) ) return true;
809 # Compatibility with old settings
810 if( $this->getNamespace() == NS_MAIN ) {
811 if( in_array( ':' . $name, $wgWhitelistRead ) ) return true;
812 }
813 }
814 return false;
815 }
816
817 /**
818 * Is this a .css or .js subpage of a user page?
819 * @return bool
820 * @access public
821 */
822 function isCssJsSubpage() {
823 return ( Namespace::getUser() == $this->mNamespace and preg_match("/\\.(css|js)$/", $this->mTextform ) );
824 }
825 /**
826 * Is this a .css subpage of a user page?
827 * @return bool
828 * @access public
829 */
830 function isCssSubpage() {
831 return ( Namespace::getUser() == $this->mNamespace and preg_match("/\\.css$/", $this->mTextform ) );
832 }
833 /**
834 * Is this a .js subpage of a user page?
835 * @return bool
836 * @access public
837 */
838 function isJsSubpage() {
839 return ( Namespace::getUser() == $this->mNamespace and preg_match("/\\.js$/", $this->mTextform ) );
840 }
841 /**
842 * Protect css/js subpages of user pages: can $wgUser edit
843 * this page?
844 *
845 * @return boolean
846 * @todo XXX: this might be better using restrictions
847 * @access public
848 */
849 function userCanEditCssJsSubpage() {
850 global $wgUser;
851 return ( $wgUser->isAllowed('editinterface') or preg_match('/^'.preg_quote($wgUser->getName(), '/').'\//', $this->mTextform) );
852 }
853
854 /**
855 * Accessor/initialisation for mRestrictions
856 * @return array the array of groups allowed to edit this article
857 * @access public
858 */
859 function getRestrictions() {
860 $id = $this->getArticleID();
861 if ( 0 == $id ) { return array(); }
862
863 if ( ! $this->mRestrictionsLoaded ) {
864 $dbr =& wfGetDB( DB_SLAVE );
865 $res = $dbr->selectField( 'cur', 'cur_restrictions', 'cur_id='.$id );
866 $this->mRestrictions = explode( ',', trim( $res ) );
867 $this->mRestrictionsLoaded = true;
868 }
869 return $this->mRestrictions;
870 }
871
872 /**
873 * Is there a version of this page in the deletion archive?
874 * @return int the number of archived revisions
875 * @access public
876 */
877 function isDeleted() {
878 $fname = 'Title::isDeleted';
879 $dbr =& wfGetDB( DB_SLAVE );
880 $n = $dbr->selectField( 'archive', 'COUNT(*)', array( 'ar_namespace' => $this->getNamespace(),
881 'ar_title' => $this->getDBkey() ), $fname );
882 return (int)$n;
883 }
884
885 /**
886 * Get the article ID for this Title from the link cache,
887 * adding it if necessary
888 * @param int $flags a bit field; may be GAID_FOR_UPDATE to select
889 * for update
890 * @return int the ID
891 * @access public
892 */
893 function getArticleID( $flags = 0 ) {
894 global $wgLinkCache;
895
896 if ( $flags & GAID_FOR_UPDATE ) {
897 $oldUpdate = $wgLinkCache->forUpdate( true );
898 $this->mArticleID = $wgLinkCache->addLinkObj( $this );
899 $wgLinkCache->forUpdate( $oldUpdate );
900 } else {
901 if ( -1 == $this->mArticleID ) {
902 $this->mArticleID = $wgLinkCache->addLinkObj( $this );
903 }
904 }
905 return $this->mArticleID;
906 }
907
908 /**
909 * This clears some fields in this object, and clears any associated
910 * keys in the "bad links" section of $wgLinkCache.
911 *
912 * - This is called from Article::insertNewArticle() to allow
913 * loading of the new cur_id. It's also called from
914 * Article::doDeleteArticle()
915 *
916 * @param int $newid the new Article ID
917 * @access public
918 */
919 function resetArticleID( $newid ) {
920 global $wgLinkCache;
921 $wgLinkCache->clearBadLink( $this->getPrefixedDBkey() );
922
923 if ( 0 == $newid ) { $this->mArticleID = -1; }
924 else { $this->mArticleID = $newid; }
925 $this->mRestrictionsLoaded = false;
926 $this->mRestrictions = array();
927 }
928
929 /**
930 * Updates cur_touched for this page; called from LinksUpdate.php
931 * @return bool true if the update succeded
932 * @access public
933 */
934 function invalidateCache() {
935 $now = wfTimestampNow();
936 $dbw =& wfGetDB( DB_MASTER );
937 $success = $dbw->update( 'cur',
938 array( /* SET */
939 'cur_touched' => $dbw->timestamp()
940 ), array( /* WHERE */
941 'cur_namespace' => $this->getNamespace() ,
942 'cur_title' => $this->getDBkey()
943 ), 'Title::invalidateCache'
944 );
945 return $success;
946 }
947
948 /**
949 * Prefix some arbitrary text with the namespace or interwiki prefix
950 * of this object
951 *
952 * @param string $name the text
953 * @return string the prefixed text
954 * @access private
955 */
956 /* private */ function prefix( $name ) {
957 global $wgContLang;
958
959 $p = '';
960 if ( '' != $this->mInterwiki ) {
961 $p = $this->mInterwiki . ':';
962 }
963 if ( 0 != $this->mNamespace ) {
964 $p .= $wgContLang->getNsText( $this->mNamespace ) . ':';
965 }
966 return $p . $name;
967 }
968
969 /**
970 * Secure and split - main initialisation function for this object
971 *
972 * Assumes that mDbkeyform has been set, and is urldecoded
973 * and uses underscores, but not otherwise munged. This function
974 * removes illegal characters, splits off the interwiki and
975 * namespace prefixes, sets the other forms, and canonicalizes
976 * everything.
977 * @return bool true on success
978 * @access private
979 */
980 /* private */ function secureAndSplit()
981 {
982 global $wgContLang, $wgLocalInterwiki, $wgCapitalLinks;
983 $fname = 'Title::secureAndSplit';
984 wfProfileIn( $fname );
985
986 static $imgpre = false;
987 static $rxTc = false;
988
989 # Initialisation
990 if ( $imgpre === false ) {
991 $imgpre = ':' . $wgContLang->getNsText( Namespace::getImage() ) . ':';
992 # % is needed as well
993 $rxTc = '/[^' . Title::legalChars() . ']|%[0-9A-Fa-f]{2}/';
994 }
995
996 $this->mInterwiki = $this->mFragment = '';
997 $this->mNamespace = $this->mDefaultNamespace; # Usually NS_MAIN
998
999 # Clean up whitespace
1000 #
1001 $t = preg_replace( "/[\\s_]+/", '_', $this->mDbkeyform );
1002 $t = preg_replace( '/^_*(.*?)_*$/', '$1', $t );
1003
1004 if ( '' == $t ) {
1005 wfProfileOut( $fname );
1006 return false;
1007 }
1008
1009 global $wgUseLatin1;
1010 if( !$wgUseLatin1 && false !== strpos( $t, UTF8_REPLACEMENT ) ) {
1011 # Contained illegal UTF-8 sequences or forbidden Unicode chars.
1012 wfProfileOut( $fname );
1013 return false;
1014 }
1015
1016 $this->mDbkeyform = $t;
1017 $done = false;
1018
1019 # :Image: namespace
1020 if ( 0 == strncasecmp( $imgpre, $t, strlen( $imgpre ) ) ) {
1021 $t = substr( $t, 1 );
1022 }
1023
1024 # Initial colon indicating main namespace
1025 if ( ':' == $t{0} ) {
1026 $r = substr( $t, 1 );
1027 $this->mNamespace = NS_MAIN;
1028 } else {
1029 # Namespace or interwiki prefix
1030 if ( preg_match( "/^(.+?)_*:_*(.*)$/", $t, $m ) ) {
1031 #$p = strtolower( $m[1] );
1032 $p = $m[1];
1033 $lowerNs = strtolower( $p );
1034 if ( $ns = Namespace::getCanonicalIndex( $lowerNs ) ) {
1035 # Canonical namespace
1036 $t = $m[2];
1037 $this->mNamespace = $ns;
1038 } elseif ( $ns = $wgContLang->getNsIndex( $lowerNs )) {
1039 # Ordinary namespace
1040 $t = $m[2];
1041 $this->mNamespace = $ns;
1042 } elseif ( $this->getInterwikiLink( $p ) ) {
1043 # Interwiki link
1044 $t = $m[2];
1045 $this->mInterwiki = $p;
1046
1047 if ( !preg_match( "/^([A-Za-z0-9_\\x80-\\xff]+):(.*)$/", $t, $m ) ) {
1048 $done = true;
1049 } elseif($this->mInterwiki != $wgLocalInterwiki) {
1050 $done = true;
1051 }
1052 }
1053 }
1054 $r = $t;
1055 }
1056
1057 # Redundant interwiki prefix to the local wiki
1058 if ( 0 == strcmp( $this->mInterwiki, $wgLocalInterwiki ) ) {
1059 $this->mInterwiki = '';
1060 }
1061 # We already know that some pages won't be in the database!
1062 #
1063 if ( '' != $this->mInterwiki || -1 == $this->mNamespace ) {
1064 $this->mArticleID = 0;
1065 }
1066 $f = strstr( $r, '#' );
1067 if ( false !== $f ) {
1068 $this->mFragment = substr( $f, 1 );
1069 $r = substr( $r, 0, strlen( $r ) - strlen( $f ) );
1070 # remove whitespace again: prevents "Foo_bar_#"
1071 # becoming "Foo_bar_"
1072 $r = preg_replace( '/_*$/', '', $r );
1073 }
1074
1075 # Reject illegal characters.
1076 #
1077 if( preg_match( $rxTc, $r ) ) {
1078 wfProfileOut( $fname );
1079 return false;
1080 }
1081
1082 # "." and ".." conflict with the directories of those namesa
1083 if ( strpos( $r, '.' ) !== false &&
1084 ( $r === '.' || $r === '..' ||
1085 strpos( $r, './' ) === 0 ||
1086 strpos( $r, '../' ) === 0 ||
1087 strpos( $r, '/./' ) !== false ||
1088 strpos( $r, '/../' ) !== false ) )
1089 {
1090 wfProfileOut( $fname );
1091 return false;
1092 }
1093
1094 # We shouldn't need to query the DB for the size.
1095 #$maxSize = $dbr->textFieldSize( 'cur', 'cur_title' );
1096 if ( strlen( $r ) > 255 ) {
1097 return false;
1098 }
1099
1100 # Initial capital letter
1101 if( $wgCapitalLinks && $this->mInterwiki == '') {
1102 $t = $wgContLang->ucfirst( $r );
1103 } else {
1104 $t = $r;
1105 }
1106
1107 # Fill fields
1108 $this->mDbkeyform = $t;
1109 $this->mUrlform = wfUrlencode( $t );
1110
1111 $this->mTextform = str_replace( '_', ' ', $t );
1112
1113 wfProfileOut( $fname );
1114 return true;
1115 }
1116
1117 /**
1118 * Get a Title object associated with the talk page of this article
1119 * @return Title the object for the talk page
1120 * @access public
1121 */
1122 function getTalkPage() {
1123 return Title::makeTitle( Namespace::getTalk( $this->getNamespace() ), $this->getDBkey() );
1124 }
1125
1126 /**
1127 * Get a title object associated with the subject page of this
1128 * talk page
1129 *
1130 * @return Title the object for the subject page
1131 * @access public
1132 */
1133 function getSubjectPage() {
1134 return Title::makeTitle( Namespace::getSubject( $this->getNamespace() ), $this->getDBkey() );
1135 }
1136
1137 /**
1138 * Get an array of Title objects linking to this Title
1139 * - Also stores the IDs in the link cache.
1140 *
1141 * @param string $options may be FOR UPDATE
1142 * @return array the Title objects linking here
1143 * @access public
1144 */
1145 function getLinksTo( $options = '' ) {
1146 global $wgLinkCache;
1147 $id = $this->getArticleID();
1148
1149 if ( $options ) {
1150 $db =& wfGetDB( DB_MASTER );
1151 } else {
1152 $db =& wfGetDB( DB_SLAVE );
1153 }
1154 $cur = $db->tableName( 'cur' );
1155 $links = $db->tableName( 'links' );
1156
1157 $sql = "SELECT cur_namespace,cur_title,cur_id FROM $cur,$links WHERE l_from=cur_id AND l_to={$id} $options";
1158 $res = $db->query( $sql, 'Title::getLinksTo' );
1159 $retVal = array();
1160 if ( $db->numRows( $res ) ) {
1161 while ( $row = $db->fetchObject( $res ) ) {
1162 if ( $titleObj = Title::makeTitle( $row->cur_namespace, $row->cur_title ) ) {
1163 $wgLinkCache->addGoodLink( $row->cur_id, $titleObj->getPrefixedDBkey() );
1164 $retVal[] = $titleObj;
1165 }
1166 }
1167 }
1168 $db->freeResult( $res );
1169 return $retVal;
1170 }
1171
1172 /**
1173 * Get an array of Title objects linking to this non-existent title.
1174 * - Also stores the IDs in the link cache.
1175 *
1176 * @param string $options may be FOR UPDATE
1177 * @return array the Title objects linking here
1178 * @access public
1179 */
1180 function getBrokenLinksTo( $options = '' ) {
1181 global $wgLinkCache;
1182
1183 if ( $options ) {
1184 $db =& wfGetDB( DB_MASTER );
1185 } else {
1186 $db =& wfGetDB( DB_SLAVE );
1187 }
1188 $cur = $db->tableName( 'cur' );
1189 $brokenlinks = $db->tableName( 'brokenlinks' );
1190 $encTitle = $db->strencode( $this->getPrefixedDBkey() );
1191
1192 $sql = "SELECT cur_namespace,cur_title,cur_id FROM $brokenlinks,$cur " .
1193 "WHERE bl_from=cur_id AND bl_to='$encTitle' $options";
1194 $res = $db->query( $sql, "Title::getBrokenLinksTo" );
1195 $retVal = array();
1196 if ( $db->numRows( $res ) ) {
1197 while ( $row = $db->fetchObject( $res ) ) {
1198 $titleObj = Title::makeTitle( $row->cur_namespace, $row->cur_title );
1199 $wgLinkCache->addGoodLink( $row->cur_id, $titleObj->getPrefixedDBkey() );
1200 $retVal[] = $titleObj;
1201 }
1202 }
1203 $db->freeResult( $res );
1204 return $retVal;
1205 }
1206
1207 /**
1208 * Get a list of URLs to purge from the Squid cache when this
1209 * page changes
1210 *
1211 * @return array the URLs
1212 * @access public
1213 */
1214 function getSquidURLs() {
1215 return array(
1216 $this->getInternalURL(),
1217 $this->getInternalURL( 'action=history' )
1218 );
1219 }
1220
1221 /**
1222 * Move this page without authentication
1223 * @param Title &$nt the new page Title
1224 * @access public
1225 */
1226 function moveNoAuth( &$nt ) {
1227 return $this->moveTo( $nt, false );
1228 }
1229
1230 /**
1231 * Move a title to a new location
1232 * @param Title &$nt the new title
1233 * @param bool $auth indicates whether $wgUser's permissions
1234 * should be checked
1235 * @return mixed true on success, message name on failure
1236 * @access public
1237 */
1238 function moveTo( &$nt, $auth = true ) {
1239 if( !$this or !$nt ) {
1240 return 'badtitletext';
1241 }
1242
1243 $fname = 'Title::move';
1244 $oldid = $this->getArticleID();
1245 $newid = $nt->getArticleID();
1246
1247 if ( strlen( $nt->getDBkey() ) < 1 ) {
1248 return 'articleexists';
1249 }
1250 if ( ( ! Namespace::isMovable( $this->getNamespace() ) ) ||
1251 ( '' == $this->getDBkey() ) ||
1252 ( '' != $this->getInterwiki() ) ||
1253 ( !$oldid ) ||
1254 ( ! Namespace::isMovable( $nt->getNamespace() ) ) ||
1255 ( '' == $nt->getDBkey() ) ||
1256 ( '' != $nt->getInterwiki() ) ) {
1257 return 'badarticleerror';
1258 }
1259
1260 if ( $auth && ( !$this->userCanEdit() || !$nt->userCanEdit() ) ) {
1261 return 'protectedpage';
1262 }
1263
1264 # The move is allowed only if (1) the target doesn't exist, or
1265 # (2) the target is a redirect to the source, and has no history
1266 # (so we can undo bad moves right after they're done).
1267
1268 if ( 0 != $newid ) { # Target exists; check for validity
1269 if ( ! $this->isValidMoveTarget( $nt ) ) {
1270 return 'articleexists';
1271 }
1272 $this->moveOverExistingRedirect( $nt );
1273 } else { # Target didn't exist, do normal move.
1274 $this->moveToNewTitle( $nt, $newid );
1275 }
1276
1277 # Fixing category links (those without piped 'alternate' names) to be sorted under the new title
1278
1279 $dbw =& wfGetDB( DB_MASTER );
1280 $sql = "UPDATE categorylinks SET cl_sortkey=" . $dbw->addQuotes( $nt->getPrefixedText() ) .
1281 " WHERE cl_from=" . $dbw->addQuotes( $this->getArticleID() ) .
1282 " AND cl_sortkey=" . $dbw->addQuotes( $this->getPrefixedText() );
1283 $dbw->query( $sql, 'SpecialMovepage::doSubmit' );
1284
1285 # Update watchlists
1286
1287 $oldnamespace = $this->getNamespace() & ~1;
1288 $newnamespace = $nt->getNamespace() & ~1;
1289 $oldtitle = $this->getDBkey();
1290 $newtitle = $nt->getDBkey();
1291
1292 if( $oldnamespace != $newnamespace || $oldtitle != $newtitle ) {
1293 WatchedItem::duplicateEntries( $this, $nt );
1294 }
1295
1296 # Update search engine
1297 $u = new SearchUpdate( $oldid, $nt->getPrefixedDBkey() );
1298 $u->doUpdate();
1299 $u = new SearchUpdate( $newid, $this->getPrefixedDBkey(), '' );
1300 $u->doUpdate();
1301
1302 return true;
1303 }
1304
1305 /**
1306 * Move page to a title which is at present a redirect to the
1307 * source page
1308 *
1309 * @param Title &$nt the page to move to, which should currently
1310 * be a redirect
1311 * @access private
1312 */
1313 /* private */ function moveOverExistingRedirect( &$nt ) {
1314 global $wgUser, $wgLinkCache, $wgUseSquid, $wgMwRedir;
1315 $fname = 'Title::moveOverExistingRedirect';
1316 $comment = wfMsg( '1movedto2', $this->getPrefixedText(), $nt->getPrefixedText() );
1317
1318 $now = wfTimestampNow();
1319 $won = wfInvertTimestamp( $now );
1320 $newid = $nt->getArticleID();
1321 $oldid = $this->getArticleID();
1322 $dbw =& wfGetDB( DB_MASTER );
1323 $links = $dbw->tableName( 'links' );
1324
1325 # Change the name of the target page:
1326 $dbw->update( 'cur',
1327 /* SET */ array(
1328 'cur_touched' => $dbw->timestamp($now),
1329 'cur_namespace' => $nt->getNamespace(),
1330 'cur_title' => $nt->getDBkey()
1331 ),
1332 /* WHERE */ array( 'cur_id' => $oldid ),
1333 $fname
1334 );
1335 $wgLinkCache->clearLink( $nt->getPrefixedDBkey() );
1336
1337 # Repurpose the old redirect. We don't save it to history since
1338 # by definition if we've got here it's rather uninteresting.
1339
1340 $redirectText = $wgMwRedir->getSynonym( 0 ) . ' [[' . $nt->getPrefixedText() . "]]\n";
1341 $dbw->update( 'cur',
1342 /* SET */ array(
1343 'cur_touched' => $dbw->timestamp($now),
1344 'cur_timestamp' => $dbw->timestamp($now),
1345 'inverse_timestamp' => $won,
1346 'cur_namespace' => $this->getNamespace(),
1347 'cur_title' => $this->getDBkey(),
1348 'cur_text' => $wgMwRedir->getSynonym( 0 ) . ' [[' . $nt->getPrefixedText() . "]]\n",
1349 'cur_comment' => $comment,
1350 'cur_user' => $wgUser->getID(),
1351 'cur_minor_edit' => 0,
1352 'cur_counter' => 0,
1353 'cur_restrictions' => '',
1354 'cur_user_text' => $wgUser->getName(),
1355 'cur_is_redirect' => 1,
1356 'cur_is_new' => 1
1357 ),
1358 /* WHERE */ array( 'cur_id' => $newid ),
1359 $fname
1360 );
1361
1362 $wgLinkCache->clearLink( $this->getPrefixedDBkey() );
1363
1364 # Fix the redundant names for the past revisions of the target page.
1365 # The redirect should have no old revisions.
1366 $dbw->update(
1367 /* table */ 'old',
1368 /* SET */ array(
1369 'old_namespace' => $nt->getNamespace(),
1370 'old_title' => $nt->getDBkey(),
1371 ),
1372 /* WHERE */ array(
1373 'old_namespace' => $this->getNamespace(),
1374 'old_title' => $this->getDBkey(),
1375 ),
1376 $fname
1377 );
1378
1379 RecentChange::notifyMoveOverRedirect( $now, $this, $nt, $wgUser, $comment );
1380
1381 # Swap links
1382
1383 # Load titles and IDs
1384 $linksToOld = $this->getLinksTo( 'FOR UPDATE' );
1385 $linksToNew = $nt->getLinksTo( 'FOR UPDATE' );
1386
1387 # Delete them all
1388 $sql = "DELETE FROM $links WHERE l_to=$oldid OR l_to=$newid";
1389 $dbw->query( $sql, $fname );
1390
1391 # Reinsert
1392 if ( count( $linksToOld ) || count( $linksToNew )) {
1393 $sql = "INSERT INTO $links (l_from,l_to) VALUES ";
1394 $first = true;
1395
1396 # Insert links to old title
1397 foreach ( $linksToOld as $linkTitle ) {
1398 if ( $first ) {
1399 $first = false;
1400 } else {
1401 $sql .= ',';
1402 }
1403 $id = $linkTitle->getArticleID();
1404 $sql .= "($id,$newid)";
1405 }
1406
1407 # Insert links to new title
1408 foreach ( $linksToNew as $linkTitle ) {
1409 if ( $first ) {
1410 $first = false;
1411 } else {
1412 $sql .= ',';
1413 }
1414 $id = $linkTitle->getArticleID();
1415 $sql .= "($id, $oldid)";
1416 }
1417
1418 $dbw->query( $sql, DB_MASTER, $fname );
1419 }
1420
1421 # Now, we record the link from the redirect to the new title.
1422 # It should have no other outgoing links...
1423 $dbw->delete( 'links', array( 'l_from' => $newid ) );
1424 $dbw->insert( 'links', array( 'l_from' => $newid, 'l_to' => $oldid ) );
1425
1426 # Clear linkscc
1427 LinkCache::linksccClearLinksTo( $oldid );
1428 LinkCache::linksccClearLinksTo( $newid );
1429
1430 # Purge squid
1431 if ( $wgUseSquid ) {
1432 $urls = array_merge( $nt->getSquidURLs(), $this->getSquidURLs() );
1433 $u = new SquidUpdate( $urls );
1434 $u->doUpdate();
1435 }
1436 }
1437
1438 /**
1439 * Move page to non-existing title.
1440 * @param Title &$nt the new Title
1441 * @param int &$newid set to be the new article ID
1442 * @access private
1443 */
1444 /* private */ function moveToNewTitle( &$nt, &$newid ) {
1445 global $wgUser, $wgLinkCache, $wgUseSquid;
1446 $fname = 'MovePageForm::moveToNewTitle';
1447 $comment = wfMsg( '1movedto2', $this->getPrefixedText(), $nt->getPrefixedText() );
1448
1449 $newid = $nt->getArticleID();
1450 $oldid = $this->getArticleID();
1451 $dbw =& wfGetDB( DB_MASTER );
1452 $now = $dbw->timestamp();
1453 $won = wfInvertTimestamp( wfTimestamp(TS_MW,$now) );
1454 wfSeedRandom();
1455 $rand = wfRandom();
1456
1457 # Rename cur entry
1458 $dbw->update( 'cur',
1459 /* SET */ array(
1460 'cur_touched' => $now,
1461 'cur_namespace' => $nt->getNamespace(),
1462 'cur_title' => $nt->getDBkey()
1463 ),
1464 /* WHERE */ array( 'cur_id' => $oldid ),
1465 $fname
1466 );
1467
1468 $wgLinkCache->clearLink( $nt->getPrefixedDBkey() );
1469
1470 # Insert redirect
1471 $dbw->insert( 'cur', array(
1472 'cur_id' => $dbw->nextSequenceValue('cur_cur_id_seq'),
1473 'cur_namespace' => $this->getNamespace(),
1474 'cur_title' => $this->getDBkey(),
1475 'cur_comment' => $comment,
1476 'cur_user' => $wgUser->getID(),
1477 'cur_user_text' => $wgUser->getName(),
1478 'cur_timestamp' => $now,
1479 'inverse_timestamp' => $won,
1480 'cur_touched' => $now,
1481 'cur_is_redirect' => 1,
1482 'cur_random' => $rand,
1483 'cur_is_new' => 1,
1484 'cur_text' => "#REDIRECT [[" . $nt->getPrefixedText() . "]]\n" ), $fname
1485 );
1486 $newid = $dbw->insertId();
1487 $wgLinkCache->clearLink( $this->getPrefixedDBkey() );
1488
1489 # Rename old entries
1490 $dbw->update(
1491 /* table */ 'old',
1492 /* SET */ array(
1493 'old_namespace' => $nt->getNamespace(),
1494 'old_title' => $nt->getDBkey()
1495 ),
1496 /* WHERE */ array(
1497 'old_namespace' => $this->getNamespace(),
1498 'old_title' => $this->getDBkey()
1499 ), $fname
1500 );
1501
1502 # Record in RC
1503 RecentChange::notifyMoveToNew( $now, $this, $nt, $wgUser, $comment );
1504
1505 # Purge squid and linkscc as per article creation
1506 Article::onArticleCreate( $nt );
1507
1508 # Any text links to the old title must be reassigned to the redirect
1509 $dbw->update( 'links', array( 'l_to' => $newid ), array( 'l_to' => $oldid ), $fname );
1510 LinkCache::linksccClearLinksTo( $oldid );
1511
1512 # Record the just-created redirect's linking to the page
1513 $dbw->insert( 'links', array( 'l_from' => $newid, 'l_to' => $oldid ), $fname );
1514
1515 # Non-existent target may have had broken links to it; these must
1516 # now be removed and made into good links.
1517 $update = new LinksUpdate( $oldid, $nt->getPrefixedDBkey() );
1518 $update->fixBrokenLinks();
1519
1520 # Purge old title from squid
1521 # The new title, and links to the new title, are purged in Article::onArticleCreate()
1522 $titles = $nt->getLinksTo();
1523 if ( $wgUseSquid ) {
1524 $urls = $this->getSquidURLs();
1525 foreach ( $titles as $linkTitle ) {
1526 $urls[] = $linkTitle->getInternalURL();
1527 }
1528 $u = new SquidUpdate( $urls );
1529 $u->doUpdate();
1530 }
1531 }
1532
1533 /**
1534 * Checks if $this can be moved to a given Title
1535 * - Selects for update, so don't call it unless you mean business
1536 *
1537 * @param Title &$nt the new title to check
1538 * @access public
1539 */
1540 function isValidMoveTarget( $nt ) {
1541 $fname = 'Title::isValidMoveTarget';
1542 $dbw =& wfGetDB( DB_MASTER );
1543
1544 # Is it a redirect?
1545 $id = $nt->getArticleID();
1546 $obj = $dbw->selectRow( 'cur', array( 'cur_is_redirect','cur_text' ),
1547 array( 'cur_id' => $id ), $fname, 'FOR UPDATE' );
1548
1549 if ( !$obj || 0 == $obj->cur_is_redirect ) {
1550 # Not a redirect
1551 return false;
1552 }
1553
1554 # Does the redirect point to the source?
1555 if ( preg_match( "/\\[\\[\\s*([^\\]\\|]*)]]/", $obj->cur_text, $m ) ) {
1556 $redirTitle = Title::newFromText( $m[1] );
1557 if( !is_object( $redirTitle ) ||
1558 $redirTitle->getPrefixedDBkey() != $this->getPrefixedDBkey() ) {
1559 return false;
1560 }
1561 }
1562
1563 # Does the article have a history?
1564 $row = $dbw->selectRow( 'old', array( 'old_id' ),
1565 array(
1566 'old_namespace' => $nt->getNamespace(),
1567 'old_title' => $nt->getDBkey()
1568 ), $fname, 'FOR UPDATE'
1569 );
1570
1571 # Return true if there was no history
1572 return $row === false;
1573 }
1574
1575 /**
1576 * Create a redirect; fails if the title already exists; does
1577 * not notify RC
1578 *
1579 * @param Title $dest the destination of the redirect
1580 * @param string $comment the comment string describing the move
1581 * @return bool true on success
1582 * @access public
1583 */
1584 function createRedirect( $dest, $comment ) {
1585 global $wgUser;
1586 if ( $this->getArticleID() ) {
1587 return false;
1588 }
1589
1590 $fname = 'Title::createRedirect';
1591 $dbw =& wfGetDB( DB_MASTER );
1592 $now = wfTimestampNow();
1593 $won = wfInvertTimestamp( $now );
1594 $seqVal = $dbw->nextSequenceValue( 'cur_cur_id_seq' );
1595
1596 $dbw->insert( 'cur', array(
1597 'cur_id' => $seqVal,
1598 'cur_namespace' => $this->getNamespace(),
1599 'cur_title' => $this->getDBkey(),
1600 'cur_comment' => $comment,
1601 'cur_user' => $wgUser->getID(),
1602 'cur_user_text' => $wgUser->getName(),
1603 'cur_timestamp' => $now,
1604 'inverse_timestamp' => $won,
1605 'cur_touched' => $now,
1606 'cur_is_redirect' => 1,
1607 'cur_is_new' => 1,
1608 'cur_text' => "#REDIRECT [[" . $dest->getPrefixedText() . "]]\n"
1609 ), $fname );
1610 $newid = $dbw->insertId();
1611 $this->resetArticleID( $newid );
1612
1613 # Link table
1614 if ( $dest->getArticleID() ) {
1615 $dbw->insert( 'links',
1616 array(
1617 'l_to' => $dest->getArticleID(),
1618 'l_from' => $newid
1619 ), $fname
1620 );
1621 } else {
1622 $dbw->insert( 'brokenlinks',
1623 array(
1624 'bl_to' => $dest->getPrefixedDBkey(),
1625 'bl_from' => $newid
1626 ), $fname
1627 );
1628 }
1629
1630 Article::onArticleCreate( $this );
1631 return true;
1632 }
1633
1634 /**
1635 * Get categories to which this Title belongs and return an array of
1636 * categories' names.
1637 *
1638 * @return array an array of parents in the form:
1639 * $parent => $currentarticle
1640 * @access public
1641 */
1642 function getParentCategories() {
1643 global $wgContLang,$wgUser;
1644
1645 $titlekey = $this->getArticleId();
1646 $sk =& $wgUser->getSkin();
1647 $parents = array();
1648 $dbr =& wfGetDB( DB_SLAVE );
1649 $cur = $dbr->tableName( 'cur' );
1650 $categorylinks = $dbr->tableName( 'categorylinks' );
1651
1652 # NEW SQL
1653 $sql = "SELECT * FROM categorylinks"
1654 ." WHERE cl_from='$titlekey'"
1655 ." AND cl_from <> '0'"
1656 ." ORDER BY cl_sortkey";
1657
1658 $res = $dbr->query ( $sql ) ;
1659
1660 if($dbr->numRows($res) > 0) {
1661 while ( $x = $dbr->fetchObject ( $res ) )
1662 //$data[] = Title::newFromText($wgContLang->getNSText ( NS_CATEGORY ).':'.$x->cl_to);
1663 $data[$wgContLang->getNSText ( NS_CATEGORY ).':'.$x->cl_to] = $this->getFullText();
1664 $dbr->freeResult ( $res ) ;
1665 } else {
1666 $data = '';
1667 }
1668 return $data;
1669 }
1670
1671 /**
1672 * Go through all parent categories of this Title
1673 * @return array
1674 * @access public
1675 */
1676 function getCategorieBrowser() {
1677 $parents = $this->getParentCategories();
1678
1679 if($parents != '') {
1680 foreach($parents as $parent => $current)
1681 {
1682 $nt = Title::newFromText($parent);
1683 $stack[$parent] = $nt->getCategorieBrowser();
1684 }
1685 return $stack;
1686 } else {
1687 return array();
1688 }
1689 }
1690
1691
1692 /**
1693 * Get an associative array for selecting this title from
1694 * the "cur" table
1695 *
1696 * @return array
1697 * @access public
1698 */
1699 function curCond() {
1700 return array( 'cur_namespace' => $this->mNamespace, 'cur_title' => $this->mDbkeyform );
1701 }
1702
1703 /**
1704 * Get an associative array for selecting this title from the
1705 * "old" table
1706 *
1707 * @return array
1708 * @access public
1709 */
1710 function oldCond() {
1711 return array( 'old_namespace' => $this->mNamespace, 'old_title' => $this->mDbkeyform );
1712 }
1713
1714 /**
1715 * Get the revision ID of the previous revision
1716 *
1717 * @param integer $revision Revision ID. Get the revision that was before this one.
1718 * @return interger $oldrevision|false
1719 */
1720 function getPreviousRevisionID( $revision ) {
1721 $dbr =& wfGetDB( DB_SLAVE );
1722 return $dbr->selectField( 'old', 'old_id',
1723 'old_title=' . $dbr->addQuotes( $this->getDBkey() ) .
1724 ' AND old_namespace=' . IntVal( $this->getNamespace() ) .
1725 ' AND old_id<' . IntVal( $revision ) . ' ORDER BY old_id DESC' );
1726 }
1727
1728 /**
1729 * Get the revision ID of the next revision
1730 *
1731 * @param integer $revision Revision ID. Get the revision that was after this one.
1732 * @return interger $oldrevision|false
1733 */
1734 function getNextRevisionID( $revision ) {
1735 $dbr =& wfGetDB( DB_SLAVE );
1736 return $dbr->selectField( 'old', 'old_id',
1737 'old_title=' . $dbr->addQuotes( $this->getDBkey() ) .
1738 ' AND old_namespace=' . IntVal( $this->getNamespace() ) .
1739 ' AND old_id>' . IntVal( $revision ) . ' ORDER BY old_id' );
1740 }
1741
1742 }
1743 ?>