various improvements
[mediawiki.git] / maintenance / importTextFile.inc
blob00efc5c6a4e7eb43a68a9c560005610de4533087
1 <?php
3 /**
4  * Support functions for the importTextFile script
5  *
6  * @package MediaWiki
7  * @subpackage Maintenance
8  * @author Rob Church <robchur@gmail.com>
9  */
11 require_once( "$IP/includes/RecentChange.php" );
13 /**
14  * Insert a new article
15  *
16  * @param $title Title of the article
17  * @param $text Text of the article
18  * @param $user User associated with the edit
19  * @param $comment Edit summary
20  * @param $rc Whether or not to add a recent changes event
21  * @return bool
22  */
23 function insertNewArticle( &$title, $text, &$user, $comment, $rc ) {
24         if( !$title->exists() ) {
25                 # Create the article
26                 $dbw =& wfGetDB( DB_MASTER );
27                 $dbw->immediateBegin();
28                 $article = new Article( $title );
29                 $articleId = $article->insertOn( $dbw );
30                 # Prepare and save associated revision
31                 $revision = new Revision( array( 'page' => $articleId, 'text' => $text, 'user' => $user->mId, 'user_text' => $user->getName(), 'comment' => $comment ) );
32                 $revisionId = $revision->insertOn( $dbw );
33                 # Make it the current revision
34                 $article->updateRevisionOn( $dbw, $revision );
35                 $dbw->immediateCommit();
36                 # Update recent changes if appropriate
37                 if( $rc )
38                         updateRecentChanges( $dbw, $title, $user, $comment, strlen( $text ), $articleId );
39                 # Touch links etc.
40                 Article::onArticleCreate( $title );     
41                 return( true );
42         } else {
43                 # Title exists; touch nothing
44                 return( false );
45         }
48 /**
49  * Turn a filename into a title
50  *
51  * @param $filename Filename to be transformed
52  * @return Title
53  */
54 function titleFromFilename( $filename ) {
55         $parts = explode( '/', $filename );
56         $parts = explode( '.', $parts[ count( $parts ) - 1 ] );
57         return( Title::newFromText( $parts[0] ) );
60 /**
61  * Update recent changes with the page creation event
62  *
63  * @param $dbw Database in use
64  * @param $title Title of the new page
65  * @param $user User responsible for the creation
66  * @param $comment Edit summary associated with the edit
67  * @param $size Size of the page
68  * @param $articleId Article identifier
69  */ 
70 function updateRecentChanges( &$dbw, &$title, &$user, $comment, $size, $articleId ) {
71         RecentChange::notifyNew( $dbw->timestamp(), $title, false, $user, $comment, 'default', '', $size, $articleId );