Difference between revisions of "Tutorials/Hacking MediaWiki"

From ThorstensHome
Jump to: navigation, search
m (1 revision(s))
(My goals)
Line 11: Line 11:
  
 
http://svn.wikimedia.org/viewvc/mediawiki/trunk/extensions/Review/?sortby=date
 
http://svn.wikimedia.org/viewvc/mediawiki/trunk/extensions/Review/?sortby=date
 +
 +
=== Subpages ===
 +
Subpages work fine, you get a breadcrumb-like navigation. If you have an article foo/bar, you will get a link to foo at the top of your article. Google for subpage. It is in mediawiki stock now. Anyway, article names get quite long like "My Tutorials/Programming Tutorials/C Programming Tutorial/How to simulate a keypress". In this case, you want the full navigation (to "My Tutorials", to "My Tutorials/Programming Tutorials" and so on), but only the article title "How to simulate a keypress". To do this, you cannot use {{DISPLAYTITLE:title}}, see [http://www.mediawiki.org/wiki/Help:Variables#Technical_metadata the reason]. But there is
 +
* http://www.mediawiki.org/wiki/Extension:TitleAlias
 +
* http://www.mediawiki.org/wiki/Extension:PageFunctions
  
 
== Tables ==
 
== Tables ==

Revision as of 02:27, 11 May 2009

This article is about my programming work on mediawiki.

Contents

My goals

I want to change the mediawiki software so that it

  • has on the Specialpages:Statistics page every site and how often it has been viewed
  • allows for exporting every page on the wiki
  • allows for rating an article

Allow rating for an article

http://svn.wikimedia.org/viewvc/mediawiki/trunk/extensions/Review/?sortby=date

Subpages

Subpages work fine, you get a breadcrumb-like navigation. If you have an article foo/bar, you will get a link to foo at the top of your article. Google for subpage. It is in mediawiki stock now. Anyway, article names get quite long like "My Tutorials/Programming Tutorials/C Programming Tutorial/How to simulate a keypress". In this case, you want the full navigation (to "My Tutorials", to "My Tutorials/Programming Tutorials" and so on), but only the article title "How to simulate a keypress". To do this, you cannot use , see the reason. But there is

Tables

the table mediawikipage has got a field counter. Besides, it seems to point to a mediawikirevision.

Show all pages

bob:/srv/www/htdocs # diff -rup wiki-org/ wiki
Only in wiki: LocalSettings.php
Only in wiki/config: LocalSettings.php
diff -rup wiki-org/includes/SpecialExport.php wiki/includes/SpecialExport.php
--- wiki-org/includes/SpecialExport.php 2007-07-13 21:07:10.000000000 +0200
+++ wiki/includes/SpecialExport.php     2007-07-13 21:15:38.000000000 +0200
@@ -182,6 +182,22 @@ function wfSpecialExport( $page = '' ) {
        }
        $form .= wfHidden( 'action', 'submit' );
        $form .= wfSubmitButton( wfMsg( 'export-submit' ) ) . '</form>';
+
+
+       $dbr = wfGetDB( DB_SLAVE );
+
+       list( $page) = $dbr->tableNamesN( 'page');
+               $sql =
+                       "SELECT
+                               page_title as title
+                       FROM $page ";
+               $where =
+                       "WHERE page_is_redirect=0 AND page_namespace";
+
+       $res = $dbr->query( $sql);
+       while ( $row = $dbr->fetchObject( $res ) ) {
+                echo $row->title;
+       }
        $wgOut->addHtml( $form );
 }

Only in wiki/includes: SpecialExport.php~
bob:/srv/www/htdocs #                   

The code

How to ...

  • edit an article: article.php, function doEdit

My patches

Allow exporting all pages

See http://bugzilla.wikimedia.org/show_bug.cgi?id=10574. I should learn from

bob:/srv/www/htdocs/wiki # php maintenance/dumpBackup.php --full  

For mediawiki 1.10

--- wiki-org/includes/SpecialExport.php	2007-07-13 21:07:10.000000000 +0200
+++ wiki/includes/SpecialExport.php	2007-07-14 12:26:15.000000000 +0200
@@ -21,6 +21,24 @@
  * @addtogroup SpecialPage
  */
 
+
+function allpages()
+/* return a list of all pages, separated by newlines */
+{
+	$dbr = wfGetDB( DB_SLAVE );
+	list( $page) = $dbr->tableNamesN( 'page');
+	$sql = "SELECT page_title as title FROM $page ";
+	$where = "WHERE page_is_redirect=0 AND page_namespace";
+	$res = $dbr->query( $sql);
+
+	while ( $row = $dbr->fetchObject( $res ) ) 
+        {
+          $result.=$row->title."\n";
+	}
+        return $result;
+
+}
+
 function wfExportGetPagesFromCategory( $title ) {
 	global $wgContLang;
 
@@ -58,8 +76,10 @@ function wfSpecialExport( $page = '' ) {
 
 	$curonly = true;
 	$doexport = false;
-
-	if ( $wgRequest->getCheck( 'addcat' ) ) {
+	if ( $wgRequest->getCheck( 'addall' ) ) {
+		$page .= "\n".allpages();
+	}
+	else if ( $wgRequest->getCheck( 'addcat' ) ) {
 		$page = $wgRequest->getText( 'pages' );
 		$catname = $wgRequest->getText( 'catname' );
 		
@@ -170,7 +190,8 @@ function wfSpecialExport( $page = '' ) {
 	$form = wfOpenElement( 'form', array( 'method' => 'post', 'action' => $titleObj->getLocalUrl() ) );
 
 	$form .= wfInputLabel( wfMsg( 'export-addcattext' ), 'catname', 'catname', 40 ) . ' ';
-	$form .= wfSubmitButton( wfMsg( 'export-addcat' ), array( 'name' => 'addcat' ) ) . '<br />';
+	$form .= wfSubmitButton( wfMsg( 'export-addcat' ), array( 'name' => 'addcat' ) ) . ' ';
+	$form .= wfSubmitButton( wfMsg( 'export-addall' ), array( 'name' => 'addall' ) ) . '<br />';
 
 	$form .= wfOpenElement( 'textarea', array( 'name' => 'pages', 'cols' => 40, 'rows' => 10 ) ) . htmlspecialchars($page). '</textarea><br />';
 
diff -rup wiki-org/languages/messages/MessagesDe.php wiki/languages/messages/MessagesDe.php
--- wiki-org/languages/messages/MessagesDe.php	2007-07-13 21:07:10.000000000 +0200
+++ wiki/languages/messages/MessagesDe.php	2007-07-14 12:12:21.000000000 +0200
@@ -1651,6 +1651,7 @@ Alternativ ist der Export auch mit der S
 '''Hinweis:''' Der Export kompletter Versionsgeschichten ist aus Performancegründen bis auf Weiteres nicht möglich.",
 'export-submit'     => 'Seiten exportieren',
 'export-addcattext' => 'Seiten aus Kategorie hinzufügen:',
+'export-addall'     => 'Alle Seiten hinzufügen',
 'export-addcat'     => 'Hinzufügen',
 
 # Namespace 8 related
diff -rup wiki-org/languages/messages/MessagesEn.php wiki/languages/messages/MessagesEn.php
--- wiki-org/languages/messages/MessagesEn.php	2007-07-13 21:07:10.000000000 +0200
+++ wiki/languages/messages/MessagesEn.php	2007-07-14 12:11:30.000000000 +0200
@@ -2127,6 +2127,7 @@ In the latter case you can also use a li
 '''Note:''' Exporting the full history of pages through this form has been disabled due to performance reasons.",
 'export-submit' => 'Export',
 'export-addcattext' => 'Add pages from category:',
+'export-addall' => 'Add all pages',
 'export-addcat' => 'Add',
 
 # Namespace 8 related

For mediawiki 1.6.10

diff -rup /home/staerk/mediawiki-1.6.10/includes/SpecialExport.php includes/SpecialExport.php
--- /home/staerk/mediawiki-1.6.10/includes/SpecialExport.php    2007-02-21 03:25:47.000000000 +0100
+++ includes/SpecialExport.php  2007-07-14 16:49:06.000000000 +0200
@@ -26,13 +26,28 @@
 require_once( 'Revision.php' );
 require_once( 'Export.php' );

+function allpages()
+/* return a list of all pages, separated by newlines */
+{
+       $dbr = wfGetDB( DB_SLAVE );
+       $page = $dbr->tableName( 'page');
+       $sql = "SELECT page_title as title FROM $page ";
+       $where = "WHERE page_is_redirect=0 AND page_namespace";
+       $res = $dbr->query( $sql);
+
+       while ( $row = $dbr->fetchObject( $res ) )
+        {
+          $result.=$row->title."\n";
+       }
+        return $result;
+}
+
 /**
  *
  */
 function wfSpecialExport( $page = '' ) {
        global $wgOut, $wgRequest, $wgExportAllowListContributors;
        global $wgExportAllowHistory;
-
        if( $wgRequest->getVal( 'action' ) == 'submit') {
                $page = $wgRequest->getText( 'pages' );
                if( $wgExportAllowHistory ) {
@@ -50,6 +65,10 @@ function wfSpecialExport( $page = '' ) {

        if( $page != '' ) {
                $wgOut->disable();
+                if ($wgRequest->getCheck('allpages'))
+               {
+                       $page=allpages();
+               }
                header( "Content-type: application/xml; charset=utf-8" );
                $pages = explode( "\n", $page );

@@ -78,6 +97,7 @@ function wfSpecialExport( $page = '' ) {
 <input type='hidden' name='action' value='submit' />
 <textarea name='pages' cols='40' rows='10'></textarea><br />
 $checkbox
+<label><input type='checkbox' name='allpages' value='true' checked='checked' />export all pages</label>
 <input type='submit' />
 </form>
 " );
Only in includes/: SpecialStatistics-0.php
Only in includes/: SpecialUpload-0.php
Only in includes/: patch

Disallow spammers

It is quite easy to spam a mediawiki although the group permissions are negative. You only need to HTTP POST to a mediawiki site:

$ cat post.txt | netcat localhost 80

where

$ cat post.txt
POST /wiki/index.php?title=Main_Page&action=submit HTTP/1.1
User-Agent: Mozilla/5.0 (compatible; Konqueror/3.5; Linux) KHTML/3.5.7 (like Gecko)
Referer: http://localhost/wiki/index.php?title=Main_Page&action=edit
Pragma: no-cache
Cache-control: no-cache
Accept: text/html, image/jpeg, image/png, text/*, image/*, */*
Accept-Encoding: x-gzip, x-deflate, gzip, deflate
Accept-Charset: utf-8, utf-8;q=0.5, *;q=0.5
Accept-Language: en
Host: localhost
Cookie: wikidb_mediawiki110_session=47mqdv89ut6fmqhe68mlk3dht3e4jndg; wikidb_mediawiki110Token=831c6a5aa57e46b7c15d1bdaeda07e21; wikidb_mediawiki110UserName=WikiSysop; wikidb_mediawiki110UserID=1
Content-Type: multipart/form-data; boundary=----------m5lfHwyI2vlBYfCp5WlOU8IZSonKHTH2W1hnxd5zXo83RLsXpbvHjEq
Connection: Keep-Alive
Content-Length: 1745

------------m5lfHwyI2vlBYfCp5WlOU8IZSonKHTH2W1hnxd5zXo83RLsXpbvHjEq
Content-Disposition: form-data; name="wpSection"


------------m5lfHwyI2vlBYfCp5WlOU8IZSonKHTH2W1hnxd5zXo83RLsXpbvHjEq
Content-Disposition: form-data; name="wpStarttime"

20070715234226
------------m5lfHwyI2vlBYfCp5WlOU8IZSonKHTH2W1hnxd5zXo83RLsXpbvHjEq
Content-Disposition: form-data; name="wpEdittime"

20070713184316
------------m5lfHwyI2vlBYfCp5WlOU8IZSonKHTH2W1hnxd5zXo83RLsXpbvHjEq
Content-Disposition: form-data; name="wpScrolltop"

0
------------m5lfHwyI2vlBYfCp5WlOU8IZSonKHTH2W1hnxd5zXo83RLsXpbvHjEq
Content-Disposition: form-data; name="wpTextbox1"

<big>'''MediaWiki has been successfully installed.'''</big>

Consult the [http://meta.wikimedia.org/wiki/Help:Contents User's Guide] for information on using the wiki software.

== Getting started ==

* [http://www.mediawiki.org/wiki/Help:Configuration_settings Configuration settings list]
* [http://www.mediawiki.org/wiki/Help:FAQ MediaWiki FAQ]
* [http://mail.wikimedia.org/mailman/listinfo/mediawiki-announce MediaWiki release mailing list]

------------m5lfHwyI2vlBYfCp5WlOU8IZSonKHTH2W1hnxd5zXo83RLsXpbvHjEq
Content-Disposition: form-data; name="wpSummary"


------------m5lfHwyI2vlBYfCp5WlOU8IZSonKHTH2W1hnxd5zXo83RLsXpbvHjEq
Content-Disposition: form-data; name="wpSave"

Save page
------------m5lfHwyI2vlBYfCp5WlOU8IZSonKHTH2W1hnxd5zXo83RLsXpbvHjEq
Content-Disposition: form-data; name="wpEditToken"

6f15ca8edfd027729e131b3cf8ac1e5d\
------------m5lfHwyI2vlBYfCp5WlOU8IZSonKHTH2W1hnxd5zXo83RLsXpbvHjEq
Content-Disposition: form-data; name="wpAutoSummary"

d41d8cd98f00b204e9800998ecf8427e
------------m5lfHwyI2vlBYfCp5WlOU8IZSonKHTH2W1hnxd5zXo83RLsXpbvHjEq--


bob:~ #    

BenchMark

To do a mediawiki benchmark, you need a big fat mediawiki. So, here's how you add pages automatedly:

Have a file newarticle.txt:

POST /wiki/index.php?title=Abd&action=submit HTTP/1.1
User-Agent: Mozilla/5.0 (compatible; Konqueror/3.5; Linux) KHTML/3.5.7 (like Gecko)
Referer: http://localhost/wiki/index.php?title=Abd&action=edit
Pragma: no-cache
Cache-control: no-cache
Accept: text/html, image/jpeg, image/png, text/*, image/*, */*
Accept-Encoding: x-gzip, x-deflate, gzip, deflate
Accept-Charset: utf-8, utf-8;q=0.5, *;q=0.5
Accept-Language: en
Host: localhost
Cookie: wikidb_mediawiki110Token=fZLWr_I2Vnova5XDLxJItLrj7mVuo9Q67vVo8lSLu_fd9b3yLs7ckJzaUY9yZWhz; wikidb_mediawiki110UserName=TThQbCefuIIrOYY3EhU98NlzLTWyIiJyaZDNcNjh2vI.; wikidb_mediawiki110UserID=8Hyx25L4Icacs7xYXIhZx-uCiqns0sLXjw76sKivlDw.; wikidb_mediawiki110_session=9eOB5ZVxLdUQ1Lhw_aC0b8qyHNz-RNgQh0eslwRcGdfUyHbiW_cDrzNU7uWhFyC3
Content-Type: multipart/form-data; boundary=----------M4ybT7DdvyeHD6ZYCABtQhxGZWgi6GmQIi1lnDOiB2xM6VvIf4C5l7L
Connection: Keep-Alive
Content-Length: 1291

------------M4ybT7DdvyeHD6ZYCABtQhxGZWgi6GmQIi1lnDOiB2xM6VvIf4C5l7L
Content-Disposition: form-data; name="wpSection"


------------M4ybT7DdvyeHD6ZYCABtQhxGZWgi6GmQIi1lnDOiB2xM6VvIf4C5l7L
Content-Disposition: form-data; name="wpStarttime"

20070717120633
------------M4ybT7DdvyeHD6ZYCABtQhxGZWgi6GmQIi1lnDOiB2xM6VvIf4C5l7L
Content-Disposition: form-data; name="wpEdittime"

20070717120633
------------M4ybT7DdvyeHD6ZYCABtQhxGZWgi6GmQIi1lnDOiB2xM6VvIf4C5l7L
Content-Disposition: form-data; name="wpScrolltop"

0
------------M4ybT7DdvyeHD6ZYCABtQhxGZWgi6GmQIi1lnDOiB2xM6VvIf4C5l7L
Content-Disposition: form-data; name="wpTextbox1"

x
------------M4ybT7DdvyeHD6ZYCABtQhxGZWgi6GmQIi1lnDOiB2xM6VvIf4C5l7L
Content-Disposition: form-data; name="wpSummary"


------------M4ybT7DdvyeHD6ZYCABtQhxGZWgi6GmQIi1lnDOiB2xM6VvIf4C5l7L
Content-Disposition: form-data; name="wpSave"

Save page
------------M4ybT7DdvyeHD6ZYCABtQhxGZWgi6GmQIi1lnDOiB2xM6VvIf4C5l7L
Content-Disposition: form-data; name="wpEditToken"

2183bb9072ad183e6b304be617503eaf\
------------M4ybT7DdvyeHD6ZYCABtQhxGZWgi6GmQIi1lnDOiB2xM6VvIf4C5l7L
Content-Disposition: form-data; name="wpAutoSummary"

d41d8cd98f00b204e9800998ecf8427e
------------M4ybT7DdvyeHD6ZYCABtQhxGZWgi6GmQIi1lnDOiB2xM6VvIf4C5l7L--


sed -i "s/Abc/Abe/" newarticle.txt
cat newarticle.txt | netcat localhost 80
HTTP/1.1 302 Found
Date: Tue, 17 Jul 2007 12:18:56 GMT
Server: Apache/2.2.3 (Linux/SUSE)
X-Powered-By: PHP/5.2.0
Vary: Accept-Encoding,Cookie
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Cache-Control: private, must-revalidate, max-age=0
Location: http://localhost/wiki/index.php/Abf
Content-Length: 0
Keep-Alive: timeout=15, max=100
Connection: Keep-Alive
Content-Type: text/html; charset=utf-8

bob:~ # cat newarticle.txt | sed "s/Abf/Abh/" | netcat localhost 80
HTTP/1.1 302 Found
Date: Tue, 17 Jul 2007 12:21:06 GMT
Server: Apache/2.2.3 (Linux/SUSE)
X-Powered-By: PHP/5.2.0
Vary: Accept-Encoding,Cookie
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Cache-Control: private, must-revalidate, max-age=0
Location: http://localhost/wiki/index.php/Abh
Content-Length: 0
Keep-Alive: timeout=15, max=100
Connection: Keep-Alive
Content-Type: text/html; charset=utf-8

for i in $(seq 1000 1 10000); do cat newarticle.txt | sed "s/Abf/Article${i}/" | netcat localhost 80; done

See also