9 require_once( 'normal/UtfNormal.php' );
11 $wgTitleInterwikiCache = array();
12 define ( 'GAID_FOR_UPDATE', 1 );
14 # Title::newFromTitle maintains a cache to avoid
15 # expensive re-normalization of commonly used titles.
16 # On a batch operation this can become a memory leak
17 # if not bounded. After hitting this many titles,
19 define( 'MW_TITLECACHE_MAX', 1000 );
23 * - Represents a title, which may contain an interwiki designation or namespace
24 * - Can fetch various kinds of data from the database, albeit inefficiently.
30 * All member variables should be considered private
31 * Please use the accessor functions
38 var $mTextform; # Text form (spaces not underscores) of the main part
39 var $mUrlform; # URL-encoded form of the main part
40 var $mDbkeyform; # Main part with underscores
41 var $mNamespace; # Namespace index, i.e. one of the NS_xxxx constants
42 var $mInterwiki; # Interwiki prefix (or null string)
43 var $mFragment; # Title fragment (i.e. the bit after the #)
44 var $mArticleID; # Article ID, fetched from the link cache on demand
45 var $mRestrictions; # Array of groups allowed to edit this article
46 # Only null or "sysop" are supported
47 var $mRestrictionsLoaded; # Boolean for initialisation on demand
48 var $mPrefixedText; # Text form including namespace/interwiki, initialised on demand
49 var $mDefaultNamespace; # Namespace index when there is no namespace
50 # Zero except in {{transclusion}} tags
58 /* private */ function Title() {
59 $this->mInterwiki
= $this->mUrlform
=
60 $this->mTextform
= $this->mDbkeyform
= '';
61 $this->mArticleID
= -1;
62 $this->mNamespace
= NS_MAIN
;
63 $this->mRestrictionsLoaded
= false;
64 $this->mRestrictions
= array();
65 # Dont change the following, NS_MAIN is hardcoded in several place
67 $this->mDefaultNamespace
= NS_MAIN
;
71 * Create a new Title from a prefixed DB key
72 * @param string $key The database key, which has underscores
73 * instead of spaces, possibly including namespace and
75 * @return Title the new object, or NULL on an error
79 /* static */ function newFromDBkey( $key ) {
81 $t->mDbkeyform
= $key;
82 if( $t->secureAndSplit() )
89 * Create a new Title from text, such as what one would
90 * find in a link. Decodes any HTML entities in the text.
92 * @param string $text the link text; spaces, prefixes,
93 * and an initial ':' indicating the main namespace
95 * @param int $defaultNamespace the namespace to use if
96 * none is specified by a prefix
97 * @return Title the new object, or NULL on an error
101 function &newFromText( $text, $defaultNamespace = NS_MAIN
) {
102 $fname = 'Title::newFromText';
103 wfProfileIn( $fname );
105 if( is_object( $text ) ) {
106 wfDebugDieBacktrace( 'Title::newFromText given an object' );
110 * Wiki pages often contain multiple links to the same page.
111 * Title normalization and parsing can become expensive on
112 * pages with many links, so we can save a little time by
115 * In theory these are value objects and won't get changed...
117 static $titleCache = array();
118 if( $defaultNamespace == NS_MAIN
&& isset( $titleCache[$text] ) ) {
119 wfProfileOut( $fname );
120 return $titleCache[$text];
124 * Convert things like é into real text...
126 global $wgInputEncoding;
127 $filteredText = do_html_entity_decode( $text, ENT_COMPAT
, $wgInputEncoding );
130 * Convert things like ā or 〗 into real text...
131 * WARNING: Not friendly to internal links on a latin-1 wiki.
133 $filteredText = wfMungeToUtf8( $filteredText );
135 # What was this for? TS 2004-03-03
136 # $text = urldecode( $text );
139 $t->mDbkeyform
= str_replace( ' ', '_', $filteredText );
140 $t->mDefaultNamespace
= $defaultNamespace;
142 if( $t->secureAndSplit() ) {
143 if( $defaultNamespace == NS_MAIN
) {
144 if( count( $titleCache ) >= MW_TITLECACHE_MAX
) {
145 # Avoid memory leaks on mass operations...
146 $titleCache = array();
148 $titleCache[$text] =& $t;
150 wfProfileOut( $fname );
153 wfProfileOut( $fname );
159 * Create a new Title from URL-encoded text. Ensures that
160 * the given title's length does not exceed the maximum.
161 * @param string $url the title, as might be taken from a URL
162 * @return Title the new object, or NULL on an error
166 /* static */ function newFromURL( $url ) {
167 global $wgLang, $wgServer;
170 # For compatibility with old buggy URLs. "+" is not valid in titles,
171 # but some URLs used it as a space replacement and they still come
172 # from some external search tools.
173 $s = str_replace( '+', ' ', $url );
175 $t->mDbkeyform
= str_replace( ' ', '_', $s );
176 if( $t->secureAndSplit() ) {
184 * Create a new Title from an article ID
185 * @todo This is inefficiently implemented, the page row is requested
186 * but not used for anything else
187 * @param int $id the page_id corresponding to the Title to create
188 * @return Title the new object, or NULL on an error
191 /* static */ 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
);
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.
211 * @param int $ns the namespace of the article
212 * @param string $title the unprefixed database key form
213 * @return Title the new object
217 /* static */ function &makeTitle( $ns, $title ) {
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 );
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 * @param int $ns the namespace of the article
234 * @param string $title the database key form
235 * @return Title the new object, or NULL on an error
239 /* static */ function makeTitleSafe( $ns, $title ) {
241 $t->mDbkeyform
= Title
::makeName( $ns, $title );
242 if( $t->secureAndSplit() ) {
250 * Create a new Title for the Main Page
252 * @return Title the new object
255 /* static */ function newMainPage() {
256 return Title
::newFromText( wfMsgForContent( 'mainpage' ) );
260 * Create a new Title for a redirect
261 * @param string $text the redirect title text
262 * @return Title the new object, or NULL if the text is not a
267 /* static */ function newFromRedirect( $text ) {
270 if ( $wgMwRedir->matchStart( $text ) ) {
271 if ( preg_match( '/\\[\\[([^\\]\\|]+)[\\]\\|]/', $text, $m ) ) {
272 # categories are escaped using : for example one can enter:
273 # #REDIRECT [[:Category:Music]]. Need to remove it.
274 if ( substr($m[1],0,1) == ':') {
275 # We don't want to keep the ':'
276 $m[1] = substr( $m[1], 1 );
279 $rt = Title
::newFromText( $m[1] );
280 # Disallow redirects to Special:Userlogout
281 if ( !is_null($rt) && $rt->getNamespace() == NS_SPECIAL
&& preg_match( '/^Userlogout/i', $rt->getText() ) ) {
289 #----------------------------------------------------------------------------
291 #----------------------------------------------------------------------------
294 * Get the prefixed DB key associated with an ID
295 * @param int $id the page_id of the article
296 * @return Title an object representing the article, or NULL
297 * if no such article was found
301 /* static */ function nameOf( $id ) {
302 $fname = 'Title::nameOf';
303 $dbr =& wfGetDB( DB_SLAVE
);
305 $s = $dbr->selectRow( 'page', array( 'page_namespace','page_title' ), array( 'page_id' => $id ), $fname );
306 if ( $s === false ) { return NULL; }
308 $n = Title
::makeName( $s->page_namespace
, $s->page_title
);
313 * Get a regex character class describing the legal characters in a link
314 * @return string the list of characters, not delimited
318 /* static */ function legalChars() {
319 # Missing characters:
320 # * []|# Needed for link syntax
321 # * % and + are corrupted by Apache when they appear in the path
323 # % seems to work though
325 # The problem with % is that URLs are double-unescaped: once by Apache's
326 # path conversion code, and again by PHP. So %253F, for example, becomes "?".
327 # Our code does not double-escape to compensate for this, indeed double escaping
328 # would break if the double-escaped title was passed in the query string
329 # rather than the path. This is a minor security issue because articles can be
330 # created such that they are hard to view or edit. -- TS
332 # Theoretically 0x80-0x9F of ISO 8859-1 should be disallowed, but
333 # this breaks interlanguage links
335 $set = " %!\"$&'()*,\\-.\\/0-9:;=?@A-Z\\\\^_`a-z~\\x80-\\xFF";
340 * Get a string representation of a title suitable for
341 * including in a search index
343 * @param int $ns a namespace index
344 * @param string $title text-form main part
345 * @return string a stripped-down title string ready for the
348 /* static */ function indexTitle( $ns, $title ) {
349 global $wgDBminWordLen, $wgContLang;
350 require_once( 'SearchEngine.php' );
352 $lc = SearchEngine
::legalSearchChars() . '&#;';
353 $t = $wgContLang->stripForSearch( $title );
354 $t = preg_replace( "/[^{$lc}]+/", ' ', $t );
355 $t = strtolower( $t );
358 $t = preg_replace( "/([{$lc}]+)'s( |$)/", "\\1 \\1's ", $t );
359 $t = preg_replace( "/([{$lc}]+)s'( |$)/", "\\1s ", $t );
361 $t = preg_replace( "/\\s+/", ' ', $t );
363 if ( $ns == NS_IMAGE
) {
364 $t = preg_replace( "/ (png|gif|jpg|jpeg|ogg)$/", "", $t );
370 * Make a prefixed DB key from a DB key and a namespace index
371 * @param int $ns numerical representation of the namespace
372 * @param string $title the DB key form the title
373 * @return string the prefixed form of the title
375 /* static */ function makeName( $ns, $title ) {
378 $n = $wgContLang->getNsText( $ns );
379 if ( '' == $n ) { return $title; }
380 else { return $n.':'.$title; }
384 * Returns the URL associated with an interwiki prefix
385 * @param string $key the interwiki prefix (e.g. "MeatBall")
386 * @return the associated URL, containing "$1", which should be
387 * replaced by an article title
391 function getInterwikiLink( $key ) {
392 global $wgMemc, $wgDBname, $wgInterwikiExpiry, $wgTitleInterwikiCache;
393 $fname = 'Title::getInterwikiLink';
395 wfProfileIn( $fname );
397 $k = $wgDBname.':interwiki:'.$key;
398 if( array_key_exists( $k, $wgTitleInterwikiCache ) ) {
399 wfProfileOut( $fname );
400 return $wgTitleInterwikiCache[$k]->iw_url
;
403 $s = $wgMemc->get( $k );
404 # Ignore old keys with no iw_local
405 if( $s && isset( $s->iw_local
) ) {
406 $wgTitleInterwikiCache[$k] = $s;
407 wfProfileOut( $fname );
411 $dbr =& wfGetDB( DB_SLAVE
);
412 $res = $dbr->select( 'interwiki',
413 array( 'iw_url', 'iw_local' ),
414 array( 'iw_prefix' => $key ), $fname );
416 wfProfileOut( $fname );
420 $s = $dbr->fetchObject( $res );
422 # Cache non-existence: create a blank object and save it to memcached
427 $wgMemc->set( $k, $s, $wgInterwikiExpiry );
428 $wgTitleInterwikiCache[$k] = $s;
430 wfProfileOut( $fname );
435 * Determine whether the object refers to a page within
438 * @return bool TRUE if this is an in-project interwiki link
439 * or a wikilink, FALSE otherwise
443 global $wgTitleInterwikiCache, $wgDBname;
445 if ( $this->mInterwiki
!= '' ) {
446 # Make sure key is loaded into cache
447 $this->getInterwikiLink( $this->mInterwiki
);
448 $k = $wgDBname.':interwiki:' . $this->mInterwiki
;
449 return (bool)($wgTitleInterwikiCache[$k]->iw_local
);
456 * Update the page_touched field for an array of title objects
457 * @todo Inefficient unless the IDs are already loaded into the
459 * @param array $titles an array of Title objects to be touched
460 * @param string $timestamp the timestamp to use instead of the
461 * default current time
465 /* static */ function touchArray( $titles, $timestamp = '' ) {
466 if ( count( $titles ) == 0 ) {
469 $dbw =& wfGetDB( DB_MASTER
);
470 if ( $timestamp == '' ) {
471 $timestamp = $dbw->timestamp();
473 $page = $dbw->tableName( 'page' );
474 $sql = "UPDATE $page SET page_touched='{$timestamp}' WHERE page_id IN (";
477 foreach ( $titles as $title ) {
482 $sql .= $title->getArticleID();
486 $dbw->query( $sql, 'Title::touchArray' );
490 #----------------------------------------------------------------------------
492 #----------------------------------------------------------------------------
494 /** Simple accessors */
496 * Get the text form (spaces not underscores) of the main part
500 function getText() { return $this->mTextform
; }
502 * Get the URL-encoded form of the main part
506 function getPartialURL() { return $this->mUrlform
; }
508 * Get the main part with underscores
512 function getDBkey() { return $this->mDbkeyform
; }
514 * Get the namespace index, i.e. one of the NS_xxxx constants
518 function getNamespace() { return $this->mNamespace
; }
520 * Get the interwiki prefix (or null string)
524 function getInterwiki() { return $this->mInterwiki
; }
526 * Get the Title fragment (i.e. the bit after the #)
530 function getFragment() { return $this->mFragment
; }
532 * Get the default namespace index, for when there is no namespace
536 function getDefaultNamespace() { return $this->mDefaultNamespace
; }
539 * Get title for search index
540 * @return string a stripped-down title string ready for the
543 function getIndexTitle() {
544 return Title
::indexTitle( $this->mNamespace
, $this->mTextform
);
548 * Get the prefixed database key form
549 * @return string the prefixed title, with underscores and
550 * any interwiki and namespace prefixes
553 function getPrefixedDBkey() {
554 $s = $this->prefix( $this->mDbkeyform
);
555 $s = str_replace( ' ', '_', $s );
560 * Get the prefixed title with spaces.
561 * This is the form usually used for display
562 * @return string the prefixed title, with spaces
565 function getPrefixedText() {
567 if ( empty( $this->mPrefixedText
) ) {
568 $s = $this->prefix( $this->mTextform
);
569 $s = str_replace( '_', ' ', $s );
570 $this->mPrefixedText
= $s;
572 return $this->mPrefixedText
;
576 * Get the prefixed title with spaces, plus any fragment
577 * (part beginning with '#')
578 * @return string the prefixed title, with spaces and
579 * the fragment, including '#'
582 function getFullText() {
584 $text = $this->getPrefixedText();
585 if( '' != $this->mFragment
) {
586 $text .= '#' . $this->mFragment
;
592 * Get a URL-encoded title (not an actual URL) including interwiki
593 * @return string the URL-encoded form
596 function getPrefixedURL() {
597 $s = $this->prefix( $this->mDbkeyform
);
598 $s = str_replace( ' ', '_', $s );
600 $s = wfUrlencode ( $s ) ;
602 # Cleaning up URL to make it look nice -- is this safe?
603 $s = str_replace( '%28', '(', $s );
604 $s = str_replace( '%29', ')', $s );
610 * Get a real URL referring to this title, with interwiki link and
613 * @param string $query an optional query string, not used
614 * for interwiki links
615 * @return string the URL
618 function getFullURL( $query = '' ) {
619 global $wgContLang, $wgArticlePath, $wgServer, $wgScript;
621 if ( '' == $this->mInterwiki
) {
623 return $wgServer . $this->getLocalUrl( $query );
625 $baseUrl = $this->getInterwikiLink( $this->mInterwiki
);
626 $namespace = $wgContLang->getNsText( $this->mNamespace
);
627 if ( '' != $namespace ) {
628 # Can this actually happen? Interwikis shouldn't be parsed.
631 $url = str_replace( '$1', $namespace . $this->mUrlform
, $baseUrl );
632 if ( '' != $this->mFragment
) {
633 $url .= '#' . $this->mFragment
;
640 * Get a URL with no fragment or server name
641 * @param string $query an optional query string; if not specified,
642 * $wgArticlePath will be used.
643 * @return string the URL
646 function getLocalURL( $query = '' ) {
647 global $wgLang, $wgArticlePath, $wgScript;
649 if ( $this->isExternal() ) {
650 return $this->getFullURL();
653 $dbkey = wfUrlencode( $this->getPrefixedDBkey() );
654 if ( $query == '' ) {
655 $url = str_replace( '$1', $dbkey, $wgArticlePath );
657 if( preg_match( '/^(.*&|)action=([^&]*)(&(.*)|)$/', $query, $matches ) ) {
658 global $wgActionPaths;
659 $action = urldecode( $matches[2] );
660 if( isset( $wgActionPaths[$action] ) ) {
661 $query = $matches[1];
662 if( isset( $matches[4] ) ) $query .= $matches[4];
663 $url = str_replace( '$1', $dbkey, $wgActionPaths[$action] );
664 if( $query != '' ) $url .= '?' . $query;
668 if ( $query == '-' ) {
671 $url = "{$wgScript}?title={$dbkey}&{$query}";
677 * Get an HTML-escaped version of the URL form, suitable for
678 * using in a link, without a server name or fragment
679 * @param string $query an optional query string
680 * @return string the URL
683 function escapeLocalURL( $query = '' ) {
684 return htmlspecialchars( $this->getLocalURL( $query ) );
688 * Get an HTML-escaped version of the URL form, suitable for
689 * using in a link, including the server name and fragment
691 * @return string the URL
692 * @param string $query an optional query string
695 function escapeFullURL( $query = '' ) {
696 return htmlspecialchars( $this->getFullURL( $query ) );
700 * Get the URL form for an internal link.
701 * - Used in various Squid-related code, in case we have a different
702 * internal hostname for the server from the exposed one.
704 * @param string $query an optional query string
705 * @return string the URL
708 function getInternalURL( $query = '' ) {
709 global $wgInternalServer;
710 return $wgInternalServer . $this->getLocalURL( $query );
714 * Get the edit URL for this Title
715 * @return string the URL, or a null string if this is an
719 function getEditURL() {
720 global $wgServer, $wgScript;
722 if ( '' != $this->mInterwiki
) { return ''; }
723 $s = $this->getLocalURL( 'action=edit' );
729 * Get the HTML-escaped displayable text form.
730 * Used for the title field in <a> tags.
731 * @return string the text, including any prefixes
734 function getEscapedText() {
735 return htmlspecialchars( $this->getPrefixedText() );
739 * Is this Title interwiki?
743 function isExternal() { return ( '' != $this->mInterwiki
); }
746 * Does the title correspond to a protected article?
747 * @param string $what the action the page is protected from,
748 * by default checks move and edit
752 function isProtected($action = '') {
753 if ( -1 == $this->mNamespace
) { return true; }
754 if($action == 'edit' ||
$action == '') {
755 $a = $this->getRestrictions("edit");
756 if ( in_array( 'sysop', $a ) ) { return true; }
758 if($action == 'move' ||
$action == '') {
759 $a = $this->getRestrictions("move");
760 if ( in_array( 'sysop', $a ) ) { return true; }
766 * Is $wgUser is watching this page?
770 function userIsWatching() {
773 if ( -1 == $this->mNamespace
) { return false; }
774 if ( 0 == $wgUser->getID() ) { return false; }
776 return $wgUser->isWatched( $this );
780 * Is $wgUser perform $action this page?
781 * @param string $action action that permission needs to be checked for
785 function userCan($action) {
786 $fname = 'Title::userCanEdit';
787 wfProfileIn( $fname );
790 if( NS_SPECIAL
== $this->mNamespace
) {
791 wfProfileOut( $fname );
794 if( NS_MEDIAWIKI
== $this->mNamespace
&&
795 !$wgUser->isAllowed('editinterface') ) {
796 wfProfileOut( $fname );
799 if( $this->mDbkeyform
== '_' ) {
800 # FIXME: Is this necessary? Shouldn't be allowed anyway...
801 wfProfileOut( $fname );
805 # protect global styles and js
806 if ( NS_MEDIAWIKI
== $this->mNamespace
807 && preg_match("/\\.(css|js)$/", $this->mTextform
)
808 && !$wgUser->isAllowed('editinterface') ) {
809 wfProfileOut( $fname );
813 # protect css/js subpages of user pages
814 # XXX: this might be better using restrictions
815 # XXX: Find a way to work around the php bug that prevents using $this->userCanEditCssJsSubpage() from working
816 if( NS_USER
== $this->mNamespace
817 && preg_match("/\\.(css|js)$/", $this->mTextform
)
818 && !$wgUser->isAllowed('editinterface')
819 && !preg_match('/^'.preg_quote($wgUser->getName(), '/').'\//', $this->mTextform
) ) {
820 wfProfileOut( $fname );
824 foreach( $this->getRestrictions($action) as $right ) {
825 if( '' != $right && !$wgUser->isAllowed( $right ) ) {
826 wfProfileOut( $fname );
830 wfProfileOut( $fname );
835 * Can $wgUser edit this page?
839 function userCanEdit() {
840 return $this->userCan('edit');
844 * Can $wgUser move this page?
848 function userCanMove() {
849 return $this->userCan('move');
853 * Can $wgUser read this page?
857 function userCanRead() {
860 if( $wgUser->isAllowed('read') ) {
863 global $wgWhitelistRead;
865 /** If anon users can create an account,
866 they need to reach the login page first! */
867 if( $wgUser->isAllowed( 'createaccount' )
868 && $this->mId
== NS_SPECIAL
869 && $this->getText() == 'Userlogin' ) {
873 /** some pages are explicitly allowed */
874 $name = $this->getPrefixedText();
875 if( in_array( $name, $wgWhitelistRead ) ) {
879 # Compatibility with old settings
880 if( $this->getNamespace() == NS_MAIN
) {
881 if( in_array( ':' . $name, $wgWhitelistRead ) ) {
890 * Is this a talk page of some sort?
894 function isTalkPage() {
895 return Namespace::isTalk( $this->getNamespace() );
899 * Is this a .css or .js subpage of a user page?
903 function isCssJsSubpage() {
904 return ( NS_USER
== $this->mNamespace
and preg_match("/\\.(css|js)$/", $this->mTextform
) );
907 * Is this a .css subpage of a user page?
911 function isCssSubpage() {
912 return ( NS_USER
== $this->mNamespace
and preg_match("/\\.css$/", $this->mTextform
) );
915 * Is this a .js subpage of a user page?
919 function isJsSubpage() {
920 return ( NS_USER
== $this->mNamespace
and preg_match("/\\.js$/", $this->mTextform
) );
923 * Protect css/js subpages of user pages: can $wgUser edit
927 * @todo XXX: this might be better using restrictions
930 function userCanEditCssJsSubpage() {
932 return ( $wgUser->isAllowed('editinterface') or preg_match('/^'.preg_quote($wgUser->getName(), '/').'\//', $this->mTextform
) );
936 * Loads a string into mRestrictions array
937 * @param string $res restrictions in string format
940 function loadRestrictions( $res ) {
941 foreach( explode( ':', trim( $res ) ) as $restrict ) {
942 $temp = explode( '=', trim( $restrict ) );
943 if(count($temp) == 1) {
944 // old format should be treated as edit/move restriction
945 $this->mRestrictions
["edit"] = explode( ',', trim( $temp[0] ) );
946 $this->mRestrictions
["move"] = explode( ',', trim( $temp[0] ) );
948 $this->mRestrictions
[$temp[0]] = explode( ',', trim( $temp[1] ) );
951 $this->mRestrictionsLoaded
= true;
955 * Accessor/initialisation for mRestrictions
956 * @param string $action action that permission needs to be checked for
957 * @return array the array of groups allowed to edit this article
960 function getRestrictions($action) {
961 $id = $this->getArticleID();
962 if ( 0 == $id ) { return array(); }
964 if ( ! $this->mRestrictionsLoaded
) {
965 $dbr =& wfGetDB( DB_SLAVE
);
966 $res = $dbr->selectField( 'page', 'page_restrictions', 'page_id='.$id );
967 $this->loadRestrictions( $res );
969 if( isset( $this->mRestrictions
[$action] ) ) {
970 return $this->mRestrictions
[$action];
976 * Is there a version of this page in the deletion archive?
977 * @return int the number of archived revisions
980 function isDeleted() {
981 $fname = 'Title::isDeleted';
982 $dbr =& wfGetDB( DB_SLAVE
);
983 $n = $dbr->selectField( 'archive', 'COUNT(*)', array( 'ar_namespace' => $this->getNamespace(),
984 'ar_title' => $this->getDBkey() ), $fname );
989 * Get the article ID for this Title from the link cache,
990 * adding it if necessary
991 * @param int $flags a bit field; may be GAID_FOR_UPDATE to select
996 function getArticleID( $flags = 0 ) {
999 if ( $flags & GAID_FOR_UPDATE
) {
1000 $oldUpdate = $wgLinkCache->forUpdate( true );
1001 $this->mArticleID
= $wgLinkCache->addLinkObj( $this );
1002 $wgLinkCache->forUpdate( $oldUpdate );
1004 if ( -1 == $this->mArticleID
) {
1005 $this->mArticleID
= $wgLinkCache->addLinkObj( $this );
1008 return $this->mArticleID
;
1012 * This clears some fields in this object, and clears any associated
1013 * keys in the "bad links" section of $wgLinkCache.
1015 * - This is called from Article::insertNewArticle() to allow
1016 * loading of the new page_id. It's also called from
1017 * Article::doDeleteArticle()
1019 * @param int $newid the new Article ID
1022 function resetArticleID( $newid ) {
1023 global $wgLinkCache;
1024 $wgLinkCache->clearBadLink( $this->getPrefixedDBkey() );
1026 if ( 0 == $newid ) { $this->mArticleID
= -1; }
1027 else { $this->mArticleID
= $newid; }
1028 $this->mRestrictionsLoaded
= false;
1029 $this->mRestrictions
= array();
1033 * Updates page_touched for this page; called from LinksUpdate.php
1034 * @return bool true if the update succeded
1037 function invalidateCache() {
1038 $now = wfTimestampNow();
1039 $dbw =& wfGetDB( DB_MASTER
);
1040 $success = $dbw->update( 'page',
1042 'page_touched' => $dbw->timestamp()
1043 ), array( /* WHERE */
1044 'page_namespace' => $this->getNamespace() ,
1045 'page_title' => $this->getDBkey()
1046 ), 'Title::invalidateCache'
1052 * Prefix some arbitrary text with the namespace or interwiki prefix
1055 * @param string $name the text
1056 * @return string the prefixed text
1059 /* private */ function prefix( $name ) {
1063 if ( '' != $this->mInterwiki
) {
1064 $p = $this->mInterwiki
. ':';
1066 if ( 0 != $this->mNamespace
) {
1067 $p .= $wgContLang->getNsText( $this->mNamespace
) . ':';
1073 * Secure and split - main initialisation function for this object
1075 * Assumes that mDbkeyform has been set, and is urldecoded
1076 * and uses underscores, but not otherwise munged. This function
1077 * removes illegal characters, splits off the interwiki and
1078 * namespace prefixes, sets the other forms, and canonicalizes
1080 * @return bool true on success
1083 /* private */ function secureAndSplit() {
1084 global $wgContLang, $wgLocalInterwiki, $wgCapitalLinks;
1085 $fname = 'Title::secureAndSplit';
1086 wfProfileIn( $fname );
1089 static $rxTc = false;
1091 # % is needed as well
1092 $rxTc = '/[^' . Title
::legalChars() . ']|%[0-9A-Fa-f]{2}/S';
1095 $this->mInterwiki
= $this->mFragment
= '';
1096 $this->mNamespace
= $this->mDefaultNamespace
; # Usually NS_MAIN
1098 # Clean up whitespace
1100 $t = preg_replace( '/[ _]+/', '_', $this->mDbkeyform
);
1101 $t = trim( $t, '_' );
1104 wfProfileOut( $fname );
1108 global $wgUseLatin1;
1109 if( !$wgUseLatin1 && false !== strpos( $t, UTF8_REPLACEMENT
) ) {
1110 # Contained illegal UTF-8 sequences or forbidden Unicode chars.
1111 wfProfileOut( $fname );
1115 $this->mDbkeyform
= $t;
1117 # Initial colon indicating main namespace
1118 if ( ':' == $t{0} ) {
1119 $r = substr( $t, 1 );
1120 $this->mNamespace
= NS_MAIN
;
1122 # Namespace or interwiki prefix
1125 if ( preg_match( "/^(.+?)_*:_*(.*)$/S", $t, $m ) ) {
1127 $lowerNs = strtolower( $p );
1128 if ( $ns = Namespace::getCanonicalIndex( $lowerNs ) ) {
1129 # Canonical namespace
1131 $this->mNamespace
= $ns;
1132 } elseif ( $ns = $wgContLang->getNsIndex( $lowerNs )) {
1133 # Ordinary namespace
1135 $this->mNamespace
= $ns;
1136 } elseif( $this->getInterwikiLink( $p ) ) {
1138 # Can't make a local interwiki link to an interwiki link.
1139 # That's just crazy!
1140 wfProfileOut( $fname );
1146 $this->mInterwiki
= $p;
1148 # Redundant interwiki prefix to the local wiki
1149 if ( 0 == strcasecmp( $this->mInterwiki
, $wgLocalInterwiki ) ) {
1151 # Can't have an empty self-link
1152 wfProfileOut( $fname );
1155 $this->mInterwiki
= '';
1157 # Do another namespace split...
1161 # If there's no recognized interwiki or namespace,
1162 # then let the colon expression be part of the title.
1169 # We already know that some pages won't be in the database!
1171 if ( '' != $this->mInterwiki ||
-1 == $this->mNamespace
) {
1172 $this->mArticleID
= 0;
1174 $f = strstr( $r, '#' );
1175 if ( false !== $f ) {
1176 $this->mFragment
= substr( $f, 1 );
1177 $r = substr( $r, 0, strlen( $r ) - strlen( $f ) );
1178 # remove whitespace again: prevents "Foo_bar_#"
1179 # becoming "Foo_bar_"
1180 $r = preg_replace( '/_*$/', '', $r );
1183 # Reject illegal characters.
1185 if( preg_match( $rxTc, $r ) ) {
1186 wfProfileOut( $fname );
1191 * Pages with "/./" or "/../" appearing in the URLs will
1192 * often be unreachable due to the way web browsers deal
1193 * with 'relative' URLs. Forbid them explicitly.
1195 if ( strpos( $r, '.' ) !== false &&
1196 ( $r === '.' ||
$r === '..' ||
1197 strpos( $r, './' ) === 0 ||
1198 strpos( $r, '../' ) === 0 ||
1199 strpos( $r, '/./' ) !== false ||
1200 strpos( $r, '/../' ) !== false ) )
1202 wfProfileOut( $fname );
1206 # We shouldn't need to query the DB for the size.
1207 #$maxSize = $dbr->textFieldSize( 'page', 'page_title' );
1208 if ( strlen( $r ) > 255 ) {
1209 wfProfileOut( $fname );
1214 * Normally, all wiki links are forced to have
1215 * an initial capital letter so [[foo]] and [[Foo]]
1216 * point to the same place.
1218 * Don't force it for interwikis, since the other
1219 * site might be case-sensitive.
1221 if( $wgCapitalLinks && $this->mInterwiki
== '') {
1222 $t = $wgContLang->ucfirst( $r );
1228 * Can't make a link to a namespace alone...
1229 * "empty" local links can only be self-links
1230 * with a fragment identifier.
1233 $this->mInterwiki
== '' &&
1234 $this->mNamespace
!= NS_MAIN
) {
1235 wfProfileOut( $fname );
1239 if( $wgUseLatin1 && $this->mInterwiki
!= '' ) {
1240 # On a Latin-1 wiki, numbered character entities may have
1241 # left us with a mix of 8-bit and UTF-8 characters, and
1242 # some of those might be Windows-1252 special chars.
1243 # Normalize interwikis to pure UTF-8.
1244 $t = Title
::mergeLatin1Utf8( $t );
1248 $this->mDbkeyform
= $t;
1249 $this->mUrlform
= wfUrlencode( $t );
1251 $this->mTextform
= str_replace( '_', ' ', $t );
1253 wfProfileOut( $fname );
1258 * Get a Title object associated with the talk page of this article
1259 * @return Title the object for the talk page
1262 function getTalkPage() {
1263 return Title
::makeTitle( Namespace::getTalk( $this->getNamespace() ), $this->getDBkey() );
1267 * Get a title object associated with the subject page of this
1270 * @return Title the object for the subject page
1273 function getSubjectPage() {
1274 return Title
::makeTitle( Namespace::getSubject( $this->getNamespace() ), $this->getDBkey() );
1278 * Get an array of Title objects linking to this Title
1279 * - Also stores the IDs in the link cache.
1281 * @param string $options may be FOR UPDATE
1282 * @return array the Title objects linking here
1285 function getLinksTo( $options = '' ) {
1286 global $wgLinkCache;
1287 $id = $this->getArticleID();
1290 $db =& wfGetDB( DB_MASTER
);
1292 $db =& wfGetDB( DB_SLAVE
);
1294 $page = $db->tableName( 'page' );
1295 $links = $db->tableName( 'links' );
1297 $sql = "SELECT page_namespace,page_title,page_id FROM $page,$links WHERE l_from=page_id AND l_to={$id} $options";
1298 $res = $db->query( $sql, 'Title::getLinksTo' );
1300 if ( $db->numRows( $res ) ) {
1301 while ( $row = $db->fetchObject( $res ) ) {
1302 if ( $titleObj = Title
::makeTitle( $row->page_namespace
, $row->page_title
) ) {
1303 $wgLinkCache->addGoodLink( $row->page_id
, $titleObj->getPrefixedDBkey() );
1304 $retVal[] = $titleObj;
1308 $db->freeResult( $res );
1313 * Get an array of Title objects linking to this non-existent title.
1314 * - Also stores the IDs in the link cache.
1316 * @param string $options may be FOR UPDATE
1317 * @return array the Title objects linking here
1320 function getBrokenLinksTo( $options = '' ) {
1321 global $wgLinkCache;
1324 $db =& wfGetDB( DB_MASTER
);
1326 $db =& wfGetDB( DB_SLAVE
);
1328 $page = $db->tableName( 'page' );
1329 $brokenlinks = $db->tableName( 'brokenlinks' );
1330 $encTitle = $db->strencode( $this->getPrefixedDBkey() );
1332 $sql = "SELECT page_namespace,page_title,page_id FROM $brokenlinks,$page " .
1333 "WHERE bl_from=page_id AND bl_to='$encTitle' $options";
1334 $res = $db->query( $sql, "Title::getBrokenLinksTo" );
1336 if ( $db->numRows( $res ) ) {
1337 while ( $row = $db->fetchObject( $res ) ) {
1338 $titleObj = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
1339 $wgLinkCache->addGoodLink( $row->page_id
, $titleObj->getPrefixedDBkey() );
1340 $retVal[] = $titleObj;
1343 $db->freeResult( $res );
1348 * Get a list of URLs to purge from the Squid cache when this
1351 * @return array the URLs
1354 function getSquidURLs() {
1356 $this->getInternalURL(),
1357 $this->getInternalURL( 'action=history' )
1362 * Move this page without authentication
1363 * @param Title &$nt the new page Title
1366 function moveNoAuth( &$nt ) {
1367 return $this->moveTo( $nt, false );
1371 * Move a title to a new location
1372 * @param Title &$nt the new title
1373 * @param bool $auth indicates whether $wgUser's permissions
1375 * @return mixed true on success, message name on failure
1378 function moveTo( &$nt, $auth = true ) {
1380 if( !$this or !$nt ) {
1381 return 'badtitletext';
1384 $fname = 'Title::move';
1385 $oldid = $this->getArticleID();
1386 $newid = $nt->getArticleID();
1388 if ( strlen( $nt->getDBkey() ) < 1 ) {
1389 return 'articleexists';
1391 if ( ( ! Namespace::isMovable( $this->getNamespace() ) ) ||
1392 ( '' == $this->getDBkey() ) ||
1393 ( '' != $this->getInterwiki() ) ||
1395 ( ! Namespace::isMovable( $nt->getNamespace() ) ) ||
1396 ( '' == $nt->getDBkey() ) ||
1397 ( '' != $nt->getInterwiki() ) ) {
1398 return 'badarticleerror';
1402 !$this->userCanEdit() ||
!$nt->userCanEdit() ||
1403 !$this->userCanMove() ||
!$nt->userCanMove() ) ) {
1404 return 'protectedpage';
1407 # The move is allowed only if (1) the target doesn't exist, or
1408 # (2) the target is a redirect to the source, and has no history
1409 # (so we can undo bad moves right after they're done).
1411 if ( 0 != $newid ) { # Target exists; check for validity
1412 if ( ! $this->isValidMoveTarget( $nt ) ) {
1413 return 'articleexists';
1415 $this->moveOverExistingRedirect( $nt );
1416 } else { # Target didn't exist, do normal move.
1417 $this->moveToNewTitle( $nt, $newid );
1420 # Fixing category links (those without piped 'alternate' names) to be sorted under the new title
1422 $dbw =& wfGetDB( DB_MASTER
);
1423 $categorylinks = $dbw->tableName( 'categorylinks' );
1424 $sql = "UPDATE $categorylinks SET cl_sortkey=" . $dbw->addQuotes( $nt->getPrefixedText() ) .
1425 " WHERE cl_from=" . $dbw->addQuotes( $this->getArticleID() ) .
1426 " AND cl_sortkey=" . $dbw->addQuotes( $this->getPrefixedText() );
1427 $dbw->query( $sql, 'SpecialMovepage::doSubmit' );
1431 $oldnamespace = $this->getNamespace() & ~
1;
1432 $newnamespace = $nt->getNamespace() & ~
1;
1433 $oldtitle = $this->getDBkey();
1434 $newtitle = $nt->getDBkey();
1436 if( $oldnamespace != $newnamespace ||
$oldtitle != $newtitle ) {
1437 WatchedItem
::duplicateEntries( $this, $nt );
1440 # Update search engine
1441 $u = new SearchUpdate( $oldid, $nt->getPrefixedDBkey() );
1443 $u = new SearchUpdate( $newid, $this->getPrefixedDBkey(), '' );
1446 wfRunHooks( 'TitleMoveComplete', $this, $nt, $wgUser, $oldid, $newid );
1451 * Move page to a title which is at present a redirect to the
1454 * @param Title &$nt the page to move to, which should currently
1458 /* private */ function moveOverExistingRedirect( &$nt ) {
1459 global $wgUser, $wgLinkCache, $wgUseSquid, $wgMwRedir;
1460 $fname = 'Title::moveOverExistingRedirect';
1461 $comment = wfMsgForContent( '1movedto2', $this->getPrefixedText(), $nt->getPrefixedText() );
1463 $now = wfTimestampNow();
1465 $newid = $nt->getArticleID();
1466 $oldid = $this->getArticleID();
1467 $dbw =& wfGetDB( DB_MASTER
);
1468 $links = $dbw->tableName( 'links' );
1470 # Delete the old redirect. We don't save it to history since
1471 # by definition if we've got here it's rather uninteresting.
1472 # We have to remove it so that the next step doesn't trigger
1473 # a conflict on the unique namespace+title index...
1474 $dbw->delete( 'page', array( 'page_id' => $newid ), $fname );
1476 # Change the name of the target page:
1477 $dbw->update( 'page',
1479 'page_touched' => $dbw->timestamp($now),
1480 'page_namespace' => $nt->getNamespace(),
1481 'page_title' => $nt->getDBkey()
1483 /* WHERE */ array( 'page_id' => $oldid ),
1486 $wgLinkCache->clearLink( $nt->getPrefixedDBkey() );
1488 # Recreate the redirect, this time in the other direction.
1489 $redirectText = $wgMwRedir->getSynonym( 0 ) . ' [[' . $nt->getPrefixedText() . "]]\n";
1490 $dbw->insert( 'revision', array(
1491 'rev_id' => $dbw->nextSequenceValue('rev_rev_id_seq'),
1492 'rev_comment' => $comment,
1493 'rev_user' => $wgUser->getID(),
1494 'rev_user_text' => $wgUser->getName(),
1495 'rev_timestamp' => $now
1498 $revid = $dbw->insertId();
1499 $dbw->insert( 'text', array(
1502 'old_text' => $redirectText,
1505 $dbw->insert( 'page', array(
1506 'page_id' => $dbw->nextSequenceValue('page_page_id_seq'),
1507 'page_namespace' => $this->getNamespace(),
1508 'page_title' => $this->getDBkey(),
1509 'page_touched' => $now,
1510 'page_is_redirect' => 1,
1511 'page_random' => $rand,
1513 'page_latest' => $revid), $fname
1515 $newid = $dbw->insertId();
1516 $wgLinkCache->clearLink( $this->getPrefixedDBkey() );
1519 // Replaced by a log entry
1520 // RecentChange::notifyMoveOverRedirect( $now, $this, $nt, $wgUser, $comment );
1523 $log = new LogPage( 'move' );
1524 $log->addEntry( 'move_redir', $this, '', array(1 => $nt->getText()) );
1528 # Load titles and IDs
1529 $linksToOld = $this->getLinksTo( 'FOR UPDATE' );
1530 $linksToNew = $nt->getLinksTo( 'FOR UPDATE' );
1533 $sql = "DELETE FROM $links WHERE l_to=$oldid OR l_to=$newid";
1534 $dbw->query( $sql, $fname );
1537 if ( count( $linksToOld ) ||
count( $linksToNew )) {
1538 $sql = "INSERT INTO $links (l_from,l_to) VALUES ";
1541 # Insert links to old title
1542 foreach ( $linksToOld as $linkTitle ) {
1548 $id = $linkTitle->getArticleID();
1549 $sql .= "($id,$newid)";
1552 # Insert links to new title
1553 foreach ( $linksToNew as $linkTitle ) {
1559 $id = $linkTitle->getArticleID();
1560 $sql .= "($id, $oldid)";
1563 $dbw->query( $sql, DB_MASTER
, $fname );
1566 # Now, we record the link from the redirect to the new title.
1567 # It should have no other outgoing links...
1568 $dbw->delete( 'links', array( 'l_from' => $newid ) );
1569 $dbw->insert( 'links', array( 'l_from' => $newid, 'l_to' => $oldid ) );
1572 LinkCache
::linksccClearLinksTo( $oldid );
1573 LinkCache
::linksccClearLinksTo( $newid );
1576 if ( $wgUseSquid ) {
1577 $urls = array_merge( $nt->getSquidURLs(), $this->getSquidURLs() );
1578 $u = new SquidUpdate( $urls );
1584 * Move page to non-existing title.
1585 * @param Title &$nt the new Title
1586 * @param int &$newid set to be the new article ID
1589 /* private */ function moveToNewTitle( &$nt, &$newid ) {
1590 global $wgUser, $wgLinkCache, $wgUseSquid;
1592 $fname = 'MovePageForm::moveToNewTitle';
1593 $comment = wfMsgForContent( '1movedto2', $this->getPrefixedText(), $nt->getPrefixedText() );
1595 $newid = $nt->getArticleID();
1596 $oldid = $this->getArticleID();
1597 $dbw =& wfGetDB( DB_MASTER
);
1598 $now = $dbw->timestamp();
1603 $dbw->update( 'page',
1605 'page_touched' => $now,
1606 'page_namespace' => $nt->getNamespace(),
1607 'page_title' => $nt->getDBkey()
1609 /* WHERE */ array( 'page_id' => $oldid ),
1613 $wgLinkCache->clearLink( $nt->getPrefixedDBkey() );
1616 $redirectText = $wgMwRedir->getSynonym( 0 ) . ' [[' . $nt->getPrefixedText() . "]]\n";
1617 $dbw->insert( 'revision', array(
1618 'rev_id' => $dbw->nextSequenceValue('rev_rev_id_seq'),
1619 'rev_comment' => $comment,
1620 'rev_user' => $wgUser->getID(),
1621 'rev_user_text' => $wgUser->getName(),
1622 'rev_timestamp' => $now
1625 $revid = $dbw->insertId();
1626 $dbw->insert( 'text', array(
1629 'old_text' => $redirectText
1632 $dbw->insert( 'page', array(
1633 'page_id' => $dbw->nextSequenceValue('page_page_id_seq'),
1634 'page_namespace' => $this->getNamespace(),
1635 'page_title' => $this->getDBkey(),
1636 'page_touched' => $now,
1637 'page_is_redirect' => 1,
1638 'page_random' => $rand,
1640 'page_latest' => $revid), $fname
1642 $newid = $dbw->insertId();
1643 $wgLinkCache->clearLink( $this->getPrefixedDBkey() );
1645 // attach revision to the new page
1646 $dbw->update( 'revision', array('rev_page' => $newid), array('rev_id' => $revid), $fname);
1649 // Replaced by a log entry
1650 // RecentChange::notifyMoveToNew( $now, $this, $nt, $wgUser, $comment );
1653 $log = new LogPage( 'move' );
1654 $log->addEntry( 'move', $this, '', array(1 => $nt->getText()) );
1656 # Purge squid and linkscc as per article creation
1657 Article
::onArticleCreate( $nt );
1659 # Any text links to the old title must be reassigned to the redirect
1660 $dbw->update( 'links', array( 'l_to' => $newid ), array( 'l_to' => $oldid ), $fname );
1661 LinkCache
::linksccClearLinksTo( $oldid );
1663 # Record the just-created redirect's linking to the page
1664 $dbw->insert( 'links', array( 'l_from' => $newid, 'l_to' => $oldid ), $fname );
1666 # Non-existent target may have had broken links to it; these must
1667 # now be removed and made into good links.
1668 $update = new LinksUpdate( $oldid, $nt->getPrefixedDBkey() );
1669 $update->fixBrokenLinks();
1671 # Purge old title from squid
1672 # The new title, and links to the new title, are purged in Article::onArticleCreate()
1673 $titles = $nt->getLinksTo();
1674 if ( $wgUseSquid ) {
1675 $urls = $this->getSquidURLs();
1676 foreach ( $titles as $linkTitle ) {
1677 $urls[] = $linkTitle->getInternalURL();
1679 $u = new SquidUpdate( $urls );
1685 * Checks if $this can be moved to a given Title
1686 * - Selects for update, so don't call it unless you mean business
1688 * @param Title &$nt the new title to check
1691 function isValidMoveTarget( $nt ) {
1693 $fname = 'Title::isValidMoveTarget';
1694 $dbw =& wfGetDB( DB_MASTER
);
1697 $id = $nt->getArticleID();
1698 $obj = $dbw->selectRow( array( 'page', 'text') ,
1699 array( 'page_is_redirect','old_text' ),
1700 array( 'page_id' => $id, 'page_latest=old_id' ),
1701 $fname, 'FOR UPDATE' );
1703 if ( !$obj ||
0 == $obj->page_is_redirect
) {
1708 # Does the redirect point to the source?
1709 if ( preg_match( "/\\[\\[\\s*([^\\]\\|]*)]]/", $obj->old_text
, $m ) ) {
1710 $redirTitle = Title
::newFromText( $m[1] );
1711 if( !is_object( $redirTitle ) ||
1712 $redirTitle->getPrefixedDBkey() != $this->getPrefixedDBkey() ) {
1717 # Does the article have a history?
1718 $row = $dbw->selectRow( array( 'page', 'revision'),
1720 array( 'page_namespace' => $nt->getNamespace(),
1721 'page_title' => $nt->getDBkey(),
1722 'page_id=rev_page AND page_latest != rev_id'
1723 ), $fname, 'FOR UPDATE'
1726 # Return true if there was no history
1727 return $row === false;
1731 * Create a redirect; fails if the title already exists; does
1734 * @param Title $dest the destination of the redirect
1735 * @param string $comment the comment string describing the move
1736 * @return bool true on success
1739 function createRedirect( $dest, $comment ) {
1741 if ( $this->getArticleID() ) {
1745 $fname = 'Title::createRedirect';
1746 $dbw =& wfGetDB( DB_MASTER
);
1747 $now = wfTimestampNow();
1749 $seqVal = $dbw->nextSequenceValue( 'page_page_id_seq' );
1750 $dbw->insert( 'page', array(
1751 'page_id' => $seqVal,
1752 'page_namespace' => $this->getNamespace(),
1753 'page_title' => $this->getDBkey(),
1754 'page_touched' => $now,
1755 'page_is_redirect' => 1,
1757 'page_latest' => NULL,
1759 $newid = $dbw->insertId();
1761 $seqVal = $dbw->nextSequenceValue( 'text_old_id_seq' );
1762 $dbw->insert( 'text', array(
1763 'old_id' => $seqVal,
1765 'old_text' => "#REDIRECT [[" . $dest->getPrefixedText() . "]]\n"
1767 $revisionId = $dbw->insertId();
1769 $dbw->insert( 'revision', array(
1770 'rev_id' => $seqVal,
1771 'rev_page' => $newid,
1772 'rev_comment' => $comment,
1773 'rev_user' => $wgUser->getID(),
1774 'rev_user_text' => $wgUser->getName(),
1775 'rev_timestamp' => $now,
1778 $dbw->update( 'page',
1779 /* SET */ array( 'page_latest' => $revisionId ),
1780 /* WHERE */ array( 'page_id' => $newid ),
1782 $this->resetArticleID( $newid );
1785 if ( $dest->getArticleID() ) {
1786 $dbw->insert( 'links',
1788 'l_to' => $dest->getArticleID(),
1793 $dbw->insert( 'brokenlinks',
1795 'bl_to' => $dest->getPrefixedDBkey(),
1801 Article
::onArticleCreate( $this );
1806 * Get categories to which this Title belongs and return an array of
1807 * categories' names.
1809 * @return array an array of parents in the form:
1810 * $parent => $currentarticle
1813 function getParentCategories() {
1814 global $wgContLang,$wgUser;
1816 $titlekey = $this->getArticleId();
1817 $sk =& $wgUser->getSkin();
1819 $dbr =& wfGetDB( DB_SLAVE
);
1820 $categorylinks = $dbr->tableName( 'categorylinks' );
1823 $sql = "SELECT * FROM $categorylinks"
1824 ." WHERE cl_from='$titlekey'"
1825 ." AND cl_from <> '0'"
1826 ." ORDER BY cl_sortkey";
1828 $res = $dbr->query ( $sql ) ;
1830 if($dbr->numRows($res) > 0) {
1831 while ( $x = $dbr->fetchObject ( $res ) )
1832 //$data[] = Title::newFromText($wgContLang->getNSText ( NS_CATEGORY ).':'.$x->cl_to);
1833 $data[$wgContLang->getNSText ( NS_CATEGORY
).':'.$x->cl_to
] = $this->getFullText();
1834 $dbr->freeResult ( $res ) ;
1842 * Get a tree of parent categories
1843 * @param array $children an array with the children in the keys, to check for circular refs
1847 function getParentCategoryTree( $children = array() ) {
1848 $parents = $this->getParentCategories();
1850 if($parents != '') {
1851 foreach($parents as $parent => $current)
1853 if ( array_key_exists( $parent, $children ) ) {
1854 # Circular reference
1855 $stack[$parent] = array();
1857 $nt = Title
::newFromText($parent);
1858 $stack[$parent] = $nt->getParentCategoryTree( $children +
array($parent => 1) );
1869 * Get an associative array for selecting this title from
1875 function curCond() {
1876 wfDebugDieBacktrace( 'curCond called' );
1877 return array( 'cur_namespace' => $this->mNamespace
, 'cur_title' => $this->mDbkeyform
);
1881 * Get an associative array for selecting this title from the
1887 function oldCond() {
1888 wfDebugDieBacktrace( 'oldCond called' );
1889 return array( 'old_namespace' => $this->mNamespace
, 'old_title' => $this->mDbkeyform
);
1893 * Get the revision ID of the previous revision
1895 * @param integer $revision Revision ID. Get the revision that was before this one.
1896 * @return interger $oldrevision|false
1898 function getPreviousRevisionID( $revision ) {
1899 $dbr =& wfGetDB( DB_SLAVE
);
1900 return $dbr->selectField( 'revision', 'rev_id',
1901 'rev_page=' . IntVal( $this->getArticleId() ) .
1902 ' AND rev_id<' . IntVal( $revision ) . ' ORDER BY rev_id DESC' );
1906 * Get the revision ID of the next revision
1908 * @param integer $revision Revision ID. Get the revision that was after this one.
1909 * @return interger $oldrevision|false
1911 function getNextRevisionID( $revision ) {
1912 $dbr =& wfGetDB( DB_SLAVE
);
1913 return $dbr->selectField( 'revision', 'rev_id',
1914 'rev_page=' . IntVal( $this->getArticleId() ) .
1915 ' AND rev_id>' . IntVal( $revision ) . ' ORDER BY rev_id' );
1919 * Compare with another title.
1921 * @param Title $title
1924 function equals( &$title ) {
1925 return $this->getInterwiki() == $title->getInterwiki()
1926 && $this->getNamespace() == $title->getNamespace()
1927 && $this->getDbkey() == $title->getDbkey();
1931 * Convert Windows-1252 extended codepoints to their real Unicode points.
1932 * @param int $codepoint
1936 function cp1252toUnicode( $codepoint ) {
1938 # http://www.unicode.org/Public/MAPPINGS/VENDORS/MICSFT/WINDOWS/CP1252.TXT
1939 static $cp1252 = array(
1940 0x80 => 0x20AC, #EURO SIGN
1941 0x81 => UNICODE_REPLACEMENT
,
1942 0x82 => 0x201A, #SINGLE LOW-9 QUOTATION MARK
1943 0x83 => 0x0192, #LATIN SMALL LETTER F WITH HOOK
1944 0x84 => 0x201E, #DOUBLE LOW-9 QUOTATION MARK
1945 0x85 => 0x2026, #HORIZONTAL ELLIPSIS
1946 0x86 => 0x2020, #DAGGER
1947 0x87 => 0x2021, #DOUBLE DAGGER
1948 0x88 => 0x02C6, #MODIFIER LETTER CIRCUMFLEX ACCENT
1949 0x89 => 0x2030, #PER MILLE SIGN
1950 0x8A => 0x0160, #LATIN CAPITAL LETTER S WITH CARON
1951 0x8B => 0x2039, #SINGLE LEFT-POINTING ANGLE QUOTATION MARK
1952 0x8C => 0x0152, #LATIN CAPITAL LIGATURE OE
1953 0x8D => UNICODE_REPLACEMENT
,
1954 0x8E => 0x017D, #LATIN CAPITAL LETTER Z WITH CARON
1955 0x8F => UNICODE_REPLACEMENT
,
1956 0x90 => UNICODE_REPLACEMENT
,
1957 0x91 => 0x2018, #LEFT SINGLE QUOTATION MARK
1958 0x92 => 0x2019, #RIGHT SINGLE QUOTATION MARK
1959 0x93 => 0x201C, #LEFT DOUBLE QUOTATION MARK
1960 0x94 => 0x201D, #RIGHT DOUBLE QUOTATION MARK
1961 0x95 => 0x2022, #BULLET
1962 0x96 => 0x2013, #EN DASH
1963 0x97 => 0x2014, #EM DASH
1964 0x98 => 0x02DC, #SMALL TILDE
1965 0x99 => 0x2122, #TRADE MARK SIGN
1966 0x9A => 0x0161, #LATIN SMALL LETTER S WITH CARON
1967 0x9B => 0x203A, #SINGLE RIGHT-POINTING ANGLE QUOTATION MARK
1968 0x9C => 0x0153, #LATIN SMALL LIGATURE OE
1969 0x9D => UNICODE_REPLACEMENT
,
1970 0x9E => 0x017E, #LATIN SMALL LETTER Z WITH CARON
1971 0x9F => 0x0178, #LATIN CAPITAL LETTER Y WITH DIAERESIS
1973 return isset( $cp1252[$codepoint] )
1974 ?
$cp1252[$codepoint]
1980 * Take a string containing a mix of CP1252 characters and UTF-8 and try
1981 * to convert it completely to UTF-8.
1983 * @param string $string
1987 function mergeLatin1Utf8( $string ) {
1988 return preg_replace_callback(
1989 # Windows CP1252 extends ISO-8859-1 by putting extra characters
1990 # into the high control chars area. We have to convert these
1991 # to their proper Unicode counterparts.
1993 create_function( '$matches',
1994 'return codepointToUtf8(
1995 Title::cp1252toUnicode(
1996 utf8ToCodepoint( $matches[1] ) ) );' ),
1997 preg_replace_callback(
1998 # Up-convert everything from 8-bit to UTF-8, then
1999 # filter the valid-looking UTF-8 back from the
2000 # double-converted form.
2001 '/((?:[\xc0-\xdf][\x80-\xbf]
2002 |[\xe0-\xef][\x80-\xbf]{2}
2003 |[\xf0-\xf7][\x80-\xbf]{3})+)/ux',
2004 create_function( '$matches',
2005 'return utf8_decode( $matches[1] );' ),
2006 utf8_encode( $string ) ) );