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