Index: trunk/phase3/maintenance/importImages.inc |
— | — | @@ -6,6 +6,7 @@ |
7 | 7 | * @file |
8 | 8 | * @ingroup Maintenance |
9 | 9 | * @author Rob Church <robchur@gmail.com> |
| 10 | + * @author Mij <mij@bitchx.it> |
10 | 11 | */ |
11 | 12 | |
12 | 13 | /** |
— | — | @@ -86,4 +87,26 @@ |
87 | 88 | } |
88 | 89 | |
89 | 90 | return false; |
90 | | -} |
\ No newline at end of file |
| 91 | +} |
| 92 | + |
| 93 | +# FIXME: Access the api in a saner way and performing just one query (preferably batching files too). |
| 94 | +function getFileCommentFromSourceWiki($wiki_host, $file) { |
| 95 | + $url = $wiki_host . '/api.php?action=query&format=xml&titles=File:' . $file . '&prop=imageinfo&&iiprop=comment'; |
| 96 | + $body = file_get_contents($url); |
| 97 | + if (preg_match('#<ii comment="([^"]*)" />#', $body, $matches) == 0) { |
| 98 | + return false; |
| 99 | + } |
| 100 | + |
| 101 | + return $matches[1]; |
| 102 | +} |
| 103 | + |
| 104 | +function getFileUserFromSourceWiki($wiki_host, $file) { |
| 105 | + $url = $wiki_host . '/api.php?action=query&format=xml&titles=File:' . $file . '&prop=imageinfo&&iiprop=user'; |
| 106 | + $body = file_get_contents($url); |
| 107 | + if (preg_match('#<ii user="([^"]*)" />#', $body, $matches) == 0) { |
| 108 | + return false; |
| 109 | + } |
| 110 | + |
| 111 | + return $matches[1]; |
| 112 | +} |
| 113 | + |
Index: trunk/phase3/maintenance/importImages.php |
— | — | @@ -2,16 +2,24 @@ |
3 | 3 | |
4 | 4 | /** |
5 | 5 | * Maintenance script to import one or more images from the local file system into |
6 | | - * the wiki without using the web-based interface |
| 6 | + * the wiki without using the web-based interface. |
7 | 7 | * |
| 8 | + * "Smart import" additions: |
| 9 | + * - aim: preserve the essential metadata (user, description) when importing medias from an existing wiki |
| 10 | + * - process: |
| 11 | + * - interface with the source wiki, don't use bare files only (see --source-wiki-url). |
| 12 | + * - fetch metadata from source wiki for each file to import. |
| 13 | + * - commit the fetched metadata to the destination wiki while submitting. |
| 14 | + * |
8 | 15 | * @file |
9 | 16 | * @ingroup Maintenance |
10 | 17 | * @author Rob Church <robchur@gmail.com> |
| 18 | + * @author Mij <mij@bitchx.it> |
11 | 19 | */ |
12 | 20 | |
13 | | -$optionsWithArgs = array( 'extensions', 'comment', 'comment-file', 'comment-ext', 'user', 'license', 'sleep', 'limit', 'from' ); |
| 21 | +$optionsWithArgs = array( 'extensions', 'comment', 'comment-file', 'comment-ext', 'user', 'license', 'sleep', 'limit', 'from', 'source-wiki-url' ); |
14 | 22 | require_once( dirname(__FILE__) . '/commandLine.inc' ); |
15 | | -require_once( 'importImages.inc' ); |
| 23 | +require_once( dirname(__FILE__) . '/importImages.inc.php' ); |
16 | 24 | $processed = $added = $ignored = $skipped = $overwritten = $failed = 0; |
17 | 25 | |
18 | 26 | echo( "Import Images\n\n" ); |
— | — | @@ -141,36 +149,51 @@ |
142 | 150 | $svar = 'added'; |
143 | 151 | } |
144 | 152 | |
145 | | - # Find comment text |
146 | | - $commentText = false; |
| 153 | + if (isset( $options['source-wiki-url'])) { |
| 154 | + /* find comment text directly from source wiki, through MW's API */ |
| 155 | + $real_comment = getFileCommentFromSourceWiki($options['source-wiki-url'], $base); |
| 156 | + if ($real_comment === false) |
| 157 | + $commentText = $comment; |
| 158 | + else |
| 159 | + $commentText = $real_comment; |
147 | 160 | |
148 | | - if ( $commentExt ) { |
149 | | - $f = findAuxFile( $file, $commentExt ); |
150 | | - if ( !$f ) { |
151 | | - echo( " No comment file with extension {$commentExt} found for {$file}. " ); |
152 | | - $commentText = $comment; |
153 | | - } else { |
154 | | - $commentText = file_get_contents( $f ); |
155 | | - if ( !$f ) { |
156 | | - echo( " Failed to load comment file {$f}. " ); |
157 | | - $commentText = $comment; |
158 | | - } else if ( $comment ) { |
159 | | - $commentText = trim( $commentText ) . "\n\n" . trim( $comment ); |
160 | | - } |
161 | | - } |
162 | | - } |
| 161 | + /* find user directly from source wiki, through MW's API */ |
| 162 | + $real_user = getFileUserFromSourceWiki($options['source-wiki-url'], $base); |
| 163 | + if ($real_user === false) { |
| 164 | + $wgUser = $user; |
| 165 | + } else { |
| 166 | + $wgUser = User::newFromName($real_user); |
| 167 | + if ($wgUser === false) { |
| 168 | + # user does not exist in target wiki |
| 169 | + echo ("failed: user '$real_user' does not exist in target wiki."); |
| 170 | + continue; |
| 171 | + } |
| 172 | + } |
| 173 | + } else { |
| 174 | + # Find comment text |
| 175 | + $commentText = false; |
163 | 176 | |
164 | | - if ( !$commentText ) { |
165 | | - $commentText = $comment; |
166 | | - } |
| 177 | + if ( $commentExt ) { |
| 178 | + $f = findAuxFile( $file, $commentExt ); |
| 179 | + if ( !$f ) { |
| 180 | + echo( " No comment file with extension {$commentExt} found for {$file}, using default comment. " ); |
| 181 | + } else { |
| 182 | + $commentText = file_get_contents( $f ); |
| 183 | + if ( !$f ) { |
| 184 | + echo( " Failed to load comment file {$f}, using default comment. " ); |
| 185 | + } |
| 186 | + } |
| 187 | + } |
167 | 188 | |
168 | | - if ( !$commentText ) { |
169 | | - $commentText = 'Importing image file'; |
170 | | - } |
| 189 | + if ( !$commentText ) { |
| 190 | + $commentText = $comment; |
| 191 | + } |
| 192 | + } |
171 | 193 | |
| 194 | + |
172 | 195 | # Import the file |
173 | 196 | if ( isset( $options['dry'] ) ) { |
174 | | - echo( " publishing {$file}... " ); |
| 197 | + echo( " publishing {$file} by '" . $wgUser->getName() . "', comment '$commentText'... " ); |
175 | 198 | } else { |
176 | 199 | $archive = $image->publish( $file ); |
177 | 200 | if( WikiError::isError( $archive ) || !$archive->isGood() ) { |
— | — | @@ -282,6 +305,8 @@ |
283 | 306 | --dry Dry run, don't import anything |
284 | 307 | --protect=<protect> Specify the protect value (autoconfirmed,sysop) |
285 | 308 | --unprotect Unprotects all uploaded images |
| 309 | +--source-wiki-url if specified, take User and Comment data for each imported file from this URL. |
| 310 | + For example, --source-wiki-url="http://en.wikipedia.org/" |
286 | 311 | |
287 | 312 | TEXT; |
288 | 313 | exit(1); |
Index: trunk/phase3/RELEASE-NOTES |
— | — | @@ -318,6 +318,8 @@ |
319 | 319 | the return value |
320 | 320 | * Separate unit test suites under t/ and tests/ were merged and moved to |
321 | 321 | maintenance/tests/. |
| 322 | +* importImages.php maintenance script can now use the original uploader and |
| 323 | +comment from another wiki. |
322 | 324 | |
323 | 325 | === Bug fixes in 1.16 === |
324 | 326 | |