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