From 650be91fb8163bcf5951dd7ec9e6b7d7147b0ab2 Mon Sep 17 00:00:00 2001 From: Aryeh Gregor Date: Wed, 23 Jul 2008 19:05:43 +0000 Subject: [PATCH] Refactor a bit preparatory to fixing bug 8068: rewrite the robot policy stuff in OutputPage to allow index and follow policy to be set separately. Also now validates input to setRobotPolicy(). And renamed setRobotpolicy to setRobotPolicy, too. If anyone was accessing $mRobotpolicy directly they're out of luck, though. --- includes/Article.php | 14 ++-- includes/Credits.php | 2 +- includes/DifferenceEngine.php | 4 +- includes/EditPage.php | 2 +- includes/Exception.php | 2 +- includes/ImagePage.php | 4 +- includes/OutputPage.php | 84 +++++++++++++++++++---- includes/PageHistory.php | 2 +- includes/ProtectionForm.php | 2 +- includes/SpecialPage.php | 2 +- includes/specials/SpecialPreferences.php | 2 +- includes/specials/SpecialSearch.php | 2 +- includes/specials/SpecialSpecialpages.php | 2 +- includes/specials/SpecialUpload.php | 2 +- includes/specials/SpecialUserlogin.php | 10 +-- includes/specials/SpecialUserlogout.php | 2 +- 16 files changed, 97 insertions(+), 41 deletions(-) diff --git a/includes/Article.php b/includes/Article.php index 4d8277bb2b..aee54637f8 100644 --- a/includes/Article.php +++ b/includes/Article.php @@ -204,7 +204,7 @@ class Article { if ( 0 == $this->getID() ) { wfProfileOut( __METHOD__ ); - $wgOut->setRobotpolicy( 'noindex,nofollow' ); + $wgOut->setRobotPolicy( 'noindex,nofollow' ); if ( $this->mTitle->getNamespace() == NS_MEDIAWIKI ) { $wgMessageCache->loadAllMessages(); @@ -822,7 +822,7 @@ class Article { # We're looking at an old revision if ( !empty( $oldid ) ) { - $wgOut->setRobotpolicy( 'noindex,nofollow' ); + $wgOut->setRobotPolicy( 'noindex,nofollow' ); if( is_null( $this->mRevision ) ) { // FIXME: This would be a nice place to load the 'no such page' text. } else { @@ -1034,7 +1034,7 @@ class Article { "\n", $msg ); $wgOut->setPageTitle( $this->mTitle->getPrefixedText() ); - $wgOut->setRobotpolicy( 'noindex,nofollow' ); + $wgOut->setRobotPolicy( 'noindex,nofollow' ); $wgOut->addHTML( $msg ); } } @@ -1696,7 +1696,7 @@ class Article { if( $this->doWatch() ) { $wgOut->setPagetitle( wfMsg( 'addedwatch' ) ); - $wgOut->setRobotpolicy( 'noindex,nofollow' ); + $wgOut->setRobotPolicy( 'noindex,nofollow' ); $wgOut->addWikiMsg( 'addedwatchtext', $this->mTitle->getPrefixedText() ); } @@ -1741,7 +1741,7 @@ class Article { if( $this->doUnwatch() ) { $wgOut->setPagetitle( wfMsg( 'removedwatch' ) ); - $wgOut->setRobotpolicy( 'noindex,nofollow' ); + $wgOut->setRobotPolicy( 'noindex,nofollow' ); $wgOut->addWikiMsg( 'removedwatchtext', $this->mTitle->getPrefixedText() ); } @@ -2177,7 +2177,7 @@ class Article { wfDebug( "Article::confirmDelete\n" ); $wgOut->setSubtitle( wfMsg( 'delete-backlink', $wgUser->getSkin()->makeKnownLinkObj( $this->mTitle ) ) ); - $wgOut->setRobotpolicy( 'noindex,nofollow' ); + $wgOut->setRobotPolicy( 'noindex,nofollow' ); $wgOut->addWikiMsg( 'confirmdeletetext' ); if( $wgUser->isAllowed( 'suppressrevision' ) ) { @@ -2264,7 +2264,7 @@ class Article { $deleted = $this->mTitle->getPrefixedText(); $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) ); - $wgOut->setRobotpolicy( 'noindex,nofollow' ); + $wgOut->setRobotPolicy( 'noindex,nofollow' ); $loglink = '[[Special:Log/delete|' . wfMsgNoTrans( 'deletionlog' ) . ']]'; diff --git a/includes/Credits.php b/includes/Credits.php index 6326e3a284..4bbac250c6 100644 --- a/includes/Credits.php +++ b/includes/Credits.php @@ -34,7 +34,7 @@ function showCreditsPage($article) { $wgOut->setSubtitle( wfMsg( 'creditspage' ) ); $wgOut->setArticleFlag( false ); $wgOut->setArticleRelated( true ); - $wgOut->setRobotpolicy( 'noindex,nofollow' ); + $wgOut->setRobotPolicy( 'noindex,nofollow' ); if( $article->mTitle->getArticleID() == 0 ) { $s = wfMsg( 'nocredits' ); diff --git a/includes/DifferenceEngine.php b/includes/DifferenceEngine.php index 0b4028cb4c..eaa706bce5 100644 --- a/includes/DifferenceEngine.php +++ b/includes/DifferenceEngine.php @@ -142,7 +142,7 @@ CONTROL; $wgOut->setPageTitle( $oldTitle . ', ' . $newTitle ); } $wgOut->setSubtitle( wfMsg( 'difference' ) ); - $wgOut->setRobotpolicy( 'noindex,nofollow' ); + $wgOut->setRobotPolicy( 'noindex,nofollow' ); if ( !( $this->mOldPage->userCanRead() && $this->mNewPage->userCanRead() ) ) { $wgOut->loginToUse(); @@ -363,7 +363,7 @@ CONTROL; $wgOut->addHTML( $header ); $wgOut->setSubtitle( wfMsg( 'difference' ) ); - $wgOut->setRobotpolicy( 'noindex,nofollow' ); + $wgOut->setRobotPolicy( 'noindex,nofollow' ); wfProfileOut( __METHOD__ ); } diff --git a/includes/EditPage.php b/includes/EditPage.php index ce741a7211..dd3544cab7 100644 --- a/includes/EditPage.php +++ b/includes/EditPage.php @@ -1042,7 +1042,7 @@ class EditPage { wfRunHooks( 'EditPage::showEditForm:initial', array( &$this ) ) ; - $wgOut->setRobotpolicy( 'noindex,nofollow' ); + $wgOut->setRobotPolicy( 'noindex,nofollow' ); # Enabled article-related sidebar, toplinks, etc. $wgOut->setArticleRelated( true ); diff --git a/includes/Exception.php b/includes/Exception.php index 74820204ee..34671ffe04 100644 --- a/includes/Exception.php +++ b/includes/Exception.php @@ -137,7 +137,7 @@ class MWException extends Exception { global $wgOut; if ( $this->useOutputPage() ) { $wgOut->setPageTitle( $this->getPageTitle() ); - $wgOut->setRobotpolicy( "noindex,nofollow" ); + $wgOut->setRobotPolicy( "noindex,nofollow" ); $wgOut->setArticleRelated( false ); $wgOut->enableClientCache( false ); $wgOut->redirect( '' ); diff --git a/includes/ImagePage.php b/includes/ImagePage.php index 30fcf13e6a..a0920e75f4 100644 --- a/includes/ImagePage.php +++ b/includes/ImagePage.php @@ -98,7 +98,7 @@ class ImagePage extends Article { } else { # Just need to set the right headers $wgOut->setArticleFlag( true ); - $wgOut->setRobotpolicy( 'noindex,nofollow' ); + $wgOut->setRobotPolicy( 'noindex,nofollow' ); $wgOut->setPageTitle( $this->mTitle->getPrefixedText() ); $this->viewUpdates(); } @@ -762,7 +762,7 @@ EOT function showError( $description ) { global $wgOut; $wgOut->setPageTitle( wfMsg( "internalerror" ) ); - $wgOut->setRobotpolicy( "noindex,nofollow" ); + $wgOut->setRobotPolicy( "noindex,nofollow" ); $wgOut->setArticleRelated( false ); $wgOut->enableClientCache( false ); $wgOut->addWikiText( $description ); diff --git a/includes/OutputPage.php b/includes/OutputPage.php index 8226cb2fbc..bc7a567ab7 100644 --- a/includes/OutputPage.php +++ b/includes/OutputPage.php @@ -8,7 +8,7 @@ if ( ! defined( 'MEDIAWIKI' ) ) class OutputPage { var $mMetatags, $mKeywords; var $mLinktags, $mPagetitle, $mBodytext, $mDebugtext; - var $mHTMLtitle, $mRobotpolicy, $mIsarticle, $mPrintable; + var $mHTMLtitle, $mIsarticle, $mPrintable; var $mSubtitle, $mRedirect, $mStatusCode; var $mLastModified, $mETag, $mCategoryLinks; var $mScripts, $mLinkColours, $mPageLinkTitle; @@ -30,6 +30,9 @@ class OutputPage { var $mPageTitleActionText = ''; var $mParseWarnings = array(); + private $mIndexPolicy = 'index'; + private $mFollowPolicy = 'follow'; + /** * Constructor * Initialise private variables @@ -39,9 +42,8 @@ class OutputPage { $this->mAllowUserJs = $wgAllowUserJs; $this->mMetatags = $this->mKeywords = $this->mLinktags = array(); $this->mHTMLtitle = $this->mPagetitle = $this->mBodytext = - $this->mRedirect = $this->mLastModified = - $this->mSubtitle = $this->mDebugtext = $this->mRobotpolicy = - $this->mOnloadHandler = $this->mPageLinkTitle = ''; + $this->mRedirect = $this->mLastModified = $this->mSubtitle = + $this->mDebugtext = $this->mOnloadHandler = $this->mPageLinkTitle = ''; $this->mIsArticleRelated = $this->mIsarticle = $this->mPrintable = true; $this->mSuppressQuickbar = $this->mPrintable = false; $this->mLanguageLinks = array(); @@ -223,7 +225,61 @@ class OutputPage { } } - public function setRobotpolicy( $str ) { $this->mRobotpolicy = $str; } + /** + * Set the robot policy for the page: + * + * @param $policy string The literal string to output as the contents of + * the meta tag. Will be parsed according to the spec and output in + * standardized form. + * @return null + */ + public function setRobotPolicy( $policy ) { + $policy = explode( ',', $policy ); + $policy = array_map( 'trim', $policy ); + + # The default policy is follow, so if nothing is said explicitly, we + # do that. + if( in_array( 'nofollow', $policy ) ) { + $this->mFollowPolicy = 'nofollow'; + } else { + $this->mFollowPolicy = 'follow'; + } + + if( in_array( 'noindex', $policy ) ) { + $this->mIndexPolicy = 'noindex'; + } else { + $this->mIndexPolicy = 'index'; + } + } + + /** + * Set the index policy for the page, but leave the follow policy un- + * touched. + * + * @param $policy string Either 'index' or 'noindex'. + * @return null + */ + public function setIndexPolicy( $policy ) { + $policy = trim( $policy ); + if( in_array( $policy, array( 'index', 'noindex' ) ) ) { + $this->mIndexPolicy = $policy; + } + } + + /** + * Set the follow policy for the page, but leave the index policy un- + * touched. + * + * @param $policy string Either 'follow' or 'nofollow'. + * @return null + */ + public function setFollowPolicy( $policy ) { + $policy = trim( $policy ); + if( in_array( $policy, array( 'follow', 'nofollow' ) ) ) { + $this->mFollowPolicy = $policy; + } + } + public function setHTMLTitle( $name ) {$this->mHTMLtitle = $name; } public function setPageTitle( $name ) { global $action, $wgContLang; @@ -879,7 +935,7 @@ class OutputPage { global $wgUser, $wgContLang, $wgTitle, $wgLang; $this->setPageTitle( wfMsg( 'blockedtitle' ) ); - $this->setRobotpolicy( 'noindex,nofollow' ); + $this->setRobotPolicy( 'noindex,nofollow' ); $this->setArticleRelated( false ); $name = User::whoIs( $wgUser->blockedBy() ); @@ -945,7 +1001,7 @@ class OutputPage { } $this->setPageTitle( wfMsg( $title ) ); $this->setHTMLTitle( wfMsg( 'errorpagetitle' ) ); - $this->setRobotpolicy( 'noindex,nofollow' ); + $this->setRobotPolicy( 'noindex,nofollow' ); $this->setArticleRelated( false ); $this->enableClientCache( false ); $this->mRedirect = ''; @@ -971,7 +1027,7 @@ class OutputPage { $wgTitle->getPrefixedText() . "\n"; $this->setPageTitle( wfMsg( 'permissionserrors' ) ); $this->setHTMLTitle( wfMsg( 'permissionserrors' ) ); - $this->setRobotpolicy( 'noindex,nofollow' ); + $this->setRobotPolicy( 'noindex,nofollow' ); $this->setArticleRelated( false ); $this->enableClientCache( false ); $this->mRedirect = ''; @@ -994,7 +1050,7 @@ class OutputPage { public function versionRequired( $version ) { $this->setPageTitle( wfMsg( 'versionrequired', $version ) ); $this->setHTMLTitle( wfMsg( 'versionrequired', $version ) ); - $this->setRobotpolicy( 'noindex,nofollow' ); + $this->setRobotPolicy( 'noindex,nofollow' ); $this->setArticleRelated( false ); $this->mBodytext = ''; @@ -1012,7 +1068,7 @@ class OutputPage { $this->setPageTitle( wfMsg( 'badaccess' ) ); $this->setHTMLTitle( wfMsg( 'errorpagetitle' ) ); - $this->setRobotpolicy( 'noindex,nofollow' ); + $this->setRobotPolicy( 'noindex,nofollow' ); $this->setArticleRelated( false ); $this->mBodytext = ''; @@ -1148,7 +1204,7 @@ class OutputPage { global $wgUser, $wgTitle; $skin = $wgUser->getSkin(); - $this->setRobotpolicy( 'noindex,nofollow' ); + $this->setRobotPolicy( 'noindex,nofollow' ); $this->setArticleRelated( false ); // If no reason is given, just supply a default "I can't let you do @@ -1238,7 +1294,7 @@ class OutputPage { public function showFatalError( $message ) { $this->setPageTitle( wfMsg( "internalerror" ) ); - $this->setRobotpolicy( "noindex,nofollow" ); + $this->setRobotPolicy( "noindex,nofollow" ); $this->setArticleRelated( false ); $this->enableClientCache( false ); $this->mRedirect = ''; @@ -1386,8 +1442,8 @@ class OutputPage { global $wgVersion; $this->addMeta( "generator", "MediaWiki $wgVersion" ); - $p = $this->mRobotpolicy; - if( $p !== '' && $p != 'index,follow' ) { + $p = "{$this->mIndexPolicy},{$this->mFollowPolicy}"; + if( $p !== 'index,follow' ) { // http://www.robotstxt.org/wc/meta-user.html // Only show if it's different from the default robots policy $this->addMeta( 'robots', $p ); diff --git a/includes/PageHistory.php b/includes/PageHistory.php index 870b57b78d..9c210cf5cd 100644 --- a/includes/PageHistory.php +++ b/includes/PageHistory.php @@ -87,7 +87,7 @@ class PageHistory { $wgOut->setPageTitleActionText( wfMsg( 'history_short' ) ); $wgOut->setArticleFlag( false ); $wgOut->setArticleRelated( true ); - $wgOut->setRobotpolicy( 'noindex,nofollow' ); + $wgOut->setRobotPolicy( 'noindex,nofollow' ); $wgOut->setSyndicated( true ); $wgOut->setFeedAppendQuery( 'action=history' ); $wgOut->addScriptFile( 'history.js' ); diff --git a/includes/ProtectionForm.php b/includes/ProtectionForm.php index e778782277..aa682bfa2a 100644 --- a/includes/ProtectionForm.php +++ b/includes/ProtectionForm.php @@ -103,7 +103,7 @@ class ProtectionForm { function show( $err = null ) { global $wgOut, $wgUser; - $wgOut->setRobotpolicy( 'noindex,nofollow' ); + $wgOut->setRobotPolicy( 'noindex,nofollow' ); if( is_null( $this->mTitle ) || $this->mTitle->getNamespace() == NS_MEDIAWIKI ) { diff --git a/includes/SpecialPage.php b/includes/SpecialPage.php index d6ad6e6e51..c7b03084d6 100644 --- a/includes/SpecialPage.php +++ b/includes/SpecialPage.php @@ -484,7 +484,7 @@ class SpecialPage if ( !$page ) { if ( !$including ) { $wgOut->setArticleRelated( false ); - $wgOut->setRobotpolicy( 'noindex,nofollow' ); + $wgOut->setRobotPolicy( 'noindex,nofollow' ); $wgOut->setStatusCode( 404 ); $wgOut->showErrorPage( 'nosuchspecialpage', 'nospecialpagetext' ); } diff --git a/includes/specials/SpecialPreferences.php b/includes/specials/SpecialPreferences.php index b3468a3c6b..11952537e9 100644 --- a/includes/specials/SpecialPreferences.php +++ b/includes/specials/SpecialPreferences.php @@ -522,7 +522,7 @@ class PreferencesForm { $wgOut->setPageTitle( wfMsg( 'preferences' ) ); $wgOut->setArticleRelated( false ); - $wgOut->setRobotpolicy( 'noindex,nofollow' ); + $wgOut->setRobotPolicy( 'noindex,nofollow' ); $wgOut->addScriptFile( 'prefs.js' ); $wgOut->disallowUserJs(); # Prevent hijacked user scripts from sniffing passwords etc. diff --git a/includes/specials/SpecialSearch.php b/includes/specials/SpecialSearch.php index 0a483af52f..f372320511 100644 --- a/includes/specials/SpecialSearch.php +++ b/includes/specials/SpecialSearch.php @@ -282,7 +282,7 @@ class SpecialSearch { $subtitlemsg = ( Title::newFromText( $term ) ? 'searchsubtitle' : 'searchsubtitleinvalid' ); $wgOut->setSubtitle( $wgOut->parse( wfMsg( $subtitlemsg, wfEscapeWikiText($term) ) ) ); $wgOut->setArticleRelated( false ); - $wgOut->setRobotpolicy( 'noindex,nofollow' ); + $wgOut->setRobotPolicy( 'noindex,nofollow' ); } /** diff --git a/includes/specials/SpecialSpecialpages.php b/includes/specials/SpecialSpecialpages.php index ca91ad514b..560ba445c8 100644 --- a/includes/specials/SpecialSpecialpages.php +++ b/includes/specials/SpecialSpecialpages.php @@ -12,7 +12,7 @@ function wfSpecialSpecialpages() { $wgMessageCache->loadAllMessages(); - $wgOut->setRobotpolicy( 'noindex,nofollow' ); # Is this really needed? + $wgOut->setRobotPolicy( 'noindex,nofollow' ); # Is this really needed? $sk = $wgUser->getSkin(); $pages = SpecialPage::getUsablePages(); diff --git a/includes/specials/SpecialUpload.php b/includes/specials/SpecialUpload.php index 8fe2f52f59..98ccdcb07f 100644 --- a/includes/specials/SpecialUpload.php +++ b/includes/specials/SpecialUpload.php @@ -1699,7 +1699,7 @@ wgUploadAutoFill = {$autofill}; function showError( $description ) { global $wgOut; $wgOut->setPageTitle( wfMsg( "internalerror" ) ); - $wgOut->setRobotpolicy( "noindex,nofollow" ); + $wgOut->setRobotPolicy( "noindex,nofollow" ); $wgOut->setArticleRelated( false ); $wgOut->enableClientCache( false ); $wgOut->addWikiText( $description ); diff --git a/includes/specials/SpecialUserlogin.php b/includes/specials/SpecialUserlogin.php index 27009eedf0..20bd853d34 100644 --- a/includes/specials/SpecialUserlogin.php +++ b/includes/specials/SpecialUserlogin.php @@ -130,7 +130,7 @@ class LoginForm { wfRunHooks( 'AddNewAccount', array( $u, true ) ); $wgOut->setPageTitle( wfMsg( 'accmailtitle' ) ); - $wgOut->setRobotpolicy( 'noindex,nofollow' ); + $wgOut->setRobotPolicy( 'noindex,nofollow' ); $wgOut->setArticleRelated( false ); if( WikiError::isError( $result ) ) { @@ -655,7 +655,7 @@ class LoginForm { wfRunHooks('UserLoginComplete', array(&$wgUser, &$injected_html)); $wgOut->setPageTitle( wfMsg( 'loginsuccesstitle' ) ); - $wgOut->setRobotpolicy( 'noindex,nofollow' ); + $wgOut->setRobotPolicy( 'noindex,nofollow' ); $wgOut->setArticleRelated( false ); $wgOut->addWikiMsgArray( $msg, $params ); $wgOut->addHtml( $injected_html ); @@ -671,7 +671,7 @@ class LoginForm { global $wgOut; $wgOut->setPageTitle( wfMsg( 'permissionserrors' ) ); - $wgOut->setRobotpolicy( 'noindex,nofollow' ); + $wgOut->setRobotPolicy( 'noindex,nofollow' ); $wgOut->setArticleRelated( false ); $wgOut->addWikitext( $wgOut->formatPermissionsErrorMessage( $errors, 'createaccount' ) ); @@ -694,7 +694,7 @@ class LoginForm { # out. $wgOut->setPageTitle( wfMsg( 'cantcreateaccounttitle' ) ); - $wgOut->setRobotpolicy( 'noindex,nofollow' ); + $wgOut->setRobotPolicy( 'noindex,nofollow' ); $wgOut->setArticleRelated( false ); $ip = wfGetIP(); @@ -810,7 +810,7 @@ class LoginForm { } $wgOut->setPageTitle( wfMsg( 'userlogin' ) ); - $wgOut->setRobotpolicy( 'noindex,nofollow' ); + $wgOut->setRobotPolicy( 'noindex,nofollow' ); $wgOut->setArticleRelated( false ); $wgOut->disallowUserJs(); // just in case... $wgOut->addTemplate( $template ); diff --git a/includes/specials/SpecialUserlogout.php b/includes/specials/SpecialUserlogout.php index 137eadb4a6..3d497bd7d7 100644 --- a/includes/specials/SpecialUserlogout.php +++ b/includes/specials/SpecialUserlogout.php @@ -12,7 +12,7 @@ function wfSpecialUserlogout() { $oldName = $wgUser->getName(); $wgUser->logout(); - $wgOut->setRobotpolicy( 'noindex,nofollow' ); + $wgOut->setRobotPolicy( 'noindex,nofollow' ); // Hook. $injected_html = ''; -- 2.20.1