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