r95780 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r95779‎ | r95780 | r95781 >
Date:13:57, 30 August 2011
Author:ialex
Status:deferred
Tags:
Comment:
* Removed usage of $wgArticle
* Moved all hooks to FavoritesHooks class
* Removed FavArticle, FavTitle, FavUser and Favorites; merged their methods where needed
* Changed calls to Xml::hidden() to Html::hidden() since the former doesn't exist anymore
* Unconditionally define $wgFavoritesPersonalURL and $wgUseIconFavorite to avoid register_globals vulnerability; check their values in the hooks
* Whitespaces fixes
Modified paths:
  • /trunk/extensions/Favorites/FavArticle.php (deleted) (history)
  • /trunk/extensions/Favorites/FavParser.php (modified) (history)
  • /trunk/extensions/Favorites/FavTitle.php (deleted) (history)
  • /trunk/extensions/Favorites/FavUser.php (deleted) (history)
  • /trunk/extensions/Favorites/FavoritelistEditor.php (modified) (history)
  • /trunk/extensions/Favorites/Favorites.php (modified) (history)
  • /trunk/extensions/Favorites/FavoritesHooks.php (added) (history)
  • /trunk/extensions/Favorites/Favorites_body.php (deleted) (history)
  • /trunk/extensions/Favorites/SpecialFavoritelist.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Favorites/FavUser.php
@@ -1,36 +0,0 @@
2 -<?php
3 -class FavUser extends User {
4 - /**
5 - * Check the Favorited status of an article.
6 - * @param $title \type{Title} Title of the article to look at
7 - * @return \bool True if article is Favorited
8 - */
9 - function isFavorited( $title ) {
10 - global $wgUser;
11 - $fl = FavoritedItem::fromUserTitle( $wgUser, $title );
12 - return $fl->isFavorited();
13 -
14 - }
15 -
16 - /**
17 - * Favorite an article.
18 - * @param $title \type{Title} Title of the article to look at
19 - */
20 - function addFavorite( $title ) {
21 - global $wgUser;
22 - $fl = FavoritedItem::fromUserTitle( $wgUser, $title );
23 - $fl->addFavorite();
24 - $title->invalidateCache();
25 - }
26 -
27 - /**
28 - * Stop Favoriting an article.
29 - * @param $title \type{Title} Title of the article to look at
30 - */
31 - function removeFavorite( $title ) {
32 - global $wgUser;
33 - $fl = FavoritedItem::fromUserTitle( $wgUser, $title );
34 - $fl->removeFavorite();
35 - $title->invalidateCache();
36 - }
37 -}
Index: trunk/extensions/Favorites/FavTitle.php
@@ -1,44 +0,0 @@
2 -<?php
3 -
4 -
5 -class FavTitle extends Title {
6 -
7 -
8 - var $mFav = null;
9 - var $mTextform;
10 - var $mTitle;
11 -
12 - function __construct() {}
13 -
14 - /**
15 - * Is $wgUser watching this page?
16 - * @return \type{\bool}
17 - */
18 - public function userIsFavoriting() {
19 -
20 - global $wgUser, $wgArticle;
21 - $favUser = new FavUser();
22 - if ($wgArticle) {
23 - if ( is_null( $this->mFav ) ) {
24 - if ( NS_SPECIAL == $this->mNamespace || !$wgUser->isLoggedIn()) {
25 - $this->mFav = false;
26 - } else {
27 - $this->mFav = $favUser->isFavorited( $wgArticle->mTitle );
28 - }
29 - }
30 - }
31 - return $this->mFav;
32 - }
33 -
34 - public function moveToFav($title, &$nt ) {
35 - # Update watchlists
36 - $oldnamespace = $title->getNamespace() & ~1;
37 - $newnamespace = $nt->getNamespace() & ~1;
38 - $oldtitle = $title->getDBkey();
39 - $newtitle = $nt->getDBkey();
40 -
41 - if( $oldnamespace != $newnamespace || $oldtitle != $newtitle ) {
42 - FavoritedItem::duplicateEntries( $title, $nt );
43 - }
44 - }
45 -}
Index: trunk/extensions/Favorites/Favorites_body.php
@@ -1,64 +0,0 @@
2 -<?php
3 -
4 -class Favorites extends QuickTemplate {
5 -
6 - var $mTitle;
7 -
8 - function execute() {
9 -
10 - }
11 -
12 - function favoritesIcon( &$sktemplate, &$links ) {
13 -
14 - global $wgUseIconFavorite, $wgRequest, $wgArticle;
15 -
16 - //$sktemplate->skin = $sktemplate->data['skin'];
17 - $action = $wgRequest->getText( 'action' );
18 -
19 - // See if this object even exists - if the user can't read it, the object doesn't get created.
20 - if ($wgArticle) {
21 -
22 - if ( $wgUseIconFavorite ) {
23 - $class = 'icon ';
24 - $place = 'views';
25 - } else {
26 - $class = '';
27 - $place = 'actions';
28 - }
29 - $favTitle = new FavTitle();
30 -
31 - //$mode = $this->mTitle->userIsFavoriting() ? 'unfavorite' : 'favorite';
32 - $mode = $favTitle->userIsFavoriting() ? 'unfavorite' : 'favorite';
33 - $links[$place][$mode] = array(
34 - 'class' => $class . ( ( $action == 'favorite' || $action == 'unfavorite' ) ? ' selected' : false ),
35 - 'text' => wfMsg( $mode ), // uses 'favorite' or 'unfavorite' message
36 - 'href' => $wgArticle->mTitle->getLocalUrl( 'action=' . $mode )
37 - );
38 -
39 -
40 - return false;
41 - }
42 -
43 -}
44 -
45 -function favoritesTabs($skin, &$content_actions) {
46 - global $wgUseIconFavorite, $wgRequest, $wgArticle;
47 -
48 - $action = $wgRequest->getText( 'action' );
49 - $favTitle = new FavTitle();
50 - $mode = $favTitle->userIsFavoriting() ? 'unfavorite' : 'favorite';
51 - // See if this object even exists - if the user can't read it, the object doesn't get created.
52 - if ($wgArticle) {
53 - $content_actions[$mode] = array (
54 - 'class' => (( $action == 'favorite' || $action == 'unfavorite' ) ? ' selected' : false ),
55 - 'text' => wfMsg( $mode ), // uses 'favorite' or 'unfavorite' message
56 - 'href' => $wgArticle->mTitle->getLocalUrl( 'action=' . $mode )
57 - );
58 - return true;
59 - }
60 -
61 -
62 -}
63 -
64 -}
65 -
Index: trunk/extensions/Favorites/FavArticle.php
@@ -1,96 +0,0 @@
2 -<?php
3 -
4 -
5 -class FavArticle extends Article {
6 -
7 -
8 - var $mTitle;
9 -
10 -
11 -
12 - /**
13 - * User-interface handler for the "favorite" action
14 - */
15 - public function favorite() {
16 - global $wgUser, $wgOut, $wgArticle;
17 -
18 - $this->mTitle = $wgArticle->mTitle;
19 -
20 - if ( $wgUser->isAnon() ) {
21 - $wgOut->showErrorPage( 'favoritenologin', 'favoritenologintext' );
22 - return;
23 - }
24 - if ( wfReadOnly() ) {
25 - $wgOut->readOnlyPage();
26 - return;
27 - }
28 - if ( $this->doFavorite() ) {
29 - $wgOut->setPagetitle( wfMsg( 'addedfavorite' ) );
30 - $wgOut->setRobotPolicy( 'noindex,nofollow' );
31 - $wgOut->addWikiMsg( 'addedfavoritetext', $this->mTitle->getPrefixedText() );
32 - }
33 - $wgOut->returnToMain( true, $this->mTitle->getPrefixedText() );
34 - }
35 -
36 - /**
37 - * Add this page to $wgUser's favoritelist
38 - * @return bool true on successful favorite operation
39 - */
40 - public function doFavorite() {
41 - global $wgUser;
42 - $favUser = new FavUser();
43 - if ( $wgUser->isAnon() ) {
44 - return false;
45 - }
46 - if ( wfRunHooks( 'FavoriteArticle', array( &$wgUser, &$this ) ) ) {
47 - $favUser->addFavorite( $this->mTitle );
48 - return wfRunHooks( 'FavoriteArticleComplete', array( &$wgUser, &$this ) );
49 - }
50 -
51 - }
52 -
53 - /**
54 - * User interface handler for the "unfavorite" action.
55 - */
56 - public function unfavorite($action, $wgArticle) {
57 - global $wgUser, $wgOut, $wgArticle;
58 - $this->mTitle = $wgArticle->mTitle;
59 - if ( $wgUser->isAnon() ) {
60 - $wgOut->showErrorPage( 'favoritenologin', 'favoritenologintext' );
61 - return;
62 - }
63 - if ( wfReadOnly() ) {
64 - $wgOut->readOnlyPage();
65 - return;
66 - }
67 - if ( $this->doUnfavorite() ) {
68 - $wgOut->setPagetitle( wfMsg( 'removedfavorite' ) );
69 - $wgOut->setRobotPolicy( 'noindex,nofollow' );
70 - $wgOut->addWikiMsg( 'removedfavoritetext', $wgArticle->mTitle->getPrefixedText() );
71 - }
72 - $wgOut->returnToMain( true, $wgArticle->mTitle->getPrefixedText() );
73 -
74 - return false;
75 -
76 - }
77 -
78 - /**
79 - * Stop favoriting a page
80 - * @return bool true on successful unfavorite
81 - */
82 - public function doUnfavorite() {
83 - global $wgUser;
84 -
85 - $favUser = new FavUser();
86 - if ( $wgUser->isAnon() ) {
87 - return false;
88 - }
89 - if ( wfRunHooks( 'UnfavoriteArticle', array( &$wgUser, &$this ) ) ) {
90 - $favUser->removeFavorite( $this->mTitle );
91 - return wfRunHooks( 'UnfavoriteArticleComplete', array( &$wgUser, &$this ) );
92 - }
93 - return false;
94 - }
95 -
96 -
97 -}
Index: trunk/extensions/Favorites/FavParser.php
@@ -4,7 +4,7 @@
55
66 function wfSpecialFavoritelist($argv, $parser) {
77
8 - global $wgUser, $wgOut, $wgLang, $wgRequest, $wgArticle;
 8+ global $wgUser, $wgOut, $wgLang, $wgRequest;
99 global $wgRCShowFavoritingUsers, $wgEnotifFavoritelist, $wgShowUpdatedMarker;
1010 $output = '';
1111
@@ -12,7 +12,7 @@
1313 $specialTitle = SpecialPage::getTitleFor( 'Favoritelist' );
1414 //$wgOut->setRobotPolicy( 'noindex,nofollow' );
1515
16 - $this->mTitle = $wgArticle->mTitle;
 16+ $this->mTitle = $parser->getTitle();
1717
1818 if ($this->mTitle->getNamespace() == NS_USER && array_key_exists('userpage', $argv) && $argv['userpage']) {
1919 $parts = explode( '/', $this->mTitle->getText() );
Index: trunk/extensions/Favorites/SpecialFavoritelist.php
@@ -343,7 +343,7 @@
344344 $self = SpecialPage::getTitleFor( 'Favoritelist' );
345345 $form = Xml::openElement( 'form', array( 'method' => 'post',
346346 'action' => $self->getLocalUrl( array( 'action' => 'edit' ) ) ) );
347 - $form .= Xml::hidden( 'token', $wgUser->editToken( 'favoritelistedit' ) );
 347+ $form .= Html::hidden( 'token', $wgUser->editToken( 'favoritelistedit' ) );
348348 //$form .= "<fieldset>\n<legend>" . wfMsgHtml( 'favoritelistedit-normal-legend' ) . "</legend>";
349349 //$form .= wfMsgExt( 'favoritelistedit-normal-explain', 'parse' );
350350 $form .= $this->buildRemoveList( $user, $wgUser->getSkin() );
@@ -459,7 +459,7 @@
460460 $self = SpecialPage::getTitleFor( 'Favoritelist' );
461461 $form = Xml::openElement( 'form', array( 'method' => 'post',
462462 'action' => $self->getLocalUrl( array( 'action' => 'raw' ) ) ) );
463 - $form .= Xml::hidden( 'token', $wgUser->editToken( 'favoritelistedit' ) );
 463+ $form .= Html::hidden( 'token', $wgUser->editToken( 'favoritelistedit' ) );
464464 $form .= '<fieldset><legend>' . wfMsgHtml( 'favoritelistedit-raw-legend' ) . '</legend>';
465465 $form .= wfMsgExt( 'favoritelistedit-raw-explain', 'parse' );
466466 $form .= Xml::label( wfMsg( 'favoritelistedit-raw-titles' ), 'titles' );
Index: trunk/extensions/Favorites/FavoritelistEditor.php
@@ -325,7 +325,7 @@
326326 $self = SpecialPage::getTitleFor( 'Favoritelist' );
327327 $form = Xml::openElement( 'form', array( 'method' => 'post',
328328 'action' => $self->getLocalUrl( array( 'action' => 'edit' ) ) ) );
329 - $form .= Xml::hidden( 'token', $wgUser->editToken( 'favoritelistedit' ) );
 329+ $form .= Html::hidden( 'token', $wgUser->editToken( 'favoritelistedit' ) );
330330 $form .= "<fieldset>\n<legend>" . wfMsgHtml( 'favoritelistedit-normal-legend' ) . "</legend>";
331331 $form .= wfMsgExt( 'favoritelistedit-normal-explain', 'parse' );
332332 $form .= $this->buildRemoveList( $user, $wgUser->getSkin() );
@@ -443,7 +443,7 @@
444444 $self = SpecialPage::getTitleFor( 'Favoritelist' );
445445 $form = Xml::openElement( 'form', array( 'method' => 'post',
446446 'action' => $self->getLocalUrl( array( 'action' => 'raw' ) ) ) );
447 - $form .= Xml::hidden( 'token', $wgUser->editToken( 'favoritelistedit' ) );
 447+ $form .= Html::hidden( 'token', $wgUser->editToken( 'favoritelistedit' ) );
448448 $form .= '<fieldset><legend>' . wfMsgHtml( 'favoritelistedit-raw-legend' ) . '</legend>';
449449 $form .= wfMsgExt( 'favoritelistedit-raw-explain', 'parse' );
450450 $form .= Xml::label( wfMsg( 'favoritelistedit-raw-titles' ), 'titles' );
Index: trunk/extensions/Favorites/Favorites.php
@@ -1,22 +1,22 @@
22 <?php
3 -/*
4 -Copyright (C) 2011 Jeremy Lemley
 3+/**
 4+ * Copyright (C) 2011 Jeremy Lemley
 5+ *
 6+ * This program is free software: you can redistribute it and/or modify
 7+ * it under the terms of the GNU General Public License as published by
 8+ * the Free Software Foundation, either version 3 of the License, or
 9+ * (at your option) any later version.
 10+ *
 11+ * This program is distributed in the hope that it will be useful,
 12+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
 13+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 14+ * GNU General Public License for more details.
 15+ *
 16+ * You should have received a copy of the GNU General Public License
 17+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
 18+ *
 19+ */
520
6 - This program is free software: you can redistribute it and/or modify
7 - it under the terms of the GNU General Public License as published by
8 - the Free Software Foundation, either version 3 of the License, or
9 - (at your option) any later version.
10 -
11 - This program is distributed in the hope that it will be useful,
12 - but WITHOUT ANY WARRANTY; without even the implied warranty of
13 - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 - GNU General Public License for more details.
15 -
16 - You should have received a copy of the GNU General Public License
17 - along with this program. If not, see <http://www.gnu.org/licenses/>.
18 -
19 -*/
20 -
2121 $wgExtensionCredits['specialpage'][] = array(
2222 'path' => __FILE__,
2323 'name' => 'Favorites',
@@ -25,156 +25,39 @@
2626 'version' => '0.2.6',
2727 'url' => "http://www.mediawiki.org/wiki/Extension:Favorites",
2828 );
29 -
30 -global $wgUseIconFavorite, $wgFavoritesPersonalURL;
3129
3230 $dir = dirname(__FILE__) . '/';
3331 $wgExtensionMessagesFiles['Favorites'] = $dir . 'favorites.i18n.php';
34 -$wgAutoloadClasses['Favorites'] = $dir . 'Favorites_body.php';
3532 $wgAutoloadClasses['FavoritelistEditor'] = $dir . 'FavoritelistEditor.php';
3633 $wgAutoloadClasses['FavoritedItem'] = $dir . 'FavoritedItem.php';
3734 $wgAutoloadClasses['SpecialFavoritelist'] = $dir . 'SpecialFavoritelist.php';
38 -$wgAutoloadClasses['FavUser'] = $dir . 'FavUser.php';
39 -$wgAutoloadClasses['FavArticle'] = $dir . 'FavArticle.php';
40 -$wgAutoloadClasses['FavTitle'] = $dir . 'FavTitle.php';
 35+$wgAutoloadClasses['FavoritesHooks'] = $dir . 'FavoritesHooks.php';
4136 $wgAutoloadClasses['FavParser'] = $dir . 'FavParser.php';
42 -$wgHooks['LoadExtensionSchemaUpdates'][] = 'FavSQLUpdate';
 37+
4338 $wgSpecialPages['Favoritelist'] = 'SpecialFavoritelist';
4439 $wgSpecialPageGroups['Favoritelist'] = 'other';
4540
 41+$wgHooks['LoadExtensionSchemaUpdates'][] = 'FavoritesHooks::onLoadExtensionSchemaUpdates';
4642
4743 //tag hook
48 -$wgHooks['ParserFirstCallInit'][] = 'ParseFavorites';
 44+$wgHooks['ParserFirstCallInit'][] = 'FavoritesHooks::onParserFirstCallInit';
4945
50 -
5146 //add the icon / link
52 -$wgHooks['SkinTemplateNavigation'][] = 'fnNavUrls'; // For Vector
53 -$wgHooks['SkinTemplateTabs'][] = 'fnNavTabs'; // For other skins
 47+$wgHooks['SkinTemplateNavigation'][] = 'FavoritesHooks::onSkinTemplateNavigation'; // For Vector
 48+$wgHooks['SkinTemplateTabs'][] = 'FavoritesHooks::onSkinTemplateTabs'; // For other skins
5449
55 -if ($wgUseIconFavorite){
56 - //add CSS - only needed for icon display
57 - $wgHooks['BeforePageDisplay'][] = 'fnAddCss';
58 -}
 50+//add CSS - only needed for icon display
 51+$wgHooks['BeforePageDisplay'][] = 'FavoritesHooks::onBeforePageDisplay';
5952
6053 //add or remove
61 -$wgHooks['UnknownAction'][] = 'fnAction';
 54+$wgHooks['UnknownAction'][] = 'FavoritesHooks::onUnknownAction';
6255
6356 //handle page moves and deletes
64 -$wgHooks['TitleMoveComplete'][] = 'fnHookMoveToFav';
65 -$wgHooks['ArticleDeleteComplete'][] = 'fnHookDeleteFav';
 57+$wgHooks['TitleMoveComplete'][] = 'FavoritesHooks::onTitleMoveComplete';
 58+$wgHooks['ArticleDeleteComplete'][] = 'FavoritesHooks::onArticleDeleteComplete';
6659
67 -// Do we want to display a "My Favorites" link in the personal urls area?
68 -if ($wgFavoritesPersonalURL) {
69 - $wgHooks['PersonalUrls'][] = 'onAddPersonalUrls';
70 -}
 60+// Display a "My Favorites" link in the personal urls area
 61+$wgHooks['PersonalUrls'][] = 'FavoritesHooks::onPersonalUrls';
7162
72 -function onAddPersonalUrls( &$personal_urls, &$wgTitle ) {
73 - global $wgUser;
74 -
75 - // $skin = $wgUser->getSkin();
76 - $title = Title::newFromText( 'Special:Favoritelist' );
77 - if( $wgUser->getID() ) {
78 - $url['userpage'] = array_shift( $personal_urls );
79 - $url[] = array_shift( $personal_urls );
80 - $url[] = array_shift( $personal_urls );
81 -
82 - $url[] = array( 'text' => wfMsg( 'myfavoritelist' ),
83 - 'href' => $title->getLocalURL( )
84 - );
85 -
86 -
87 - $personal_urls = $url + $personal_urls;
88 - }
89 -
90 - return true;
91 -}
92 -
93 -function fnAction ($action, $article) {
94 - $title = new Title();
95 - $favArticle = new FavArticle($title);
96 -
97 - if ($action == 'unfavorite') {
98 - $favArticle->unfavorite($action, $article);
99 -
100 - } elseif ($action == 'favorite') {
101 - $favArticle->favorite($action, $article);
102 - } else {
103 - return true;
104 - }
105 - return false;
106 -}
107 -
108 -function fnNavUrls($sktemplate, &$links) {
109 - $fNav = new Favorites();
110 - $fNav->favoritesIcon($sktemplate, $links);
111 - return true;
112 -}
113 -
114 -function fnNavTabs( $skin, &$content_actions ){
115 - $fNav = new Favorites();
116 - $fNav->favoritesTabs($skin, $content_actions);
117 - return true;
118 -}
119 -
120 -
121 -function fnHookMoveToFav(&$title, &$nt, &$wgUser, $pageid, $redirid ) {
122 - $favTitle = new FavTitle();
123 - $favTitle->moveToFav($title, $nt, $wgUser, $pageid, $redirid );
124 - return true;
125 -}
126 -
127 -function fnHookDeleteFav(&$article, &$user, $reason, $id ){
128 - $dbw = wfGetDB( DB_MASTER );
129 - $dbw->delete('favoritelist', array(
130 - 'fl_title' => $article->mTitle->getDBKey()),
131 - $fname = 'Database::delete');
132 - return true;
133 -}
134 -
135 -function fnAddCss (&$out) {
136 - global $wgExtensionAssetsPath;
137 - $out->addStyle($wgExtensionAssetsPath . '/Favorites/favorites.css');
138 - return true;
139 -}
140 -
141 -function ParseFavorites(Parser &$parser) {
142 -
143 - $parser->setHook( 'favorites', 'favParser_Render' );
144 -
145 - return true;
146 -}
147 -
148 -
149 -$markerList = array();
150 -function favParser_Render ( $input, $argv, $parser) {
151 - # The parser function itself
152 - # The input parameters are wikitext with templates expanded
153 - # The output should be wikitext too
154 - //$output = "Parser Output goes here.";
155 -
156 - $favParse = new FavParser();
157 - $output = $favParse->wfSpecialFavoritelist($argv, $parser);
158 - $parser->disableCache();
159 - return $output;
160 -
161 -}
162 -
163 -
164 -# Schema updates for update.php
165 -function FavSQLUpdate( $updater = null ) {
166 - if ( $updater === null ) {
167 - // <= 1.16 support
168 - global $wgExtNewTables, $wgExtModifiedFields;
169 - $wgExtNewTables[] = array(
170 - 'favoritelist',
171 - dirname( __FILE__ ) . '/favorites.sql'
172 - );
173 -
174 - } else {
175 - // >= 1.17 support
176 - $updater->addExtensionUpdate( array( 'addTable', 'favoritelist',
177 - dirname( __FILE__ ) . '/favorites.sql', true ) );
178 -
179 - }
180 - return true;
181 -}
\ No newline at end of file
 63+$wgFavoritesPersonalURL = true;
 64+$wgUseIconFavorite = true;
Index: trunk/extensions/Favorites/FavoritesHooks.php
@@ -0,0 +1,199 @@
 2+<?php
 3+
 4+class FavoritesHooks {
 5+
 6+ public static function onLoadExtensionSchemaUpdates( $updater = null ) {
 7+ if ( $updater === null ) { // <= 1.16 support
 8+ global $wgExtNewTables, $wgExtModifiedFields;
 9+ $wgExtNewTables[] = array(
 10+ 'favoritelist',
 11+ dirname( __FILE__ ) . '/favorites.sql'
 12+ );
 13+ } else { // >= 1.17 support
 14+ $updater->addExtensionUpdate( array( 'addTable', 'favoritelist',
 15+ dirname( __FILE__ ) . '/favorites.sql', true ) );
 16+ }
 17+ return true;
 18+ }
 19+
 20+ public static function onUnknownAction( $action, $article ) {
 21+ global $wgUser, $wgOut;
 22+
 23+ if ( $action !== 'unfavorite' && $action !== 'favorite' ) {
 24+ return true;
 25+ }
 26+
 27+ if ( $wgUser->isAnon() ) {
 28+ $wgOut->showErrorPage( 'favoritenologin', 'favoritenologintext' );
 29+ return;
 30+ }
 31+ if ( wfReadOnly() ) {
 32+ $wgOut->readOnlyPage();
 33+ return;
 34+ }
 35+
 36+ $wgOut->setRobotPolicy( 'noindex,nofollow' );
 37+
 38+ if ( $action === 'favorite' ) {
 39+ if ( self::doFavorite( $article ) ) {
 40+ $wgOut->setPagetitle( wfMsg( 'addedfavorite' ) );
 41+ $wgOut->addWikiMsg( 'addedfavoritetext', $article->getTitle()->getPrefixedText() );
 42+ }
 43+ } else {
 44+ if ( self::doUnfavorite( $article ) ) {
 45+ $wgOut->setPagetitle( wfMsg( 'removedfavorite' ) );
 46+ $wgOut->addWikiMsg( 'removedfavoritetext', $article->getTitle()->getPrefixedText() );
 47+ }
 48+ }
 49+
 50+ $wgOut->returnToMain( true, $article->getTitle()->getPrefixedText() );
 51+
 52+ return false;
 53+ }
 54+
 55+ /**
 56+ * Add this page to $wgUser's favoritelist
 57+ * @return bool true on successful favorite operation
 58+ */
 59+ private static function doFavorite( $article ) {
 60+ global $wgUser;
 61+
 62+ if ( $wgUser->isAnon() ) {
 63+ return false;
 64+ }
 65+ if ( wfRunHooks( 'FavoriteArticle', array( &$wgUser, &$article ) ) ) {
 66+ $fl = FavoritedItem::fromUserTitle( $wgUser, $article->getTitle() );
 67+ $fl->addFavorite();
 68+ $article->getTitle()->invalidateCache();
 69+ return wfRunHooks( 'FavoriteArticleComplete', array( &$wgUser, &$article ) );
 70+ }
 71+
 72+ }
 73+
 74+ /**
 75+ * Stop favoriting a page
 76+ * @return bool true on successful unfavorite
 77+ */
 78+ private static function doUnfavorite( $article ) {
 79+ global $wgUser;
 80+
 81+ if ( $wgUser->isAnon() ) {
 82+ return false;
 83+ }
 84+ if ( wfRunHooks( 'UnfavoriteArticle', array( &$wgUser, &$article ) ) ) {
 85+ $fl = FavoritedItem::fromUserTitle( $wgUser, $article->getTitle() );
 86+ $fl->removeFavorite();
 87+ $article->getTitle()->invalidateCache();
 88+ return wfRunHooks( 'UnfavoriteArticleComplete', array( &$wgUser, &$article ) );
 89+ }
 90+ return false;
 91+ }
 92+
 93+ public static function onPersonalUrls( &$personal_urls, &$wgTitle ) {
 94+ global $wgFavoritesPersonalURL, $wgUser;
 95+
 96+ if ( $wgFavoritesPersonalURL && $wgUser->isLoggedIn() ) {
 97+ $url['userpage'] = array_shift( $personal_urls );
 98+ $url[] = array_shift( $personal_urls );
 99+ $url[] = array_shift( $personal_urls );
 100+
 101+ $url[] = array( 'text' => wfMsg( 'myfavoritelist' ),
 102+ 'href' => SpecialPage::getTitleFor( 'Favoritelist' )->getLocalURL() );
 103+ $personal_urls = $url + $personal_urls;
 104+ }
 105+
 106+ return true;
 107+ }
 108+
 109+ public static function onParserFirstCallInit( &$parser ) {
 110+ $parser->setHook( 'favorites', array( __CLASS__, 'renderFavorites' ) );
 111+ return true;
 112+ }
 113+
 114+ function renderFavorites( $input, $argv, $parser ) {
 115+ # The parser function itself
 116+ # The input parameters are wikitext with templates expanded
 117+ # The output should be wikitext too
 118+ //$output = "Parser Output goes here.";
 119+
 120+ $favParse = new FavParser();
 121+ $output = $favParse->wfSpecialFavoritelist( $argv, $parser );
 122+ $parser->disableCache();
 123+ return $output;
 124+ }
 125+
 126+ public static function onSkinTemplateNavigation( $skin, &$links ) {
 127+ global $wgUseIconFavorite, $wgRequest, $wgUser;
 128+
 129+ $action = $wgRequest->getText( 'action' );
 130+ $title = $skin->getTitle();
 131+
 132+ if ( $title->getNamespace() >= NS_MAIN ) {
 133+ if ( $wgUseIconFavorite ) {
 134+ $class = 'icon ';
 135+ $place = 'views';
 136+ } else {
 137+ $class = '';
 138+ $place = 'actions';
 139+ }
 140+
 141+ $fl = FavoritedItem::fromUserTitle( $wgUser, $title );
 142+
 143+ $mode = $fl->isFavorited() ? 'unfavorite' : 'favorite';
 144+ $links[$place][$mode] = array(
 145+ 'class' => $class . ( ( $action == 'favorite' || $action == 'unfavorite' ) ? ' selected' : false ),
 146+ 'text' => wfMsg( $mode ), // uses 'favorite' or 'unfavorite' message
 147+ 'href' => $title->getLocalUrl( 'action=' . $mode )
 148+ );
 149+ }
 150+ return true;
 151+ }
 152+
 153+ public static function onSkinTemplateTabs( $skin, &$content_actions ) {
 154+ global $wgUseIconFavorite, $wgUser, $wgRequest;
 155+
 156+ $action = $wgRequest->getText( 'action' );
 157+ $title = $skin->getTitle();
 158+ if ( $title->getNamespace() >= NS_MAIN ) {
 159+ $fl = FavoritedItem::fromUserTitle( $wgUser, $title );
 160+ $mode = $fl->isFavorited() ? 'unfavorite' : 'favorite';
 161+ $content_actions[$mode] = array (
 162+ 'class' => (( $action == 'favorite' || $action == 'unfavorite' ) ? ' selected' : false ),
 163+ 'text' => wfMsg( $mode ), // uses 'favorite' or 'unfavorite' message
 164+ 'href' => $title->getLocalUrl( 'action=' . $mode )
 165+ );
 166+ }
 167+ return true;
 168+ }
 169+
 170+ public static function onTitleMoveComplete( &$title, &$nt, &$user, $pageid, $redirid ) {
 171+ # Update watchlists
 172+ $oldnamespace = $title->getNamespace() & ~1;
 173+ $newnamespace = $nt->getNamespace() & ~1;
 174+ $oldtitle = $title->getDBkey();
 175+ $newtitle = $nt->getDBkey();
 176+
 177+ if ( $oldnamespace != $newnamespace || $oldtitle != $newtitle ) {
 178+ FavoritedItem::duplicateEntries( $title, $nt );
 179+ }
 180+ return true;
 181+ }
 182+
 183+ public static function onArticleDeleteComplete(&$article, &$user, $reason, $id ){
 184+ $dbw = wfGetDB( DB_MASTER );
 185+ $dbw->delete( 'favoritelist', array(
 186+ 'fl_title' => $article->mTitle->getDBKey() ),
 187+ __METHOD__ );
 188+ return true;
 189+ }
 190+
 191+ public static function onBeforePageDisplay( $out ) {
 192+ global $wgUseIconFavorite, $wgExtensionAssetsPath;
 193+
 194+ if ( $wgUseIconFavorite ) {
 195+ $out->addStyle( $wgExtensionAssetsPath . '/Favorites/favorites.css' );
 196+ }
 197+
 198+ return true;
 199+ }
 200+}
Property changes on: trunk/extensions/Favorites/FavoritesHooks.php
___________________________________________________________________
Added: svn:eol-style
1201 + native

Status & tagging log