revert r14636, private static arrays seem to crash APC on half of our boxen, will...
[lhc/web/wiklou.git] / includes / Title.php
1 <?php
2 /**
3 * See title.txt
4 *
5 * @package MediaWiki
6 */
7
8 /** */
9 require_once( 'normal/UtfNormal.php' );
10
11 $wgTitleInterwikiCache = array();
12 $wgTitleCache = array();
13
14 define ( 'GAID_FOR_UPDATE', 1 );
15
16 # Title::newFromTitle maintains a cache to avoid
17 # expensive re-normalization of commonly used titles.
18 # On a batch operation this can become a memory leak
19 # if not bounded. After hitting this many titles,
20 # reset the cache.
21 define( 'MW_TITLECACHE_MAX', 1000 );
22
23 /**
24 * Title class
25 * - Represents a title, which may contain an interwiki designation or namespace
26 * - Can fetch various kinds of data from the database, albeit inefficiently.
27 *
28 * @package MediaWiki
29 */
30 class Title {
31 /**
32 * All member variables should be considered private
33 * Please use the accessor functions
34 */
35
36 /**#@+
37 * @access private
38 */
39
40 var $mTextform; # Text form (spaces not underscores) of the main part
41 var $mUrlform; # URL-encoded form of the main part
42 var $mDbkeyform; # Main part with underscores
43 var $mNamespace; # Namespace index, i.e. one of the NS_xxxx constants
44 var $mInterwiki; # Interwiki prefix (or null string)
45 var $mFragment; # Title fragment (i.e. the bit after the #)
46 var $mArticleID; # Article ID, fetched from the link cache on demand
47 var $mLatestID; # ID of most recent revision
48 var $mRestrictions; # Array of groups allowed to edit this article
49 # Only null or "sysop" are supported
50 var $mRestrictionsLoaded; # Boolean for initialisation on demand
51 var $mPrefixedText; # Text form including namespace/interwiki, initialised on demand
52 var $mDefaultNamespace; # Namespace index when there is no namespace
53 # Zero except in {{transclusion}} tags
54 var $mWatched; # Is $wgUser watching this page? NULL if unfilled, accessed through userIsWatching()
55 /**#@-*/
56
57
58 /**
59 * Constructor
60 * @access private
61 */
62 /* private */ function Title() {
63 $this->mInterwiki = $this->mUrlform =
64 $this->mTextform = $this->mDbkeyform = '';
65 $this->mArticleID = -1;
66 $this->mNamespace = NS_MAIN;
67 $this->mRestrictionsLoaded = false;
68 $this->mRestrictions = array();
69 # Dont change the following, NS_MAIN is hardcoded in several place
70 # See bug #696
71 $this->mDefaultNamespace = NS_MAIN;
72 $this->mWatched = NULL;
73 $this->mLatestID = false;
74 }
75
76 /**
77 * Create a new Title from a prefixed DB key
78 * @param string $key The database key, which has underscores
79 * instead of spaces, possibly including namespace and
80 * interwiki prefixes
81 * @return Title the new object, or NULL on an error
82 * @static
83 * @access public
84 */
85 /* static */ function newFromDBkey( $key ) {
86 $t = new Title();
87 $t->mDbkeyform = $key;
88 if( $t->secureAndSplit() )
89 return $t;
90 else
91 return NULL;
92 }
93
94 /**
95 * Create a new Title from text, such as what one would
96 * find in a link. Decodes any HTML entities in the text.
97 *
98 * @param string $text the link text; spaces, prefixes,
99 * and an initial ':' indicating the main namespace
100 * are accepted
101 * @param int $defaultNamespace the namespace to use if
102 * none is specified by a prefix
103 * @return Title the new object, or NULL on an error
104 * @static
105 * @access public
106 */
107 function newFromText( $text, $defaultNamespace = NS_MAIN ) {
108 global $wgTitleCache;
109 $fname = 'Title::newFromText';
110
111 if( is_object( $text ) ) {
112 throw new MWException( 'Title::newFromText given an object' );
113 }
114
115 /**
116 * Wiki pages often contain multiple links to the same page.
117 * Title normalization and parsing can become expensive on
118 * pages with many links, so we can save a little time by
119 * caching them.
120 *
121 * In theory these are value objects and won't get changed...
122 */
123 if( $defaultNamespace == NS_MAIN && isset( $wgTitleCache[$text] ) ) {
124 return $wgTitleCache[$text];
125 }
126
127 /**
128 * Convert things like &eacute; &#257; or &#x3017; into real text...
129 */
130 $filteredText = Sanitizer::decodeCharReferences( $text );
131
132 $t =& new Title();
133 $t->mDbkeyform = str_replace( ' ', '_', $filteredText );
134 $t->mDefaultNamespace = $defaultNamespace;
135
136 static $cachedcount = 0 ;
137 if( $t->secureAndSplit() ) {
138 if( $defaultNamespace == NS_MAIN ) {
139 if( $cachedcount >= MW_TITLECACHE_MAX ) {
140 # Avoid memory leaks on mass operations...
141 $wgTitleCache = array();
142 $cachedcount=0;
143 }
144 $cachedcount++;
145 $wgTitleCache[$text] =& $t;
146 }
147 return $t;
148 } else {
149 $ret = NULL;
150 return $ret;
151 }
152 }
153
154 /**
155 * Create a new Title from URL-encoded text. Ensures that
156 * the given title's length does not exceed the maximum.
157 * @param string $url the title, as might be taken from a URL
158 * @return Title the new object, or NULL on an error
159 * @static
160 * @access public
161 */
162 function newFromURL( $url ) {
163 global $wgLegalTitleChars;
164 $t = new Title();
165
166 # For compatibility with old buggy URLs. "+" is usually not valid in titles,
167 # but some URLs used it as a space replacement and they still come
168 # from some external search tools.
169 if ( strpos( $wgLegalTitleChars, '+' ) === false ) {
170 $url = str_replace( '+', ' ', $url );
171 }
172
173 $t->mDbkeyform = str_replace( ' ', '_', $url );
174 if( $t->secureAndSplit() ) {
175 return $t;
176 } else {
177 return NULL;
178 }
179 }
180
181 /**
182 * Create a new Title from an article ID
183 *
184 * @todo This is inefficiently implemented, the page row is requested
185 * but not used for anything else
186 *
187 * @param int $id the page_id corresponding to the Title to create
188 * @return Title the new object, or NULL on an error
189 * @access public
190 * @static
191 */
192 function newFromID( $id ) {
193 $fname = 'Title::newFromID';
194 $dbr =& wfGetDB( DB_SLAVE );
195 $row = $dbr->selectRow( 'page', array( 'page_namespace', 'page_title' ),
196 array( 'page_id' => $id ), $fname );
197 if ( $row !== false ) {
198 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
199 } else {
200 $title = NULL;
201 }
202 return $title;
203 }
204
205 /**
206 * Create a new Title from a namespace index and a DB key.
207 * It's assumed that $ns and $title are *valid*, for instance when
208 * they came directly from the database or a special page name.
209 * For convenience, spaces are converted to underscores so that
210 * eg user_text fields can be used directly.
211 *
212 * @param int $ns the namespace of the article
213 * @param string $title the unprefixed database key form
214 * @return Title the new object
215 * @static
216 * @access public
217 */
218 function &makeTitle( $ns, $title ) {
219 $t =& new Title();
220 $t->mInterwiki = '';
221 $t->mFragment = '';
222 $t->mNamespace = intval( $ns );
223 $t->mDbkeyform = str_replace( ' ', '_', $title );
224 $t->mArticleID = ( $ns >= 0 ) ? -1 : 0;
225 $t->mUrlform = wfUrlencode( $t->mDbkeyform );
226 $t->mTextform = str_replace( '_', ' ', $title );
227 return $t;
228 }
229
230 /**
231 * Create a new Title frrom a namespace index and a DB key.
232 * The parameters will be checked for validity, which is a bit slower
233 * than makeTitle() but safer for user-provided data.
234 *
235 * @param int $ns the namespace of the article
236 * @param string $title the database key form
237 * @return Title the new object, or NULL on an error
238 * @static
239 * @access public
240 */
241 function makeTitleSafe( $ns, $title ) {
242 $t = new Title();
243 $t->mDbkeyform = Title::makeName( $ns, $title );
244 if( $t->secureAndSplit() ) {
245 return $t;
246 } else {
247 return NULL;
248 }
249 }
250
251 /**
252 * Create a new Title for the Main Page
253 *
254 * @static
255 * @return Title the new object
256 * @access public
257 */
258 function newMainPage() {
259 return Title::newFromText( wfMsgForContent( 'mainpage' ) );
260 }
261
262 /**
263 * Create a new Title for a redirect
264 * @param string $text the redirect title text
265 * @return Title the new object, or NULL if the text is not a
266 * valid redirect
267 * @static
268 * @access public
269 */
270 function newFromRedirect( $text ) {
271 global $wgMwRedir;
272 $rt = NULL;
273 if ( $wgMwRedir->matchStart( $text ) ) {
274 if ( preg_match( '/\[{2}(.*?)(?:\||\]{2})/', $text, $m ) ) {
275 # categories are escaped using : for example one can enter:
276 # #REDIRECT [[:Category:Music]]. Need to remove it.
277 if ( substr($m[1],0,1) == ':') {
278 # We don't want to keep the ':'
279 $m[1] = substr( $m[1], 1 );
280 }
281
282 $rt = Title::newFromText( $m[1] );
283 # Disallow redirects to Special:Userlogout
284 if ( !is_null($rt) && $rt->getNamespace() == NS_SPECIAL && preg_match( '/^Userlogout/i', $rt->getText() ) ) {
285 $rt = NULL;
286 }
287 }
288 }
289 return $rt;
290 }
291
292 #----------------------------------------------------------------------------
293 # Static functions
294 #----------------------------------------------------------------------------
295
296 /**
297 * Get the prefixed DB key associated with an ID
298 * @param int $id the page_id of the article
299 * @return Title an object representing the article, or NULL
300 * if no such article was found
301 * @static
302 * @access public
303 */
304 function nameOf( $id ) {
305 $fname = 'Title::nameOf';
306 $dbr =& wfGetDB( DB_SLAVE );
307
308 $s = $dbr->selectRow( 'page', array( 'page_namespace','page_title' ), array( 'page_id' => $id ), $fname );
309 if ( $s === false ) { return NULL; }
310
311 $n = Title::makeName( $s->page_namespace, $s->page_title );
312 return $n;
313 }
314
315 /**
316 * Get a regex character class describing the legal characters in a link
317 * @return string the list of characters, not delimited
318 * @static
319 * @access public
320 */
321 function legalChars() {
322 global $wgLegalTitleChars;
323 return $wgLegalTitleChars;
324 }
325
326 /**
327 * Get a string representation of a title suitable for
328 * including in a search index
329 *
330 * @param int $ns a namespace index
331 * @param string $title text-form main part
332 * @return string a stripped-down title string ready for the
333 * search index
334 */
335 /* static */ function indexTitle( $ns, $title ) {
336 global $wgContLang;
337
338 $lc = SearchEngine::legalSearchChars() . '&#;';
339 $t = $wgContLang->stripForSearch( $title );
340 $t = preg_replace( "/[^{$lc}]+/", ' ', $t );
341 $t = strtolower( $t );
342
343 # Handle 's, s'
344 $t = preg_replace( "/([{$lc}]+)'s( |$)/", "\\1 \\1's ", $t );
345 $t = preg_replace( "/([{$lc}]+)s'( |$)/", "\\1s ", $t );
346
347 $t = preg_replace( "/\\s+/", ' ', $t );
348
349 if ( $ns == NS_IMAGE ) {
350 $t = preg_replace( "/ (png|gif|jpg|jpeg|ogg)$/", "", $t );
351 }
352 return trim( $t );
353 }
354
355 /*
356 * Make a prefixed DB key from a DB key and a namespace index
357 * @param int $ns numerical representation of the namespace
358 * @param string $title the DB key form the title
359 * @return string the prefixed form of the title
360 */
361 /* static */ function makeName( $ns, $title ) {
362 global $wgContLang;
363
364 $n = $wgContLang->getNsText( $ns );
365 return $n == '' ? $title : "$n:$title";
366 }
367
368 /**
369 * Returns the URL associated with an interwiki prefix
370 * @param string $key the interwiki prefix (e.g. "MeatBall")
371 * @return the associated URL, containing "$1", which should be
372 * replaced by an article title
373 * @static (arguably)
374 * @access public
375 */
376 function getInterwikiLink( $key ) {
377 global $wgMemc, $wgDBname, $wgInterwikiExpiry, $wgTitleInterwikiCache;
378 global $wgInterwikiCache;
379 $fname = 'Title::getInterwikiLink';
380
381 $key = strtolower( $key );
382
383 $k = $wgDBname.':interwiki:'.$key;
384 if( array_key_exists( $k, $wgTitleInterwikiCache ) ) {
385 return $wgTitleInterwikiCache[$k]->iw_url;
386 }
387
388 if ($wgInterwikiCache) {
389 return Title::getInterwikiCached( $key );
390 }
391
392 $s = $wgMemc->get( $k );
393 # Ignore old keys with no iw_local
394 if( $s && isset( $s->iw_local ) && isset($s->iw_trans)) {
395 $wgTitleInterwikiCache[$k] = $s;
396 return $s->iw_url;
397 }
398
399 $dbr =& wfGetDB( DB_SLAVE );
400 $res = $dbr->select( 'interwiki',
401 array( 'iw_url', 'iw_local', 'iw_trans' ),
402 array( 'iw_prefix' => $key ), $fname );
403 if( !$res ) {
404 return '';
405 }
406
407 $s = $dbr->fetchObject( $res );
408 if( !$s ) {
409 # Cache non-existence: create a blank object and save it to memcached
410 $s = (object)false;
411 $s->iw_url = '';
412 $s->iw_local = 0;
413 $s->iw_trans = 0;
414 }
415 $wgMemc->set( $k, $s, $wgInterwikiExpiry );
416 $wgTitleInterwikiCache[$k] = $s;
417
418 return $s->iw_url;
419 }
420
421 /**
422 * Fetch interwiki prefix data from local cache in constant database
423 *
424 * More logic is explained in DefaultSettings
425 *
426 * @return string URL of interwiki site
427 * @access public
428 */
429 function getInterwikiCached( $key ) {
430 global $wgDBname, $wgInterwikiCache, $wgInterwikiScopes, $wgInterwikiFallbackSite;
431 global $wgTitleInterwikiCache;
432 static $db, $site;
433
434 if (!$db)
435 $db=dba_open($wgInterwikiCache,'r','cdb');
436 /* Resolve site name */
437 if ($wgInterwikiScopes>=3 and !$site) {
438 $site = dba_fetch("__sites:{$wgDBname}", $db);
439 if ($site=="")
440 $site = $wgInterwikiFallbackSite;
441 }
442 $value = dba_fetch("{$wgDBname}:{$key}", $db);
443 if ($value=='' and $wgInterwikiScopes>=3) {
444 /* try site-level */
445 $value = dba_fetch("_{$site}:{$key}", $db);
446 }
447 if ($value=='' and $wgInterwikiScopes>=2) {
448 /* try globals */
449 $value = dba_fetch("__global:{$key}", $db);
450 }
451 if ($value=='undef')
452 $value='';
453 $s = (object)false;
454 $s->iw_url = '';
455 $s->iw_local = 0;
456 $s->iw_trans = 0;
457 if ($value!='') {
458 list($local,$url)=explode(' ',$value,2);
459 $s->iw_url=$url;
460 $s->iw_local=(int)$local;
461 }
462 $wgTitleInterwikiCache[$wgDBname.':interwiki:'.$key] = $s;
463 return $s->iw_url;
464 }
465 /**
466 * Determine whether the object refers to a page within
467 * this project.
468 *
469 * @return bool TRUE if this is an in-project interwiki link
470 * or a wikilink, FALSE otherwise
471 * @access public
472 */
473 function isLocal() {
474 global $wgTitleInterwikiCache, $wgDBname;
475
476 if ( $this->mInterwiki != '' ) {
477 # Make sure key is loaded into cache
478 $this->getInterwikiLink( $this->mInterwiki );
479 $k = $wgDBname.':interwiki:' . $this->mInterwiki;
480 return (bool)($wgTitleInterwikiCache[$k]->iw_local);
481 } else {
482 return true;
483 }
484 }
485
486 /**
487 * Determine whether the object refers to a page within
488 * this project and is transcludable.
489 *
490 * @return bool TRUE if this is transcludable
491 * @access public
492 */
493 function isTrans() {
494 global $wgTitleInterwikiCache, $wgDBname;
495
496 if ($this->mInterwiki == '')
497 return false;
498 # Make sure key is loaded into cache
499 $this->getInterwikiLink( $this->mInterwiki );
500 $k = $wgDBname.':interwiki:' . $this->mInterwiki;
501 return (bool)($wgTitleInterwikiCache[$k]->iw_trans);
502 }
503
504 /**
505 * Update the page_touched field for an array of title objects
506 * @todo Inefficient unless the IDs are already loaded into the
507 * link cache
508 * @param array $titles an array of Title objects to be touched
509 * @param string $timestamp the timestamp to use instead of the
510 * default current time
511 * @static
512 * @access public
513 */
514 function touchArray( $titles, $timestamp = '' ) {
515
516 if ( count( $titles ) == 0 ) {
517 return;
518 }
519 $dbw =& wfGetDB( DB_MASTER );
520 if ( $timestamp == '' ) {
521 $timestamp = $dbw->timestamp();
522 }
523 /*
524 $page = $dbw->tableName( 'page' );
525 $sql = "UPDATE $page SET page_touched='{$timestamp}' WHERE page_id IN (";
526 $first = true;
527
528 foreach ( $titles as $title ) {
529 if ( $wgUseFileCache ) {
530 $cm = new CacheManager($title);
531 @unlink($cm->fileCacheName());
532 }
533
534 if ( ! $first ) {
535 $sql .= ',';
536 }
537 $first = false;
538 $sql .= $title->getArticleID();
539 }
540 $sql .= ')';
541 if ( ! $first ) {
542 $dbw->query( $sql, 'Title::touchArray' );
543 }
544 */
545 // hack hack hack -- brion 2005-07-11. this was unfriendly to db.
546 // do them in small chunks:
547 $fname = 'Title::touchArray';
548 foreach( $titles as $title ) {
549 $dbw->update( 'page',
550 array( 'page_touched' => $timestamp ),
551 array(
552 'page_namespace' => $title->getNamespace(),
553 'page_title' => $title->getDBkey() ),
554 $fname );
555 }
556 }
557
558 #----------------------------------------------------------------------------
559 # Other stuff
560 #----------------------------------------------------------------------------
561
562 /** Simple accessors */
563 /**
564 * Get the text form (spaces not underscores) of the main part
565 * @return string
566 * @access public
567 */
568 function getText() { return $this->mTextform; }
569 /**
570 * Get the URL-encoded form of the main part
571 * @return string
572 * @access public
573 */
574 function getPartialURL() { return $this->mUrlform; }
575 /**
576 * Get the main part with underscores
577 * @return string
578 * @access public
579 */
580 function getDBkey() { return $this->mDbkeyform; }
581 /**
582 * Get the namespace index, i.e. one of the NS_xxxx constants
583 * @return int
584 * @access public
585 */
586 function getNamespace() { return $this->mNamespace; }
587 /**
588 * Get the namespace text
589 * @return string
590 * @access public
591 */
592 function getNsText() {
593 global $wgContLang;
594 return $wgContLang->getNsText( $this->mNamespace );
595 }
596 /**
597 * Get the namespace text of the subject (rather than talk) page
598 * @return string
599 * @access public
600 */
601 function getSubjectNsText() {
602 global $wgContLang;
603 return $wgContLang->getNsText( Namespace::getSubject( $this->mNamespace ) );
604 }
605
606 /**
607 * Get the namespace text of the talk page
608 * @return string
609 */
610 function getTalkNsText() {
611 global $wgContLang;
612 return( $wgContLang->getNsText( Namespace::getTalk( $this->mNamespace ) ) );
613 }
614
615 /**
616 * Could this title have a corresponding talk page?
617 * @return bool
618 */
619 function canTalk() {
620 return( Namespace::canTalk( $this->mNamespace ) );
621 }
622
623 /**
624 * Get the interwiki prefix (or null string)
625 * @return string
626 * @access public
627 */
628 function getInterwiki() { return $this->mInterwiki; }
629 /**
630 * Get the Title fragment (i.e. the bit after the #)
631 * @return string
632 * @access public
633 */
634 function getFragment() { return $this->mFragment; }
635 /**
636 * Get the default namespace index, for when there is no namespace
637 * @return int
638 * @access public
639 */
640 function getDefaultNamespace() { return $this->mDefaultNamespace; }
641
642 /**
643 * Get title for search index
644 * @return string a stripped-down title string ready for the
645 * search index
646 */
647 function getIndexTitle() {
648 return Title::indexTitle( $this->mNamespace, $this->mTextform );
649 }
650
651 /**
652 * Get the prefixed database key form
653 * @return string the prefixed title, with underscores and
654 * any interwiki and namespace prefixes
655 * @access public
656 */
657 function getPrefixedDBkey() {
658 $s = $this->prefix( $this->mDbkeyform );
659 $s = str_replace( ' ', '_', $s );
660 return $s;
661 }
662
663 /**
664 * Get the prefixed title with spaces.
665 * This is the form usually used for display
666 * @return string the prefixed title, with spaces
667 * @access public
668 */
669 function getPrefixedText() {
670 if ( empty( $this->mPrefixedText ) ) { // FIXME: bad usage of empty() ?
671 $s = $this->prefix( $this->mTextform );
672 $s = str_replace( '_', ' ', $s );
673 $this->mPrefixedText = $s;
674 }
675 return $this->mPrefixedText;
676 }
677
678 /**
679 * Get the prefixed title with spaces, plus any fragment
680 * (part beginning with '#')
681 * @return string the prefixed title, with spaces and
682 * the fragment, including '#'
683 * @access public
684 */
685 function getFullText() {
686 $text = $this->getPrefixedText();
687 if( '' != $this->mFragment ) {
688 $text .= '#' . $this->mFragment;
689 }
690 return $text;
691 }
692
693 /**
694 * Get the base name, i.e. the leftmost parts before the /
695 * @return string Base name
696 */
697 function getBaseText() {
698 global $wgNamespacesWithSubpages;
699 if( isset( $wgNamespacesWithSubpages[ $this->mNamespace ] ) && $wgNamespacesWithSubpages[ $this->mNamespace ] ) {
700 $parts = explode( '/', $this->getText() );
701 # Don't discard the real title if there's no subpage involved
702 if( count( $parts ) > 1 )
703 unset( $parts[ count( $parts ) - 1 ] );
704 return implode( '/', $parts );
705 } else {
706 return $this->getText();
707 }
708 }
709
710 /**
711 * Get the lowest-level subpage name, i.e. the rightmost part after /
712 * @return string Subpage name
713 */
714 function getSubpageText() {
715 global $wgNamespacesWithSubpages;
716 if( isset( $wgNamespacesWithSubpages[ $this->mNamespace ] ) && $wgNamespacesWithSubpages[ $this->mNamespace ] ) {
717 $parts = explode( '/', $this->mTextform );
718 return( $parts[ count( $parts ) - 1 ] );
719 } else {
720 return( $this->mTextform );
721 }
722 }
723
724 /**
725 * Get a URL-encoded form of the subpage text
726 * @return string URL-encoded subpage name
727 */
728 function getSubpageUrlForm() {
729 $text = $this->getSubpageText();
730 $text = wfUrlencode( str_replace( ' ', '_', $text ) );
731 $text = str_replace( '%28', '(', str_replace( '%29', ')', $text ) ); # Clean up the URL; per below, this might not be safe
732 return( $text );
733 }
734
735 /**
736 * Get a URL-encoded title (not an actual URL) including interwiki
737 * @return string the URL-encoded form
738 * @access public
739 */
740 function getPrefixedURL() {
741 $s = $this->prefix( $this->mDbkeyform );
742 $s = str_replace( ' ', '_', $s );
743
744 $s = wfUrlencode ( $s ) ;
745
746 # Cleaning up URL to make it look nice -- is this safe?
747 $s = str_replace( '%28', '(', $s );
748 $s = str_replace( '%29', ')', $s );
749
750 return $s;
751 }
752
753 /**
754 * Get a real URL referring to this title, with interwiki link and
755 * fragment
756 *
757 * @param string $query an optional query string, not used
758 * for interwiki links
759 * @return string the URL
760 * @access public
761 */
762 function getFullURL( $query = '' ) {
763 global $wgContLang, $wgServer, $wgRequest;
764
765 if ( '' == $this->mInterwiki ) {
766 $url = $this->getLocalUrl( $query );
767
768 // Ugly quick hack to avoid duplicate prefixes (bug 4571 etc)
769 // Correct fix would be to move the prepending elsewhere.
770 if ($wgRequest->getVal('action') != 'render') {
771 $url = $wgServer . $url;
772 }
773 } else {
774 $baseUrl = $this->getInterwikiLink( $this->mInterwiki );
775
776 $namespace = $wgContLang->getNsText( $this->mNamespace );
777 if ( '' != $namespace ) {
778 # Can this actually happen? Interwikis shouldn't be parsed.
779 $namespace .= ':';
780 }
781 $url = str_replace( '$1', $namespace . $this->mUrlform, $baseUrl );
782 if( $query != '' ) {
783 if( false === strpos( $url, '?' ) ) {
784 $url .= '?';
785 } else {
786 $url .= '&';
787 }
788 $url .= $query;
789 }
790 }
791
792 # Finally, add the fragment.
793 if ( '' != $this->mFragment ) {
794 $url .= '#' . $this->mFragment;
795 }
796
797 wfRunHooks( 'GetFullURL', array( &$this, &$url, $query ) );
798 return $url;
799 }
800
801 /**
802 * Get a URL with no fragment or server name. If this page is generated
803 * with action=render, $wgServer is prepended.
804 * @param string $query an optional query string; if not specified,
805 * $wgArticlePath will be used.
806 * @return string the URL
807 * @access public
808 */
809 function getLocalURL( $query = '' ) {
810 global $wgArticlePath, $wgScript, $wgServer, $wgRequest;
811
812 if ( $this->isExternal() ) {
813 $url = $this->getFullURL();
814 if ( $query ) {
815 // This is currently only used for edit section links in the
816 // context of interwiki transclusion. In theory we should
817 // append the query to the end of any existing query string,
818 // but interwiki transclusion is already broken in that case.
819 $url .= "?$query";
820 }
821 } else {
822 $dbkey = wfUrlencode( $this->getPrefixedDBkey() );
823 if ( $query == '' ) {
824 $url = str_replace( '$1', $dbkey, $wgArticlePath );
825 } else {
826 global $wgActionPaths;
827 $url = false;
828 if( !empty( $wgActionPaths ) &&
829 preg_match( '/^(.*&|)action=([^&]*)(&(.*)|)$/', $query, $matches ) )
830 {
831 $action = urldecode( $matches[2] );
832 if( isset( $wgActionPaths[$action] ) ) {
833 $query = $matches[1];
834 if( isset( $matches[4] ) ) $query .= $matches[4];
835 $url = str_replace( '$1', $dbkey, $wgActionPaths[$action] );
836 if( $query != '' ) $url .= '?' . $query;
837 }
838 }
839 if ( $url === false ) {
840 if ( $query == '-' ) {
841 $query = '';
842 }
843 $url = "{$wgScript}?title={$dbkey}&{$query}";
844 }
845 }
846
847 // FIXME: this causes breakage in various places when we
848 // actually expected a local URL and end up with dupe prefixes.
849 if ($wgRequest->getVal('action') == 'render') {
850 $url = $wgServer . $url;
851 }
852 }
853 wfRunHooks( 'GetLocalURL', array( &$this, &$url, $query ) );
854 return $url;
855 }
856
857 /**
858 * Get an HTML-escaped version of the URL form, suitable for
859 * using in a link, without a server name or fragment
860 * @param string $query an optional query string
861 * @return string the URL
862 * @access public
863 */
864 function escapeLocalURL( $query = '' ) {
865 return htmlspecialchars( $this->getLocalURL( $query ) );
866 }
867
868 /**
869 * Get an HTML-escaped version of the URL form, suitable for
870 * using in a link, including the server name and fragment
871 *
872 * @return string the URL
873 * @param string $query an optional query string
874 * @access public
875 */
876 function escapeFullURL( $query = '' ) {
877 return htmlspecialchars( $this->getFullURL( $query ) );
878 }
879
880 /**
881 * Get the URL form for an internal link.
882 * - Used in various Squid-related code, in case we have a different
883 * internal hostname for the server from the exposed one.
884 *
885 * @param string $query an optional query string
886 * @return string the URL
887 * @access public
888 */
889 function getInternalURL( $query = '' ) {
890 global $wgInternalServer;
891 $url = $wgInternalServer . $this->getLocalURL( $query );
892 wfRunHooks( 'GetInternalURL', array( &$this, &$url, $query ) );
893 return $url;
894 }
895
896 /**
897 * Get the edit URL for this Title
898 * @return string the URL, or a null string if this is an
899 * interwiki link
900 * @access public
901 */
902 function getEditURL() {
903 if ( '' != $this->mInterwiki ) { return ''; }
904 $s = $this->getLocalURL( 'action=edit' );
905
906 return $s;
907 }
908
909 /**
910 * Get the HTML-escaped displayable text form.
911 * Used for the title field in <a> tags.
912 * @return string the text, including any prefixes
913 * @access public
914 */
915 function getEscapedText() {
916 return htmlspecialchars( $this->getPrefixedText() );
917 }
918
919 /**
920 * Is this Title interwiki?
921 * @return boolean
922 * @access public
923 */
924 function isExternal() { return ( '' != $this->mInterwiki ); }
925
926 /**
927 * Is this page "semi-protected" - the *only* protection is autoconfirm?
928 *
929 * @param string Action to check (default: edit)
930 * @return bool
931 */
932 function isSemiProtected( $action = 'edit' ) {
933 $restrictions = $this->getRestrictions( $action );
934 # We do a full compare because this could be an array
935 foreach( $restrictions as $restriction ) {
936 if( strtolower( $restriction ) != 'autoconfirmed' ) {
937 return( false );
938 }
939 }
940 return( true );
941 }
942
943 /**
944 * Does the title correspond to a protected article?
945 * @param string $what the action the page is protected from,
946 * by default checks move and edit
947 * @return boolean
948 * @access public
949 */
950 function isProtected( $action = '' ) {
951 global $wgRestrictionLevels;
952 if ( -1 == $this->mNamespace ) { return true; }
953
954 if( $action == 'edit' || $action == '' ) {
955 $r = $this->getRestrictions( 'edit' );
956 foreach( $wgRestrictionLevels as $level ) {
957 if( in_array( $level, $r ) && $level != '' ) {
958 return( true );
959 }
960 }
961 }
962
963 if( $action == 'move' || $action == '' ) {
964 $r = $this->getRestrictions( 'move' );
965 foreach( $wgRestrictionLevels as $level ) {
966 if( in_array( $level, $r ) && $level != '' ) {
967 return( true );
968 }
969 }
970 }
971
972 return false;
973 }
974
975 /**
976 * Is $wgUser is watching this page?
977 * @return boolean
978 * @access public
979 */
980 function userIsWatching() {
981 global $wgUser;
982
983 if ( is_null( $this->mWatched ) ) {
984 if ( -1 == $this->mNamespace || 0 == $wgUser->getID()) {
985 $this->mWatched = false;
986 } else {
987 $this->mWatched = $wgUser->isWatched( $this );
988 }
989 }
990 return $this->mWatched;
991 }
992
993 /**
994 * Can $wgUser perform $action this page?
995 * @param string $action action that permission needs to be checked for
996 * @return boolean
997 * @access private
998 */
999 function userCan($action) {
1000 $fname = 'Title::userCan';
1001 wfProfileIn( $fname );
1002
1003 global $wgUser;
1004
1005 $result = null;
1006 wfRunHooks( 'userCan', array( &$this, &$wgUser, $action, &$result ) );
1007 if ( $result !== null ) {
1008 wfProfileOut( $fname );
1009 return $result;
1010 }
1011
1012 if( NS_SPECIAL == $this->mNamespace ) {
1013 wfProfileOut( $fname );
1014 return false;
1015 }
1016 // XXX: This is the code that prevents unprotecting a page in NS_MEDIAWIKI
1017 // from taking effect -ævar
1018 if( NS_MEDIAWIKI == $this->mNamespace &&
1019 !$wgUser->isAllowed('editinterface') ) {
1020 wfProfileOut( $fname );
1021 return false;
1022 }
1023
1024 if( $this->mDbkeyform == '_' ) {
1025 # FIXME: Is this necessary? Shouldn't be allowed anyway...
1026 wfProfileOut( $fname );
1027 return false;
1028 }
1029
1030 # protect css/js subpages of user pages
1031 # XXX: this might be better using restrictions
1032 # XXX: Find a way to work around the php bug that prevents using $this->userCanEditCssJsSubpage() from working
1033 if( NS_USER == $this->mNamespace
1034 && preg_match("/\\.(css|js)$/", $this->mTextform )
1035 && !$wgUser->isAllowed('editinterface')
1036 && !preg_match('/^'.preg_quote($wgUser->getName(), '/').'\//', $this->mTextform) ) {
1037 wfProfileOut( $fname );
1038 return false;
1039 }
1040
1041 foreach( $this->getRestrictions($action) as $right ) {
1042 // Backwards compatibility, rewrite sysop -> protect
1043 if ( $right == 'sysop' ) {
1044 $right = 'protect';
1045 }
1046 if( '' != $right && !$wgUser->isAllowed( $right ) ) {
1047 wfProfileOut( $fname );
1048 return false;
1049 }
1050 }
1051
1052 if( $action == 'move' &&
1053 !( $this->isMovable() && $wgUser->isAllowed( 'move' ) ) ) {
1054 wfProfileOut( $fname );
1055 return false;
1056 }
1057
1058 if( $action == 'create' ) {
1059 if( ( $this->isTalkPage() && !$wgUser->isAllowed( 'createtalk' ) ) ||
1060 ( !$this->isTalkPage() && !$wgUser->isAllowed( 'createpage' ) ) ) {
1061 return false;
1062 }
1063 }
1064
1065 wfProfileOut( $fname );
1066 return true;
1067 }
1068
1069 /**
1070 * Can $wgUser edit this page?
1071 * @return boolean
1072 * @access public
1073 */
1074 function userCanEdit() {
1075 return $this->userCan('edit');
1076 }
1077
1078 /**
1079 * Can $wgUser move this page?
1080 * @return boolean
1081 * @access public
1082 */
1083 function userCanMove() {
1084 return $this->userCan('move');
1085 }
1086
1087 /**
1088 * Would anybody with sufficient privileges be able to move this page?
1089 * Some pages just aren't movable.
1090 *
1091 * @return boolean
1092 * @access public
1093 */
1094 function isMovable() {
1095 return Namespace::isMovable( $this->getNamespace() )
1096 && $this->getInterwiki() == '';
1097 }
1098
1099 /**
1100 * Can $wgUser read this page?
1101 * @return boolean
1102 * @access public
1103 */
1104 function userCanRead() {
1105 global $wgUser;
1106
1107 $result = null;
1108 wfRunHooks( 'userCan', array( &$this, &$wgUser, 'read', &$result ) );
1109 if ( $result !== null ) {
1110 return $result;
1111 }
1112
1113 if( $wgUser->isAllowed('read') ) {
1114 return true;
1115 } else {
1116 global $wgWhitelistRead;
1117
1118 /** If anon users can create an account,
1119 they need to reach the login page first! */
1120 if( $wgUser->isAllowed( 'createaccount' )
1121 && $this->getNamespace() == NS_SPECIAL
1122 && $this->getText() == 'Userlogin' ) {
1123 return true;
1124 }
1125
1126 /** some pages are explicitly allowed */
1127 $name = $this->getPrefixedText();
1128 if( $wgWhitelistRead && in_array( $name, $wgWhitelistRead ) ) {
1129 return true;
1130 }
1131
1132 # Compatibility with old settings
1133 if( $wgWhitelistRead && $this->getNamespace() == NS_MAIN ) {
1134 if( in_array( ':' . $name, $wgWhitelistRead ) ) {
1135 return true;
1136 }
1137 }
1138 }
1139 return false;
1140 }
1141
1142 /**
1143 * Is this a talk page of some sort?
1144 * @return bool
1145 * @access public
1146 */
1147 function isTalkPage() {
1148 return Namespace::isTalk( $this->getNamespace() );
1149 }
1150
1151 /**
1152 * Is this a .css or .js subpage of a user page?
1153 * @return bool
1154 * @access public
1155 */
1156 function isCssJsSubpage() {
1157 return ( NS_USER == $this->mNamespace and preg_match("/\\.(css|js)$/", $this->mTextform ) );
1158 }
1159 /**
1160 * Is this a *valid* .css or .js subpage of a user page?
1161 * Check that the corresponding skin exists
1162 */
1163 function isValidCssJsSubpage() {
1164 global $wgValidSkinNames;
1165 return( $this->isCssJsSubpage() && array_key_exists( $this->getSkinFromCssJsSubpage(), $wgValidSkinNames ) );
1166 }
1167 /**
1168 * Trim down a .css or .js subpage title to get the corresponding skin name
1169 */
1170 function getSkinFromCssJsSubpage() {
1171 $subpage = explode( '/', $this->mTextform );
1172 $subpage = $subpage[ count( $subpage ) - 1 ];
1173 return( str_replace( array( '.css', '.js' ), array( '', '' ), $subpage ) );
1174 }
1175 /**
1176 * Is this a .css subpage of a user page?
1177 * @return bool
1178 * @access public
1179 */
1180 function isCssSubpage() {
1181 return ( NS_USER == $this->mNamespace and preg_match("/\\.css$/", $this->mTextform ) );
1182 }
1183 /**
1184 * Is this a .js subpage of a user page?
1185 * @return bool
1186 * @access public
1187 */
1188 function isJsSubpage() {
1189 return ( NS_USER == $this->mNamespace and preg_match("/\\.js$/", $this->mTextform ) );
1190 }
1191 /**
1192 * Protect css/js subpages of user pages: can $wgUser edit
1193 * this page?
1194 *
1195 * @return boolean
1196 * @todo XXX: this might be better using restrictions
1197 * @access public
1198 */
1199 function userCanEditCssJsSubpage() {
1200 global $wgUser;
1201 return ( $wgUser->isAllowed('editinterface') or preg_match('/^'.preg_quote($wgUser->getName(), '/').'\//', $this->mTextform) );
1202 }
1203
1204 /**
1205 * Loads a string into mRestrictions array
1206 * @param string $res restrictions in string format
1207 * @access public
1208 */
1209 function loadRestrictions( $res ) {
1210 foreach( explode( ':', trim( $res ) ) as $restrict ) {
1211 $temp = explode( '=', trim( $restrict ) );
1212 if(count($temp) == 1) {
1213 // old format should be treated as edit/move restriction
1214 $this->mRestrictions["edit"] = explode( ',', trim( $temp[0] ) );
1215 $this->mRestrictions["move"] = explode( ',', trim( $temp[0] ) );
1216 } else {
1217 $this->mRestrictions[$temp[0]] = explode( ',', trim( $temp[1] ) );
1218 }
1219 }
1220 $this->mRestrictionsLoaded = true;
1221 }
1222
1223 /**
1224 * Accessor/initialisation for mRestrictions
1225 * @param string $action action that permission needs to be checked for
1226 * @return array the array of groups allowed to edit this article
1227 * @access public
1228 */
1229 function getRestrictions($action) {
1230 $id = $this->getArticleID();
1231 if ( 0 == $id ) { return array(); }
1232
1233 if ( ! $this->mRestrictionsLoaded ) {
1234 $dbr =& wfGetDB( DB_SLAVE );
1235 $res = $dbr->selectField( 'page', 'page_restrictions', 'page_id='.$id );
1236 $this->loadRestrictions( $res );
1237 }
1238 if( isset( $this->mRestrictions[$action] ) ) {
1239 return $this->mRestrictions[$action];
1240 }
1241 return array();
1242 }
1243
1244 /**
1245 * Is there a version of this page in the deletion archive?
1246 * @return int the number of archived revisions
1247 * @access public
1248 */
1249 function isDeleted() {
1250 $fname = 'Title::isDeleted';
1251 if ( $this->getNamespace() < 0 ) {
1252 $n = 0;
1253 } else {
1254 $dbr =& wfGetDB( DB_SLAVE );
1255 $n = $dbr->selectField( 'archive', 'COUNT(*)', array( 'ar_namespace' => $this->getNamespace(),
1256 'ar_title' => $this->getDBkey() ), $fname );
1257 }
1258 return (int)$n;
1259 }
1260
1261 /**
1262 * Get the article ID for this Title from the link cache,
1263 * adding it if necessary
1264 * @param int $flags a bit field; may be GAID_FOR_UPDATE to select
1265 * for update
1266 * @return int the ID
1267 * @access public
1268 */
1269 function getArticleID( $flags = 0 ) {
1270 $linkCache =& LinkCache::singleton();
1271 if ( $flags & GAID_FOR_UPDATE ) {
1272 $oldUpdate = $linkCache->forUpdate( true );
1273 $this->mArticleID = $linkCache->addLinkObj( $this );
1274 $linkCache->forUpdate( $oldUpdate );
1275 } else {
1276 if ( -1 == $this->mArticleID ) {
1277 $this->mArticleID = $linkCache->addLinkObj( $this );
1278 }
1279 }
1280 return $this->mArticleID;
1281 }
1282
1283 function getLatestRevID() {
1284 if ($this->mLatestID !== false)
1285 return $this->mLatestID;
1286
1287 $db =& wfGetDB(DB_SLAVE);
1288 return $this->mLatestID = $db->selectField( 'revision',
1289 "max(rev_id)",
1290 array('rev_page' => $this->getArticleID()),
1291 'Title::getLatestRevID' );
1292 }
1293
1294 /**
1295 * This clears some fields in this object, and clears any associated
1296 * keys in the "bad links" section of the link cache.
1297 *
1298 * - This is called from Article::insertNewArticle() to allow
1299 * loading of the new page_id. It's also called from
1300 * Article::doDeleteArticle()
1301 *
1302 * @param int $newid the new Article ID
1303 * @access public
1304 */
1305 function resetArticleID( $newid ) {
1306 $linkCache =& LinkCache::singleton();
1307 $linkCache->clearBadLink( $this->getPrefixedDBkey() );
1308
1309 if ( 0 == $newid ) { $this->mArticleID = -1; }
1310 else { $this->mArticleID = $newid; }
1311 $this->mRestrictionsLoaded = false;
1312 $this->mRestrictions = array();
1313 }
1314
1315 /**
1316 * Updates page_touched for this page; called from LinksUpdate.php
1317 * @return bool true if the update succeded
1318 * @access public
1319 */
1320 function invalidateCache() {
1321 global $wgUseFileCache;
1322
1323 if ( wfReadOnly() ) {
1324 return;
1325 }
1326
1327 $dbw =& wfGetDB( DB_MASTER );
1328 $success = $dbw->update( 'page',
1329 array( /* SET */
1330 'page_touched' => $dbw->timestamp()
1331 ), array( /* WHERE */
1332 'page_namespace' => $this->getNamespace() ,
1333 'page_title' => $this->getDBkey()
1334 ), 'Title::invalidateCache'
1335 );
1336
1337 if ($wgUseFileCache) {
1338 $cache = new CacheManager($this);
1339 @unlink($cache->fileCacheName());
1340 }
1341
1342 return $success;
1343 }
1344
1345 /**
1346 * Prefix some arbitrary text with the namespace or interwiki prefix
1347 * of this object
1348 *
1349 * @param string $name the text
1350 * @return string the prefixed text
1351 * @access private
1352 */
1353 /* private */ function prefix( $name ) {
1354 global $wgContLang;
1355
1356 $p = '';
1357 if ( '' != $this->mInterwiki ) {
1358 $p = $this->mInterwiki . ':';
1359 }
1360 if ( 0 != $this->mNamespace ) {
1361 $p .= $wgContLang->getNsText( $this->mNamespace ) . ':';
1362 }
1363 return $p . $name;
1364 }
1365
1366 /**
1367 * Secure and split - main initialisation function for this object
1368 *
1369 * Assumes that mDbkeyform has been set, and is urldecoded
1370 * and uses underscores, but not otherwise munged. This function
1371 * removes illegal characters, splits off the interwiki and
1372 * namespace prefixes, sets the other forms, and canonicalizes
1373 * everything.
1374 * @return bool true on success
1375 * @access private
1376 */
1377 /* private */ function secureAndSplit() {
1378 global $wgContLang, $wgLocalInterwiki, $wgCapitalLinks;
1379 $fname = 'Title::secureAndSplit';
1380
1381 # Initialisation
1382 static $rxTc = false;
1383 if( !$rxTc ) {
1384 # % is needed as well
1385 $rxTc = '/[^' . Title::legalChars() . ']|%[0-9A-Fa-f]{2}/S';
1386 }
1387
1388 $this->mInterwiki = $this->mFragment = '';
1389 $this->mNamespace = $this->mDefaultNamespace; # Usually NS_MAIN
1390
1391 # Clean up whitespace
1392 #
1393 $t = preg_replace( '/[ _]+/', '_', $this->mDbkeyform );
1394 $t = trim( $t, '_' );
1395
1396 if ( '' == $t ) {
1397 return false;
1398 }
1399
1400 if( false !== strpos( $t, UTF8_REPLACEMENT ) ) {
1401 # Contained illegal UTF-8 sequences or forbidden Unicode chars.
1402 return false;
1403 }
1404
1405 $this->mDbkeyform = $t;
1406
1407 # Initial colon indicates main namespace rather than specified default
1408 # but should not create invalid {ns,title} pairs such as {0,Project:Foo}
1409 if ( ':' == $t{0} ) {
1410 $this->mNamespace = NS_MAIN;
1411 $t = substr( $t, 1 ); # remove the colon but continue processing
1412 }
1413
1414 # Namespace or interwiki prefix
1415 $firstPass = true;
1416 do {
1417 if ( preg_match( "/^(.+?)_*:_*(.*)$/S", $t, $m ) ) {
1418 $p = $m[1];
1419 $lowerNs = strtolower( $p );
1420 if ( $ns = Namespace::getCanonicalIndex( $lowerNs ) ) {
1421 # Canonical namespace
1422 $t = $m[2];
1423 $this->mNamespace = $ns;
1424 } elseif ( $ns = $wgContLang->getNsIndex( $lowerNs )) {
1425 # Ordinary namespace
1426 $t = $m[2];
1427 $this->mNamespace = $ns;
1428 } elseif( $this->getInterwikiLink( $p ) ) {
1429 if( !$firstPass ) {
1430 # Can't make a local interwiki link to an interwiki link.
1431 # That's just crazy!
1432 return false;
1433 }
1434
1435 # Interwiki link
1436 $t = $m[2];
1437 $this->mInterwiki = strtolower( $p );
1438
1439 # Redundant interwiki prefix to the local wiki
1440 if ( 0 == strcasecmp( $this->mInterwiki, $wgLocalInterwiki ) ) {
1441 if( $t == '' ) {
1442 # Can't have an empty self-link
1443 return false;
1444 }
1445 $this->mInterwiki = '';
1446 $firstPass = false;
1447 # Do another namespace split...
1448 continue;
1449 }
1450
1451 # If there's an initial colon after the interwiki, that also
1452 # resets the default namespace
1453 if ( $t !== '' && $t[0] == ':' ) {
1454 $this->mNamespace = NS_MAIN;
1455 $t = substr( $t, 1 );
1456 }
1457 }
1458 # If there's no recognized interwiki or namespace,
1459 # then let the colon expression be part of the title.
1460 }
1461 break;
1462 } while( true );
1463 $r = $t;
1464
1465 # We already know that some pages won't be in the database!
1466 #
1467 if ( '' != $this->mInterwiki || -1 == $this->mNamespace ) {
1468 $this->mArticleID = 0;
1469 }
1470 $f = strstr( $r, '#' );
1471 if ( false !== $f ) {
1472 $this->mFragment = substr( $f, 1 );
1473 $r = substr( $r, 0, strlen( $r ) - strlen( $f ) );
1474 # remove whitespace again: prevents "Foo_bar_#"
1475 # becoming "Foo_bar_"
1476 $r = preg_replace( '/_*$/', '', $r );
1477 }
1478
1479 # Reject illegal characters.
1480 #
1481 if( preg_match( $rxTc, $r ) ) {
1482 return false;
1483 }
1484
1485 /**
1486 * Pages with "/./" or "/../" appearing in the URLs will
1487 * often be unreachable due to the way web browsers deal
1488 * with 'relative' URLs. Forbid them explicitly.
1489 */
1490 if ( strpos( $r, '.' ) !== false &&
1491 ( $r === '.' || $r === '..' ||
1492 strpos( $r, './' ) === 0 ||
1493 strpos( $r, '../' ) === 0 ||
1494 strpos( $r, '/./' ) !== false ||
1495 strpos( $r, '/../' ) !== false ) )
1496 {
1497 return false;
1498 }
1499
1500 # We shouldn't need to query the DB for the size.
1501 #$maxSize = $dbr->textFieldSize( 'page', 'page_title' );
1502 if ( strlen( $r ) > 255 ) {
1503 return false;
1504 }
1505
1506 /**
1507 * Normally, all wiki links are forced to have
1508 * an initial capital letter so [[foo]] and [[Foo]]
1509 * point to the same place.
1510 *
1511 * Don't force it for interwikis, since the other
1512 * site might be case-sensitive.
1513 */
1514 if( $wgCapitalLinks && $this->mInterwiki == '') {
1515 $t = $wgContLang->ucfirst( $r );
1516 } else {
1517 $t = $r;
1518 }
1519
1520 /**
1521 * Can't make a link to a namespace alone...
1522 * "empty" local links can only be self-links
1523 * with a fragment identifier.
1524 */
1525 if( $t == '' &&
1526 $this->mInterwiki == '' &&
1527 $this->mNamespace != NS_MAIN ) {
1528 return false;
1529 }
1530
1531 // Any remaining initial :s are illegal.
1532 if ( $t !== '' && ':' == $t{0} ) {
1533 return false;
1534 }
1535
1536 # Fill fields
1537 $this->mDbkeyform = $t;
1538 $this->mUrlform = wfUrlencode( $t );
1539
1540 $this->mTextform = str_replace( '_', ' ', $t );
1541
1542 return true;
1543 }
1544
1545 /**
1546 * Get a Title object associated with the talk page of this article
1547 * @return Title the object for the talk page
1548 * @access public
1549 */
1550 function getTalkPage() {
1551 return Title::makeTitle( Namespace::getTalk( $this->getNamespace() ), $this->getDBkey() );
1552 }
1553
1554 /**
1555 * Get a title object associated with the subject page of this
1556 * talk page
1557 *
1558 * @return Title the object for the subject page
1559 * @access public
1560 */
1561 function getSubjectPage() {
1562 return Title::makeTitle( Namespace::getSubject( $this->getNamespace() ), $this->getDBkey() );
1563 }
1564
1565 /**
1566 * Get an array of Title objects linking to this Title
1567 * Also stores the IDs in the link cache.
1568 *
1569 * @param string $options may be FOR UPDATE
1570 * @return array the Title objects linking here
1571 * @access public
1572 */
1573 function getLinksTo( $options = '', $table = 'pagelinks', $prefix = 'pl' ) {
1574 $linkCache =& LinkCache::singleton();
1575 $id = $this->getArticleID();
1576
1577 if ( $options ) {
1578 $db =& wfGetDB( DB_MASTER );
1579 } else {
1580 $db =& wfGetDB( DB_SLAVE );
1581 }
1582
1583 $res = $db->select( array( 'page', $table ),
1584 array( 'page_namespace', 'page_title', 'page_id' ),
1585 array(
1586 "{$prefix}_from=page_id",
1587 "{$prefix}_namespace" => $this->getNamespace(),
1588 "{$prefix}_title" => $this->getDbKey() ),
1589 'Title::getLinksTo',
1590 $options );
1591
1592 $retVal = array();
1593 if ( $db->numRows( $res ) ) {
1594 while ( $row = $db->fetchObject( $res ) ) {
1595 if ( $titleObj = Title::makeTitle( $row->page_namespace, $row->page_title ) ) {
1596 $linkCache->addGoodLinkObj( $row->page_id, $titleObj );
1597 $retVal[] = $titleObj;
1598 }
1599 }
1600 }
1601 $db->freeResult( $res );
1602 return $retVal;
1603 }
1604
1605 /**
1606 * Get an array of Title objects using this Title as a template
1607 * Also stores the IDs in the link cache.
1608 *
1609 * @param string $options may be FOR UPDATE
1610 * @return array the Title objects linking here
1611 * @access public
1612 */
1613 function getTemplateLinksTo( $options = '' ) {
1614 return $this->getLinksTo( $options, 'templatelinks', 'tl' );
1615 }
1616
1617 /**
1618 * Get an array of Title objects referring to non-existent articles linked from this page
1619 *
1620 * @param string $options may be FOR UPDATE
1621 * @return array the Title objects
1622 * @access public
1623 */
1624 function getBrokenLinksFrom( $options = '' ) {
1625 if ( $options ) {
1626 $db =& wfGetDB( DB_MASTER );
1627 } else {
1628 $db =& wfGetDB( DB_SLAVE );
1629 }
1630
1631 $res = $db->safeQuery(
1632 "SELECT pl_namespace, pl_title
1633 FROM !
1634 LEFT JOIN !
1635 ON pl_namespace=page_namespace
1636 AND pl_title=page_title
1637 WHERE pl_from=?
1638 AND page_namespace IS NULL
1639 !",
1640 $db->tableName( 'pagelinks' ),
1641 $db->tableName( 'page' ),
1642 $this->getArticleId(),
1643 $options );
1644
1645 $retVal = array();
1646 if ( $db->numRows( $res ) ) {
1647 while ( $row = $db->fetchObject( $res ) ) {
1648 $retVal[] = Title::makeTitle( $row->pl_namespace, $row->pl_title );
1649 }
1650 }
1651 $db->freeResult( $res );
1652 return $retVal;
1653 }
1654
1655
1656 /**
1657 * Get a list of URLs to purge from the Squid cache when this
1658 * page changes
1659 *
1660 * @return array the URLs
1661 * @access public
1662 */
1663 function getSquidURLs() {
1664 return array(
1665 $this->getInternalURL(),
1666 $this->getInternalURL( 'action=history' )
1667 );
1668 }
1669
1670 /**
1671 * Move this page without authentication
1672 * @param Title &$nt the new page Title
1673 * @access public
1674 */
1675 function moveNoAuth( &$nt ) {
1676 return $this->moveTo( $nt, false );
1677 }
1678
1679 /**
1680 * Check whether a given move operation would be valid.
1681 * Returns true if ok, or a message key string for an error message
1682 * if invalid. (Scarrrrry ugly interface this.)
1683 * @param Title &$nt the new title
1684 * @param bool $auth indicates whether $wgUser's permissions
1685 * should be checked
1686 * @return mixed true on success, message name on failure
1687 * @access public
1688 */
1689 function isValidMoveOperation( &$nt, $auth = true ) {
1690 if( !$this or !$nt ) {
1691 return 'badtitletext';
1692 }
1693 if( $this->equals( $nt ) ) {
1694 return 'selfmove';
1695 }
1696 if( !$this->isMovable() || !$nt->isMovable() ) {
1697 return 'immobile_namespace';
1698 }
1699
1700 $oldid = $this->getArticleID();
1701 $newid = $nt->getArticleID();
1702
1703 if ( strlen( $nt->getDBkey() ) < 1 ) {
1704 return 'articleexists';
1705 }
1706 if ( ( '' == $this->getDBkey() ) ||
1707 ( !$oldid ) ||
1708 ( '' == $nt->getDBkey() ) ) {
1709 return 'badarticleerror';
1710 }
1711
1712 if ( $auth && (
1713 !$this->userCanEdit() || !$nt->userCanEdit() ||
1714 !$this->userCanMove() || !$nt->userCanMove() ) ) {
1715 return 'protectedpage';
1716 }
1717
1718 # The move is allowed only if (1) the target doesn't exist, or
1719 # (2) the target is a redirect to the source, and has no history
1720 # (so we can undo bad moves right after they're done).
1721
1722 if ( 0 != $newid ) { # Target exists; check for validity
1723 if ( ! $this->isValidMoveTarget( $nt ) ) {
1724 return 'articleexists';
1725 }
1726 }
1727 return true;
1728 }
1729
1730 /**
1731 * Move a title to a new location
1732 * @param Title &$nt the new title
1733 * @param bool $auth indicates whether $wgUser's permissions
1734 * should be checked
1735 * @return mixed true on success, message name on failure
1736 * @access public
1737 */
1738 function moveTo( &$nt, $auth = true, $reason = '' ) {
1739 $err = $this->isValidMoveOperation( $nt, $auth );
1740 if( is_string( $err ) ) {
1741 return $err;
1742 }
1743
1744 $pageid = $this->getArticleID();
1745 if( $nt->exists() ) {
1746 $this->moveOverExistingRedirect( $nt, $reason );
1747 $pageCountChange = 0;
1748 } else { # Target didn't exist, do normal move.
1749 $this->moveToNewTitle( $nt, $reason );
1750 $pageCountChange = 1;
1751 }
1752 $redirid = $this->getArticleID();
1753
1754 # Fixing category links (those without piped 'alternate' names) to be sorted under the new title
1755 $dbw =& wfGetDB( DB_MASTER );
1756 $categorylinks = $dbw->tableName( 'categorylinks' );
1757 $sql = "UPDATE $categorylinks SET cl_sortkey=" . $dbw->addQuotes( $nt->getPrefixedText() ) .
1758 " WHERE cl_from=" . $dbw->addQuotes( $pageid ) .
1759 " AND cl_sortkey=" . $dbw->addQuotes( $this->getPrefixedText() );
1760 $dbw->query( $sql, 'SpecialMovepage::doSubmit' );
1761
1762 # Update watchlists
1763
1764 $oldnamespace = $this->getNamespace() & ~1;
1765 $newnamespace = $nt->getNamespace() & ~1;
1766 $oldtitle = $this->getDBkey();
1767 $newtitle = $nt->getDBkey();
1768
1769 if( $oldnamespace != $newnamespace || $oldtitle != $newtitle ) {
1770 WatchedItem::duplicateEntries( $this, $nt );
1771 }
1772
1773 # Update search engine
1774 $u = new SearchUpdate( $pageid, $nt->getPrefixedDBkey() );
1775 $u->doUpdate();
1776 $u = new SearchUpdate( $redirid, $this->getPrefixedDBkey(), '' );
1777 $u->doUpdate();
1778
1779 # Update site_stats
1780 if ( $this->getNamespace() == NS_MAIN and $nt->getNamespace() != NS_MAIN ) {
1781 # Moved out of main namespace
1782 # not viewed, edited, removing
1783 $u = new SiteStatsUpdate( 0, 1, -1, $pageCountChange);
1784 } elseif ( $this->getNamespace() != NS_MAIN and $nt->getNamespace() == NS_MAIN ) {
1785 # Moved into main namespace
1786 # not viewed, edited, adding
1787 $u = new SiteStatsUpdate( 0, 1, +1, $pageCountChange );
1788 } elseif ( $pageCountChange ) {
1789 # Added redirect
1790 $u = new SiteStatsUpdate( 0, 0, 0, 1 );
1791 } else{
1792 $u = false;
1793 }
1794 if ( $u ) {
1795 $u->doUpdate();
1796 }
1797
1798 global $wgUser;
1799 wfRunHooks( 'TitleMoveComplete', array( &$this, &$nt, &$wgUser, $pageid, $redirid ) );
1800 return true;
1801 }
1802
1803 /**
1804 * Move page to a title which is at present a redirect to the
1805 * source page
1806 *
1807 * @param Title &$nt the page to move to, which should currently
1808 * be a redirect
1809 * @access private
1810 */
1811 function moveOverExistingRedirect( &$nt, $reason = '' ) {
1812 global $wgUseSquid, $wgMwRedir;
1813 $fname = 'Title::moveOverExistingRedirect';
1814 $comment = wfMsgForContent( '1movedto2', $this->getPrefixedText(), $nt->getPrefixedText() );
1815
1816 if ( $reason ) {
1817 $comment .= ": $reason";
1818 }
1819
1820 $now = wfTimestampNow();
1821 $rand = wfRandom();
1822 $newid = $nt->getArticleID();
1823 $oldid = $this->getArticleID();
1824 $dbw =& wfGetDB( DB_MASTER );
1825 $linkCache =& LinkCache::singleton();
1826
1827 # Delete the old redirect. We don't save it to history since
1828 # by definition if we've got here it's rather uninteresting.
1829 # We have to remove it so that the next step doesn't trigger
1830 # a conflict on the unique namespace+title index...
1831 $dbw->delete( 'page', array( 'page_id' => $newid ), $fname );
1832
1833 # Save a null revision in the page's history notifying of the move
1834 $nullRevision = Revision::newNullRevision( $dbw, $oldid, $comment, true );
1835 $nullRevId = $nullRevision->insertOn( $dbw );
1836
1837 # Change the name of the target page:
1838 $dbw->update( 'page',
1839 /* SET */ array(
1840 'page_touched' => $dbw->timestamp($now),
1841 'page_namespace' => $nt->getNamespace(),
1842 'page_title' => $nt->getDBkey(),
1843 'page_latest' => $nullRevId,
1844 ),
1845 /* WHERE */ array( 'page_id' => $oldid ),
1846 $fname
1847 );
1848 $linkCache->clearLink( $nt->getPrefixedDBkey() );
1849
1850 # Recreate the redirect, this time in the other direction.
1851 $redirectText = $wgMwRedir->getSynonym( 0 ) . ' [[' . $nt->getPrefixedText() . "]]\n";
1852 $redirectArticle = new Article( $this );
1853 $newid = $redirectArticle->insertOn( $dbw );
1854 $redirectRevision = new Revision( array(
1855 'page' => $newid,
1856 'comment' => $comment,
1857 'text' => $redirectText ) );
1858 $revid = $redirectRevision->insertOn( $dbw );
1859 $redirectArticle->updateRevisionOn( $dbw, $redirectRevision, 0 );
1860 $linkCache->clearLink( $this->getPrefixedDBkey() );
1861
1862 # Log the move
1863 $log = new LogPage( 'move' );
1864 $log->addEntry( 'move_redir', $this, $reason, array( 1 => $nt->getPrefixedText() ) );
1865
1866 # Now, we record the link from the redirect to the new title.
1867 # It should have no other outgoing links...
1868 $dbw->delete( 'pagelinks', array( 'pl_from' => $newid ), $fname );
1869 $dbw->insert( 'pagelinks',
1870 array(
1871 'pl_from' => $newid,
1872 'pl_namespace' => $nt->getNamespace(),
1873 'pl_title' => $nt->getDbKey() ),
1874 $fname );
1875
1876 # Purge squid
1877 if ( $wgUseSquid ) {
1878 $urls = array_merge( $nt->getSquidURLs(), $this->getSquidURLs() );
1879 $u = new SquidUpdate( $urls );
1880 $u->doUpdate();
1881 }
1882 }
1883
1884 /**
1885 * Move page to non-existing title.
1886 * @param Title &$nt the new Title
1887 * @access private
1888 */
1889 function moveToNewTitle( &$nt, $reason = '' ) {
1890 global $wgUseSquid;
1891 global $wgMwRedir;
1892 $fname = 'MovePageForm::moveToNewTitle';
1893 $comment = wfMsgForContent( '1movedto2', $this->getPrefixedText(), $nt->getPrefixedText() );
1894 if ( $reason ) {
1895 $comment .= ": $reason";
1896 }
1897
1898 $newid = $nt->getArticleID();
1899 $oldid = $this->getArticleID();
1900 $dbw =& wfGetDB( DB_MASTER );
1901 $now = $dbw->timestamp();
1902 $rand = wfRandom();
1903 $linkCache =& LinkCache::singleton();
1904
1905 # Save a null revision in the page's history notifying of the move
1906 $nullRevision = Revision::newNullRevision( $dbw, $oldid, $comment, true );
1907 $nullRevId = $nullRevision->insertOn( $dbw );
1908
1909 # Rename cur entry
1910 $dbw->update( 'page',
1911 /* SET */ array(
1912 'page_touched' => $now,
1913 'page_namespace' => $nt->getNamespace(),
1914 'page_title' => $nt->getDBkey(),
1915 'page_latest' => $nullRevId,
1916 ),
1917 /* WHERE */ array( 'page_id' => $oldid ),
1918 $fname
1919 );
1920
1921 $linkCache->clearLink( $nt->getPrefixedDBkey() );
1922
1923 # Insert redirect
1924 $redirectText = $wgMwRedir->getSynonym( 0 ) . ' [[' . $nt->getPrefixedText() . "]]\n";
1925 $redirectArticle = new Article( $this );
1926 $newid = $redirectArticle->insertOn( $dbw );
1927 $redirectRevision = new Revision( array(
1928 'page' => $newid,
1929 'comment' => $comment,
1930 'text' => $redirectText ) );
1931 $revid = $redirectRevision->insertOn( $dbw );
1932 $redirectArticle->updateRevisionOn( $dbw, $redirectRevision, 0 );
1933 $linkCache->clearLink( $this->getPrefixedDBkey() );
1934
1935 # Log the move
1936 $log = new LogPage( 'move' );
1937 $log->addEntry( 'move', $this, $reason, array( 1 => $nt->getPrefixedText()) );
1938
1939 # Purge caches as per article creation
1940 Article::onArticleCreate( $nt );
1941
1942 # Record the just-created redirect's linking to the page
1943 $dbw->insert( 'pagelinks',
1944 array(
1945 'pl_from' => $newid,
1946 'pl_namespace' => $nt->getNamespace(),
1947 'pl_title' => $nt->getDBkey() ),
1948 $fname );
1949
1950 # Non-existent target may have had broken links to it; these must
1951 # now be touched to update link coloring.
1952 $nt->touchLinks();
1953
1954 # Purge old title from squid
1955 # The new title, and links to the new title, are purged in Article::onArticleCreate()
1956 $titles = $nt->getLinksTo();
1957 if ( $wgUseSquid ) {
1958 $urls = $this->getSquidURLs();
1959 foreach ( $titles as $linkTitle ) {
1960 $urls[] = $linkTitle->getInternalURL();
1961 }
1962 $u = new SquidUpdate( $urls );
1963 $u->doUpdate();
1964 }
1965 }
1966
1967 /**
1968 * Checks if $this can be moved to a given Title
1969 * - Selects for update, so don't call it unless you mean business
1970 *
1971 * @param Title &$nt the new title to check
1972 * @access public
1973 */
1974 function isValidMoveTarget( $nt ) {
1975
1976 $fname = 'Title::isValidMoveTarget';
1977 $dbw =& wfGetDB( DB_MASTER );
1978
1979 # Is it a redirect?
1980 $id = $nt->getArticleID();
1981 $obj = $dbw->selectRow( array( 'page', 'revision', 'text'),
1982 array( 'page_is_redirect','old_text','old_flags' ),
1983 array( 'page_id' => $id, 'page_latest=rev_id', 'rev_text_id=old_id' ),
1984 $fname, 'FOR UPDATE' );
1985
1986 if ( !$obj || 0 == $obj->page_is_redirect ) {
1987 # Not a redirect
1988 return false;
1989 }
1990 $text = Revision::getRevisionText( $obj );
1991
1992 # Does the redirect point to the source?
1993 if ( preg_match( "/\\[\\[\\s*([^\\]\\|]*)]]/", $text, $m ) ) {
1994 $redirTitle = Title::newFromText( $m[1] );
1995 if( !is_object( $redirTitle ) ||
1996 $redirTitle->getPrefixedDBkey() != $this->getPrefixedDBkey() ) {
1997 return false;
1998 }
1999 } else {
2000 # Fail safe
2001 return false;
2002 }
2003
2004 # Does the article have a history?
2005 $row = $dbw->selectRow( array( 'page', 'revision'),
2006 array( 'rev_id' ),
2007 array( 'page_namespace' => $nt->getNamespace(),
2008 'page_title' => $nt->getDBkey(),
2009 'page_id=rev_page AND page_latest != rev_id'
2010 ), $fname, 'FOR UPDATE'
2011 );
2012
2013 # Return true if there was no history
2014 return $row === false;
2015 }
2016
2017 /**
2018 * Create a redirect; fails if the title already exists; does
2019 * not notify RC
2020 *
2021 * @param Title $dest the destination of the redirect
2022 * @param string $comment the comment string describing the move
2023 * @return bool true on success
2024 * @access public
2025 */
2026 function createRedirect( $dest, $comment ) {
2027 if ( $this->getArticleID() ) {
2028 return false;
2029 }
2030
2031 $fname = 'Title::createRedirect';
2032 $dbw =& wfGetDB( DB_MASTER );
2033
2034 $article = new Article( $this );
2035 $newid = $article->insertOn( $dbw );
2036 $revision = new Revision( array(
2037 'page' => $newid,
2038 'comment' => $comment,
2039 'text' => "#REDIRECT [[" . $dest->getPrefixedText() . "]]\n",
2040 ) );
2041 $revisionId = $revision->insertOn( $dbw );
2042 $article->updateRevisionOn( $dbw, $revision, 0 );
2043
2044 # Link table
2045 $dbw->insert( 'pagelinks',
2046 array(
2047 'pl_from' => $newid,
2048 'pl_namespace' => $dest->getNamespace(),
2049 'pl_title' => $dest->getDbKey()
2050 ), $fname
2051 );
2052
2053 Article::onArticleCreate( $this );
2054 return true;
2055 }
2056
2057 /**
2058 * Get categories to which this Title belongs and return an array of
2059 * categories' names.
2060 *
2061 * @return array an array of parents in the form:
2062 * $parent => $currentarticle
2063 * @access public
2064 */
2065 function getParentCategories() {
2066 global $wgContLang;
2067
2068 $titlekey = $this->getArticleId();
2069 $dbr =& wfGetDB( DB_SLAVE );
2070 $categorylinks = $dbr->tableName( 'categorylinks' );
2071
2072 # NEW SQL
2073 $sql = "SELECT * FROM $categorylinks"
2074 ." WHERE cl_from='$titlekey'"
2075 ." AND cl_from <> '0'"
2076 ." ORDER BY cl_sortkey";
2077
2078 $res = $dbr->query ( $sql ) ;
2079
2080 if($dbr->numRows($res) > 0) {
2081 while ( $x = $dbr->fetchObject ( $res ) )
2082 //$data[] = Title::newFromText($wgContLang->getNSText ( NS_CATEGORY ).':'.$x->cl_to);
2083 $data[$wgContLang->getNSText ( NS_CATEGORY ).':'.$x->cl_to] = $this->getFullText();
2084 $dbr->freeResult ( $res ) ;
2085 } else {
2086 $data = '';
2087 }
2088 return $data;
2089 }
2090
2091 /**
2092 * Get a tree of parent categories
2093 * @param array $children an array with the children in the keys, to check for circular refs
2094 * @return array
2095 * @access public
2096 */
2097 function getParentCategoryTree( $children = array() ) {
2098 $parents = $this->getParentCategories();
2099
2100 if($parents != '') {
2101 foreach($parents as $parent => $current) {
2102 if ( array_key_exists( $parent, $children ) ) {
2103 # Circular reference
2104 $stack[$parent] = array();
2105 } else {
2106 $nt = Title::newFromText($parent);
2107 $stack[$parent] = $nt->getParentCategoryTree( $children + array($parent => 1) );
2108 }
2109 }
2110 return $stack;
2111 } else {
2112 return array();
2113 }
2114 }
2115
2116
2117 /**
2118 * Get an associative array for selecting this title from
2119 * the "page" table
2120 *
2121 * @return array
2122 * @access public
2123 */
2124 function pageCond() {
2125 return array( 'page_namespace' => $this->mNamespace, 'page_title' => $this->mDbkeyform );
2126 }
2127
2128 /**
2129 * Get the revision ID of the previous revision
2130 *
2131 * @param integer $revision Revision ID. Get the revision that was before this one.
2132 * @return interger $oldrevision|false
2133 */
2134 function getPreviousRevisionID( $revision ) {
2135 $dbr =& wfGetDB( DB_SLAVE );
2136 return $dbr->selectField( 'revision', 'rev_id',
2137 'rev_page=' . intval( $this->getArticleId() ) .
2138 ' AND rev_id<' . intval( $revision ) . ' ORDER BY rev_id DESC' );
2139 }
2140
2141 /**
2142 * Get the revision ID of the next revision
2143 *
2144 * @param integer $revision Revision ID. Get the revision that was after this one.
2145 * @return interger $oldrevision|false
2146 */
2147 function getNextRevisionID( $revision ) {
2148 $dbr =& wfGetDB( DB_SLAVE );
2149 return $dbr->selectField( 'revision', 'rev_id',
2150 'rev_page=' . intval( $this->getArticleId() ) .
2151 ' AND rev_id>' . intval( $revision ) . ' ORDER BY rev_id' );
2152 }
2153
2154 /**
2155 * Compare with another title.
2156 *
2157 * @param Title $title
2158 * @return bool
2159 */
2160 function equals( $title ) {
2161 // Note: === is necessary for proper matching of number-like titles.
2162 return $this->getInterwiki() === $title->getInterwiki()
2163 && $this->getNamespace() == $title->getNamespace()
2164 && $this->getDbkey() === $title->getDbkey();
2165 }
2166
2167 /**
2168 * Check if page exists
2169 * @return bool
2170 */
2171 function exists() {
2172 return $this->getArticleId() != 0;
2173 }
2174
2175 /**
2176 * Should a link should be displayed as a known link, just based on its title?
2177 *
2178 * Currently, a self-link with a fragment and special pages are in
2179 * this category. Special pages never exist in the database.
2180 */
2181 function isAlwaysKnown() {
2182 return $this->isExternal() || ( 0 == $this->mNamespace && "" == $this->mDbkeyform )
2183 || NS_SPECIAL == $this->mNamespace;
2184 }
2185
2186 /**
2187 * Update page_touched timestamps on pages linking to this title.
2188 * In principal, this could be backgrounded and could also do squid
2189 * purging.
2190 */
2191 function touchLinks() {
2192 $fname = 'Title::touchLinks';
2193
2194 $dbw =& wfGetDB( DB_MASTER );
2195
2196 $res = $dbw->select( 'pagelinks',
2197 array( 'pl_from' ),
2198 array(
2199 'pl_namespace' => $this->getNamespace(),
2200 'pl_title' => $this->getDbKey() ),
2201 $fname );
2202
2203 $toucharr = array();
2204 while( $row = $dbw->fetchObject( $res ) ) {
2205 $toucharr[] = $row->pl_from;
2206 }
2207 $dbw->freeResult( $res );
2208
2209 if( $this->getNamespace() == NS_CATEGORY ) {
2210 // Categories show up in a separate set of links as well
2211 $res = $dbw->select( 'categorylinks',
2212 array( 'cl_from' ),
2213 array( 'cl_to' => $this->getDbKey() ),
2214 $fname );
2215 while( $row = $dbw->fetchObject( $res ) ) {
2216 $toucharr[] = $row->cl_from;
2217 }
2218 $dbw->freeResult( $res );
2219 }
2220
2221 if (!count($toucharr))
2222 return;
2223 $dbw->update( 'page', /* SET */ array( 'page_touched' => $dbw->timestamp() ),
2224 /* WHERE */ array( 'page_id' => $toucharr ),$fname);
2225 }
2226
2227 function trackbackURL() {
2228 global $wgTitle, $wgScriptPath, $wgServer;
2229
2230 return "$wgServer$wgScriptPath/trackback.php?article="
2231 . htmlspecialchars(urlencode($wgTitle->getPrefixedDBkey()));
2232 }
2233
2234 function trackbackRDF() {
2235 $url = htmlspecialchars($this->getFullURL());
2236 $title = htmlspecialchars($this->getText());
2237 $tburl = $this->trackbackURL();
2238
2239 return "
2240 <rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"
2241 xmlns:dc=\"http://purl.org/dc/elements/1.1/\"
2242 xmlns:trackback=\"http://madskills.com/public/xml/rss/module/trackback/\">
2243 <rdf:Description
2244 rdf:about=\"$url\"
2245 dc:identifier=\"$url\"
2246 dc:title=\"$title\"
2247 trackback:ping=\"$tburl\" />
2248 </rdf:RDF>";
2249 }
2250
2251 /**
2252 * Generate strings used for xml 'id' names in monobook tabs
2253 * @return string
2254 */
2255 function getNamespaceKey() {
2256 switch ($this->getNamespace()) {
2257 case NS_MAIN:
2258 case NS_TALK:
2259 return 'nstab-main';
2260 case NS_USER:
2261 case NS_USER_TALK:
2262 return 'nstab-user';
2263 case NS_MEDIA:
2264 return 'nstab-media';
2265 case NS_SPECIAL:
2266 return 'nstab-special';
2267 case NS_PROJECT:
2268 case NS_PROJECT_TALK:
2269 return 'nstab-project';
2270 case NS_IMAGE:
2271 case NS_IMAGE_TALK:
2272 return 'nstab-image';
2273 case NS_MEDIAWIKI:
2274 case NS_MEDIAWIKI_TALK:
2275 return 'nstab-mediawiki';
2276 case NS_TEMPLATE:
2277 case NS_TEMPLATE_TALK:
2278 return 'nstab-template';
2279 case NS_HELP:
2280 case NS_HELP_TALK:
2281 return 'nstab-help';
2282 case NS_CATEGORY:
2283 case NS_CATEGORY_TALK:
2284 return 'nstab-category';
2285 default:
2286 return 'nstab-' . strtolower( $this->getSubjectNsText() );
2287 }
2288 }
2289 }
2290 ?>