forked from svn2github/wikia
/
SpecialCreateBlogPage.php
217 lines (182 loc) · 6.01 KB
/
SpecialCreateBlogPage.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php
class CreateBlogPage extends SpecialCustomEditPage {
const FIELD_IS_COMMENTING_ENABLED = 'wpIsCommentingEnabled';
const STATUS_BLOG_PERMISSION_DENIED = -101;
protected $mFormData = array();
protected $titleNS = NS_BLOG_ARTICLE;
public function __construct() {
//TODO create some abstract metod to force user to get CreateBlogPage
parent::__construct('CreateBlogPage');
}
protected function initializeEditPage() {
$editPage = parent::initializeEditPage();
$editPage->isCreateBlogPage = true;
return $editPage;
}
public function execute($par) {
if( !$this->user->isLoggedIn() ) {
$this->out->showErrorPage( 'create-blog-no-login', 'create-blog-login-required', array(wfGetReturntoParam()));
return;
}
if( $this->user->isBlocked() ) {
$this->out->blockedPage();
return;
}
if( wfReadOnly() ) {
$this->out->readOnlyPage();
return;
}
parent::execute($par);
/* bugId::34933 Actions::getActionName() assumes every Special page is a view. Forcing a wgAction override for this page */
RequestContext::getMain()->getOutput()->addJsConfigVars('wgAction', 'edit');
}
protected function afterArticleInitialize($mode, $title, $article) {
if( $mode == self::MODE_EDIT ) {
$aPageProps = BlogArticle::getProps($article->getId());
$this->mFormData['isCommentingEnabled'] = empty($aPageProps['commenting']) ? 0 :$aPageProps['commenting'];
$isAllowed = $this->user->isAllowed( "blog-articles-edit" );
if((strtolower($this->user->getName()) != strtolower( BlogArticle::getOwner($title))) && !$isAllowed) {
$this->titleStatus = self::STATUS_BLOG_PERMISSION_DENIED;
$this->addEditNotice( wfMsg('create-blog-permission-denied') );
}
} else {
$this->mFormData['isCommentingEnabled'] = true;
}
}
/**
* Return wikitext for generating preview / diff / to be saved
*/
public function getWikitextFromRequest() {
$wikitext = parent::getWikitextFromRequest();
if( $this->mode == self::MODE_NEW ) {
$catName = wfMsgForContent("create-blog-post-category");
$sCategoryNSName = $this->contLang->getFormattedNsText( NS_CATEGORY );
$wikitext .= "\n[[" . $sCategoryNSName . ":" . $catName . "]]";
}
return $wikitext;
}
protected function getTitlePrefix() {
return $this->user->getName() . '/';
}
/**
* add some default values
*/
public function beforeSave() {
if( empty($this->mEditPage->summary )) {
$this->mEditPage->summary = wfMsgForContent('create-blog-updated');
}
$this->mEditPage->recreate = true;
}
/**
* Perform additional checks when saving an article
*/
protected function processSubmit() {
//used to set some default values */
if ($this->mode != self::MODE_NEW_SETUP) {
if ($this->contentStatus == EditPage::AS_BLANK_ARTICLE) {
$this->addEditNotice(wfMsg('plb-create-empty-body-error'));
}
switch ($this->titleStatus) {
case self::STATUS_EMPTY:
$this->addEditNotice(wfMsg( 'create-blog-empty-title-error' ));
break;
case self::STATUS_INVALID:
$this->addEditNotice( wfMsg('create-blog-invalid-title-error') );
break;
case self::STATUS_ALREADY_EXISTS:
$this->addEditNotice(wfMsg( 'create-blog-article-already-exists' ));
break;
}
}
}
public function getPageTitle() {
if( $this->mode == self::MODE_EDIT ) {
return wfMsg( 'create-blog-post-title-edit' );
} else {
return wfMsg( 'create-blog-post-title' );
}
}
public function renderHeader($par) {
$this->forceUserToProvideTitle('create-blog-form-post-title');
$this->addCustomCheckbox(self::FIELD_IS_COMMENTING_ENABLED, wfMsg('blog-comments-label'), $this->mFormData['isCommentingEnabled']);
}
protected function afterSave( $status ) {
switch( $status->value ) {
case EditPage::AS_SUCCESS_UPDATE:
case EditPage::AS_SUCCESS_NEW_ARTICLE:
$article = $this->getEditedArticle();
$articleId = $article->getID();
$aPageProps = array();
$aPageProps['commenting'] = 0;
if( $this->getField(self::FIELD_IS_COMMENTING_ENABLED) != "" ) {
$aPageProps['commenting'] = 1;
}
if( count( $aPageProps ) ) {
BlogArticle::setProps( $articleId, $aPageProps );
}
$this->invalidateCacheConnected( $article );
$this->createListingPage();
// BugID:25123 purge the main blog listing pages cache
global $wgMemc;
$user = $article->getTitle()->getBaseText();
$ns = $article->getTitle()->getNSText();
foreach( range(0, 5) as $page ) {
$wgMemc->delete( wfMemcKey( 'blog', 'listing', $ns, $user, $page ) );
}
$this->out->redirect($article->getTitle()->getFullUrl());
break;
default:
Wikia::log( __METHOD__, "editpage", $status->value );
if( $status->value == EditPage::AS_READ_ONLY_PAGE_LOGGED ) {
$sMsg = wfMsg('create-blog-cant-edit');
}
else {
$sMsg = wfMsg('create-blog-spam');
}
Wikia::log( __METHOD__, "save error", $sMsg, true );
break;
}
}
/**
* purge cache for connected articles
*
* @access public
* @author Krzysztof Krzyżaniak <eloy@wikia-inc.com>
*
*/
public function invalidateCacheConnected( BlogArticle $article ) {
$title = $article->getTitle();
$title->invalidateCache();
/**
* this should be subpage, invalidate page as well
*/
list( $page, $subpage ) = explode( "/", $title->getDBkey() );
$title = Title::newFromDBkey( $page );
$title->invalidateCache();
$article->clearBlogListing();
}
/**
* createListingPage -- create listing article if not exists
*
* @access private
* @author Krzysztof Krzyżaniak <eloy@wikia-inc.com>
*/
private function createListingPage() {
global $wgUser;
$oTitle = Title::newFromText( $wgUser->getName(), NS_BLOG_ARTICLE );
$oArticle = new Article( $oTitle, 0 );
if( !$oArticle->exists( ) ) {
/**
* add empty article for newlycreated blog
*/
$oArticle->doEdit(
wfMsg("create-blog-empty-article"), # body
wfMsg("create-blog-empty-article-log"), # summary
EDIT_NEW | EDIT_MINOR | EDIT_FORCE_BOT # flags
);
}
}
protected function setEditedTitle(Title $title) {
$this->setEditedArticle(new BlogArticle($title));
}
}