* @static
* @access public
*/
- /* static */ function newFromText( $text, $defaultNamespace = 0 ) {
+ /* static */ function &newFromText( $text, $defaultNamespace = 0 ) {
$fname = 'Title::newFromText';
wfProfileIn( $fname );
-
- if( is_object( $text ) ) {
- wfDebugDieBacktrace( 'Called with object instead of string.' );
+
+ /**
+ * Wiki pages often contain multiple links to the same page.
+ * Title normalization and parsing can become expensive on
+ * pages with many links, so we can save a little time by
+ * caching them.
+ *
+ * In theory these are value objects and won't get changed...
+ */
+ static $titleCache = array();
+ if( $defaultNamespace == 0 && isset( $titleCache[$text] ) ) {
+ wfProfileOut( $fname );
+ return $titleCache[$text];
}
+
+ /**
+ * Convert things like é into real text...
+ */
global $wgInputEncoding;
- $text = do_html_entity_decode( $text, ENT_COMPAT, $wgInputEncoding );
+ $filteredText = do_html_entity_decode( $text, ENT_COMPAT, $wgInputEncoding );
- $text = wfMungeToUtf8( $text );
-
+ /**
+ * Convert things like ā or 〗 into real text...
+ * WARNING: Not friendly to internal links on a latin-1 wiki.
+ */
+ $filteredText = wfMungeToUtf8( $filteredText );
# What was this for? TS 2004-03-03
# $text = urldecode( $text );
- $t = new Title();
- $t->mDbkeyform = str_replace( ' ', '_', $text );
+ $t =& new Title();
+ $t->mDbkeyform = str_replace( ' ', '_', $filteredText );
$t->mDefaultNamespace = $defaultNamespace;
- wfProfileOut( $fname );
- if ( !is_object( $t ) ) {
- var_dump( debug_backtrace() );
- }
if( $t->secureAndSplit() ) {
+ if( $defaultNamespace == 0 ) {
+ $titleCache[$text] =& $t;
+ }
+ wfProfileOut( $fname );
return $t;
} else {
+ $titleCache[$text] = null;
+ wfProfileOut( $fname );
return NULL;
}
}
/* static */ function newFromID( $id ) {
$fname = 'Title::newFromID';
$dbr =& wfGetDB( DB_SLAVE );
- $row = $dbr->getArray( 'cur', array( 'cur_namespace', 'cur_title' ),
+ $row = $dbr->selectRow( 'cur', array( 'cur_namespace', 'cur_title' ),
array( 'cur_id' => $id ), $fname );
if ( $row !== false ) {
$title = Title::makeTitle( $row->cur_namespace, $row->cur_title );
* Create a new Title from a namespace index and a DB key.
* It's assumed that $ns and $title are *valid*, for instance when
* they came directly from the database or a special page name.
+ * For convenience, spaces are converted to underscores so that
+ * eg user_text fields can be used directly.
+ *
* @param int $ns the namespace of the article
* @param string $title the unprefixed database key form
* @return Title the new object
$t->mInterwiki = '';
$t->mFragment = '';
$t->mNamespace = IntVal( $ns );
- $t->mDbkeyform = $title;
+ $t->mDbkeyform = str_replace( ' ', '_', $title );
$t->mArticleID = ( $ns >= 0 ) ? -1 : 0;
- $t->mUrlform = wfUrlencode( $title );
+ $t->mUrlform = wfUrlencode( $t->mDbkeyform );
$t->mTextform = str_replace( '_', ' ', $title );
return $t;
}
$fname = 'Title::nameOf';
$dbr =& wfGetDB( DB_SLAVE );
- $s = $dbr->getArray( 'cur', array( 'cur_namespace','cur_title' ), array( 'cur_id' => $id ), $fname );
+ $s = $dbr->selectRow( 'cur', array( 'cur_namespace','cur_title' ), array( 'cur_id' => $id ), $fname );
if ( $s === false ) { return NULL; }
$n = Title::makeName( $s->cur_namespace, $s->cur_title );
$t = preg_replace( "/\\s+/", ' ', $t );
- if ( $ns == Namespace::getImage() ) {
+ if ( $ns == NS_IMAGE ) {
$t = preg_replace( "/ (png|gif|jpg|jpeg|ogg)$/", "", $t );
}
return trim( $t );
function getInterwikiLink( $key ) {
global $wgMemc, $wgDBname, $wgInterwikiExpiry, $wgTitleInterwikiCache;
$fname = 'Title::getInterwikiLink';
+
+ wfProfileIn( $fname );
+
$k = $wgDBname.':interwiki:'.$key;
-
- if( array_key_exists( $k, $wgTitleInterwikiCache ) )
+ if( array_key_exists( $k, $wgTitleInterwikiCache ) ) {
+ wfProfileOut( $fname );
return $wgTitleInterwikiCache[$k]->iw_url;
+ }
$s = $wgMemc->get( $k );
# Ignore old keys with no iw_local
if( $s && isset( $s->iw_local ) ) {
$wgTitleInterwikiCache[$k] = $s;
+ wfProfileOut( $fname );
return $s->iw_url;
}
+
$dbr =& wfGetDB( DB_SLAVE );
- $res = $dbr->select( 'interwiki', array( 'iw_url', 'iw_local' ), array( 'iw_prefix' => $key ), $fname );
- if(!$res) return '';
+ $res = $dbr->select( 'interwiki',
+ array( 'iw_url', 'iw_local' ),
+ array( 'iw_prefix' => $key ), $fname );
+ if( !$res ) {
+ wfProfileOut( $fname );
+ return '';
+ }
$s = $dbr->fetchObject( $res );
- if(!$s) {
+ if( !$s ) {
# Cache non-existence: create a blank object and save it to memcached
$s = (object)false;
$s->iw_url = '';
}
$wgMemc->set( $k, $s, $wgInterwikiExpiry );
$wgTitleInterwikiCache[$k] = $s;
+
+ wfProfileOut( $fname );
return $s->iw_url;
}
* @access public
*/
function getNamespace() { return $this->mNamespace; }
- /**
- * Set the namespace index
- * @param int $n the namespace index, one of the NS_xxxx constants
- * @access public
- */
- function setNamespace( $n ) { $this->mNamespace = IntVal( $n ); }
/**
* Get the interwiki prefix (or null string)
* @return string
$s = wfUrlencode ( $s ) ;
# Cleaning up URL to make it look nice -- is this safe?
- $s = preg_replace( '/%3[Aa]/', ':', $s );
- $s = preg_replace( '/%2[Ff]/', '/', $s );
$s = str_replace( '%28', '(', $s );
$s = str_replace( '%29', ')', $s );
}
}
- /**
- * @deprecated
- */
- function getURL() {
- die( 'Call to obsolete obsolete function Title::getURL()' );
- }
-
/**
* Get a URL with no fragment or server name
* @param string $query an optional query string; if not specified,
if ( $query == '-' ) {
$query = '';
}
- if ( $wgScript != '' ) {
- $url = "{$wgScript}?title={$dbkey}&{$query}";
- } else {
- # Top level wiki
- $url = "/{$dbkey}?{$query}";
- }
+ $url = "{$wgScript}?title={$dbkey}&{$query}";
}
return $url;
}
/**
* Does the title correspond to a protected article?
+ * @param string $what the action the page is protected from,
+ * by default checks move and edit
* @return boolean
* @access public
*/
- function isProtected() {
+ function isProtected($action = '') {
if ( -1 == $this->mNamespace ) { return true; }
- $a = $this->getRestrictions();
- if ( in_array( 'sysop', $a ) ) { return true; }
- return false;
- }
-
- /**
- * Is the page a log page, i.e. one where the history is messed up by
- * LogPage.php? This used to be used for suppressing diff links in
- * recent changes, but now that's done by setting a flag in the
- * recentchanges table. Hence, this probably is no longer used.
- *
- * @deprecated
- * @access public
- */
- function isLog() {
- if ( $this->mNamespace != Namespace::getWikipedia() ) {
- return false;
- }
- if ( ( 0 == strcmp( wfMsg( 'uploadlogpage' ), $this->mDbkeyform ) ) ||
- ( 0 == strcmp( wfMsg( 'dellogpage' ), $this->mDbkeyform ) ) ) {
- return true;
+ if($action == 'edit' || $action == '') {
+ $a = $this->getRestrictions("edit");
+ if ( in_array( 'sysop', $a ) ) { return true; }
}
+ if($action == 'move' || $action == '') {
+ $a = $this->getRestrictions("move");
+ if ( in_array( 'sysop', $a ) ) { return true; }
+ }
return false;
}
return $wgUser->isWatched( $this );
}
- /**
- * Can $wgUser edit this page?
+ /**
+ * Is $wgUser perform $action this page?
+ * @param string $action action that permission needs to be checked for
* @return boolean
- * @access public
- */
- function userCanEdit() {
+ * @access private
+ */
+ function userCan($action) {
+ $fname = 'Title::userCanEdit';
+ wfProfileIn( $fname );
+
global $wgUser;
- if ( -1 == $this->mNamespace ) { return false; }
- if ( NS_MEDIAWIKI == $this->mNamespace && !$wgUser->isSysop() ) { return false; }
- # if ( 0 == $this->getArticleID() ) { return false; }
- if ( $this->mDbkeyform == '_' ) { return false; }
+ if( NS_SPECIAL == $this->mNamespace ) {
+ wfProfileOut( $fname );
+ return false;
+ }
+ if( NS_MEDIAWIKI == $this->mNamespace &&
+ !$wgUser->isAllowed('editinterface') ) {
+ wfProfileOut( $fname );
+ return false;
+ }
+ if( $this->mDbkeyform == '_' ) {
+ # FIXME: Is this necessary? Shouldn't be allowed anyway...
+ wfProfileOut( $fname );
+ return false;
+ }
+
# protect global styles and js
if ( NS_MEDIAWIKI == $this->mNamespace
- && preg_match("/\\.(css|js)$/", $this->mTextform )
- && !$wgUser->isSysop() )
- { return false; }
- //if ( $this->isCssJsSubpage() and !$this->userCanEditCssJsSubpage() ) { return false; }
+ && preg_match("/\\.(css|js)$/", $this->mTextform )
+ && !$wgUser->isAllowed('editinterface') ) {
+ wfProfileOut( $fname );
+ return false;
+ }
+
# protect css/js subpages of user pages
# XXX: this might be better using restrictions
# XXX: Find a way to work around the php bug that prevents using $this->userCanEditCssJsSubpage() from working
- if( Namespace::getUser() == $this->mNamespace
- and preg_match("/\\.(css|js)$/", $this->mTextform )
- and !$wgUser->isSysop()
- and !preg_match('/^'.preg_quote($wgUser->getName(), '/').'/', $this->mTextform) )
- { return false; }
- $ur = $wgUser->getRights();
- foreach ( $this->getRestrictions() as $r ) {
- if ( '' != $r && ( ! in_array( $r, $ur ) ) ) {
+ if( NS_USER == $this->mNamespace
+ && preg_match("/\\.(css|js)$/", $this->mTextform )
+ && !$wgUser->isAllowed('editinterface')
+ && !preg_match('/^'.preg_quote($wgUser->getName(), '/').'\//', $this->mTextform) ) {
+ wfProfileOut( $fname );
+ return false;
+ }
+
+ foreach( $this->getRestrictions($action) as $right ) {
+ if( '' != $right && !$wgUser->isAllowed( $right ) ) {
+ wfProfileOut( $fname );
return false;
}
}
+ wfProfileOut( $fname );
return true;
}
+ /**
+ * Can $wgUser edit this page?
+ * @return boolean
+ * @access public
+ */
+ function userCanEdit() {
+ return $this->userCan('edit');
+ }
+
+ /**
+ * Can $wgUser move this page?
+ * @return boolean
+ * @access public
+ */
+ function userCanMove() {
+ return $this->userCan('move');
+ }
+
/**
* Can $wgUser read this page?
* @return boolean
*/
function userCanRead() {
global $wgUser;
- global $wgWhitelistRead;
-
- if( 0 != $wgUser->getID() ) return true;
- if( !is_array( $wgWhitelistRead ) ) return true;
- $name = $this->getPrefixedText();
- if( in_array( $name, $wgWhitelistRead ) ) return true;
-
- # Compatibility with old settings
- if( $this->getNamespace() == NS_MAIN ) {
- if( in_array( ':' . $name, $wgWhitelistRead ) ) return true;
+ if( $wgUser->isAllowed('read') ) {
+ return true;
+ } else {
+ global $wgWhitelistRead;
+
+ /** If anon users can create an account,
+ they need to reach the login page first! */
+ if( $wgUser->isAllowed( 'createaccount' )
+ && $this->mId == NS_SPECIAL
+ && $this->getText() == 'Userlogin' ) {
+ return true;
+ }
+
+ /** some pages are explicitly allowed */
+ $name = $this->getPrefixedText();
+ if( in_array( $name, $wgWhitelistRead ) ) {
+ return true;
+ }
+
+ # Compatibility with old settings
+ if( $this->getNamespace() == NS_MAIN ) {
+ if( in_array( ':' . $name, $wgWhitelistRead ) ) {
+ return true;
+ }
+ }
}
return false;
}
* @access public
*/
function isCssJsSubpage() {
- return ( Namespace::getUser() == $this->mNamespace and preg_match("/\\.(css|js)$/", $this->mTextform ) );
+ return ( NS_USER == $this->mNamespace and preg_match("/\\.(css|js)$/", $this->mTextform ) );
}
/**
* Is this a .css subpage of a user page?
* @access public
*/
function isCssSubpage() {
- return ( Namespace::getUser() == $this->mNamespace and preg_match("/\\.css$/", $this->mTextform ) );
+ return ( NS_USER == $this->mNamespace and preg_match("/\\.css$/", $this->mTextform ) );
}
/**
* Is this a .js subpage of a user page?
* @access public
*/
function isJsSubpage() {
- return ( Namespace::getUser() == $this->mNamespace and preg_match("/\\.js$/", $this->mTextform ) );
+ return ( NS_USER == $this->mNamespace and preg_match("/\\.js$/", $this->mTextform ) );
}
/**
* Protect css/js subpages of user pages: can $wgUser edit
*/
function userCanEditCssJsSubpage() {
global $wgUser;
- return ( $wgUser->isSysop() or preg_match('/^'.preg_quote($wgUser->getName()).'/', $this->mTextform) );
+ return ( $wgUser->isAllowed('editinterface') or preg_match('/^'.preg_quote($wgUser->getName(), '/').'\//', $this->mTextform) );
+ }
+
+ /**
+ * Loads a string into mRestrictions array
+ * @param string $res restrictions in string format
+ * @access public
+ */
+ function loadRestrictions( $res ) {
+ foreach( explode( ':', trim( $res ) ) as $restrict ) {
+ $temp = explode( '=', trim( $restrict ) );
+ if(count($temp) == 1) {
+ // old format should be treated as edit/move restriction
+ $this->mRestrictions["edit"] = explode( ',', trim( $temp[0] ) );
+ $this->mRestrictions["move"] = explode( ',', trim( $temp[0] ) );
+ } else {
+ $this->mRestrictions[$temp[0]] = explode( ',', trim( $temp[1] ) );
+ }
+ }
+ $this->mRestrictionsLoaded = true;
}
/**
* Accessor/initialisation for mRestrictions
+ * @param string $action action that permission needs to be checked for
* @return array the array of groups allowed to edit this article
* @access public
*/
- function getRestrictions() {
+ function getRestrictions($action) {
$id = $this->getArticleID();
if ( 0 == $id ) { return array(); }
if ( ! $this->mRestrictionsLoaded ) {
$dbr =& wfGetDB( DB_SLAVE );
- $res = $dbr->getField( 'cur', 'cur_restrictions', 'cur_id='.$id );
- $this->mRestrictions = explode( ',', trim( $res ) );
- $this->mRestrictionsLoaded = true;
+ $res = $dbr->selectField( 'cur', 'cur_restrictions', 'cur_id='.$id );
+ $this->loadRestrictions( $res );
}
- return $this->mRestrictions;
+ if( isset( $this->mRestrictions[$action] ) ) {
+ return $this->mRestrictions[$action];
+ }
+ return array();
}
/**
function isDeleted() {
$fname = 'Title::isDeleted';
$dbr =& wfGetDB( DB_SLAVE );
- $n = $dbr->getField( 'archive', 'COUNT(*)', array( 'ar_namespace' => $this->getNamespace(),
+ $n = $dbr->selectField( 'archive', 'COUNT(*)', array( 'ar_namespace' => $this->getNamespace(),
'ar_title' => $this->getDBkey() ), $fname );
return (int)$n;
}
function invalidateCache() {
$now = wfTimestampNow();
$dbw =& wfGetDB( DB_MASTER );
- $success = $dbw->updateArray( 'cur',
+ $success = $dbw->update( 'cur',
array( /* SET */
'cur_touched' => $dbw->timestamp()
), array( /* WHERE */
* @return bool true on success
* @access private
*/
- /* private */ function secureAndSplit()
- {
+ /* private */ function secureAndSplit() {
global $wgContLang, $wgLocalInterwiki, $wgCapitalLinks;
$fname = 'Title::secureAndSplit';
wfProfileIn( $fname );
# Initialisation
if ( $imgpre === false ) {
- $imgpre = ':' . $wgContLang->getNsText( Namespace::getImage() ) . ':';
+ $imgpre = ':' . $wgContLang->getNsText( NS_IMAGE ) . ':';
# % is needed as well
- $rxTc = '/[^' . Title::legalChars() . ']|%[0-9A-Fa-f]{2}/';
+ $rxTc = '/[^' . Title::legalChars() . ']|%[0-9A-Fa-f]{2}/S';
}
$this->mInterwiki = $this->mFragment = '';
$this->mNamespace = NS_MAIN;
} else {
# Namespace or interwiki prefix
- if ( preg_match( "/^(.+?)_*:_*(.*)$/", $t, $m ) ) {
+ if ( preg_match( "/^(.+?)_*:_*(.*)$/S", $t, $m ) ) {
#$p = strtolower( $m[1] );
$p = $m[1];
$lowerNs = strtolower( $p );
return false;
}
- # "." and ".." conflict with the directories of those namesa
+ /**
+ * Pages with "/./" or "/../" appearing in the URLs will
+ * often be unreachable due to the way web browsers deal
+ * with 'relative' URLs. Forbid them explicitly.
+ */
if ( strpos( $r, '.' ) !== false &&
( $r === '.' || $r === '..' ||
strpos( $r, './' ) === 0 ||
return false;
}
- # Initial capital letter
+ /**
+ * Normally, all wiki links are forced to have
+ * an initial capital letter so [[foo]] and [[Foo]]
+ * point to the same place.
+ *
+ * Don't force it for interwikis, since the other
+ * site might be case-sensitive.
+ */
if( $wgCapitalLinks && $this->mInterwiki == '') {
$t = $wgContLang->ucfirst( $r );
} else {
$links = $dbw->tableName( 'links' );
# Change the name of the target page:
- $dbw->updateArray( 'cur',
+ $dbw->update( 'cur',
/* SET */ array(
'cur_touched' => $dbw->timestamp($now),
'cur_namespace' => $nt->getNamespace(),
# by definition if we've got here it's rather uninteresting.
$redirectText = $wgMwRedir->getSynonym( 0 ) . ' [[' . $nt->getPrefixedText() . "]]\n";
- $dbw->updateArray( 'cur',
+ $dbw->update( 'cur',
/* SET */ array(
'cur_touched' => $dbw->timestamp($now),
'cur_timestamp' => $dbw->timestamp($now),
# Fix the redundant names for the past revisions of the target page.
# The redirect should have no old revisions.
- $dbw->updateArray(
+ $dbw->update(
/* table */ 'old',
/* SET */ array(
'old_namespace' => $nt->getNamespace(),
# Now, we record the link from the redirect to the new title.
# It should have no other outgoing links...
$dbw->delete( 'links', array( 'l_from' => $newid ) );
- $dbw->insertArray( 'links', array( 'l_from' => $newid, 'l_to' => $oldid ) );
+ $dbw->insert( 'links', array( 'l_from' => $newid, 'l_to' => $oldid ) );
# Clear linkscc
LinkCache::linksccClearLinksTo( $oldid );
$rand = wfRandom();
# Rename cur entry
- $dbw->updateArray( 'cur',
+ $dbw->update( 'cur',
/* SET */ array(
'cur_touched' => $now,
'cur_namespace' => $nt->getNamespace(),
$wgLinkCache->clearLink( $nt->getPrefixedDBkey() );
# Insert redirect
- $dbw->insertArray( 'cur', array(
+ $dbw->insert( 'cur', array(
'cur_id' => $dbw->nextSequenceValue('cur_cur_id_seq'),
'cur_namespace' => $this->getNamespace(),
'cur_title' => $this->getDBkey(),
$wgLinkCache->clearLink( $this->getPrefixedDBkey() );
# Rename old entries
- $dbw->updateArray(
+ $dbw->update(
/* table */ 'old',
/* SET */ array(
'old_namespace' => $nt->getNamespace(),
Article::onArticleCreate( $nt );
# Any text links to the old title must be reassigned to the redirect
- $dbw->updateArray( 'links', array( 'l_to' => $newid ), array( 'l_to' => $oldid ), $fname );
+ $dbw->update( 'links', array( 'l_to' => $newid ), array( 'l_to' => $oldid ), $fname );
LinkCache::linksccClearLinksTo( $oldid );
# Record the just-created redirect's linking to the page
- $dbw->insertArray( 'links', array( 'l_from' => $newid, 'l_to' => $oldid ), $fname );
+ $dbw->insert( 'links', array( 'l_from' => $newid, 'l_to' => $oldid ), $fname );
# Non-existent target may have had broken links to it; these must
# now be removed and made into good links.
# Is it a redirect?
$id = $nt->getArticleID();
- $obj = $dbw->getArray( 'cur', array( 'cur_is_redirect','cur_text' ),
+ $obj = $dbw->selectRow( 'cur', array( 'cur_is_redirect','cur_text' ),
array( 'cur_id' => $id ), $fname, 'FOR UPDATE' );
if ( !$obj || 0 == $obj->cur_is_redirect ) {
}
# Does the article have a history?
- $row = $dbw->getArray( 'old', array( 'old_id' ),
+ $row = $dbw->selectRow( 'old', array( 'old_id' ),
array(
'old_namespace' => $nt->getNamespace(),
'old_title' => $nt->getDBkey()
$won = wfInvertTimestamp( $now );
$seqVal = $dbw->nextSequenceValue( 'cur_cur_id_seq' );
- $dbw->insertArray( 'cur', array(
+ $dbw->insert( 'cur', array(
'cur_id' => $seqVal,
'cur_namespace' => $this->getNamespace(),
'cur_title' => $this->getDBkey(),
# Link table
if ( $dest->getArticleID() ) {
- $dbw->insertArray( 'links',
+ $dbw->insert( 'links',
array(
'l_to' => $dest->getArticleID(),
'l_from' => $newid
), $fname
);
} else {
- $dbw->insertArray( 'brokenlinks',
+ $dbw->insert( 'brokenlinks',
array(
'bl_to' => $dest->getPrefixedDBkey(),
'bl_from' => $newid
$sk =& $wgUser->getSkin();
$parents = array();
$dbr =& wfGetDB( DB_SLAVE );
- $cur = $dbr->tableName( 'cur' );
$categorylinks = $dbr->tableName( 'categorylinks' );
# NEW SQL
- $sql = "SELECT * FROM categorylinks"
+ $sql = "SELECT * FROM $categorylinks"
." WHERE cl_from='$titlekey'"
." AND cl_from <> '0'"
." ORDER BY cl_sortkey";
}
/**
- * Go through all parent categories of this Title
+ * Get a tree of parent categories
+ * @param array $children an array with the children in the keys, to check for circular refs
* @return array
* @access public
*/
- function getCategorieBrowser() {
+ function getParentCategoryTree( $children = array() ) {
$parents = $this->getParentCategories();
if($parents != '') {
foreach($parents as $parent => $current)
{
- $nt = Title::newFromText($parent);
- $stack[$parent] = $nt->getCategorieBrowser();
+ if ( array_key_exists( $parent, $children ) ) {
+ # Circular reference
+ $stack[$parent] = array();
+ } else {
+ $nt = Title::newFromText($parent);
+ $stack[$parent] = $nt->getParentCategoryTree( $children + array($parent => 1) );
+ }
}
return $stack;
} else {