-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathWikilogItem.php
348 lines (321 loc) · 9.77 KB
/
WikilogItem.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
<?php
/**
* MediaWiki Wikilog extension
* Copyright © 2008-2010 Juliano F. Ravasi
* http://www.mediawiki.org/wiki/Extension:Wikilog
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*/
/**
* @file
* @ingroup Extensions
* @author Juliano F. Ravasi < dev juliano info >
*/
if ( !defined( 'MEDIAWIKI' ) )
die();
/**
* Wikilog article database entry.
*/
class WikilogItem
{
/**
* General data about the article.
*/
public $mID = null; ///< Article ID.
public $mName = null; ///< Article title text (as in DB).
public $mTitle = null; ///< Article Title object.
public $mParent = null; ///< Parent wikilog article ID.
public $mParentName = null; ///< Parent wikilog title text.
public $mParentTitle = null; ///< Parent wikilog Title object.
public $mPublish = null; ///< Article is published.
public $mPubDate = null; ///< Date the article was published.
public $mUpdated = null; ///< Date the article was last updated.
public $mAuthors = array(); ///< Array of authors.
public $mTags = array(); ///< Array of tags.
public $mNumComments = null; ///< Cached number of comments.
public $mVisited = null; ///< Is post already visited by current user?
public $mTalkUpdated = null; ///< Maximum of post and its talk update date.
/**
* Constructor.
*/
public function __construct( ) {
}
/**
* Returns the wikilog article id.
*/
public function getID() {
return $this->mID;
}
/**
* Checks for the existence of the article in the database.
*/
public function exists() {
return $this->getID() != 0;
}
/**
* Returns whether the article is published.
*/
public function getIsPublished() {
return $this->mPublish;
}
/**
* Returns the publication date of the article.
*/
public function getPublishDate() {
return $this->mPubDate;
}
/**
* Returns the last update date of the article.
*/
public function getUpdatedDate() {
return $this->mUpdated;
}
/**
* Returns the last update date of the article or its talk.
*/
public function getTalkUpdatedDate() {
return $this->mTalkUpdated;
}
/**
* Returns the number of comments in the article.
*/
public function getNumComments() {
return $this->mNumComments;
}
/**
* Saves article data in the database.
*/
public function saveData() {
$dbw = wfGetDB( DB_MASTER );
$dbw->replace(
'wikilog_posts',
'wlp_page',
array(
'wlp_page' => $this->mID,
'wlp_parent' => $this->mParent,
'wlp_title' => $this->mName,
'wlp_publish' => $this->mPublish,
'wlp_pubdate' => $this->mPubDate ? $dbw->timestamp( $this->mPubDate ) : '',
'wlp_updated' => $this->mUpdated ? $dbw->timestamp( $this->mUpdated ) : '',
'wlp_authors' => serialize( $this->mAuthors ),
'wlp_tags' => serialize( $this->mTags ),
),
__METHOD__
);
WikilogUtils::updateTalkInfo( $this->mID, true );
# Mark post created/edited by a user already read by him
foreach ( $this->mAuthors as $text => $id ) {
WikilogUtils::updateLastVisit( $this->mID, $this->mUpdated, $id );
}
}
/**
* Deletes article data from the database.
*/
public function deleteData() {
$dbw = wfGetDB( DB_MASTER );
$dbw->delete( 'wikilog_posts', array( 'wlp_page' => $this->getID() ), __METHOD__ );
}
/**
* Resets the article id.
*/
public function resetID( $id ) {
$this->mTitle->resetArticleID( $id );
$this->mID = $id;
}
/**
* Returns an array with common header and footer system message
* parameters.
*/
public function getMsgParams( $extended = false, $pout = null ) {
global $wgLang, $wgWikilogEnableTags;
$authors = array_keys( $this->mAuthors );
$authorsFmt = WikilogUtils::authorList( $authors );
$commentsFmt = WikilogUtils::getCommentsWikiText( $this );
$categories = array();
$categoriesFmt = '';
$tags = array();
$tagsFmt = '';
if ( $extended ) {
if ( $pout !== null ) {
$categories = $pout->getCategoryLinks();
if ( count( $categories ) > 0 ) {
$categoriesFmt = wfMessage( 'wikilog-summary-categories',
count( $categories ),
WikilogUtils::categoryList( $categories )
)->inContentLanguage()->text();
} else {
$categoriesFmt = wfMessage( 'wikilog-summary-uncategorized' )->inContentLanguage()->text();
}
}
if ( $wgWikilogEnableTags ) {
$tags = array_keys( $this->mTags );
$tagsFmt = WikilogUtils::tagList( $tags );
}
}
list( $date, $time, $tz ) = WikilogUtils::getLocalDateTime( $this->mPubDate );
/*
* This is probably the largest amount of parameters to a
* system message in MediaWiki. This is the price of allowing
* the user to customize the presentation of wikilog articles.
*/
return array(
/* $1 */ $this->mParentTitle->getPrefixedURL(),
/* $2 */ $this->mParentName,
/* $3 */ $this->mTitle->getPrefixedURL(),
/* $4 */ $this->mName,
/* $5 */ count( $authors ),
/* $6 */ ( count( $authors ) > 0 ? $authors[0] : '' ),
/* $7 */ $authorsFmt,
/* $8 */ $date,
/* $9 */ $time,
/* $10 */ $commentsFmt,
/* $11 */ count( $categories ),
/* $12 */ $categoriesFmt,
/* $13 */ count( $tags ),
/* $14 */ $tagsFmt,
/* $15 */ $tz,
);
}
/**
* Creates a new wikilog article object from a database row.
* @param $row Row from database.
* @return New WikilogItem object.
*/
public static function newFromRow( $row ) {
$item = new WikilogItem();
$item->mID = intval( $row->wlp_page );
$item->mName = strval( $row->wlp_title );
$item->mTitle = Title::makeTitle( $row->page_namespace, $row->page_title );
$item->mParent = intval( $row->wlp_parent );
$item->mParentName = str_replace( '_', ' ', $row->wlw_title );
$item->mParentTitle = Title::makeTitle( $row->wlw_namespace, $row->wlw_title );
$item->mPublish = intval( $row->wlp_publish );
$item->mPubDate = $row->wlp_pubdate ? wfTimestamp( TS_MW, $row->wlp_pubdate ) : null;
$item->mUpdated = $row->wlp_updated ? wfTimestamp( TS_MW, $row->wlp_updated ) : null;
$item->mTalkUpdated = $row->wti_talk_updated ? wfTimestamp( TS_MW, $row->wti_talk_updated ) : null;
$item->mNumComments = $row->wti_num_comments;
$item->mAuthors = unserialize( $row->wlp_authors );
$item->mTags = unserialize( $row->wlp_tags );
if ( !is_array( $item->mAuthors ) ) {
$item->mAuthors = array();
}
if ( !is_array( $item->mTags ) ) {
$item->mTags = array();
}
return $item;
}
/**
* Creates a new wikilog article object from an existing article id.
* Data is fetched from the database.
* @param $id Article id.
* @return New WikilogItem object, or NULL if article doesn't exist.
*/
public static function newFromID( $id ) {
$dbr = wfGetDB( DB_SLAVE );
$row = self::loadFromID( $dbr, $id );
if ( $row ) {
return self::newFromRow( $row );
}
return null;
}
/**
* Creates a new wikilog article object from a wikilog info object.
* Data is fetched from the database.
* @param $wi WikilogItem object.
* @return New WikilogItem object, or NULL if article doesn't exist.
*/
public static function newFromInfo( WikilogInfo &$wi ) {
$itemTitle = $wi->getItemTitle();
if ( $itemTitle ) {
return self::newFromID( $itemTitle->getArticleID() );
} else {
return null;
}
}
/**
* Load information about a wikilog article from the database given a set
* of conditions.
* @param $dbr Database connection object.
* @param $conds Conditions.
* @return Database row, or false.
*/
private static function loadFromConds( $dbr, $conds ) {
$tables = self::selectTables( $dbr );
$fields = self::selectFields();
$row = $dbr->selectRow(
$tables['tables'],
$fields,
$conds,
__METHOD__,
array(),
$tables['join_conds']
);
return $row;
}
/**
* Load information about a wikilog article from the database given an
* article id.
* @param $dbr Database connection object.
* @param $id Article id.
* @return Database row, or false.
*/
private static function loadFromID( $dbr, $id ) {
return self::loadFromConds( $dbr, array( 'wlp_page' => $id ) );
}
/**
* Return the list of database tables required to create a new instance
* of WikilogItem.
*/
public static function selectTables( $dbr = null ) {
if ( !$dbr ) $dbr = wfGetDB( DB_SLAVE );
$page = $dbr->tableName( 'page' );
return array(
'tables' => array(
'wikilog_posts',
'wikilog_talkinfo',
"{$page} AS w",
"{$page} AS p"
),
'join_conds' => array(
"{$page} AS w" => array( 'LEFT JOIN', 'w.page_id = wlp_parent' ),
"{$page} AS p" => array( 'LEFT JOIN', 'p.page_id = wlp_page' ),
'wikilog_talkinfo' => array( 'LEFT JOIN', 'wti_page = wlp_page' ),
)
);
}
/**
* Return the list of post fields required to create a new instance of
* WikilogItem.
*/
public static function selectFields() {
return array(
'wlp_page',
'wlp_parent',
'w.page_namespace AS wlw_namespace',
'w.page_title AS wlw_title',
'p.page_namespace AS page_namespace',
'p.page_title AS page_title',
'wlp_title',
'wlp_publish',
'wlp_pubdate',
'wlp_updated',
'wlp_authors',
'wlp_tags',
'wti_talk_updated',
'wti_num_comments',
);
}
}