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