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