<?php
/**
- * See title.doc
+ * See title.txt
*
* @package MediaWiki
*/
$wgTitleInterwikiCache = array();
define ( 'GAID_FOR_UPDATE', 1 );
+# Title::newFromTitle maintains a cache to avoid
+# expensive re-normalization of commonly used titles.
+# On a batch operation this can become a memory leak
+# if not bounded. After hitting this many titles,
+# reset the cache.
+define( 'MW_TITLECACHE_MAX', 1000 );
+
/**
* Title class
* - Represents a title, which may contain an interwiki designation or namespace
$this->mInterwiki = $this->mUrlform =
$this->mTextform = $this->mDbkeyform = '';
$this->mArticleID = -1;
- $this->mNamespace = 0;
+ $this->mNamespace = NS_MAIN;
$this->mRestrictionsLoaded = false;
$this->mRestrictions = array();
- $this->mDefaultNamespace = 0;
+ # Dont change the following, NS_MAIN is hardcoded in several place
+ # See bug #696
+ $this->mDefaultNamespace = NS_MAIN;
}
/**
* @static
* @access public
*/
- /* static */ function &newFromText( $text, $defaultNamespace = 0 ) {
+ function &newFromText( $text, $defaultNamespace = NS_MAIN ) {
$fname = 'Title::newFromText';
wfProfileIn( $fname );
+ if( is_object( $text ) ) {
+ wfDebugDieBacktrace( 'Title::newFromText given an object' );
+ }
+
/**
* Wiki pages often contain multiple links to the same page.
* Title normalization and parsing can become expensive on
* In theory these are value objects and won't get changed...
*/
static $titleCache = array();
- if( $defaultNamespace == 0 && isset( $titleCache[$text] ) ) {
+ if( $defaultNamespace == NS_MAIN && isset( $titleCache[$text] ) ) {
wfProfileOut( $fname );
return $titleCache[$text];
}
$t->mDefaultNamespace = $defaultNamespace;
if( $t->secureAndSplit() ) {
- if( $defaultNamespace == 0 ) {
+ if( $defaultNamespace == NS_MAIN ) {
+ if( count( $titleCache ) >= MW_TITLECACHE_MAX ) {
+ # Avoid memory leaks on mass operations...
+ $titleCache = array();
+ }
$titleCache[$text] =& $t;
}
wfProfileOut( $fname );
global $wgContLang;
$n = $wgContLang->getNsText( $ns );
- if ( '' == $n ) { return $title; }
- else { return $n.':'.$title; }
+ return $n == '' ? $title : "$n:$title";
}
/**
* @access public
*/
function getFullURL( $query = '' ) {
- global $wgContLang, $wgArticlePath, $wgServer, $wgScript;
+ global $wgContLang, $wgServer, $wgScript;
if ( '' == $this->mInterwiki ) {
- $p = $wgArticlePath;
return $wgServer . $this->getLocalUrl( $query );
} else {
$baseUrl = $this->getInterwikiLink( $this->mInterwiki );
$namepace .= ':';
}
$url = str_replace( '$1', $namespace . $this->mUrlform, $baseUrl );
+ if( $query != '' ) {
+ if( false === strpos( $url, '?' ) ) {
+ $url .= '?';
+ } else {
+ $url .= '&';
+ }
+ $url .= $query;
+ }
if ( '' != $this->mFragment ) {
$url .= '#' . $this->mFragment;
}
}
}
+ /**
+ * Get a relative directory for putting an HTML version of this article into
+ */
+ function getHashedDirectory() {
+ $dbkey = $this->getPrefixedDBkey();
+ if ( strlen( $dbkey ) < 2 ) {
+ $dbkey = sprintf( "%2s", $dbkey );
+ }
+ $dir = '';
+ for ( $i=0; $i<=1; $i++ ) {
+ if ( $i ) {
+ $dir .= '/';
+ }
+ if ( ord( $dbkey{$i} ) < 128 && ord( $dbkey{$i} ) > 32 ) {
+ $dir .= strtolower( $dbkey{$i} );
+ } else {
+ $dir .= sprintf( "%02X", ord( $dbkey{$i} ) );
+ }
+ }
+ return $dir;
+ }
+
+ function getHashedFilename() {
+ $dbkey = $this->getPrefixedDBkey();
+ $dir = $this->getHashedDirectory();
+ $friendlyName = strtr( $dbkey, '/\\:*?"<>|', '_________' );
+ return "$dir/$friendlyName.html";
+ }
+
/**
* Get a URL with no fragment or server name
* @param string $query an optional query string; if not specified,
* @access public
*/
function getLocalURL( $query = '' ) {
- global $wgLang, $wgArticlePath, $wgScript;
+ global $wgLang, $wgArticlePath, $wgScript, $wgMakeDumpLinks;
if ( $this->isExternal() ) {
return $this->getFullURL();
}
-
+
$dbkey = wfUrlencode( $this->getPrefixedDBkey() );
- if ( $query == '' ) {
+ if ( $wgMakeDumpLinks ) {
+ $url = str_replace( '$1', wfUrlencode( $this->getHashedFilename() ), $wgArticlePath );
+ } elseif ( $query == '' ) {
$url = str_replace( '$1', $dbkey, $wgArticlePath );
} else {
+ if( preg_match( '/^(.*&|)action=([^&]*)(&(.*)|)$/', $query, $matches ) ) {
+ global $wgActionPaths;
+ $action = urldecode( $matches[2] );
+ if( isset( $wgActionPaths[$action] ) ) {
+ $query = $matches[1];
+ if( isset( $matches[4] ) ) $query .= $matches[4];
+ $url = str_replace( '$1', $dbkey, $wgActionPaths[$action] );
+ if( $query != '' ) $url .= '?' . $query;
+ return $url;
+ }
+ }
if ( $query == '-' ) {
$query = '';
}
function userCan($action) {
$fname = 'Title::userCanEdit';
wfProfileIn( $fname );
-
+
global $wgUser;
if( NS_SPECIAL == $this->mNamespace ) {
wfProfileOut( $fname );
return false;
}
}
+
+ if( $action == 'move' && !$this->isMovable() ) {
+ wfProfileOut( $fname );
+ return false;
+ }
+
wfProfileOut( $fname );
return true;
}
return $this->userCan('move');
}
+ /**
+ * Would anybody with sufficient privileges be able to move this page?
+ * Some pages just aren't movable.
+ *
+ * @return boolean
+ * @access public
+ */
+ function isMovable() {
+ return Namespace::isMovable( $this->getNamespace() )
+ && $this->getInterwiki() == '';
+ }
+
/**
* Can $wgUser read this page?
* @return boolean
}
return false;
}
+
+ /**
+ * Is this a talk page of some sort?
+ * @return bool
+ * @access public
+ */
+ function isTalkPage() {
+ return Namespace::isTalk( $this->getNamespace() );
+ }
/**
* Is this a .css or .js subpage of a user page?
* @access public
*/
function invalidateCache() {
+ if ( wfReadOnly() ) {
+ return;
+ }
+
$now = wfTimestampNow();
$dbw =& wfGetDB( DB_MASTER );
$success = $dbw->update( 'page',
# Clean up whitespace
#
- $t = preg_replace( '/[\\s_]+/', '_', $this->mDbkeyform );
+ $t = preg_replace( '/[ _]+/', '_', $this->mDbkeyform );
$t = trim( $t, '_' );
if ( '' == $t ) {
return false;
}
- global $wgUseLatin1;
- if( !$wgUseLatin1 && false !== strpos( $t, UTF8_REPLACEMENT ) ) {
+ if( false !== strpos( $t, UTF8_REPLACEMENT ) ) {
# Contained illegal UTF-8 sequences or forbidden Unicode chars.
wfProfileOut( $fname );
return false;
# We shouldn't need to query the DB for the size.
#$maxSize = $dbr->textFieldSize( 'page', 'page_title' );
if ( strlen( $r ) > 255 ) {
+ wfProfileOut( $fname );
return false;
}
$t = $r;
}
+ /**
+ * Can't make a link to a namespace alone...
+ * "empty" local links can only be self-links
+ * with a fragment identifier.
+ */
+ if( $t == '' &&
+ $this->mInterwiki == '' &&
+ $this->mNamespace != NS_MAIN ) {
+ wfProfileOut( $fname );
+ return false;
+ }
+
# Fill fields
$this->mDbkeyform = $t;
$this->mUrlform = wfUrlencode( $t );
return $retVal;
}
+
+ /**
+ * Get an array of Title objects referring to non-existent articles linked from this page
+ *
+ * @param string $options may be FOR UPDATE
+ * @return array the Title objects
+ * @access public
+ */
+ function getBrokenLinksFrom( $options = '' ) {
+ global $wgLinkCache;
+
+ if ( $options ) {
+ $db =& wfGetDB( DB_MASTER );
+ } else {
+ $db =& wfGetDB( DB_SLAVE );
+ }
+ $page = $db->tableName( 'page' );
+ $brokenlinks = $db->tableName( 'brokenlinks' );
+ $id = $this->getArticleID();
+
+ $sql = "SELECT bl_to FROM $brokenlinks WHERE bl_from=$id $options";
+ $res = $db->query( $sql, "Title::getBrokenLinksFrom" );
+ $retVal = array();
+ if ( $db->numRows( $res ) ) {
+ while ( $row = $db->fetchObject( $res ) ) {
+ $retVal[] = Title::newFromText( $row->bl_to );
+ }
+ }
+ $db->freeResult( $res );
+ return $retVal;
+ }
+
+
/**
* Get a list of URLs to purge from the Squid cache when this
* page changes
}
/**
- * Move a title to a new location
+ * Check whether a given move operation would be valid.
+ * Returns true if ok, or a message key string for an error message
+ * if invalid. (Scarrrrry ugly interface this.)
* @param Title &$nt the new title
* @param bool $auth indicates whether $wgUser's permissions
* should be checked
* @return mixed true on success, message name on failure
* @access public
*/
- function moveTo( &$nt, $auth = true ) {
+ function isValidMoveOperation( &$nt, $auth = true, $reason = '' ) {
+ global $wgUser;
if( !$this or !$nt ) {
return 'badtitletext';
}
+ if( $this->equals( $nt ) ) {
+ return 'selfmove';
+ }
+ if( !$this->isMovable() || !$nt->isMovable() ) {
+ return 'immobile_namespace';
+ }
$fname = 'Title::move';
$oldid = $this->getArticleID();
if ( strlen( $nt->getDBkey() ) < 1 ) {
return 'articleexists';
}
- if ( ( ! Namespace::isMovable( $this->getNamespace() ) ) ||
- ( '' == $this->getDBkey() ) ||
- ( '' != $this->getInterwiki() ) ||
+ if ( ( '' == $this->getDBkey() ) ||
( !$oldid ) ||
- ( ! Namespace::isMovable( $nt->getNamespace() ) ) ||
- ( '' == $nt->getDBkey() ) ||
- ( '' != $nt->getInterwiki() ) ) {
+ ( '' == $nt->getDBkey() ) ) {
return 'badarticleerror';
}
if ( ! $this->isValidMoveTarget( $nt ) ) {
return 'articleexists';
}
- $this->moveOverExistingRedirect( $nt );
+ }
+ return true;
+ }
+
+ /**
+ * Move a title to a new location
+ * @param Title &$nt the new title
+ * @param bool $auth indicates whether $wgUser's permissions
+ * should be checked
+ * @return mixed true on success, message name on failure
+ * @access public
+ */
+ function moveTo( &$nt, $auth = true, $reason = '' ) {
+ $err = $this->isValidMoveOperation( $nt, $auth, $reason );
+ if( is_string( $err ) ) {
+ return $err;
+ }
+ if( $nt->exists() ) {
+ $this->moveOverExistingRedirect( $nt, $reason );
} else { # Target didn't exist, do normal move.
- $this->moveToNewTitle( $nt, $newid );
+ $this->moveToNewTitle( $nt, $newid, $reason );
}
# Fixing category links (those without piped 'alternate' names) to be sorted under the new title
$u = new SearchUpdate( $newid, $this->getPrefixedDBkey(), '' );
$u->doUpdate();
+ wfRunHooks( 'TitleMoveComplete', array(&$this, &$nt, &$wgUser, $oldid, $newid) );
return true;
}
* be a redirect
* @access private
*/
- /* private */ function moveOverExistingRedirect( &$nt ) {
+ /* private */ function moveOverExistingRedirect( &$nt, $reason = '' ) {
global $wgUser, $wgLinkCache, $wgUseSquid, $wgMwRedir;
$fname = 'Title::moveOverExistingRedirect';
- $comment = wfMsg( '1movedto2', $this->getPrefixedText(), $nt->getPrefixedText() );
+ $comment = wfMsgForContent( '1movedto2', $this->getPrefixedText(), $nt->getPrefixedText() );
+
+ if ( $reason ) {
+ $comment .= ": $reason";
+ }
$now = wfTimestampNow();
- $won = wfInvertTimestamp( $now );
$rand = wfRandom();
$newid = $nt->getArticleID();
$oldid = $this->getArticleID();
# a conflict on the unique namespace+title index...
$dbw->delete( 'page', array( 'page_id' => $newid ), $fname );
+ # Save a null revision in the page's history notifying of the move
+ $nullRevision = Revision::newNullRevision( $dbw, $oldid,
+ wfMsg( '1movedto2', $this->getPrefixedText(), $nt->getPrefixedText() ),
+ true );
+ $nullRevId = $nullRevision->insertOn( $dbw );
+
# Change the name of the target page:
$dbw->update( 'page',
/* SET */ array(
- 'page_touched' => $dbw->timestamp($now),
+ 'page_touched' => $dbw->timestamp($now),
'page_namespace' => $nt->getNamespace(),
- 'page_title' => $nt->getDBkey()
+ 'page_title' => $nt->getDBkey(),
+ 'page_latest' => $nullRevId,
),
/* WHERE */ array( 'page_id' => $oldid ),
$fname
# Recreate the redirect, this time in the other direction.
$redirectText = $wgMwRedir->getSynonym( 0 ) . ' [[' . $nt->getPrefixedText() . "]]\n";
- $dbw->insert( 'revision', array(
- 'rev_id' => $dbw->nextSequenceValue('rev_rev_id_seq'),
- 'rev_comment' => $comment,
- 'rev_user' => $wgUser->getID(),
- 'rev_user_text' => $wgUser->getName(),
- 'rev_timestamp' => $now,
- 'inverse_timestamp' => $won ), $fname
- );
- $revid = $dbw->insertId();
- $dbw->insert( 'text', array(
- 'old_id' => $revid,
- 'old_flags' => '',
- 'old_text' => $redirectText,
- ), $fname
- );
- $dbw->insert( 'page', array(
- 'page_id' => $dbw->nextSequenceValue('page_page_id_seq'),
- 'page_namespace' => $this->getNamespace(),
- 'page_title' => $this->getDBkey(),
- 'page_touched' => $now,
- 'page_is_redirect' => 1,
- 'page_random' => $rand,
- 'page_is_new' => 1,
- 'page_latest' => $revid), $fname
- );
- $newid = $dbw->insertId();
+ $redirectArticle = new Article( $this );
+ $newid = $redirectArticle->insertOn( $dbw );
+ $redirectRevision = new Revision( array(
+ 'page' => $newid,
+ 'comment' => $comment,
+ 'text' => $redirectText ) );
+ $revid = $redirectRevision->insertOn( $dbw );
+ $redirectArticle->updateRevisionOn( $dbw, $redirectRevision, 0 );
$wgLinkCache->clearLink( $this->getPrefixedDBkey() );
-
- RecentChange::notifyMoveOverRedirect( $now, $this, $nt, $wgUser, $comment );
+ # Log the move
+ $log = new LogPage( 'move' );
+ $log->addEntry( 'move_redir', $this, $reason, array( 1 => $nt->getPrefixedText() ) );
+
# Swap links
# Load titles and IDs
$sql .= "($id, $oldid)";
}
- $dbw->query( $sql, DB_MASTER, $fname );
+ $dbw->query( $sql, $fname );
}
# Now, we record the link from the redirect to the new title.
* @param int &$newid set to be the new article ID
* @access private
*/
- /* private */ function moveToNewTitle( &$nt, &$newid ) {
+ /* private */ function moveToNewTitle( &$nt, &$newid, $reason = '' ) {
global $wgUser, $wgLinkCache, $wgUseSquid;
global $wgMwRedir;
$fname = 'MovePageForm::moveToNewTitle';
- $comment = wfMsg( '1movedto2', $this->getPrefixedText(), $nt->getPrefixedText() );
+ $comment = wfMsgForContent( '1movedto2', $this->getPrefixedText(), $nt->getPrefixedText() );
+ if ( $reason ) {
+ $comment .= ": $reason";
+ }
$newid = $nt->getArticleID();
$oldid = $this->getArticleID();
$dbw =& wfGetDB( DB_MASTER );
$now = $dbw->timestamp();
- $won = wfInvertTimestamp( wfTimestamp(TS_MW,$now) );
wfSeedRandom();
$rand = wfRandom();
+ # Save a null revision in the page's history notifying of the move
+ $nullRevision = Revision::newNullRevision( $dbw, $oldid,
+ wfMsg( '1movedto2', $this->getPrefixedText(), $nt->getPrefixedText() ),
+ true );
+ $nullRevId = $nullRevision->insertOn( $dbw );
+
# Rename cur entry
$dbw->update( 'page',
/* SET */ array(
- 'page_touched' => $now,
+ 'page_touched' => $now,
'page_namespace' => $nt->getNamespace(),
- 'page_title' => $nt->getDBkey()
+ 'page_title' => $nt->getDBkey(),
+ 'page_latest' => $nullRevId,
),
/* WHERE */ array( 'page_id' => $oldid ),
$fname
# Insert redirect
$redirectText = $wgMwRedir->getSynonym( 0 ) . ' [[' . $nt->getPrefixedText() . "]]\n";
- $dbw->insert( 'revision', array(
- 'rev_id' => $dbw->nextSequenceValue('rev_rev_id_seq'),
- 'rev_comment' => $comment,
- 'rev_user' => $wgUser->getID(),
- 'rev_user_text' => $wgUser->getName(),
- 'rev_timestamp' => $now,
- 'inverse_timestamp' => $won ), $fname
- );
- $revid = $dbw->insertId();
- $dbw->insert( 'text', array(
- 'old_id' => $revid,
- 'old_flags' => '',
- 'old_text' => $redirectText
- ), $fname
- );
- $dbw->insert( 'page', array(
- 'page_id' => $dbw->nextSequenceValue('page_page_id_seq'),
- 'page_namespace' => $this->getNamespace(),
- 'page_title' => $this->getDBkey(),
- 'page_touched' => $now,
- 'page_is_redirect' => 1,
- 'page_random' => $rand,
- 'page_is_new' => 1,
- 'page_latest' => $revid), $fname
- );
- $newid = $dbw->insertId();
+ $redirectArticle = new Article( $this );
+ $newid = $redirectArticle->insertOn( $dbw );
+ $redirectRevision = new Revision( array(
+ 'page' => $newid,
+ 'comment' => $comment,
+ 'text' => $redirectText ) );
+ $revid = $redirectRevision->insertOn( $dbw );
+ $redirectArticle->updateRevisionOn( $dbw, $redirectRevision, 0 );
$wgLinkCache->clearLink( $this->getPrefixedDBkey() );
- # Record in RC
- RecentChange::notifyMoveToNew( $now, $this, $nt, $wgUser, $comment );
+ # Log the move
+ $log = new LogPage( 'move' );
+ $log->addEntry( 'move', $this, $reason, array( 1 => $nt->getPrefixedText()) );
# Purge squid and linkscc as per article creation
Article::onArticleCreate( $nt );
# Is it a redirect?
$id = $nt->getArticleID();
- $obj = $dbw->selectRow( array( 'page', 'text') ,
- array( 'page_is_redirect','old_text' ),
- array( 'page_id' => $id, 'page_latest=old_id' ),
+ $obj = $dbw->selectRow( array( 'page', 'revision', 'text'),
+ array( 'page_is_redirect','old_text' ),
+ array( 'page_id' => $id, 'page_latest=rev_id', 'rev_text_id=old_id' ),
$fname, 'FOR UPDATE' );
if ( !$obj || 0 == $obj->page_is_redirect ) {
$fname = 'Title::createRedirect';
$dbw =& wfGetDB( DB_MASTER );
- $now = wfTimestampNow();
- $won = wfInvertTimestamp( $now );
-
- $seqVal = $dbw->nextSequenceValue( 'page_page_id_seq' );
- $dbw->insert( 'page', array(
- 'page_id' => $seqVal,
- 'page_namespace' => $this->getNamespace(),
- 'page_title' => $this->getDBkey(),
- 'page_touched' => $now,
- 'page_is_redirect' => 1,
- 'page_is_new' => 1,
- 'page_latest' => NULL,
- ), $fname );
- $newid = $dbw->insertId();
-
- $seqVal = $dbw->nextSequenceValue( 'text_old_id_seq' );
- $dbw->insert( 'text', array(
- 'old_id' => $seqVal,
- 'old_flags' => '',
- 'old_text' => "#REDIRECT [[" . $dest->getPrefixedText() . "]]\n"
- ), $fname );
- $revisionId = $dbw->insertId();
- $dbw->insert( 'revision', array(
- 'rev_id' => $seqVal,
- 'rev_page' => $newid,
- 'rev_comment' => $comment,
- 'rev_user' => $wgUser->getID(),
- 'rev_user_text' => $wgUser->getName(),
- 'rev_timestamp' => $now,
- 'inverse_timestamp' => $won,
- ), $fname );
-
- $dbw->update( 'page',
- /* SET */ array( 'page_latest' => $revisionId ),
- /* WHERE */ array( 'page_id' => $newid ),
- $fname );
- $this->resetArticleID( $newid );
+ $article = new Article( $this );
+ $newid = $article->insertOn( $dbw );
+ $revision = new Revision( array(
+ 'page' => $newid,
+ 'comment' => $comment,
+ 'text' => "#REDIRECT [[" . $dest->getPrefixedText() . "]]\n",
+ ) );
+ $revisionId = $revision->insertOn( $dbw );
+ $article->updateRevisionOn( $dbw, $revision, 0 );
# Link table
if ( $dest->getArticleID() ) {
'rev_page=' . IntVal( $this->getArticleId() ) .
' AND rev_id>' . IntVal( $revision ) . ' ORDER BY rev_id' );
}
+
+ /**
+ * Compare with another title.
+ *
+ * @param Title $title
+ * @return bool
+ */
+ function equals( &$title ) {
+ return $this->getInterwiki() == $title->getInterwiki()
+ && $this->getNamespace() == $title->getNamespace()
+ && $this->getDbkey() == $title->getDbkey();
+ }
+
+ /**
+ * Check if page exists
+ * @return bool
+ */
+ function exists() {
+ return $this->getArticleId() != 0;
+ }
+ /**
+ * Should a link should be displayed as a known link, just based on its title?
+ *
+ * Currently, a self-link with a fragment, special pages and image pages are in
+ * this category. Special pages never exist in the database. Some images do not
+ * have description pages in the database, but the description page contains
+ * useful history information that the user may want to link to.
+ *
+ */
+ function isAlwaysKnown() {
+ return ( 0 == $this->mNamespace && "" == $this->mDbkeyform )
+ || NS_SPECIAL == $this->mNamespace || NS_IMAGE == $this->mNamespace;
+ }
}
?>